-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
kawaii.tsx
50 lines (40 loc) · 1.26 KB
/
kawaii.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import React from 'react'
import {isWeb} from '#/platform/detection'
import * as persisted from '#/state/persisted'
type StateContext = persisted.Schema['kawaii']
const stateContext = React.createContext<StateContext>(
persisted.defaults.kawaii,
)
export function Provider({children}: React.PropsWithChildren<{}>) {
const [state, setState] = React.useState(persisted.get('kawaii'))
const setStateWrapped = React.useCallback(
(kawaii: persisted.Schema['kawaii']) => {
setState(kawaii)
persisted.write('kawaii', kawaii)
},
[setState],
)
React.useEffect(() => {
return persisted.onUpdate('kawaii', nextKawaii => {
setState(nextKawaii)
})
}, [setStateWrapped])
React.useEffect(() => {
// dumb and stupid but it's web only so just refresh the page if you want to change it
if (isWeb) {
const kawaii = new URLSearchParams(window.location.search).get('kawaii')
switch (kawaii) {
case 'true':
setStateWrapped(true)
break
case 'false':
setStateWrapped(false)
break
}
}
}, [setStateWrapped])
return <stateContext.Provider value={state}>{children}</stateContext.Provider>
}
export function useKawaiiMode() {
return React.useContext(stateContext)
}