-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
PriorityMode.ts
131 lines (108 loc) · 4.49 KB
/
PriorityMode.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
import debounce from 'lodash/debounce';
import Onyx from 'react-native-onyx';
import Log from '@libs/Log';
import * as ReportConnection from '@libs/ReportConnection';
import * as ReportUtils from '@libs/ReportUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
/**
* This actions file is used to automatically switch a user into #focus mode when they exceed a certain number of reports. We do this primarily for performance reasons.
* Similar to the "Welcome action" we must wait for a number of things to happen when the user signs in or refreshes the page:
*
* - NVP that tracks whether they have already been switched over. We only do this once.
* - Priority mode NVP (that dictates the ordering/filtering logic of the LHN)
* - Reports to load (in ReconnectApp or OpenApp). As we check the count of the reports to determine whether the user is eligible to be automatically switched.
*/
let resolveIsReadyPromise: (args?: unknown[]) => void;
let isReadyPromise = new Promise((resolve) => {
resolveIsReadyPromise = resolve;
});
let currentUserAccountID: number | undefined;
Onyx.connect({
key: ONYXKEYS.SESSION,
callback: (val) => {
currentUserAccountID = val?.accountID;
},
});
/**
* Debounce the prompt to promote focus mode as many reports updates could happen in a short burst
*/
// eslint-disable-next-line @typescript-eslint/no-use-before-define
const autoSwitchToFocusMode = debounce(tryFocusModeUpdate, 300, {leading: true});
let isLoadingReportData = true;
Onyx.connect({
key: ONYXKEYS.IS_LOADING_REPORT_DATA,
initWithStoredValues: false,
callback: (value) => {
isLoadingReportData = value ?? false;
// eslint-disable-next-line @typescript-eslint/no-use-before-define
checkRequiredData();
},
});
let isInFocusMode: boolean | undefined;
Onyx.connect({
key: ONYXKEYS.NVP_PRIORITY_MODE,
callback: (priorityMode) => {
isInFocusMode = priorityMode === CONST.PRIORITY_MODE.GSD;
// eslint-disable-next-line @typescript-eslint/no-use-before-define
checkRequiredData();
},
});
let hasTriedFocusMode: boolean | undefined;
Onyx.connect({
key: ONYXKEYS.NVP_TRY_FOCUS_MODE,
callback: (val) => {
hasTriedFocusMode = val;
// eslint-disable-next-line @typescript-eslint/no-use-before-define
checkRequiredData();
},
});
function resetHasReadRequiredDataFromStorage() {
// Create a new promise and a new resolve function
isReadyPromise = new Promise((resolve) => {
resolveIsReadyPromise = resolve;
});
isLoadingReportData = true;
}
function checkRequiredData() {
if (ReportConnection.getAllReports() === undefined || hasTriedFocusMode === undefined || isInFocusMode === undefined || isLoadingReportData) {
return;
}
resolveIsReadyPromise();
}
function tryFocusModeUpdate() {
isReadyPromise.then(() => {
// User is signed out so do not try to switch them
if (!currentUserAccountID) {
return;
}
// Check to see if the user is using #focus mode, has tried it before, or we have already switched them over automatically.
if ((isInFocusMode ?? false) || hasTriedFocusMode) {
Log.info('Not switching user to optimized focus mode.', false, {isInFocusMode, hasTriedFocusMode});
return;
}
const validReports = [];
const allReports = ReportConnection.getAllReports();
Object.keys(allReports ?? {}).forEach((key) => {
const report = allReports?.[key];
if (!report) {
return;
}
if (!ReportUtils.isValidReport(report) || !ReportUtils.isReportParticipant(currentUserAccountID ?? -1, report)) {
return;
}
validReports.push(report);
});
const reportCount = validReports.length;
if (reportCount < CONST.REPORT.MAX_COUNT_BEFORE_FOCUS_UPDATE) {
Log.info('Not switching user to optimized focus mode as they do not have enough reports', false, {reportCount});
return;
}
Log.info('Switching user to optimized focus mode', false, {reportCount, hasTriedFocusMode, isInFocusMode});
// Record that we automatically switched them so we don't ask again.
hasTriedFocusMode = true;
// Setting this triggers a modal to open and notify the user.
Onyx.set(ONYXKEYS.FOCUS_MODE_NOTIFICATION, true);
});
}
export {resetHasReadRequiredDataFromStorage, autoSwitchToFocusMode};