-
Notifications
You must be signed in to change notification settings - Fork 614
Expand file tree
/
Copy pathindex.ts
More file actions
220 lines (197 loc) · 5.78 KB
/
index.ts
File metadata and controls
220 lines (197 loc) · 5.78 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import * as React from "react"
import { useLocation } from "@reach/router"
import { useState, useEffect, useCallback } from "react"
import { useDispatch } from "react-redux"
import { StorageKey, GAVersion } from "@/constants"
import { Dispatch } from "@/types"
import copyToClipboard from "copy-to-clipboard"
type UsePersistentBoolean = (
key: StorageKey,
initialValue: boolean,
overwrite?: boolean
) => [boolean, React.Dispatch<React.SetStateAction<boolean>>]
export const usePersistentBoolean: UsePersistentBoolean = (
key,
initialValue,
overwrite
) => {
const [value, setValue] = useState<boolean>(() => {
if (overwrite !== undefined) {
return overwrite
}
const fromStorage = IS_SSR ? null : window.localStorage.getItem(key)
if (fromStorage === null) {
return initialValue
}
return JSON.parse(fromStorage).value
})
useEffect(() => {
if (IS_SSR) {
return
}
window.localStorage.setItem(key, JSON.stringify({ value }))
}, [value, key])
return [value, setValue]
}
type UsePersistentString = (
key: StorageKey,
initialValue?: string,
overwrite?: string
) => [
string | undefined,
React.Dispatch<React.SetStateAction<string | undefined>>
]
export const usePersistentString: UsePersistentString = (
key,
initialValue,
overwrite
) => {
if (IS_SSR) {
// This is okay to disable because this will _only_ be true during server
// side rendireng
// eslint-disable-next-line react-hooks/rules-of-hooks
return useState(initialValue)
}
// eslint-disable-next-line react-hooks/rules-of-hooks
const [value, setValue] = useState<string | undefined>(() => {
if (overwrite !== undefined) {
return overwrite
}
const fromStorage = window.localStorage.getItem(key)
if (fromStorage === null) {
return initialValue
}
if (fromStorage === "undefined" || fromStorage === "") {
return undefined
}
return JSON.parse(fromStorage).value
})
// eslint-disable-next-line react-hooks/rules-of-hooks
useEffect(() => {
if (IS_SSR) {
return
}
if (value === undefined) {
window.localStorage.setItem(key, JSON.stringify(undefined))
} else {
window.localStorage.setItem(key, JSON.stringify({ value }))
}
}, [value, key])
return [value, setValue]
}
const getObjectFromLocalStorage = <T>(
key: StorageKey,
defaultValue?: T
): T | undefined => {
if (IS_SSR) {
return undefined
}
let asString = window.localStorage.getItem(key)
if (asString === null || asString === "undefined") {
return defaultValue
}
return JSON.parse(asString)
}
export const usePersistantObject = <T extends {}>(
key: StorageKey,
defaultValue?: T
): [T | undefined, Dispatch<T | undefined>] => {
if (IS_SSR) {
// This is okay to disable because this will _only_ be true during server
// side rendireng
// eslint-disable-next-line react-hooks/rules-of-hooks
return useState<T | undefined>(defaultValue)
}
// eslint-disable-next-line react-hooks/rules-of-hooks
const [localValue, setValueLocal] = useState(() => {
return getObjectFromLocalStorage(key, defaultValue)
})
// eslint-disable-next-line react-hooks/rules-of-hooks
const setValue: Dispatch<T | undefined> = useCallback(
v => {
setValueLocal(old => {
let nu: T | undefined = undefined
if (v instanceof Function) {
nu = v(old)
} else {
nu = v
}
if (!IS_SSR) {
window.localStorage.setItem(key, JSON.stringify(nu))
}
return nu
})
},
[key]
)
// Note - This feels wrong, but I have to depend on localValue changing to
// catch changes that only happen when setValue is run. Don't remove
// localValue
// eslint-disable-next-line react-hooks/rules-of-hooks
const fromStorage = React.useMemo(() => {
// We want to depend on localValue so this stays up to date when it's
// updated, but ultimately we want to use the value from localStorage which
// we _know_ is going to be up to date when `key` changes.
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
localValue
return getObjectFromLocalStorage(key, defaultValue)
}, [key, localValue, defaultValue])
return [fromStorage, setValue]
}
export const useSetToast = () => {
const dispatch = useDispatch()
const setToast = React.useCallback(
(toast: string) => {
dispatch({ type: "setToast", toast })
},
[dispatch]
)
return setToast
}
export const IS_SSR = typeof window === "undefined"
export const useScrollTo = () => {
const [initialLoad, setInitialLoad] = useState(true)
const location = useLocation()
useEffect(() => {
if (initialLoad && location.hash.startsWith("#")) {
setInitialLoad(false)
const element = document.getElementById(location.hash.substr(1))
if (element) {
element.scrollIntoView()
}
}
}, [location.hash, initialLoad])
}
export const useAddToArray = <T>(setArray: Dispatch<T[] | undefined>) => {
return useCallback(
(nu: T) => {
setArray((old = []) => old.concat([nu]))
},
[setArray]
)
}
export const useRemoveByIndex = <T>(setArray: Dispatch<T[] | undefined>) => {
return useCallback(
(idx: number) => {
setArray((old = []) => old.filter((_, i) => i !== idx))
},
[setArray]
)
}
export const useUpdateByIndex = <T>(setArray: Dispatch<T[] | undefined>) => {
return useCallback(
(idx: number, update: (old: T) => T) => {
setArray((old = []) =>
old.map((entry, i) => (i === idx ? update(entry) : entry))
)
},
[setArray]
)
}
export const useCopy = (toCopy: string, toast?: string): (() => void) => {
const setToast = useSetToast()
return React.useCallback(() => {
copyToClipboard(toCopy)
toast && setToast(toast)
}, [toCopy, setToast, toast])
}