-
Notifications
You must be signed in to change notification settings - Fork 22
/
useCachedLoadable.ts
200 lines (183 loc) · 7.16 KB
/
useCachedLoadable.ts
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
import { useEffect, useMemo, useState } from 'react'
import { RecoilValue, constSelector, useRecoilValueLoadable } from 'recoil'
import { useDeepCompareMemoize } from 'use-deep-compare-effect'
import {
CachedLoadable,
LoadingData,
LoadingDataWithError,
} from '@dao-dao/types'
import {
loadableToLoadingData,
loadableToLoadingDataWithError,
transformLoadingDataWithError,
} from '@dao-dao/utils'
import { useUpdatingRef } from './useUpdatingRef'
const constSelectorRegex = /^__constant__selectorFamily\/(.+)\/\d+$/
// Keep cache of previously loaded data until next data is ready. Essentially,
// memoize a loadable to prevent UI flickering. If recoilValue is undefined,
// pretend like we are loading until we get a selector to load. This may happen
// if a query depends on data not available right away, such as a wallet
// address.
export const useCachedLoadable = <T extends unknown>(
recoilValue: RecoilValue<T> | undefined
): CachedLoadable<T> => {
const loadable = useRecoilValueLoadable(
// If not on a browser, or recoilValue is undefined, return undefined as we
// cannot load yet.
typeof window === 'undefined' || !recoilValue
? constSelector(undefined)
: recoilValue
)
const loadableLoadingOrNotReady =
loadable.state === 'loading' ||
typeof window === 'undefined' ||
!recoilValue
// Since `contents` is set in a `useEffect`, it will take 1 extra render once
// the loadable has data ready before the cached `contents` state will contain
// the loaded value. This flag ensures that loading continues to display until
// `contents` has been updated with its first `loadable.contents` value. If we
// didn't do this, there would be a moment where `state === "hasValue"` with
// `contents` still undefined.
const [contentsHasValue, setContentsHasValue] = useState(
// If the loadable is ready on first render, just set it right away.
loadable.state === 'hasValue'
)
const [contents, setContents] = useState<T | undefined>(
// If the loadable is ready on first render, just set it right away.
loadable.state === 'hasValue' ? loadable.contents : undefined
)
// Store the cached recoil value key for comparison.
const [cachedKey, setCachedKey] = useState<string | undefined>(
recoilValue?.key
)
const [initialLoading, setInitialLoading] = useState(
loadableLoadingOrNotReady
)
const [updating, setUpdating] = useState(loadableLoadingOrNotReady)
// Store the last cached key for use in the effect below.
const lastCachedKey = useUpdatingRef(cachedKey)
useEffect(() => {
if (loadableLoadingOrNotReady) {
setUpdating(true)
setCachedKey(recoilValue?.key)
// Reset state if recoilValue becomes undefined. This may happen if a
// query depends on form input state, like an address, that may toggle
// between valid and invalid. This ensures that old data is not shown for
// a moment while waiting for a new query.
if (!recoilValue) {
setInitialLoading(true)
setContents(undefined)
setContentsHasValue(false)
}
} else if (loadable.state === 'hasValue') {
// Special handling for `constSelector` to prevent infinite loops.
// `constSelector`s change on every re-render with an incrementing ID, so
// if we are using the same constant object, we don't want to cause
// another re-render by updating state above. If it re-renders here, the
// selector will change again, causing infinite loops. To prevent infinite
// loops, no need to update state if the constant selector is for the same
// value as the currently cached value.
if (
lastCachedKey.current &&
constSelectorRegex.test(recoilValue.key) &&
constSelectorRegex.test(lastCachedKey.current) &&
// Ensure that constant selectors are for the same data.
recoilValue.key.match(constSelectorRegex)?.[1] ===
lastCachedKey.current.match(constSelectorRegex)?.[1]
) {
return
}
setInitialLoading(false)
setUpdating(false)
setContents(loadable.contents)
setContentsHasValue(true)
setCachedKey(recoilValue.key)
} else if (loadable.state === 'hasError') {
setInitialLoading(false)
setUpdating(false)
setCachedKey(recoilValue?.key)
}
}, [lastCachedKey, loadable, loadableLoadingOrNotReady, recoilValue])
// Memoize the loadable so it can be used in `useEffect` dependencies to
// prevent causing infinite loops. If this is not memoized, it will change on
// every render, which may cause infinite loops if the `useEffect` sets some
// state that causes additional re-renders.
const cachedLoadable = useMemo(
(): CachedLoadable<T> =>
initialLoading ||
!recoilValue ||
// Keep loading until contents has first value set. However if an error is
// present, override and return the error.
(loadable.state !== 'hasError' && !contentsHasValue)
? {
state: 'loading',
contents: undefined,
}
: loadable.state === 'hasError'
? {
state: 'hasError',
contents: !loadable.contents
? new Error('Unknown error')
: loadable.contents instanceof Error
? loadable.contents
: new Error(`${loadable.contents}`),
}
: {
state: 'hasValue',
contents: contents as T,
updating,
},
[
contents,
contentsHasValue,
initialLoading,
loadable.contents,
loadable.state,
recoilValue,
updating,
]
)
return cachedLoadable
}
// The following hooks are convenience hooks that use the above hook to cache
// loaded data and then convert the loadable to our convenience loading types,
// which are more useful in UI components. Read why they are useful in the
// comment above the LoadingData types.
// Convert to LoadingDataWithError for convenience, memoized.
export const useCachedLoadingWithError = <
T extends unknown,
U extends unknown = T
>(
recoilValue: RecoilValue<T> | undefined,
/**
* Optional function to transform the data.
*/
transform?: (data: T) => U
): LoadingDataWithError<U> => {
const loadable = useCachedLoadable(recoilValue)
const transformRef = useUpdatingRef(transform)
return useMemo(() => {
const data = loadableToLoadingDataWithError(loadable)
if (transformRef.current) {
return transformLoadingDataWithError(data, transformRef.current)
}
return data as LoadingDataWithError<U>
}, [loadable, transformRef])
}
// Convert to LoadingData for convenience, memoized.
export const useCachedLoading = <T extends unknown>(
recoilValue: RecoilValue<T> | undefined,
defaultValue: T,
onError?: (error: any) => void
): LoadingData<T> => {
const loadable = useCachedLoadable(recoilValue)
const onErrorRef = useUpdatingRef(onError)
// Use deep compare to prevent memoize on every re-render if an object is
// passed as the default value.
const memoizedDefaultValue = useDeepCompareMemoize(defaultValue)
return useMemo(
() =>
loadableToLoadingData(loadable, memoizedDefaultValue, onErrorRef.current),
[loadable, memoizedDefaultValue, onErrorRef]
)
}