forked from artsy/eigen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GlobalStore.tsx
287 lines (252 loc) · 9.15 KB
/
GlobalStore.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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import { useNavigationState } from "@react-navigation/native"
import { __unsafe_mainModalStackRef } from "app/NativeModules/ARScreenPresenterModule"
import { ArtsyNativeModule } from "app/NativeModules/ArtsyNativeModule"
import { switchTab } from "app/navigation/navigate"
import { loadDevNavigationStateCache } from "app/navigation/useReloadedDevNavigationState"
import { BottomTabType } from "app/Scenes/BottomTabs/BottomTabType"
import { logAction } from "app/utils/loggers"
import { createStore, createTypedHooks, StoreProvider } from "easy-peasy"
import { Platform } from "react-native"
import { getBuildNumber, getUserAgentSync } from "react-native-device-info"
import { Action, Middleware } from "redux"
import { version } from "./../../../app.json"
import { DevToggleName, FeatureName, features } from "./config/features"
import { FeatureMap } from "./config/FeaturesModel"
import { VisualClueName, visualClueNames } from "./config/visualClues"
import { getGlobalStoreModel, GlobalStoreModel, GlobalStoreState } from "./GlobalStoreModel"
import { persistenceMiddleware, unpersist } from "./persistence"
function createGlobalStore() {
const middleware: Middleware[] = []
if (!__TEST__) {
middleware.push(persistenceMiddleware)
}
if (__DEV__ && !__TEST__) {
const reduxInFlipper = require("redux-flipper").default
middleware.push(reduxInFlipper())
}
// At dev time but not test time, let's log out each action that is dispatched
if (__DEV__ && !__TEST__) {
middleware.push((_api) => (next) => (_action) => {
if (logAction) {
console.log(`ACTION ${_action.type}`, _action)
}
next(_action)
})
}
// At test time let's keep a log of all dispatched actions so that tests can make assertions based on what
// has been dispatched
if (__TEST__ && __globalStoreTestUtils__) {
__globalStoreTestUtils__.dispatchedActions = []
middleware.push((_api) => (next) => (_action) => {
__globalStoreTestUtils__.dispatchedActions.push(_action)
next(_action)
})
}
const store = createStore(getGlobalStoreModel(), {
middleware,
})
if (!__TEST__) {
unpersist().then(async (state) => {
await loadDevNavigationStateCache(switchTab)
store.getActions().rehydrate(state)
})
}
return store
}
// tslint:disable-next-line:variable-name
export const __globalStoreTestUtils__ = __TEST__
? {
// this can be used to mock the initial state before mounting a test renderer
// e.g. `__globalStoreTestUtils__?.injectState({ nativeState: { selectedTab: "sell" } })`
// takes effect until the next test starts
injectState: (state: DeepPartial<GlobalStoreState>) => {
GlobalStore.actions.__inject(state)
},
setProductionMode() {
this.injectState({ devicePrefs: { environment: { env: "production" } } })
},
injectFeatureFlags(options: Partial<FeatureMap>) {
this.injectState({ artsyPrefs: { features: { localOverrides: options } } })
},
getCurrentState: () => globalStoreInstance().getState(),
dispatchedActions: [] as Action[],
getLastAction() {
return this.dispatchedActions[this.dispatchedActions.length - 1]
},
reset: () => {
_globalStoreInstance = undefined
},
}
: undefined
if (__TEST__) {
beforeEach(() => {
__globalStoreTestUtils__?.reset()
})
}
const hooks = createTypedHooks<GlobalStoreModel>()
export const GlobalStore = {
useAppState: hooks.useStoreState,
get actions() {
return globalStoreInstance().getActions()
},
}
export const GlobalStoreProvider: React.FC<{}> = ({ children }) => {
return <StoreProvider store={globalStoreInstance()}>{children}</StoreProvider>
}
export function useSelectedTab(): BottomTabType {
const tabState = useNavigationState(
(state) => state.routes.find((r) => r.state?.type === "tab")?.state
)
if (!tabState) {
return "home"
} else {
const { index, routes } = tabState
return routes[index!].name as BottomTabType
}
}
let _globalStoreInstance: ReturnType<typeof createGlobalStore> | undefined
const globalStoreInstance = (): ReturnType<typeof createGlobalStore> => {
if (_globalStoreInstance === undefined) {
_globalStoreInstance = createGlobalStore()
}
return _globalStoreInstance
}
export function useFeatureFlag(key: FeatureName) {
return GlobalStore.useAppState((state) => state.artsyPrefs.features.flags[key])
}
export function useDevToggle(key: DevToggleName) {
return GlobalStore.useAppState((state) => state.artsyPrefs.features.devToggles[key])
}
/**
* This is marked as unsafe because it will not cause a re-render
* if used in a react component. Use `useFeatureFlag` instead.
* It is safe to use in contexts that don't require reactivity.
*/
export function unsafe_getFeatureFlag(key: FeatureName): boolean {
const state = globalStoreInstance().getState() ?? null
if (state) {
return state.artsyPrefs.features.flags[key]
}
if (__DEV__) {
throw new Error(`Unable to access ${key} before GlobalStore bootstraps`)
}
return features[key].readyForRelease
}
/**
* This is marked as unsafe because it will not cause a re-render
* if used in a react component. Use `useLocalizedUnit` instead.
* It is safe to use in contexts that don't require reactivity.
*/
export function unsafe_getLocalizedUnit() {
const state = globalStoreInstance().getState()
if (state) {
return state.userPrefs.metric
}
if (__DEV__) {
throw new Error(`Unable to access metric before GlobalStore bootstraps`)
}
}
export function unsafe_getDevToggle(key: DevToggleName) {
const state = globalStoreInstance().getState() ?? null
if (state) {
return state.artsyPrefs.features.devToggles[key]
}
if (__DEV__) {
throw new Error(`Unable to access ${key} before GlobalStore bootstraps`)
}
return false
}
export const useVisualClue = () => {
const seenVisualClues = GlobalStore.useAppState((state) => state.visualClue.seenVisualClues)
const sessionVisualClues = GlobalStore.useAppState((state) => state.visualClue.sessionState.clues)
const showVisualClue = (clueName?: VisualClueName | string): boolean => {
if (!clueName) {
return false
}
if (visualClueNames.includes(clueName)) {
return !seenVisualClues.includes(clueName)
}
return sessionVisualClues.includes(clueName)
}
return { seenVisualClues, showVisualClue }
}
export const addClue = GlobalStore.actions.visualClue.addClue
export const setVisualClueAsSeen = GlobalStore.actions.visualClue.setVisualClueAsSeen
export function unsafe_getUserAccessToken() {
const state = globalStoreInstance().getState() ?? null
if (state) {
return state.auth.userAccessToken
}
if (__DEV__) {
throw new Error(`Unable to access userAccessToken before GlobalStore bootstraps`)
}
return null
}
export function unsafe_getUserEmail() {
const state = globalStoreInstance().getState() ?? null
if (state) {
return state.auth.userEmail
}
if (__DEV__) {
throw new Error(`Unable to retrieve user email`)
}
return null
}
export function getCurrentEmissionState() {
const state = globalStoreInstance().getState() ?? null
// `getUserAgentSync` breaks the Chrome Debugger, so we use a string instead.
const userAgent = `${
__DEV__ ? "Artsy-Mobile " + Platform.OS : getUserAgentSync()
} Artsy-Mobile/${version} Eigen/${getBuildNumber()}/${version}`
const data: GlobalStoreModel["native"]["sessionState"] = {
authenticationToken: state?.auth.userAccessToken || "",
launchCount: ArtsyNativeModule.launchCount,
userAgent,
userID: state?.auth.userID!,
userEmail: "[email protected]", // not used on android
}
return data
}
/**
* This is safe, but is marked unsafe because it should not be used within react components since it does not cause re-renders.
* Use `useSelectedTab` in react components, and use this in rare cases where you need to know the current tab outside of
* react components.
*/
export function unsafe__getSelectedTab(): BottomTabType {
const tabState = __unsafe_mainModalStackRef.current
?.getRootState()
.routes.find((r) => r.state?.type === "tab")?.state
if (!tabState) {
return "home"
} else {
const { index, routes } = tabState
return routes[index!].name as BottomTabType
}
}
export function useIsStaging() {
return GlobalStore.useAppState((state) => state.devicePrefs.environment.env === "staging")
}
/**
* This is marked as unsafe because it will not cause a re-render
* if used during a react component's render. Use `useEnvironment` instead.
* This is safe to use in contexts that don't require reactivity, e.g. onPress handlers.
*/
export function unsafe__getEnvironment() {
const {
echo: { stripePublishableKey },
userIsDev: { value },
} = globalStoreInstance().getState().artsyPrefs
const {
environment: { env, strings },
} = globalStoreInstance().getState().devicePrefs
return { ...strings, stripePublishableKey, env, userIsDev: value }
}
export function useEnvironment() {
const {
echo: { stripePublishableKey },
} = GlobalStore.useAppState((state) => state.artsyPrefs)
const {
environment: { env, strings },
} = GlobalStore.useAppState((state) => state.devicePrefs)
return { ...strings, stripePublishableKey, env }
}