-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpreferences-context.tsx
More file actions
106 lines (92 loc) · 3.17 KB
/
preferences-context.tsx
File metadata and controls
106 lines (92 loc) · 3.17 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import Cookies from "js-cookie"
import React, { useContext, useEffect, useState } from "react"
import * as Dailp from "src/graphql/dailp"
import { LevelOfDetail } from "./types"
// Set up context for preferences
const PreferencesContext = React.createContext({
levelOfDetail: LevelOfDetail.Pronunciation,
setLevelOfDetail: (p: LevelOfDetail) => {},
cherokeeRepresentation: Dailp.CherokeeOrthography.Learner,
setCherokeeRepresentation: (p: Dailp.CherokeeOrthography) => {},
})
export const usePreferences = () => useContext(PreferencesContext)
export const PreferencesProvider = (props: any) => {
// Some preferences hooks setup
const [levelOfDetail, setLevelOfDetail] = useState(savedLevelOfDetail())
const [cherokeeRepresentation, setCherokeeRepresentation] = useState(
savedCherokeeRepresentation()
)
useEffect(() => {
save(PreferenceKey.LevelOfDetail, levelOfDetail)
save(PreferenceKey.CherokeeRepresentation, cherokeeRepresentation)
}, [levelOfDetail, cherokeeRepresentation])
useEffect(() => {
// Remove all deprecated cookies, keep this code for at least several months
// to let users clear their site cookies.
Cookies.remove(DeprecatedCookie.LevelOfDetail)
Cookies.remove(DeprecatedCookie.Romanization)
}, [])
return (
<PreferencesContext.Provider
value={{
levelOfDetail,
setLevelOfDetail,
cherokeeRepresentation,
setCherokeeRepresentation,
}}
>
{props.children}
</PreferencesContext.Provider>
)
}
function save(key: PreferenceKey, value: any) {
LocalStorage.setItem(key, value.toString())
}
// Avoid changing these keys at all costs, because that will essentially reset
// saved user preferences.
enum PreferenceKey {
LevelOfDetail = "level-of-detail",
CherokeeRepresentation = "cherokee-system",
}
enum DeprecatedCookie {
// TODO Remove this after several months, giving users time to have their
// settings migrated.
LevelOfDetail = "experienceLevel",
Romanization = "phonetics",
}
const LocalStorage =
typeof window !== "undefined"
? window.localStorage
: { getItem: () => null, setItem: () => {} }
const savedLevelOfDetail = (): LevelOfDetail => {
const newValue = LocalStorage.getItem(PreferenceKey.LevelOfDetail)
const oldValue = Cookies.get(DeprecatedCookie.LevelOfDetail)
if (newValue) {
return Number.parseInt(newValue) as LevelOfDetail
} else if (oldValue) {
const parsed = Number.parseInt(oldValue) as LevelOfDetail
return Math.min(parsed, LevelOfDetail.Segmentation)
} else {
return LevelOfDetail.Pronunciation
}
}
const savedCherokeeRepresentation = (): Dailp.CherokeeOrthography => {
const newValue = LocalStorage.getItem(
PreferenceKey.CherokeeRepresentation
) as Dailp.CherokeeOrthography
const oldValue = Cookies.get(DeprecatedCookie.LevelOfDetail)
if (newValue) {
return newValue
} else if (oldValue) {
const parsed = Number.parseInt(oldValue)
if (parsed === 3) {
return Dailp.CherokeeOrthography.Crg
} else if (parsed === 4) {
return Dailp.CherokeeOrthography.Taoc
} else {
return Dailp.CherokeeOrthography.Learner
}
} else {
return Dailp.CherokeeOrthography.Learner
}
}