forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreportAttributes.ts
More file actions
222 lines (191 loc) · 10.4 KB
/
reportAttributes.ts
File metadata and controls
222 lines (191 loc) · 10.4 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
import type {OnyxEntry} from 'react-native-onyx';
import {computeReportName, generateIsEmptyReport, generateReportAttributes, isArchivedReport, isValidReport} from '@libs/ReportUtils';
import SidebarUtils from '@libs/SidebarUtils';
import createOnyxDerivedValueConfig from '@userActions/OnyxDerived/createOnyxDerivedValueConfig';
import {hasKeyTriggeredCompute} from '@userActions/OnyxDerived/utils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {PersonalDetailsList, ReportAttributesDerivedValue} from '@src/types/onyx';
let isFullyComputed = false;
let previousDisplayNames: Record<string, string | undefined> = {};
let previousPersonalDetails: OnyxEntry<PersonalDetailsList> | undefined;
const prepareReportKeys = (keys: string[]) => {
return [
...new Set(
keys.map((key) =>
key
.replace(ONYXKEYS.COLLECTION.REPORT_METADATA, ONYXKEYS.COLLECTION.REPORT)
.replace(ONYXKEYS.COLLECTION.REPORT_ACTIONS, ONYXKEYS.COLLECTION.REPORT)
.replace(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, ONYXKEYS.COLLECTION.REPORT),
),
),
];
};
const checkDisplayNamesChanged = (personalDetails: OnyxEntry<PersonalDetailsList>) => {
if (!personalDetails) {
return false;
}
// Fast path: if reference hasn't changed, display names are definitely the same
if (previousPersonalDetails === personalDetails) {
return false;
}
const currentDisplayNames = Object.fromEntries(Object.entries(personalDetails).map(([key, value]) => [key, value?.displayName]));
if (Object.keys(previousDisplayNames).length === 0) {
previousDisplayNames = currentDisplayNames;
previousPersonalDetails = personalDetails;
return false;
}
const currentKeys = Object.keys(currentDisplayNames);
const previousKeys = Object.keys(previousDisplayNames);
const displayNamesChanged = currentKeys.length !== previousKeys.length || currentKeys.some((key) => currentDisplayNames[key] !== previousDisplayNames[key]);
previousDisplayNames = currentDisplayNames;
previousPersonalDetails = personalDetails;
return displayNamesChanged;
};
/**
* This derived value is used to get the report attributes for the report.
*/
export default createOnyxDerivedValueConfig({
key: ONYXKEYS.DERIVED.REPORT_ATTRIBUTES,
dependencies: [
ONYXKEYS.COLLECTION.REPORT,
ONYXKEYS.NVP_PREFERRED_LOCALE,
ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS,
ONYXKEYS.COLLECTION.REPORT_ACTIONS,
ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS,
ONYXKEYS.COLLECTION.TRANSACTION,
ONYXKEYS.PERSONAL_DETAILS_LIST,
ONYXKEYS.COLLECTION.POLICY,
ONYXKEYS.COLLECTION.REPORT_METADATA,
],
compute: ([reports, preferredLocale, transactionViolations, reportActions, reportNameValuePairs, transactions, personalDetails], {currentValue, sourceValues, areAllConnectionsSet}) => {
if (!areAllConnectionsSet) {
return {
reports: {},
locale: null,
};
}
// Check if display names changed when personal details are updated
let displayNamesChanged = false;
if (hasKeyTriggeredCompute(ONYXKEYS.PERSONAL_DETAILS_LIST, sourceValues)) {
displayNamesChanged = checkDisplayNamesChanged(personalDetails);
if (!displayNamesChanged) {
return currentValue ?? {reports: {}, locale: null};
}
}
// if any of those keys changed, reset the isFullyComputed flag to recompute all reports
// we need to recompute all report attributes on locale change because the report names are locale dependent
if (hasKeyTriggeredCompute(ONYXKEYS.NVP_PREFERRED_LOCALE, sourceValues) || displayNamesChanged) {
isFullyComputed = false;
}
// if we already computed the report attributes and there is no new reports data, return the current value
if ((isFullyComputed && !sourceValues) || !reports) {
return currentValue ?? {reports: {}, locale: null};
}
const reportUpdates = sourceValues?.[ONYXKEYS.COLLECTION.REPORT] ?? {};
const reportMetadataUpdates = sourceValues?.[ONYXKEYS.COLLECTION.REPORT_METADATA] ?? {};
const reportActionsUpdates = sourceValues?.[ONYXKEYS.COLLECTION.REPORT_ACTIONS] ?? {};
const reportNameValuePairsUpdates = sourceValues?.[ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS] ?? {};
const transactionsUpdates = sourceValues?.[ONYXKEYS.COLLECTION.TRANSACTION];
const transactionViolationsUpdates = sourceValues?.[ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS];
let dataToIterate = Object.keys(reports);
// check if there are any report-related updates
const reportUpdatesRelatedToReportActions = new Set<string>();
for (const actions of Object.values(reportActionsUpdates)) {
if (!actions) {
continue;
}
for (const reportAction of Object.values(actions)) {
if (reportAction?.childReportID) {
reportUpdatesRelatedToReportActions.add(`${ONYXKEYS.COLLECTION.REPORT}${reportAction.childReportID}`);
}
}
}
const updates = [
...Object.keys(reportUpdates),
...Object.keys(reportMetadataUpdates),
...Object.keys(reportActionsUpdates),
...Object.keys(reportNameValuePairsUpdates),
...Array.from(reportUpdatesRelatedToReportActions),
];
if (isFullyComputed) {
// if there are report-related updates, iterate over the updates
if (updates.length > 0 || !!transactionsUpdates || !!transactionViolationsUpdates) {
if (updates.length > 0) {
dataToIterate = prepareReportKeys(updates);
}
if (!!transactionsUpdates || !!transactionViolationsUpdates) {
let transactionReportIDs: string[] = [];
if (transactionsUpdates) {
transactionReportIDs = Object.values(transactionsUpdates).map((transaction) => `${ONYXKEYS.COLLECTION.REPORT}${transaction?.reportID}`);
}
// Also handle transaction violations updates by extracting transaction IDs and finding their reports
if (transactionViolationsUpdates) {
const violationTransactionIDs = Object.keys(transactionViolationsUpdates).map((key) => key.replace(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS, ''));
const violationReportIDs = violationTransactionIDs
.map((transactionID) => transactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`]?.reportID)
.filter(Boolean)
.map((reportID) => `${ONYXKEYS.COLLECTION.REPORT}${reportID}`);
// Also include chat reports for expense reports that have violations
const chatReportIDs = violationReportIDs
.map((reportKey) => reports?.[reportKey]?.chatReportID)
.filter(Boolean)
.map((chatReportID) => `${ONYXKEYS.COLLECTION.REPORT}${chatReportID}`);
transactionReportIDs = [...transactionReportIDs, ...violationReportIDs, ...chatReportIDs];
}
dataToIterate.push(...prepareReportKeys(transactionReportIDs));
}
} else {
// No updates to process, return current value to prevent unnecessary computation
return currentValue ?? {reports: {}, locale: null};
}
}
const reportAttributes = dataToIterate.reduce<ReportAttributesDerivedValue['reports']>((acc, key) => {
// source value sends partial data, so we need an entire report object to do computations
const report = reports[key];
if (!report || !isValidReport(report)) {
const reportID = key.replace(ONYXKEYS.COLLECTION.REPORT, '');
if (acc[reportID]) {
delete acc[reportID];
}
return acc;
}
const chatReport = reports?.[`${ONYXKEYS.COLLECTION.REPORT}${report.chatReportID}`];
const reportNameValuePair = reportNameValuePairs?.[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${report.reportID}`];
const reportActionsList = reportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.reportID}`];
const isReportArchived = isArchivedReport(reportNameValuePair);
const {hasAnyViolations, requiresAttention, reportErrors} = generateReportAttributes({
report,
chatReport,
reportActions,
transactionViolations,
isReportArchived,
});
let brickRoadStatus;
// if report has errors or violations, show red dot
if (SidebarUtils.shouldShowRedBrickRoad(report, chatReport, reportActionsList, hasAnyViolations, reportErrors, transactions, transactionViolations, !!isReportArchived)) {
brickRoadStatus = CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR;
}
// if report does not have error, check if it should show green dot
if (brickRoadStatus !== CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR && requiresAttention) {
brickRoadStatus = CONST.BRICK_ROAD_INDICATOR_STATUS.INFO;
}
acc[report.reportID] = {
reportName: report ? computeReportName(report, undefined, undefined, undefined, undefined, undefined, undefined, isReportArchived) : '',
isEmpty: generateIsEmptyReport(report, isReportArchived),
brickRoadStatus,
requiresAttention,
reportErrors,
};
return acc;
}, currentValue?.reports ?? {});
// mark the report attributes as fully computed after first iteration to avoid unnecessary recomputation on all objects
if (!Object.keys(reportUpdates).length && Object.keys(reports ?? {}).length > 0 && !isFullyComputed) {
isFullyComputed = true;
}
return {
reports: reportAttributes,
locale: preferredLocale ?? null,
};
},
});