From 072568ae2b8b35dc1f9e005d973857789cd7bbba Mon Sep 17 00:00:00 2001 From: Jesse Chan Date: Mon, 8 Feb 2021 14:40:32 +0000 Subject: [PATCH] client: consolidate isSmallScreen to ConfigStore --- client/src/javascript/app.tsx | 14 ++++++++++---- .../components/general/ListViewport.tsx | 2 +- .../modals/settings-modal/SettingsModal.tsx | 9 ++++----- .../TorrentDetailsModal.tsx | 10 ++++++---- .../components/sidebar/ThemeSwitchButton.tsx | 4 ++-- client/src/javascript/stores/ConfigStore.ts | 18 ++++++++++++------ 6 files changed, 35 insertions(+), 22 deletions(-) diff --git a/client/src/javascript/app.tsx b/client/src/javascript/app.tsx index 20f2aea1..a4e8e15d 100644 --- a/client/src/javascript/app.tsx +++ b/client/src/javascript/app.tsx @@ -68,17 +68,23 @@ const FloodApp: FC = observer(() => { ); }, []); - const isDarkTheme = useMedia('(prefers-color-scheme: dark)'); + const isSystemPreferDark = useMedia('(prefers-color-scheme: dark)'); useEffect(() => { - ConfigStore.setSystemPreferDark(isDarkTheme); - }, [isDarkTheme]); + ConfigStore.setSystemPreferDark(isSystemPreferDark); + }, [isSystemPreferDark]); + + // max-width here must sync with CSS + const isSmallScreen = useMedia('(max-width: 720px)'); + useEffect(() => { + ConfigStore.setSmallScreen(isSmallScreen); + }, [isSmallScreen]); return ( }> - + diff --git a/client/src/javascript/components/general/ListViewport.tsx b/client/src/javascript/components/general/ListViewport.tsx index 73a7a175..79b09c5c 100644 --- a/client/src/javascript/components/general/ListViewport.tsx +++ b/client/src/javascript/components/general/ListViewport.tsx @@ -65,7 +65,7 @@ const ListViewport = forwardRef((props: ListVi return ( { +const SettingsModal: FC = observer(() => { const {i18n} = useLingui(); - const isSmallScreen = useMedia('(max-width: 720px)'); const [changedClientSettings, setChangedClientSettings] = useState>({}); const [changedFloodSettings, setChangedFloodSettings] = useState>({}); @@ -123,10 +122,10 @@ const SettingsModal: FC = () => { ]} size="large" heading={i18n._('settings.tabs.heading')} - orientation={isSmallScreen ? 'horizontal' : 'vertical'} + orientation={ConfigStore.isSmallScreen ? 'horizontal' : 'vertical'} tabs={tabs} /> ); -}; +}); export default SettingsModal; diff --git a/client/src/javascript/components/modals/torrent-details-modal/TorrentDetailsModal.tsx b/client/src/javascript/components/modals/torrent-details-modal/TorrentDetailsModal.tsx index 5797aa1b..51f597ad 100644 --- a/client/src/javascript/components/modals/torrent-details-modal/TorrentDetailsModal.tsx +++ b/client/src/javascript/components/modals/torrent-details-modal/TorrentDetailsModal.tsx @@ -1,6 +1,8 @@ import {FC} from 'react'; +import {observer} from 'mobx-react'; import {useLingui} from '@lingui/react'; -import {useMedia} from 'react-use'; + +import ConfigStore from '@client/stores/ConfigStore'; import Modal from '../Modal'; import TorrentMediainfo from './TorrentMediainfo'; @@ -10,9 +12,9 @@ import TorrentHeading from './TorrentHeading'; import TorrentPeers from './TorrentPeers'; import TorrentTrackers from './TorrentTrackers'; -const TorrentDetailsModal: FC = () => { +const TorrentDetailsModal: FC = observer(() => { const {i18n} = useLingui(); - const isSmallScreen = useMedia('(max-width: 720px)'); + const {isSmallScreen} = ConfigStore; const tabs = { 'torrent-details': { @@ -48,6 +50,6 @@ const TorrentDetailsModal: FC = () => { tabsInBody={!isSmallScreen} /> ); -}; +}); export default TorrentDetailsModal; diff --git a/client/src/javascript/components/sidebar/ThemeSwitchButton.tsx b/client/src/javascript/components/sidebar/ThemeSwitchButton.tsx index 1ede7cd9..3e9ce5ac 100644 --- a/client/src/javascript/components/sidebar/ThemeSwitchButton.tsx +++ b/client/src/javascript/components/sidebar/ThemeSwitchButton.tsx @@ -11,8 +11,8 @@ const ThemeSwitchButton: FC = () => { return ( ConfigStore.setUserPreferDark(!ConfigStore.preferDark)} + content={i18n._(ConfigStore.isPreferDark ? 'sidebar.button.theme.light' : 'sidebar.button.theme.dark')} + onClick={() => ConfigStore.setUserPreferDark(!ConfigStore.isPreferDark)} position="bottom" wrapperClassName="sidebar__action sidebar__icon-button sidebar__icon-button--interactive tooltip__wrapper"> diff --git a/client/src/javascript/stores/ConfigStore.ts b/client/src/javascript/stores/ConfigStore.ts index 114984f3..af14ddc6 100644 --- a/client/src/javascript/stores/ConfigStore.ts +++ b/client/src/javascript/stores/ConfigStore.ts @@ -24,22 +24,28 @@ class ConfigStore { authMethod: AuthMethod = 'default'; pollInterval = 2000; - systemPreferDark = false; - userPreferDark: boolean | null = queryUserPreferDark(); - @computed get preferDark(): boolean { - return this.userPreferDark ?? this.systemPreferDark; + isSmallScreen = false; + + isSystemPreferDark = false; + isUserPreferDark: boolean | null = queryUserPreferDark(); + @computed get isPreferDark(): boolean { + return this.isUserPreferDark ?? this.isSystemPreferDark; } constructor() { makeAutoObservable(this); } + setSmallScreen(preference: boolean): void { + this.isSmallScreen = preference; + } + setSystemPreferDark(preference: boolean): void { - this.systemPreferDark = preference; + this.isSystemPreferDark = preference; } setUserPreferDark(preference: boolean | null): void { - this.userPreferDark = preference; + this.isUserPreferDark = preference; if (preference == null) { window.localStorage.removeItem('userPreferDark'); } else {