Bluesky app fork with some witchin' additions 馃挮 witchsky.app
bluesky fork client
at feat/custom-appview 60 lines 1.6 kB view raw
1import React from 'react' 2 3import {type EmbedPlayerSource} from '#/lib/strings/embed-player' 4import * as persisted from '#/state/persisted' 5 6type StateContext = persisted.Schema['externalEmbeds'] 7type SetContext = ( 8 source: EmbedPlayerSource, 9 value: 'show' | 'hide' | undefined, 10) => void 11 12const stateContext = React.createContext<StateContext>( 13 persisted.defaults.externalEmbeds, 14) 15stateContext.displayName = 'ExternalEmbedsPrefsStateContext' 16const setContext = React.createContext<SetContext>({} as SetContext) 17setContext.displayName = 'ExternalEmbedsPrefsSetContext' 18 19export function Provider({children}: React.PropsWithChildren<{}>) { 20 const [state, setState] = React.useState(persisted.get('externalEmbeds')) 21 22 const setStateWrapped = React.useCallback( 23 (source: EmbedPlayerSource, value: 'show' | 'hide' | undefined) => { 24 setState(prev => { 25 persisted.write('externalEmbeds', { 26 ...prev, 27 [source]: value, 28 }) 29 30 return { 31 ...prev, 32 [source]: value, 33 } 34 }) 35 }, 36 [setState], 37 ) 38 39 React.useEffect(() => { 40 return persisted.onUpdate('externalEmbeds', nextExternalEmbeds => { 41 setState(nextExternalEmbeds) 42 }) 43 }, [setStateWrapped]) 44 45 return ( 46 <stateContext.Provider value={state}> 47 <setContext.Provider value={setStateWrapped}> 48 {children} 49 </setContext.Provider> 50 </stateContext.Provider> 51 ) 52} 53 54export function useExternalEmbedsPrefs() { 55 return React.useContext(stateContext) 56} 57 58export function useSetExternalEmbedPref() { 59 return React.useContext(setContext) 60}