From 4e680401acadaffcdf3359f2945d7c8f173ced8f Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Mon, 17 May 2021 00:06:50 +0800 Subject: [PATCH] refactor: simplify hooks --- src/components/SideBar.tsx | 85 ++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 44 deletions(-) diff --git a/src/components/SideBar.tsx b/src/components/SideBar.tsx index 8967889..e2b022d 100644 --- a/src/components/SideBar.tsx +++ b/src/components/SideBar.tsx @@ -36,9 +36,7 @@ export function SideBar() { const $showSettings = useStateIO(false) const showSettings = $showSettings.value - const toggleShowSettings = React.useCallback(function toggleShowSettings() { - $showSettings.onChange(show => !show) - }, []) + const toggleShowSettings = React.useCallback(() => $showSettings.onChange(show => !show), []) const $logoContainerElement = useStateIO(null) @@ -57,58 +55,34 @@ export function SideBar() { const shouldShow = $shouldShow.value React.useEffect(() => { DOMHelper.setBodyIndent(shouldShow) - }, [shouldShow]) - - React.useEffect(() => { if (shouldShow) { DOMHelper.focusFileExplorer() // TODO: verify if it works } }, [shouldShow]) - const toggleShowSideBar = React.useCallback(() => { - $shouldShow.onChange(shouldShow => { - const { - value: { intelligentToggle }, - } = configContext - if (intelligentToggle !== null) { - configContext.onChange({ intelligentToggle: !shouldShow }) - } - - return !shouldShow - }) - }, []) - useToggleSideBarWithKeyboard(state, configContext, toggleShowSideBar) const intelligentToggle = configContext.value.intelligentToggle - // Hide sidebar when error due to auth but token is set #128 - const hideSidebarOnInvalidToken: boolean = - intelligentToggle === null && Boolean(state === 'error-due-to-auth' && accessToken) + + // Save expand state on toggle if auto expand if not on React.useEffect(() => { - if (hideSidebarOnInvalidToken) { - $shouldShow.onChange(false) - } else { - const shouldShow = intelligentToggle === null ? platform.shouldShow() : intelligentToggle - $shouldShow.onChange(shouldShow) + if (intelligentToggle !== null) { + configContext.onChange({ intelligentToggle: shouldShow }) } - }, [intelligentToggle, hideSidebarOnInvalidToken, metaData]) + }, [shouldShow, intelligentToggle]) const error = useLoadedContext(SideBarErrorContext).value + // Lock shouldShow on error React.useEffect(() => { - if (error) { + if (error && shouldShow) { $shouldShow.onChange(false) } - }, [error]) + }, [error, shouldShow]) - const updateSideBarVisibility = React.useCallback( - function updateSideBarVisibility() { - if (hideSidebarOnInvalidToken) { - $shouldShow.onChange(false) - } else if (intelligentToggle === null) { - $shouldShow.onChange(platform.shouldShow()) - } - }, - [metaData?.branchName, intelligentToggle, hideSidebarOnInvalidToken], - ) - useOnPJAXDone(updateSideBarVisibility) + const toggleShowSideBar = React.useCallback(() => { + if (!error) $shouldShow.onChange(show => !show) + }, [error]) + useToggleSideBarWithKeyboard(state, configContext, toggleShowSideBar) + + useSetShouldShowOnPJAXDone(intelligentToggle, $shouldShow.onChange) useGitHubAttachCopyFileButton(configContext.value.copyFileButton) useGitHubAttachCopySnippetButton(configContext.value.copySnippetButton) @@ -116,13 +90,20 @@ export function SideBar() { usePJAX() useProgressBar() + // Hide sidebar when error due to auth but token is set #128 + const hideSidebarOnInvalidToken: boolean = + intelligentToggle === null && Boolean(state === 'error-due-to-auth' && accessToken) + React.useEffect(() => { + if (hideSidebarOnInvalidToken) { + $shouldShow.onChange(false) + } + }, [hideSidebarOnInvalidToken]) + return (
- {!shouldShow && ( - - )} + {!shouldShow && }
@@ -175,3 +156,19 @@ export function SideBar() { ) } + +function useSetShouldShowOnPJAXDone( + intelligentToggle: boolean | null, + set: (value: boolean) => void, +) { + useOnPJAXDone( + React.useCallback( + function updateSideBarVisibility() { + if (intelligentToggle === null) { + set(platform.shouldShow()) + } + }, + [intelligentToggle], + ), + ) +}