-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
Welcome.ts
219 lines (187 loc) · 6.28 KB
/
Welcome.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import {NativeModules} from 'react-native';
import type {OnyxUpdate} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import * as API from '@libs/API';
import {WRITE_COMMANDS} from '@libs/API/types';
import Navigation from '@libs/Navigation/Navigation';
import variables from '@styles/variables';
import type {OnboardingPurposeType} from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type Onboarding from '@src/types/onyx/Onboarding';
import type TryNewDot from '@src/types/onyx/TryNewDot';
type OnboardingData = Onboarding | [] | undefined;
let isLoadingReportData = true;
let tryNewDotData: TryNewDot | undefined;
type HasCompletedOnboardingFlowProps = {
onCompleted?: () => void;
onNotCompleted?: () => void;
};
type HasOpenedForTheFirstTimeFromHybridAppProps = {
onFirstTimeInHybridApp?: () => void;
onSubsequentRuns?: () => void;
};
let resolveIsReadyPromise: (value?: Promise<void>) => void | undefined;
let isServerDataReadyPromise = new Promise<void>((resolve) => {
resolveIsReadyPromise = resolve;
});
let resolveOnboardingFlowStatus: (value?: OnboardingData) => void;
let isOnboardingFlowStatusKnownPromise = new Promise<OnboardingData>((resolve) => {
resolveOnboardingFlowStatus = resolve;
});
let resolveTryNewDotStatus: (value?: Promise<void>) => void | undefined;
const tryNewDotStatusPromise = new Promise<void>((resolve) => {
resolveTryNewDotStatus = resolve;
});
function onServerDataReady(): Promise<void> {
return isServerDataReadyPromise;
}
function isOnboardingFlowCompleted({onCompleted, onNotCompleted}: HasCompletedOnboardingFlowProps) {
isOnboardingFlowStatusKnownPromise.then((onboarding) => {
if (Array.isArray(onboarding) || onboarding?.hasCompletedGuidedSetupFlow === undefined) {
return;
}
if (onboarding?.hasCompletedGuidedSetupFlow) {
onCompleted?.();
} else {
onNotCompleted?.();
}
});
}
/**
* Determines whether the application is being launched for the first time by a hybrid app user,
* and executes corresponding callback functions.
*/
function isFirstTimeHybridAppUser({onFirstTimeInHybridApp, onSubsequentRuns}: HasOpenedForTheFirstTimeFromHybridAppProps) {
tryNewDotStatusPromise.then(() => {
let completedHybridAppOnboarding = tryNewDotData?.classicRedirect?.completedHybridAppOnboarding;
// Backend might return strings instead of booleans
if (typeof completedHybridAppOnboarding === 'string') {
completedHybridAppOnboarding = completedHybridAppOnboarding === 'true';
}
if (NativeModules.HybridAppModule && !completedHybridAppOnboarding) {
onFirstTimeInHybridApp?.();
return;
}
onSubsequentRuns?.();
});
}
/**
* Handles HybridApp onboarding flow if it's possible and necessary.
*/
function handleHybridAppOnboarding() {
if (!NativeModules.HybridAppModule) {
return;
}
isFirstTimeHybridAppUser({
// When user opens New Expensify for the first time from HybridApp we always want to show explanation modal first.
onFirstTimeInHybridApp: () => Navigation.navigate(ROUTES.EXPLANATION_MODAL_ROOT),
// In other scenarios we need to check if onboarding was completed.
onSubsequentRuns: () =>
isOnboardingFlowCompleted({
onNotCompleted: () =>
setTimeout(() => {
Navigation.navigate(ROUTES.ONBOARDING_ROOT);
}, variables.explanationModalDelay),
}),
});
}
/**
* Check if report data are loaded
*/
function checkServerDataReady() {
if (isLoadingReportData) {
return;
}
resolveIsReadyPromise?.();
}
/**
* Check if user completed HybridApp onboarding
*/
function checkTryNewDotDataReady() {
if (tryNewDotData === undefined) {
return;
}
resolveTryNewDotStatus?.();
}
function setOnboardingPurposeSelected(value: OnboardingPurposeType) {
Onyx.set(ONYXKEYS.ONBOARDING_PURPOSE_SELECTED, value ?? null);
}
function setOnboardingErrorMessage(value: string) {
Onyx.set(ONYXKEYS.ONBOARDING_ERROR_MESSAGE, value ?? null);
}
function setOnboardingAdminsChatReportID(adminsChatReportID?: string) {
Onyx.set(ONYXKEYS.ONBOARDING_ADMINS_CHAT_REPORT_ID, adminsChatReportID ?? null);
}
function setOnboardingPolicyID(policyID?: string) {
Onyx.set(ONYXKEYS.ONBOARDING_POLICY_ID, policyID ?? null);
}
function completeHybridAppOnboarding() {
const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.NVP_TRYNEWDOT,
value: {
classicRedirect: {
completedHybridAppOnboarding: true,
},
},
},
];
const failureData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.NVP_TRYNEWDOT,
value: {
classicRedirect: {
completedHybridAppOnboarding: false,
},
},
},
];
API.write(WRITE_COMMANDS.COMPLETE_HYBRID_APP_ONBOARDING, {}, {optimisticData, failureData});
}
Onyx.connect({
key: ONYXKEYS.NVP_ONBOARDING,
callback: (value) => {
if (value === undefined) {
return;
}
resolveOnboardingFlowStatus(value);
},
});
Onyx.connect({
key: ONYXKEYS.IS_LOADING_REPORT_DATA,
initWithStoredValues: false,
callback: (value) => {
isLoadingReportData = value ?? false;
checkServerDataReady();
},
});
Onyx.connect({
key: ONYXKEYS.NVP_TRYNEWDOT,
callback: (value) => {
tryNewDotData = value;
checkTryNewDotDataReady();
},
});
function resetAllChecks() {
isServerDataReadyPromise = new Promise((resolve) => {
resolveIsReadyPromise = resolve;
});
isOnboardingFlowStatusKnownPromise = new Promise<OnboardingData>((resolve) => {
resolveOnboardingFlowStatus = resolve;
});
isLoadingReportData = true;
}
export {
onServerDataReady,
isOnboardingFlowCompleted,
setOnboardingPurposeSelected,
resetAllChecks,
setOnboardingAdminsChatReportID,
setOnboardingPolicyID,
completeHybridAppOnboarding,
handleHybridAppOnboarding,
setOnboardingErrorMessage,
};