Bluesky app fork with some witchin' additions 馃挮
0
fork

Configure Feed

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

at theme-changes 89 lines 2.2 kB view raw
1import { 2 createContext, 3 type PropsWithChildren, 4 useCallback, 5 useContext, 6 useEffect, 7 useState, 8} from 'react' 9 10import * as persisted from '#/state/persisted' 11 12interface PostReplacementState { 13 enabled: boolean 14 postName: string 15 postsName: string 16} 17 18type StateContext = PostReplacementState 19type SetContext = ( 20 v: 21 | PostReplacementState 22 | ((curr: PostReplacementState) => PostReplacementState), 23) => void 24 25const stateContext = createContext<StateContext>( 26 persisted.defaults.postReplacement as PostReplacementState, 27) 28const setContext = createContext<SetContext>( 29 ( 30 _: 31 | PostReplacementState 32 | ((curr: PostReplacementState) => PostReplacementState), 33 ) => {}, 34) 35 36export function Provider({children}: PropsWithChildren<{}>) { 37 const [state, _setState] = useState<PostReplacementState>(() => { 38 const persistedState = persisted.get('postReplacement') 39 return { 40 enabled: 41 persistedState?.enabled ?? persisted.defaults.postReplacement.enabled!, 42 postName: 43 persistedState?.postName ?? 44 persisted.defaults.postReplacement.postName!, 45 postsName: 46 persistedState?.postsName ?? 47 persisted.defaults.postReplacement.postsName!, 48 } 49 }) 50 51 const setState = useCallback( 52 ( 53 val: 54 | PostReplacementState 55 | ((curr: PostReplacementState) => PostReplacementState), 56 ) => { 57 _setState(curr => { 58 const next = typeof val === 'function' ? val(curr) : val 59 persisted.write('postReplacement', next) 60 return next 61 }) 62 }, 63 [], 64 ) 65 66 useEffect(() => { 67 return persisted.onUpdate('postReplacement', next => { 68 setState({ 69 postName: next.postName ?? 'skeet', 70 postsName: next.postsName ?? 'skeets', 71 enabled: next.enabled ?? true, 72 }) 73 }) 74 }, [setState]) 75 76 return ( 77 <stateContext.Provider value={state}> 78 <setContext.Provider value={setState}>{children}</setContext.Provider> 79 </stateContext.Provider> 80 ) 81} 82 83export function usePostReplacement() { 84 return useContext(stateContext) 85} 86 87export function useSetPostReplacement() { 88 return useContext(setContext) 89}