diff --git a/packages/logos-docusaurus-theme/src/client/containers/GlobalStore/GlobalStore.tsx b/packages/logos-docusaurus-theme/src/client/containers/GlobalStore/GlobalStore.tsx deleted file mode 100644 index cb83999..0000000 --- a/packages/logos-docusaurus-theme/src/client/containers/GlobalStore/GlobalStore.tsx +++ /dev/null @@ -1,32 +0,0 @@ -import { Store as StoreBase } from '@logos-theme/lib/store/index' - -export namespace GlobalStore { - export type State = { - hiddenSidebar: boolean - } - - export type Actions = { - toggleSidebar: () => void - setHiddenSidebar: (payload: any) => void - } - - export class Store extends StoreBase {} -} - -export const globalStore = new GlobalStore.Store(() => { - return { - toggleSidebar: (payload, state, setState, dispatch) => { - setState((state) => void (state.hiddenSidebar = !state.hiddenSidebar)) - }, - setHiddenSidebar: (payload, state, setState, dispatch) => { - setState((state) => { - state.hiddenSidebar = - typeof payload === 'function' ? payload(state.hiddenSidebar) : payload - }) - }, - } -}, []) - -export const selectHiddenSidebar = globalStore.selector( - (state) => state.hiddenSidebar, -) diff --git a/packages/logos-docusaurus-theme/src/client/containers/GlobalStore/index.ts b/packages/logos-docusaurus-theme/src/client/containers/GlobalStore/index.ts deleted file mode 100644 index 9e01b41..0000000 --- a/packages/logos-docusaurus-theme/src/client/containers/GlobalStore/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './GlobalStore' diff --git a/packages/logos-docusaurus-theme/src/client/lib/store/Hook.tsx b/packages/logos-docusaurus-theme/src/client/lib/store/Hook.tsx deleted file mode 100644 index c4ca4e9..0000000 --- a/packages/logos-docusaurus-theme/src/client/lib/store/Hook.tsx +++ /dev/null @@ -1,14 +0,0 @@ -import React, { useEffect } from 'react' - -export const Hook: React.FC<{ - hook: Function - onChange: (value: any) => void -}> = ({ hook: useHook, onChange }) => { - const value = useHook() - - useEffect(() => { - onChange(value) - }, [value]) - - return <> -} diff --git a/packages/logos-docusaurus-theme/src/client/lib/store/index.ts b/packages/logos-docusaurus-theme/src/client/lib/store/index.ts deleted file mode 100644 index 16c8633..0000000 --- a/packages/logos-docusaurus-theme/src/client/lib/store/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './store' diff --git a/packages/logos-docusaurus-theme/src/client/lib/store/store.tsx b/packages/logos-docusaurus-theme/src/client/lib/store/store.tsx deleted file mode 100644 index 2e70d3f..0000000 --- a/packages/logos-docusaurus-theme/src/client/lib/store/store.tsx +++ /dev/null @@ -1,137 +0,0 @@ -import produce from 'immer' -import React, { useContext, useMemo, useRef, useState } from 'react' -import { updateArray } from '../array.utils' -import { Hook } from './Hook' - -type SetState = (value: S | ((currentState: S) => void)) => void - -type StoreType = { - state: S - setState: SetState - - dispatch: D -} - -type Dispatch = (payload: T) => void - -type DispatchActions = { - [key: string]: Dispatch -} - -type StoreActions< - S, - A extends DispatchActions, - I extends Array = [], -> = { - [K in keyof A]: ( - payload: Parameters[0], - state: S, - setState: SetState, - dispatch: A, - ...injected: I - ) => void -} - -export class Store< - S = {}, - A extends DispatchActions = {}, - I extends any[] | [] = [], -> { - stateRef: React.MutableRefObject - setStateRef: React.MutableRefObject> - injectedRef: React.MutableRefObject> - - private context: React.Context> - - constructor( - private actions: () => StoreActions, - private inject: any[] = [], - ) {} - - public useCreateStore = (initialState: S): StoreType => { - const [state, _setState] = useState(initialState) - - const setState: SetState = (value) => - typeof value === 'function' - ? _setState(produce(value)) - : _setState(value) - - const dispatch = useMemo(() => this.createDispatch(), []) - - return { state, setState, dispatch } - } - - public Provider: React.FC }>> = - ({ children, store }) => { - this.stateRef = useRef(store.state) - this.setStateRef = useRef(store.setState) - this.injectedRef = useRef(this.inject.map((hook) => null)) - - if (!this.context) { - this.context = React.createContext(null as any) - } - - const P = this.context.Provider - - this.stateRef.current = store.state - this.setStateRef.current = store.setState - - return ( -

- {children} - - {this.inject.map((hook, index) => ( - { - this.injectedRef.current = updateArray( - this.injectedRef.current, - index, - index, - () => value, - ) - }} - /> - ))} -

- ) - } - - public useStore = () => { - return useContext(this.context) - } - - public useDispatch = () => this.useStore().dispatch - - useSelector(selector: (state: S) => T): T { - const { state } = this.useStore() - const value = selector(state) - - return useMemo(() => value, [value]) - } - - selector(selector: (state: S) => T) { - return selector - } - - private createDispatch = (): A => { - const dispatch = Object.fromEntries( - Object.entries(this.actions()).map(([name, func]) => [ - name, - (payload: any) => { - const set = (args: any) => this.setStateRef.current(args) - func( - payload, - this.stateRef.current, - set, - dispatch, - ...this.injectedRef.current, - ) - }, - ]), - ) - - return dispatch as A - } -}