-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathApp.js
223 lines (201 loc) · 6.46 KB
/
App.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
import {AppState, Linking} from 'react-native';
import Onyx from 'react-native-onyx';
import lodashGet from 'lodash/get';
import Str from 'expensify-common/lib/str';
import _ from 'underscore';
import * as API from '../API';
import ONYXKEYS from '../../ONYXKEYS';
import * as DeprecatedAPI from '../deprecatedAPI';
import CONST from '../../CONST';
import Log from '../Log';
import Performance from '../Performance';
import Timing from './Timing';
import * as Report from './Report';
import * as BankAccounts from './BankAccounts';
import * as Policy from './Policy';
import NetworkConnection from '../NetworkConnection';
import Navigation from '../Navigation/Navigation';
import ROUTES from '../../ROUTES';
import * as SessionUtils from '../SessionUtils';
let currentUserAccountID;
Onyx.connect({
key: ONYXKEYS.SESSION,
callback: (val) => {
currentUserAccountID = lodashGet(val, 'accountID', '');
},
});
let isSidebarLoaded;
Onyx.connect({
key: ONYXKEYS.IS_SIDEBAR_LOADED,
callback: val => isSidebarLoaded = val,
initWithStoredValues: false,
});
const allPolicies = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.POLICY,
callback: (val, key) => {
if (!val || !key) {
return;
}
allPolicies[key] = {...allPolicies[key], ...val};
},
});
/**
* @param {String} url
*/
function setCurrentURL(url) {
Onyx.set(ONYXKEYS.CURRENT_URL, url);
}
/**
* @param {String} locale
*/
function setLocale(locale) {
// If user is not signed in, change just locally.
if (!currentUserAccountID) {
Onyx.merge(ONYXKEYS.NVP_PREFERRED_LOCALE, locale);
return;
}
// Optimistically change preferred locale
const optimisticData = [
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.NVP_PREFERRED_LOCALE,
value: locale,
},
];
API.write('UpdatePreferredLocale', {
value: locale,
}, {optimisticData});
}
function setSidebarLoaded() {
if (isSidebarLoaded) {
return;
}
Onyx.set(ONYXKEYS.IS_SIDEBAR_LOADED, true);
Timing.end(CONST.TIMING.SIDEBAR_LOADED);
Performance.markEnd(CONST.TIMING.SIDEBAR_LOADED);
Performance.markStart(CONST.TIMING.REPORT_INITIAL_RENDER);
}
let appState;
AppState.addEventListener('change', (nextAppState) => {
if (nextAppState.match(/inactive|background/) && appState === 'active') {
Log.info('Flushing logs as app is going inactive', true, {}, true);
}
appState = nextAppState;
});
/**
* Fetches data needed for app initialization
* @returns {Promise}
*/
function getAppData() {
BankAccounts.fetchUserWallet();
// We should update the syncing indicator when personal details and reports are both done fetching.
return Promise.all([
Report.fetchAllReports(true),
]);
}
/**
* Gets a comma separated list of locally stored policy ids
*
* @param {Array} policies
* @return {String}
*/
function getPolicyIDList(policies) {
return _.chain(policies)
.filter(Boolean)
.map(policy => policy.id)
.join(',');
}
/**
* Fetches data needed for app initialization
* @param {Array} policies
*/
function openApp(policies) {
API.read('OpenApp', {
policyIDList: getPolicyIDList(policies),
});
}
/**
* Refreshes data when the app reconnects
*/
function reconnectApp() {
API.read('ReconnectApp', {
policyIDList: getPolicyIDList(allPolicies),
});
}
/**
* Run FixAccount to check if we need to fix anything for the user or run migrations. Reinitialize the data if anything changed
* because some migrations might create new chat reports or their change data.
*/
function fixAccountAndReloadData() {
DeprecatedAPI.User_FixAccount()
.then((response) => {
if (!response.changed) {
return;
}
Log.info('FixAccount found updates for this user, so data will be reinitialized', true, response);
getAppData();
});
}
/**
* This action runs every time the AuthScreens are mounted. The navigator may
* not be ready yet, and therefore we need to wait before navigating within this
* action and any actions this method calls.
*
* getInitialURL allows us to access params from the transition link more easily
* than trying to extract them from the navigation state.
* The transition link contains an exitTo param that contains the route to
* navigate to after the user is signed in. A user can transition from OldDot
* with a different account than the one they are currently signed in with, so
* we only navigate if they are not signing in as a new user. Once they are
* signed in as that new user, this action will run again and the navigation
* will occur.
* When the exitTo route is 'workspace/new', we create a new
* workspace and navigate to it via Policy.createAndGetPolicyList.
*
* We subscribe to the session using withOnyx in the AuthScreens and
* pass it in as a parameter. withOnyx guarantees that the value has been read
* from Onyx because it will not render the AuthScreens until that point.
* @param {Object} session
*/
function setUpPoliciesAndNavigate(session) {
Linking.getInitialURL()
.then((url) => {
if (!url) {
return;
}
const path = new URL(url).pathname;
const params = new URLSearchParams(url);
const exitTo = params.get('exitTo');
const isLoggingInAsNewUser = SessionUtils.isLoggingInAsNewUser(url, session.email);
const shouldCreateFreePolicy = !isLoggingInAsNewUser
&& Str.startsWith(path, Str.normalizeUrl(ROUTES.TRANSITION_FROM_OLD_DOT))
&& exitTo === ROUTES.WORKSPACE_NEW;
if (shouldCreateFreePolicy) {
Policy.createAndGetPolicyList();
return;
}
if (!isLoggingInAsNewUser && exitTo) {
Navigation.isNavigationReady()
.then(() => {
// We must call dismissModal() to remove the /transition route from history
Navigation.dismissModal();
Navigation.navigate(exitTo);
});
}
});
}
// When the app reconnects from being offline, fetch all initialization data
NetworkConnection.onReconnect(() => {
getAppData();
reconnectApp();
});
export {
setCurrentURL,
setLocale,
setSidebarLoaded,
getAppData,
fixAccountAndReloadData,
setUpPoliciesAndNavigate,
openApp,
};