From 593ba275579c49f3fa78090f5109e7b891bb6fa5 Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Thu, 5 Jan 2023 18:39:00 +0800 Subject: [PATCH] refactor: raise error context higher than state context fixes #275 --- src/components/Gitako.tsx | 8 ++--- src/components/SideBar.tsx | 46 ++++++++++++++++++----------- src/components/ToggleShowButton.tsx | 7 +++-- src/containers/SideBarState.tsx | 22 ++++++++++---- 4 files changed, 54 insertions(+), 29 deletions(-) diff --git a/src/components/Gitako.tsx b/src/components/Gitako.tsx index b3ce0ad..3151b60 100644 --- a/src/components/Gitako.tsx +++ b/src/components/Gitako.tsx @@ -18,15 +18,15 @@ export function Gitako() { - - + + - - + + diff --git a/src/components/SideBar.tsx b/src/components/SideBar.tsx index d6c88fa..8c30f9e 100644 --- a/src/components/SideBar.tsx +++ b/src/components/SideBar.tsx @@ -65,24 +65,12 @@ export function SideBar() { return ( + - - {() => { - const logoContainerElement = useLogoContainerElement() - return ( - - setShouldExpand(true) : undefined} - onClick={toggleShowSideBar} - /> - - ) - }} -
> + toggleShowSideBar: () => void +}) { + const logoContainerElement = useLogoContainerElement() + const { sidebarToggleMode } = useConfigs().value + return ( + + setShouldExpand(true) : undefined} + onClick={toggleShowSideBar} + /> + + ) +} + function useFocusSidebarOnExpand(shouldExpand: boolean) { React.useEffect(() => { // prevent keeping focus within Gitako diff --git a/src/components/ToggleShowButton.tsx b/src/components/ToggleShowButton.tsx index 5e23d19..11f83b4 100644 --- a/src/components/ToggleShowButton.tsx +++ b/src/components/ToggleShowButton.tsx @@ -1,15 +1,16 @@ import { SyncIcon } from '@primer/octicons-react' import iconURL from 'assets/icons/Gitako.png' import { useConfigs } from 'containers/ConfigsContext' +import { SideBarErrorContext } from 'containers/ErrorContext' import { ReloadContext } from 'containers/ReloadContext' import * as React from 'react' import { useDebounce, useWindowSize } from 'react-use' import { cx } from 'utils/cx' +import { useLoadedContext } from 'utils/hooks/useLoadedContext' import { useResizeHandler } from 'utils/hooks/useResizeHandler' import { RoundIconButton } from './RoundIconButton' type Props = { - error?: string | null className?: React.HTMLAttributes['className'] onHover?: React.HTMLAttributes['onMouseEnter'] onClick?: (e: PointerEvent) => void @@ -21,8 +22,10 @@ function getSafeDistance(y: number, height: number) { return Math.max(0, Math.min(y, height - buttonHeight)) } -export function ToggleShowButton({ error, className, onClick, onHover }: Props) { +export function ToggleShowButton({ className, onClick, onHover }: Props) { const reload = React.useContext(ReloadContext) + const error = useLoadedContext(SideBarErrorContext).value + const ref = React.useRef(null) const config = useConfigs() const [distance, setDistance] = React.useState(config.value.toggleButtonVerticalDistance) diff --git a/src/containers/SideBarState.tsx b/src/containers/SideBarState.tsx index d70816e..6030adf 100644 --- a/src/containers/SideBarState.tsx +++ b/src/containers/SideBarState.tsx @@ -1,6 +1,8 @@ import { PropsWithChildren } from 'common' import * as React from 'react' +import { useLoadedContext } from 'utils/hooks/useLoadedContext' import { useStateIO } from 'utils/hooks/useStateIO' +import { SideBarErrorContext } from './ErrorContext' import { useInspector } from './StateInspector' export type SideBarState = @@ -13,7 +15,8 @@ export type SideBarState = | 'tree-rendering' | 'tree-rendered' | 'idle' - | 'error-due-to-auth' + | 'error' // when error occurs, sidebar should never expand + | 'error-due-to-auth' // this is a special error, user can expand sidebar and set token to fix the error export type SideBarStateContextShape = IO @@ -22,10 +25,17 @@ export const SideBarStateContext = React.createContext('disabled') useInspector('SideBarStateContext', $state.value) - - return ( - - {$state.value !== null && children} - + const error = useLoadedContext(SideBarErrorContext).value + const $$state: IO = React.useMemo( + () => + error && $state.value !== 'error' + ? { + ...$state, + value: 'error', + } + : $state, + [$state, error], ) + + return {children} }