From 900d4b696bd6ba0a4398487d41f228d38e31ed04 Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Thu, 13 Jun 2024 16:11:16 +0800 Subject: [PATCH] Reduce flicker when loading settings from local storage --- .../web/components/templates/NavigationLayout.tsx | 14 ++++++++++++-- packages/web/lib/hooks/usePersistedState.tsx | 6 ++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/packages/web/components/templates/NavigationLayout.tsx b/packages/web/components/templates/NavigationLayout.tsx index a1bb2f4ad..c3a8b09d0 100644 --- a/packages/web/components/templates/NavigationLayout.tsx +++ b/packages/web/components/templates/NavigationLayout.tsx @@ -44,7 +44,7 @@ export function NavigationLayout(props: NavigationLayoutProps): JSX.Element { const [showKeyboardCommandsModal, setShowKeyboardCommandsModal] = useState(false) - const [showNavMenu, setShowNavMenu] = usePersistedState({ + const [showNavMenu, setShowNavMenu, isLoading] = usePersistedState({ key: 'nav-show-menu', isSessionStorage: false, initialValue: true, @@ -111,6 +111,16 @@ export function NavigationLayout(props: NavigationLayoutProps): JSX.Element { } }, [showLogout]) + if (isLoading) { + return ( + + ) + } + return ( - {showNavMenu && ( + {!isLoading && showNavMenu && ( ({ key: string initialValue: T isSessionStorage?: boolean -}): [T, (x: T | ((prev: T) => T)) => void] { +}): [T, (x: T | ((prev: T) => T)) => void, boolean] { // State to store our value // Pass initial state function to useState so logic is only executed once + const [isLoading, setIsLoading] = useState(true) const [storedValue, setStoredValue] = useState(initialValue) useEffect(() => { @@ -27,6 +28,7 @@ export function usePersistedState({ if (item) { setStoredValue(JSON.parse(item)) } + setIsLoading(false) } catch (error) { // If error also return initialValue console.log(error) @@ -56,5 +58,5 @@ export function usePersistedState({ } } - return [storedValue, setValue] + return [storedValue, setValue, isLoading] }