forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import React from 'react'
2
3import * as persisted from '#/state/persisted'
4
5type StateContext = persisted.Schema['showExternalShareButtons']
6type SetContext = (v: persisted.Schema['showExternalShareButtons']) => void
7
8const stateContext = React.createContext<StateContext>(
9 persisted.defaults.showExternalShareButtons,
10)
11const setContext = React.createContext<SetContext>(
12 (_: persisted.Schema['showExternalShareButtons']) => {},
13)
14
15export function Provider({children}: React.PropsWithChildren<{}>) {
16 const [state, setState] = React.useState(
17 persisted.get('showExternalShareButtons'),
18 )
19
20 const setStateWrapped = React.useCallback(
21 (
22 showExternalShareButtons: persisted.Schema['showExternalShareButtons'],
23 ) => {
24 setState(showExternalShareButtons)
25 persisted.write('showExternalShareButtons', showExternalShareButtons)
26 },
27 [setState],
28 )
29
30 React.useEffect(() => {
31 return persisted.onUpdate(
32 'showExternalShareButtons',
33 nextShowExternalShareButtons => {
34 setState(nextShowExternalShareButtons)
35 },
36 )
37 }, [setStateWrapped])
38
39 return (
40 <stateContext.Provider value={state}>
41 <setContext.Provider value={setStateWrapped}>
42 {children}
43 </setContext.Provider>
44 </stateContext.Provider>
45 )
46}
47
48export function useShowExternalShareButtons() {
49 return React.useContext(stateContext)
50}
51
52export function useSetShowExternalShareButtons() {
53 return React.useContext(setContext)
54}