forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpensify.js
241 lines (204 loc) · 8.98 KB
/
Expensify.js
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
import _ from 'underscore';
import lodashGet from 'lodash/get';
import PropTypes from 'prop-types';
import React, {useCallback, useState, useEffect, useRef, useLayoutEffect, useMemo} from 'react';
import {AppState, Linking} from 'react-native';
import Onyx, {withOnyx} from 'react-native-onyx';
import * as Report from './libs/actions/Report';
import BootSplash from './libs/BootSplash';
import * as ActiveClientManager from './libs/ActiveClientManager';
import ONYXKEYS from './ONYXKEYS';
import NavigationRoot from './libs/Navigation/NavigationRoot';
import migrateOnyx from './libs/migrateOnyx';
import PushNotification from './libs/Notification/PushNotification';
import UpdateAppModal from './components/UpdateAppModal';
import Visibility from './libs/Visibility';
import GrowlNotification from './components/GrowlNotification';
import * as Growl from './libs/Growl';
import StartupTimer from './libs/StartupTimer';
import Log from './libs/Log';
import ConfirmModal from './components/ConfirmModal';
import compose from './libs/compose';
import withLocalize, {withLocalizePropTypes} from './components/withLocalize';
import * as User from './libs/actions/User';
import NetworkConnection from './libs/NetworkConnection';
import Navigation from './libs/Navigation/Navigation';
import DeeplinkWrapper from './components/DeeplinkWrapper';
import PopoverReportActionContextMenu from './pages/home/report/ContextMenu/PopoverReportActionContextMenu';
import * as ReportActionContextMenu from './pages/home/report/ContextMenu/ReportActionContextMenu';
import SplashScreenHider from './components/SplashScreenHider';
import KeyboardShortcutsModal from './components/KeyboardShortcutsModal';
// This lib needs to be imported, but it has nothing to export since all it contains is an Onyx connection
// eslint-disable-next-line no-unused-vars
import UnreadIndicatorUpdater from './libs/UnreadIndicatorUpdater';
Onyx.registerLogger(({level, message}) => {
if (level === 'alert') {
Log.alert(message);
console.error(message);
} else {
Log.info(message);
}
});
const propTypes = {
/* Onyx Props */
/** Session info for the currently logged in user. */
session: PropTypes.shape({
/** Currently logged in user authToken */
authToken: PropTypes.string,
/** Currently logged in user accountID */
accountID: PropTypes.number,
}),
/** Whether a new update is available and ready to install. */
updateAvailable: PropTypes.bool,
/** Tells us if the sidebar has rendered */
isSidebarLoaded: PropTypes.bool,
/** Information about a screen share call requested by a GuidesPlus agent */
screenShareRequest: PropTypes.shape({
/** Access token required to join a screen share room, generated by the backend */
accessToken: PropTypes.string,
/** Name of the screen share room to join */
roomName: PropTypes.string,
}),
/** Whether the app is waiting for the server's response to determine if a room is public */
isCheckingPublicRoom: PropTypes.bool,
...withLocalizePropTypes,
};
const defaultProps = {
session: {
authToken: null,
accountID: null,
},
updateAvailable: false,
isSidebarLoaded: false,
screenShareRequest: null,
isCheckingPublicRoom: true,
};
function Expensify(props) {
const appStateChangeListener = useRef(null);
const [isNavigationReady, setIsNavigationReady] = useState(false);
const [isOnyxMigrated, setIsOnyxMigrated] = useState(false);
const [isSplashHidden, setIsSplashHidden] = useState(false);
const [hasAttemptedToOpenPublicRoom, setAttemptedToOpenPublicRoom] = useState(false);
useEffect(() => {
if (props.isCheckingPublicRoom) return;
setAttemptedToOpenPublicRoom(true);
}, [props.isCheckingPublicRoom]);
const isAuthenticated = useMemo(() => Boolean(lodashGet(props.session, 'authToken', null)), [props.session]);
const shouldInit = isNavigationReady && (!isAuthenticated || props.isSidebarLoaded) && hasAttemptedToOpenPublicRoom;
const shouldHideSplash = shouldInit && !isSplashHidden;
const initializeClient = () => {
if (!Visibility.isVisible()) {
return;
}
ActiveClientManager.init();
};
const setNavigationReady = useCallback(() => {
setIsNavigationReady(true);
// Navigate to any pending routes now that the NavigationContainer is ready
Navigation.setIsNavigationReady();
}, []);
const onSplashHide = useCallback(() => {
setIsSplashHidden(true);
}, []);
useLayoutEffect(() => {
// Initialize this client as being an active client
ActiveClientManager.init();
// Used for the offline indicator appearing when someone is offline
NetworkConnection.subscribeToNetInfo();
}, []);
useEffect(() => {
setTimeout(() => {
BootSplash.getVisibilityStatus().then((status) => {
const appState = AppState.currentState;
Log.info('[BootSplash] splash screen status', false, {appState, status});
if (status === 'visible') {
const propsToLog = _.omit(props, ['children', 'session']);
propsToLog.isAuthenticated = isAuthenticated;
Log.alert('[BootSplash] splash screen is still visible', {propsToLog}, false);
}
});
}, 30 * 1000);
// This timer is set in the native layer when launching the app and we stop it here so we can measure how long
// it took for the main app itself to load.
StartupTimer.stop();
// 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);
// If the app is opened from a deep link, get the reportID (if exists) from the deep link and navigate to the chat report
Linking.getInitialURL().then((url) => Report.openReportFromDeepLink(url, isAuthenticated));
// Open chat report from a deep link (only mobile native)
Linking.addEventListener('url', (state) => Report.openReportFromDeepLink(state.url, isAuthenticated));
return () => {
if (!appStateChangeListener.current) {
return;
}
appStateChangeListener.current.remove();
};
// eslint-disable-next-line react-hooks/exhaustive-deps -- we don't want this effect to run again
}, []);
// Display a blank page until the onyx migration completes
if (!isOnyxMigrated) {
return null;
}
return (
<DeeplinkWrapper>
{shouldInit && (
<>
<KeyboardShortcutsModal />
<GrowlNotification ref={Growl.growlRef} />
<PopoverReportActionContextMenu ref={ReportActionContextMenu.contextMenuRef} />
{/* We include the modal for showing a new update at the top level so the option is always present. */}
{props.updateAvailable ? <UpdateAppModal /> : null}
{props.screenShareRequest ? (
<ConfirmModal
title={props.translate('guides.screenShare')}
onConfirm={() => User.joinScreenShare(props.screenShareRequest.accessToken, props.screenShareRequest.roomName)}
onCancel={User.clearScreenShareRequest}
prompt={props.translate('guides.screenShareRequest')}
confirmText={props.translate('common.join')}
cancelText={props.translate('common.decline')}
isVisible
/>
) : null}
</>
)}
{hasAttemptedToOpenPublicRoom && (
<NavigationRoot
onReady={setNavigationReady}
authenticated={isAuthenticated}
/>
)}
{shouldHideSplash && <SplashScreenHider onHide={onSplashHide} />}
</DeeplinkWrapper>
);
}
Expensify.propTypes = propTypes;
Expensify.defaultProps = defaultProps;
export default compose(
withLocalize,
withOnyx({
isCheckingPublicRoom: {
key: ONYXKEYS.IS_CHECKING_PUBLIC_ROOM,
initWithStoredValues: false,
},
session: {
key: ONYXKEYS.SESSION,
},
updateAvailable: {
key: ONYXKEYS.UPDATE_AVAILABLE,
initWithStoredValues: false,
},
isSidebarLoaded: {
key: ONYXKEYS.IS_SIDEBAR_LOADED,
},
screenShareRequest: {
key: ONYXKEYS.SCREEN_SHARE_REQUEST,
},
}),
)(Expensify);