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