From 58504852073a516012b55a619699f7a03151136d Mon Sep 17 00:00:00 2001 From: Lim Chee Aun Date: Sat, 21 Oct 2023 17:40:03 +0800 Subject: [PATCH] Refactor some code --- src/app.jsx | 178 +++++++++++++++++++---------------- src/components/shortcuts.jsx | 8 +- 2 files changed, 104 insertions(+), 82 deletions(-) diff --git a/src/app.jsx b/src/app.jsx index d3bb1fce..03554578 100644 --- a/src/app.jsx +++ b/src/app.jsx @@ -10,7 +10,7 @@ import { } from 'preact/hooks'; import { matchPath, Route, Routes, useLocation } from 'react-router-dom'; import 'swiped-events'; -import { useSnapshot } from 'valtio'; +import { subscribe, useSnapshot } from 'valtio'; import BackgroundService from './components/background-service'; import ComposeButton from './components/compose-button'; @@ -108,8 +108,25 @@ setTimeout(() => { // ); })(); +subscribe(states, (changes) => { + for (const [action, path, value, prevValue] of changes) { + // Change #app dataset based on settings.shortcutsViewMode + if (path.join('.') === 'settings.shortcutsViewMode') { + const $app = document.getElementById('app'); + if ($app) { + $app.dataset.shortcutsViewMode = states.shortcuts?.length ? value : ''; + } + } + + // Add/Remove cloak class to body + if (path.join('.') === 'settings.cloakMode') { + const $body = document.body; + $body.classList.toggle('cloak', value); + } + } +}); + function App() { - const snapStates = useSnapshot(states); const [isLoggedIn, setIsLoggedIn] = useState(false); const [uiState, setUIState] = useState('loading'); @@ -192,12 +209,70 @@ function App() { }, []); let location = useLocation(); - states.currentLocation = location.pathname; + // states.currentLocation = location.pathname; + useLayoutEffect(() => { + states.currentLocation = location.pathname; + }, [location.pathname]); useEffect(focusDeck, [location, isLoggedIn]); + if (/\/https?:/.test(location.pathname)) { + return ; + } + + return ( + <> + + + {uiState === 'default' && ( + + } /> + + )} + {isLoggedIn && } + {isLoggedIn && } + + {isLoggedIn && } + + {uiState !== 'loading' && } + + + ); +} + +function PrimaryRoutes({ isLoggedIn, loading }) { + const location = useLocation(); + const nonRootLocation = useMemo(() => { + const { pathname } = location; + return !/^\/(login|welcome)/.test(pathname); + }, [location]); + + return ( + + + ) : loading ? ( + + ) : ( + + ) + } + /> + } /> + } /> + + ); +} + +function SecondaryRoutes({ isLoggedIn }) { + const snapStates = useSnapshot(states); + const location = useLocation(); const prevLocation = snapStates.prevLocation; const backgroundLocation = useRef(prevLocation || null); + const isModalPage = useMemo(() => { return ( matchPath('/:instance/s/:id', location.pathname) || @@ -214,91 +289,32 @@ function App() { location, }); - if (/\/https?:/.test(location.pathname)) { - return ; - } - - const nonRootLocation = useMemo(() => { - const { pathname } = location; - return !/^\/(login|welcome)/.test(pathname); - }, [location]); - - // Change #app dataset based on snapStates.settings.shortcutsViewMode - useEffect(() => { - const $app = document.getElementById('app'); - if ($app) { - $app.dataset.shortcutsViewMode = snapStates.shortcuts?.length - ? snapStates.settings.shortcutsViewMode - : ''; - } - }, [snapStates.shortcuts, snapStates.settings.shortcutsViewMode]); - - // Add/Remove cloak class to body - useEffect(() => { - const $body = document.body; - $body.classList.toggle('cloak', snapStates.settings.cloakMode); - }, [snapStates.settings.cloakMode]); - return ( - <> - - - ) : uiState === 'loading' ? ( - - ) : ( - - ) - } - /> - } /> - } /> - - - {isLoggedIn && ( + + {isLoggedIn && ( + <> } /> - )} - {isLoggedIn && } />} - {isLoggedIn && } />} - {isLoggedIn && } />} - {isLoggedIn && } />} - {isLoggedIn && ( + } /> + } /> + } /> + } /> } /> } /> - )} - {isLoggedIn && } />} - } /> - } /> - - } /> - } /> - - } /> - } /> - {/* } /> */} - - {uiState === 'default' && ( - - } /> - + } /> + )} - {isLoggedIn && } - {isLoggedIn && - !snapStates.settings.shortcutsColumnsMode && - snapStates.settings.shortcutsViewMode !== 'multi-column' && ( - - )} - - {isLoggedIn && } - - {uiState !== 'loading' && } - - + } /> + } /> + + } /> + } /> + + } /> + } /> + {/* } /> */} + ); } diff --git a/src/components/shortcuts.jsx b/src/components/shortcuts.jsx index a9166772..e02f76a8 100644 --- a/src/components/shortcuts.jsx +++ b/src/components/shortcuts.jsx @@ -18,11 +18,17 @@ import MenuLink from './menu-link'; function Shortcuts() { const { instance } = api(); const snapStates = useSnapshot(states); - const { shortcuts } = snapStates; + const { shortcuts, settings } = snapStates; if (!shortcuts.length) { return null; } + if ( + settings.shortcutsColumnsMode || + settings.shortcutsViewMode === 'multi-column' + ) { + return null; + } const menuRef = useRef();