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