add store

This commit is contained in:
Hailey
2024-06-07 10:51:49 -07:00
parent 49f1ef2218
commit 070873ebbc
3 changed files with 41 additions and 1 deletions
+35
View File
@@ -0,0 +1,35 @@
import React from 'react'
import * as persisted from '#/state/persisted'
type StateContext = string | undefined
type SetContext = (v: string) => void
const stateContext = React.createContext<StateContext>(undefined)
const setContext = React.createContext<SetContext>((_: string) => {})
export function Provider({children}: {children: React.ReactNode}) {
const [state, setState] = React.useState<string>()
const setStateWrapped = (v: string) => {
setState(v)
persisted.write('starterPackId', v)
}
React.useEffect(() => {
return persisted.onUpdate(() => {
setState(persisted.get('starterPackId'))
})
}, [])
return (
<stateContext.Provider value={state}>
<setContext.Provider value={setStateWrapped}>
{children}
</setContext.Provider>
</stateContext.Provider>
)
}
export const useStarterPack = () => React.useContext(stateContext)
export const useSetStarterPack = () => React.useContext(setContext)