-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathExpensify.tsx
More file actions
326 lines (278 loc) · 13.5 KB
/
Expensify.tsx
File metadata and controls
326 lines (278 loc) · 13.5 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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import HybridAppModule from '@expensify/react-native-hybrid-app';
import type * as Sentry from '@sentry/react-native';
import React, {useCallback, useEffect, useLayoutEffect, useRef, useState} from 'react';
import type {NativeEventSubscription} from 'react-native';
import {AppState, Platform} from 'react-native';
import Onyx from 'react-native-onyx';
import DelegateNoAccessModalProvider from './components/DelegateNoAccessModalProvider';
import EmojiPicker from './components/EmojiPicker/EmojiPicker';
import GrowlNotification from './components/GrowlNotification';
import {useInitialURLActions} from './components/InitialURLContextProvider';
import ProactiveAppReviewModalManager from './components/ProactiveAppReviewModalManager';
import ScreenShareRequestModal from './components/ScreenShareRequestModal';
import AppleAuthWrapper from './components/SignInButtons/AppleAuthWrapper';
import SplashScreenHider from './components/SplashScreenHider';
import TrialPaymentReminderModalManager from './components/TrialPaymentReminderModalManager';
import UpdateAppModal from './components/UpdateAppModal';
import CONFIG from './CONFIG';
import CONST from './CONST';
import DeepLinkHandler from './DeepLinkHandler';
import DelegateAccessHandler from './DelegateAccessHandler';
import FullstoryInitHandler from './FullstoryInitHandler';
import useDebugShortcut from './hooks/useDebugShortcut';
import useIsAuthenticated from './hooks/useIsAuthenticated';
import useLocalize from './hooks/useLocalize';
import useOnyx from './hooks/useOnyx';
import {updateLastRoute} from './libs/actions/App';
import * as EmojiPickerAction from './libs/actions/EmojiPickerAction';
// This lib needs to be imported, but it has nothing to export since all it contains is an Onyx connection
import './libs/actions/replaceOptimisticReportWithActualReport';
import * as ActiveClientManager from './libs/ActiveClientManager';
import {isSafari} from './libs/Browser';
import {growlRef} from './libs/Growl';
import Log from './libs/Log';
import migrateOnyx from './libs/migrateOnyx';
import Navigation from './libs/Navigation/Navigation';
import NavigationRoot from './libs/Navigation/NavigationRoot';
import NetworkConnection from './libs/NetworkConnection';
import PushNotification from './libs/Notification/PushNotification';
import './libs/Notification/PushNotification/subscribeToPushNotifications';
// This lib needs to be imported, but it has nothing to export since all it contains is an Onyx connection
import './libs/registerPaginationConfig';
import {endSpan, getSpan, startSpan} from './libs/telemetry/activeSpans';
import {cleanupMemoryTrackingTelemetry, initializeMemoryTrackingTelemetry} from './libs/telemetry/TelemetrySynchronizer';
// This lib needs to be imported, but it has nothing to export since all it contains is an Onyx connection
import './libs/UnreadIndicatorUpdater';
import Visibility from './libs/Visibility';
import ONYXKEYS from './ONYXKEYS';
import PopoverReportActionContextMenu from './pages/inbox/report/ContextMenu/PopoverReportActionContextMenu';
import * as ReportActionContextMenu from './pages/inbox/report/ContextMenu/ReportActionContextMenu';
import PriorityModeHandler from './PriorityModeHandler';
import type {Route} from './ROUTES';
import {accountIDSelector} from './selectors/Session';
import {useSplashScreenActions, useSplashScreenState} from './SplashScreenStateContext';
Onyx.registerLogger(({level, message, parameters}) => {
if (level === 'alert') {
Log.alert(message, parameters);
console.error(message);
} else if (level === 'hmmm') {
Log.hmmm(message, parameters);
} else {
Log.info(message, undefined, parameters);
}
});
function Expensify() {
const appStateChangeListener = useRef<NativeEventSubscription | null>(null);
const [isNavigationReady, setIsNavigationReady] = useState(false);
const [isOnyxMigrated, setIsOnyxMigrated] = useState(false);
const {splashScreenState} = useSplashScreenState();
const {setSplashScreenState} = useSplashScreenActions();
const [hasAttemptedToOpenPublicRoom, setAttemptedToOpenPublicRoom] = useState(false);
const {preferredLocale} = useLocalize();
const [accountID] = useOnyx(ONYXKEYS.SESSION, {selector: accountIDSelector});
const [lastRoute] = useOnyx(ONYXKEYS.LAST_ROUTE);
const [isCheckingPublicRoom = true] = useOnyx(ONYXKEYS.IS_CHECKING_PUBLIC_ROOM, {initWithStoredValues: false});
const [updateAvailable] = useOnyx(ONYXKEYS.UPDATE_AVAILABLE, {initWithStoredValues: false});
const [updateRequired] = useOnyx(ONYXKEYS.UPDATE_REQUIRED, {initWithStoredValues: false});
const [lastVisitedPath] = useOnyx(ONYXKEYS.LAST_VISITED_PATH);
useDebugShortcut();
useEffect(() => {
initializeMemoryTrackingTelemetry();
return () => {
cleanupMemoryTrackingTelemetry();
};
}, []);
const bootsplashSpan = useRef<Sentry.Span>(null);
const [initialUrl, setInitialUrl] = useState<Route | null>(null);
const {setIsAuthenticatedAtStartup} = useInitialURLActions();
useEffect(() => {
bootsplashSpan.current = startSpan(CONST.TELEMETRY.SPAN_BOOTSPLASH.ROOT, {
name: CONST.TELEMETRY.SPAN_BOOTSPLASH.ROOT,
op: CONST.TELEMETRY.SPAN_BOOTSPLASH.ROOT,
parentSpan: getSpan(CONST.TELEMETRY.SPAN_APP_STARTUP),
});
startSpan(CONST.TELEMETRY.SPAN_BOOTSPLASH.ONYX, {
name: CONST.TELEMETRY.SPAN_BOOTSPLASH.ONYX,
op: CONST.TELEMETRY.SPAN_BOOTSPLASH.ONYX,
parentSpan: bootsplashSpan.current,
});
startSpan(CONST.TELEMETRY.SPAN_BOOTSPLASH.PUBLIC_ROOM_CHECK, {
name: CONST.TELEMETRY.SPAN_BOOTSPLASH.PUBLIC_ROOM_CHECK,
op: CONST.TELEMETRY.SPAN_BOOTSPLASH.PUBLIC_ROOM_CHECK,
parentSpan: bootsplashSpan.current,
});
startSpan(CONST.TELEMETRY.SPAN_BOOTSPLASH.LOCALE, {
name: CONST.TELEMETRY.SPAN_BOOTSPLASH.LOCALE,
op: CONST.TELEMETRY.SPAN_BOOTSPLASH.LOCALE,
parentSpan: bootsplashSpan.current,
});
}, []);
const isAuthenticated = useIsAuthenticated();
useEffect(() => {
if (isCheckingPublicRoom) {
return;
}
endSpan(CONST.TELEMETRY.SPAN_BOOTSPLASH.ONYX);
endSpan(CONST.TELEMETRY.SPAN_BOOTSPLASH.PUBLIC_ROOM_CHECK);
// End the PUBLIC_ROOM_API span if it was started (it's started conditionally in openReportFromDeepLink)
// endSpan handles non-existent spans gracefully, so it's safe to call unconditionally
endSpan(CONST.TELEMETRY.SPAN_BOOTSPLASH.PUBLIC_ROOM_API);
setAttemptedToOpenPublicRoom(true);
startSpan(CONST.TELEMETRY.SPAN_BOOTSPLASH.NAVIGATION, {
name: CONST.TELEMETRY.SPAN_BOOTSPLASH.NAVIGATION,
op: CONST.TELEMETRY.SPAN_BOOTSPLASH.NAVIGATION,
parentSpan: bootsplashSpan.current,
});
}, [isCheckingPublicRoom]);
useEffect(() => {
if (!preferredLocale) {
return;
}
endSpan(CONST.TELEMETRY.SPAN_BOOTSPLASH.LOCALE);
}, [preferredLocale]);
const isSplashReadyToBeHidden = splashScreenState === CONST.BOOT_SPLASH_STATE.READY_TO_BE_HIDDEN;
const isSplashVisible = splashScreenState === CONST.BOOT_SPLASH_STATE.VISIBLE;
const shouldInit = isNavigationReady && hasAttemptedToOpenPublicRoom && !!preferredLocale;
const shouldHideSplash = shouldInit && (CONFIG.IS_HYBRID_APP ? isSplashReadyToBeHidden : isSplashVisible);
useEffect(() => {
if (!shouldHideSplash) {
return;
}
startSpan(CONST.TELEMETRY.SPAN_BOOTSPLASH.SPLASH_HIDER, {
name: CONST.TELEMETRY.SPAN_BOOTSPLASH.SPLASH_HIDER,
op: CONST.TELEMETRY.SPAN_BOOTSPLASH.SPLASH_HIDER,
parentSpan: bootsplashSpan.current,
});
}, [shouldHideSplash]);
useEffect(() => {
if (!shouldInit || splashScreenState !== CONST.BOOT_SPLASH_STATE.HIDDEN) {
return;
}
// Clears OldDot UI after sign-out, if there's no OldDot UI left it has no effect.
HybridAppModule.clearOldDotAfterSignOut();
}, [shouldInit, splashScreenState]);
const initializeClient = () => {
if (!Visibility.isVisible()) {
return;
}
// Delay client init to avoid issues with delayed Onyx events on iOS. All iOS browsers use WebKit, which suspends events in background tabs.
// Events are flushed only when the tab becomes active again causing issues with client initialization.
// See: https://stackoverflow.com/questions/54095584/page-becomes-inactive-when-switching-tabs-on-ios
if (isSafari()) {
setTimeout(ActiveClientManager.init, 400);
} else {
ActiveClientManager.init();
}
};
const setNavigationReady = useCallback(() => {
setIsNavigationReady(true);
// Navigate to any pending routes now that the NavigationContainer is ready
Navigation.setIsNavigationReady();
}, []);
const onSplashHide = useCallback(() => {
setSplashScreenState(CONST.BOOT_SPLASH_STATE.HIDDEN);
endSpan(CONST.TELEMETRY.SPAN_OD_ND_TRANSITION);
endSpan(CONST.TELEMETRY.SPAN_APP_STARTUP);
endSpan(CONST.TELEMETRY.SPAN_BOOTSPLASH.ROOT);
endSpan(CONST.TELEMETRY.SPAN_BOOTSPLASH.SPLASH_HIDER);
}, [setSplashScreenState]);
useLayoutEffect(() => {
// Initialize this client as being an active client
ActiveClientManager.init();
// Used for the offline indicator appearing when someone is offline
const unsubscribeNetInfo = NetworkConnection.subscribeToNetInfo(accountID);
return unsubscribeNetInfo;
}, [accountID]);
// Log the platform and config to debug .env issues
useEffect(() => {
Log.info('App launched', false, {Platform, CONFIG});
}, []);
useEffect(() => {
setTimeout(() => {
const appState = AppState.currentState;
Log.info('[BootSplash] splash screen status', false, {appState, splashScreenState});
if (splashScreenState === CONST.BOOT_SPLASH_STATE.VISIBLE) {
const propsToLog = {
isCheckingPublicRoom,
updateRequired,
updateAvailable,
isAuthenticated,
lastVisitedPath,
};
Log.alert('[BootSplash] splash screen is still visible', {propsToLog}, false);
}
}, 30 * 1000);
// Run any Onyx schema migrations and then continue loading the main app
migrateOnyx().then(() => {
// In case of a crash that led to disconnection, we want to remove all the push notifications.
if (!isAuthenticated) {
PushNotification.clearNotifications();
}
setIsOnyxMigrated(true);
});
appStateChangeListener.current = AppState.addEventListener('change', initializeClient);
setIsAuthenticatedAtStartup(isAuthenticated);
startSpan(CONST.TELEMETRY.SPAN_BOOTSPLASH.DEEP_LINK, {
name: CONST.TELEMETRY.SPAN_BOOTSPLASH.DEEP_LINK,
op: CONST.TELEMETRY.SPAN_BOOTSPLASH.DEEP_LINK,
parentSpan: bootsplashSpan.current,
});
if (CONFIG.IS_HYBRID_APP) {
HybridAppModule.onURLListenerAdded();
}
return () => {
appStateChangeListener.current?.remove();
};
// eslint-disable-next-line react-hooks/exhaustive-deps -- we don't want this effect to run again
}, []);
useLayoutEffect(() => {
if (!isNavigationReady || !lastRoute) {
return;
}
updateLastRoute('');
Navigation.navigate(lastRoute as Route);
// Disabling this rule because we only want it to run on the first render.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isNavigationReady]);
// Display a blank page until the onyx migration completes
if (!isOnyxMigrated) {
return null;
}
if (updateRequired) {
throw new Error(CONST.ERROR.UPDATE_REQUIRED);
}
return (
<>
{shouldInit && (
<>
<GrowlNotification ref={growlRef} />
<DelegateNoAccessModalProvider>
<PopoverReportActionContextMenu ref={ReportActionContextMenu.contextMenuRef} />
</DelegateNoAccessModalProvider>
<EmojiPicker ref={EmojiPickerAction.emojiPickerRef} />
{/* We include the modal for showing a new update at the top level so the option is always present. */}
{updateAvailable && !updateRequired ? <UpdateAppModal /> : null}
{/* Proactive app review modal shown when user has completed a trigger action */}
<ProactiveAppReviewModalManager />
<TrialPaymentReminderModalManager />
<ScreenShareRequestModal />
</>
)}
<PriorityModeHandler />
<DelegateAccessHandler />
<FullstoryInitHandler />
<DeepLinkHandler onInitialUrl={setInitialUrl} />
<AppleAuthWrapper />
{hasAttemptedToOpenPublicRoom && (
<NavigationRoot
onReady={setNavigationReady}
authenticated={isAuthenticated}
lastVisitedPath={lastVisitedPath as Route}
initialUrl={initialUrl}
/>
)}
{shouldHideSplash && <SplashScreenHider onHide={onSplashHide} />}
</>
);
}
export default Expensify;