From e740d67d44903cf83cd11b74bca0889d07b5d405 Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Mon, 28 Mar 2022 00:03:00 +0800 Subject: [PATCH] fix: prevent auto collapsing sidebar when enabling `Auto Expand` or pinning sidebar fix #155 --- src/components/SideBar.tsx | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/components/SideBar.tsx b/src/components/SideBar.tsx index a2cb811..f2b82d5 100644 --- a/src/components/SideBar.tsx +++ b/src/components/SideBar.tsx @@ -55,7 +55,13 @@ export function SideBar() { const sidebarToggleMode = configContext.value.sidebarToggleMode const intelligentToggle = configContext.value.intelligentToggle - const $shouldShow = useStateIO(intelligentToggle === null ? false : intelligentToggle) + const $shouldShow = useStateIO(() => + intelligentToggle === null + ? sidebarToggleMode === 'persistent' + ? platform.shouldShow() + : false + : intelligentToggle, + ) const shouldShow = $shouldShow.value React.useEffect(() => { if (sidebarToggleMode === 'persistent') { @@ -69,7 +75,7 @@ export function SideBar() { } }, [shouldShow, sidebarToggleMode]) - // Save expand state on toggle if auto expand if not on + // Save expand state on toggle if auto expand is off React.useEffect(() => { if (intelligentToggle !== null) { configContext.onChange({ intelligentToggle: shouldShow }) @@ -96,7 +102,13 @@ export function SideBar() { }, [error]) useToggleSideBarWithKeyboard(state, configContext, toggleShowSideBar) - useSetShouldShowOnPJAXDone(setShowSideBar) + const updateSideBarVisibility = React.useCallback(() => { + if (intelligentToggle === null && sidebarToggleMode === 'persistent') { + setShowSideBar(platform.shouldShow()) + } + }, [intelligentToggle, sidebarToggleMode]) + + useOnPJAXDone(updateSideBarVisibility) platform.usePlatformHooks?.() @@ -201,16 +213,3 @@ export function SideBar() { ) } - -function useSetShouldShowOnPJAXDone(setShouldShow: (value: boolean) => void) { - const { intelligentToggle, sidebarToggleMode } = useConfigs().value - const updateSideBarVisibility = React.useCallback(() => { - if (intelligentToggle === null && sidebarToggleMode === 'persistent') { - setShouldShow(platform.shouldShow()) - } - }, [intelligentToggle, sidebarToggleMode]) - React.useEffect(() => { - updateSideBarVisibility() - }, [updateSideBarVisibility]) - useOnPJAXDone(updateSideBarVisibility) -}