-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
85 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { type PropsWithChildren, useRef } from 'react'; | ||
import type { StoreInterface, StoreType } from './store'; | ||
import { initializeStore, StoreContextProvider } from './store'; | ||
|
||
export default function StoreProvider({ | ||
children, | ||
...props | ||
}: PropsWithChildren) { | ||
const storeRef = useRef<StoreType>(); | ||
|
||
if (!storeRef.current) { | ||
storeRef.current = initializeStore(props); | ||
} | ||
|
||
return ( | ||
<StoreContextProvider value={storeRef.current}> | ||
{children} | ||
</StoreContextProvider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { createContext, useContext } from 'react'; | ||
import { createStore, useStore as useZustandStore } from 'zustand'; | ||
import { PreloadedStoreInterface } from './store-provider'; | ||
|
||
export interface StoreInterface { | ||
lastUpdate: number; | ||
light: boolean; | ||
count: number; | ||
tick: (lastUpdate: number) => void; | ||
increment: () => void; | ||
decrement: () => void; | ||
reset: () => void; | ||
} | ||
|
||
function getDefaultInitialState() { | ||
return { | ||
lastUpdate: new Date(1970, 1, 1).getTime(), | ||
light: false, | ||
count: 0, | ||
} as const; | ||
} | ||
|
||
export type StoreType = ReturnType<typeof initializeStore>; | ||
|
||
const storeContext = createContext<StoreType | null>(null); | ||
|
||
export const StoreContextProvider = storeContext.Provider; | ||
|
||
export function useStore<T>(selector: (state: StoreInterface) => T) { | ||
const store = useContext(storeContext); | ||
|
||
if (!store) throw new Error('Store is missing the provider'); | ||
|
||
return useZustandStore(store, selector); | ||
} | ||
|
||
export const initializeStore = (preloadedState: PreloadedStoreInterface) => { | ||
return createStore<StoreInterface>((set, get) => ({ | ||
...getDefaultInitialState(), | ||
...preloadedState, | ||
tick: (lastUpdate) => | ||
set({ | ||
lastUpdate, | ||
light: !get().light, | ||
}), | ||
increment: () => | ||
set({ | ||
count: get().count + 1, | ||
}), | ||
decrement: () => | ||
set({ | ||
count: get().count - 1, | ||
}), | ||
reset: () => | ||
set({ | ||
count: getDefaultInitialState().count, | ||
}), | ||
})); | ||
}; |