diff --git a/src/common.d.ts b/src/common.d.ts new file mode 100644 index 0000000..eee4e97 --- /dev/null +++ b/src/common.d.ts @@ -0,0 +1,7 @@ +// Similar to `global.d.ts` but with import/export +import { Dispatch, SetStateAction } from 'react' + +type ReactIO = { + value: T + onChange: Dispatch> +} diff --git a/src/components/Gitako.tsx b/src/components/Gitako.tsx index 8fc22db..a119218 100644 --- a/src/components/Gitako.tsx +++ b/src/components/Gitako.tsx @@ -1,6 +1,7 @@ import { SideBar } from 'components/SideBar' import { ConfigsContextWrapper } from 'containers/ConfigsContext' import { ReloadContextWrapper } from 'containers/ReloadContext' +import { InspectorContextWrapper } from 'containers/StateInspector' import * as React from 'react' import { ErrorBoundary } from '../containers/ErrorBoundary' import { StateBarErrorContextWrapper } from '../containers/ErrorContext' @@ -10,20 +11,22 @@ import { StateBarStateContextWrapper } from '../containers/SideBarState' export function Gitako() { return ( - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + ) } diff --git a/src/containers/ErrorContext.tsx b/src/containers/ErrorContext.tsx index 53d9455..01c3c7a 100644 --- a/src/containers/ErrorContext.tsx +++ b/src/containers/ErrorContext.tsx @@ -1,3 +1,4 @@ +import { useInspector } from 'containers/StateInspector' import * as React from 'react' import { useStateIO } from 'utils/hooks/useStateIO' @@ -7,6 +8,7 @@ export const SideBarErrorContext = React.createContext) { const $error = useStateIO(null) + useInspector('SideBarErrorContext', $error.value) return {children} } diff --git a/src/containers/SideBarState.tsx b/src/containers/SideBarState.tsx index 815a122..8b1ba9d 100644 --- a/src/containers/SideBarState.tsx +++ b/src/containers/SideBarState.tsx @@ -1,5 +1,6 @@ import * as React from 'react' import { useStateIO } from 'utils/hooks/useStateIO' +import { useInspector } from './StateInspector' export type SideBarState = | 'disabled' @@ -19,6 +20,7 @@ export const SideBarStateContext = React.createContext) { const $state = useStateIO('disabled') + useInspector('SideBarStateContext', $state.value) return ( diff --git a/src/containers/StateInspector.tsx b/src/containers/StateInspector.tsx new file mode 100644 index 0000000..3591642 --- /dev/null +++ b/src/containers/StateInspector.tsx @@ -0,0 +1,58 @@ +import { ReactIO } from 'common' +import { IN_PRODUCTION_MODE } from 'env' +import * as React from 'react' +import { useStateIO } from 'utils/hooks/useStateIO' + +export type InspectorContextShape = ReactIO + +export const InspectorContext = React.createContext(null) + +export const InspectorContextWrapper = IN_PRODUCTION_MODE + ? React.Fragment + : function InspectorContextWrapper({ children }: React.PropsWithChildren<{}>) { + const $ = useStateIO({}) + const [show, setShow] = React.useState(true) + + return ( + + {show ? ( +
+
{JSON.stringify($.value, null, 2)}
+
+ +
+
+ ) : ( +
+ +
+ )} + {children} +
+ ) + } + +export function useInspector(key: string, value: JSONValue) { + const $ = React.useContext(InspectorContext) + React.useEffect(() => { + $?.onChange(prev => ({ ...prev, [key]: value })) + }, [key, value]) // eslint-disable-line react-hooks/exhaustive-deps +}