mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at next/base 42 lines 1.2 kB view raw
1import React from 'react' 2 3import * as persisted from '#/state/persisted' 4 5type StateContext = boolean 6type SetContext = (v: boolean) => void 7 8const stateContext = React.createContext<StateContext>( 9 Boolean(persisted.defaults.subtitlesEnabled), 10) 11const setContext = React.createContext<SetContext>((_: boolean) => {}) 12 13export function Provider({children}: {children: React.ReactNode}) { 14 const [state, setState] = React.useState( 15 Boolean(persisted.get('subtitlesEnabled')), 16 ) 17 18 const setStateWrapped = React.useCallback( 19 (subtitlesEnabled: persisted.Schema['subtitlesEnabled']) => { 20 setState(Boolean(subtitlesEnabled)) 21 persisted.write('subtitlesEnabled', subtitlesEnabled) 22 }, 23 [setState], 24 ) 25 26 React.useEffect(() => { 27 return persisted.onUpdate('subtitlesEnabled', nextSubtitlesEnabled => { 28 setState(Boolean(nextSubtitlesEnabled)) 29 }) 30 }, [setStateWrapped]) 31 32 return ( 33 <stateContext.Provider value={state}> 34 <setContext.Provider value={setStateWrapped}> 35 {children} 36 </setContext.Provider> 37 </stateContext.Provider> 38 ) 39} 40 41export const useSubtitlesEnabled = () => React.useContext(stateContext) 42export const useSetSubtitlesEnabled = () => React.useContext(setContext)