diff options
author | Paul Frazee <pfrazee@gmail.com> | 2023-11-08 09:10:59 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-08 09:10:59 -0800 |
commit | e75b2d508baf9b19e7340657ac2951e9f057b735 (patch) | |
tree | 2c9647d9dc3d47261e4e838313c4b815f622fb6a /src/state/invites.tsx | |
parent | 74f8390f1d879350ebb6516fade2b1d83d1601e7 (diff) | |
download | voidsky-e75b2d508baf9b19e7340657ac2951e9f057b735.tar.zst |
Move invite-state to new persistence + context and replace the notifications with just showing uses in the modal (#1840)
Diffstat (limited to 'src/state/invites.tsx')
-rw-r--r-- | src/state/invites.tsx | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/state/invites.tsx b/src/state/invites.tsx new file mode 100644 index 000000000..6a0d1b590 --- /dev/null +++ b/src/state/invites.tsx @@ -0,0 +1,56 @@ +import React from 'react' +import * as persisted from '#/state/persisted' + +type StateContext = persisted.Schema['invites'] +type ApiContext = { + setInviteCopied: (code: string) => void +} + +const stateContext = React.createContext<StateContext>( + persisted.defaults.invites, +) +const apiContext = React.createContext<ApiContext>({ + setInviteCopied(_: string) {}, +}) + +export function Provider({children}: React.PropsWithChildren<{}>) { + const [state, setState] = React.useState(persisted.get('invites')) + + const api = React.useMemo( + () => ({ + setInviteCopied(code: string) { + setState(state => { + state = { + ...state, + copiedInvites: state.copiedInvites.includes(code) + ? state.copiedInvites + : state.copiedInvites.concat([code]), + } + persisted.write('invites', state) + return state + }) + }, + }), + [setState], + ) + + React.useEffect(() => { + return persisted.onUpdate(() => { + setState(persisted.get('invites')) + }) + }, [setState]) + + return ( + <stateContext.Provider value={state}> + <apiContext.Provider value={api}>{children}</apiContext.Provider> + </stateContext.Provider> + ) +} + +export function useInvitesState() { + return React.useContext(stateContext) +} + +export function useInvitesAPI() { + return React.useContext(apiContext) +} |