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['showLinkInHandle']
6type SetContext = (v: persisted.Schema['showLinkInHandle']) => void
7
8const stateContext = React.createContext<StateContext>(
9 persisted.defaults.showLinkInHandle,
10)
11const setContext = React.createContext<SetContext>(
12 (_: persisted.Schema['showLinkInHandle']) => {},
13)
14
15export function Provider({children}: React.PropsWithChildren<{}>) {
16 const [state, setState] = React.useState(persisted.get('showLinkInHandle'))
17
18 const setStateWrapped = React.useCallback(
19 (showLinkInHandle: persisted.Schema['showLinkInHandle']) => {
20 setState(showLinkInHandle)
21 persisted.write('showLinkInHandle', showLinkInHandle)
22 },
23 [setState],
24 )
25
26 React.useEffect(() => {
27 return persisted.onUpdate('showLinkInHandle', nextShowLinkInHandle => {
28 setState(nextShowLinkInHandle)
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 function useShowLinkInHandle() {
42 return React.useContext(stateContext)
43}
44
45export function useSetShowLinkInHandle() {
46 return React.useContext(setContext)
47}