|
1 | 1 | import {rand} from '@ngneat/falso'; |
2 | | -import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; |
| 2 | +import type {OnyxCollection} from 'react-native-onyx'; |
3 | 3 | import Onyx from 'react-native-onyx'; |
4 | 4 | import {measureFunction} from 'reassure'; |
5 | 5 | import {getMovedReportID} from '@libs/ModifiedExpenseMessage'; |
6 | 6 | import {getLastMessageTextForReport} from '@libs/OptionsListUtils'; |
7 | 7 | import { |
8 | | - getOriginalMessage, |
9 | 8 | getSortedReportActions, |
10 | 9 | getSortedReportActionsForDisplay, |
11 | | - isInviteOrRemovedAction, |
12 | 10 | shouldReportActionBeVisibleAsLastAction, |
13 | 11 | } from '@libs/ReportActionsUtils'; |
14 | | -import {canUserPerformWriteAction} from '@libs/ReportUtils'; |
15 | 12 | import SidebarUtils from '@libs/SidebarUtils'; |
16 | 13 | import CONST from '@src/CONST'; |
17 | 14 | import ONYXKEYS from '@src/ONYXKEYS'; |
@@ -75,12 +72,12 @@ const createLHNReportsWithActions = (count: number) => { |
75 | 72 | const reports: OnyxCollection<Report> = {}; |
76 | 73 | const reportActions: OnyxCollection<ReportActions> = {}; |
77 | 74 | const reportNameValuePairs: OnyxCollection<ReportNameValuePairs> = {}; |
78 | | - const policies: OnyxCollection<Policy> = {}; |
| 75 | + const lhnPoliciesMap: OnyxCollection<Policy> = {}; |
79 | 76 | const personalDetailsWithActions: OnyxCollection<PersonalDetails> = {}; |
80 | 77 | const reportMetadata: OnyxCollection<ReportMetadata> = {}; |
81 | 78 |
|
82 | 79 | const basePolicy = createRandomPolicy(1); |
83 | | - policies[`${ONYXKEYS.COLLECTION.POLICY}${basePolicy.id}`] = basePolicy; |
| 80 | + lhnPoliciesMap[`${ONYXKEYS.COLLECTION.POLICY}${basePolicy.id}`] = basePolicy; |
84 | 81 |
|
85 | 82 | for (let i = 1; i <= count; i++) { |
86 | 83 | const reportID = String(i); |
@@ -118,7 +115,7 @@ const createLHNReportsWithActions = (count: number) => { |
118 | 115 | reports, |
119 | 116 | reportActions, |
120 | 117 | reportNameValuePairs, |
121 | | - policies, |
| 118 | + policies: lhnPoliciesMap, |
122 | 119 | personalDetailsWithActions, |
123 | 120 | reportMetadata, |
124 | 121 | }; |
@@ -252,120 +249,61 @@ describe('SidebarUtils', () => { |
252 | 249 | test('[SidebarUtils LHN] getLastMessageTextForReport across all reports', async () => { |
253 | 250 | await waitForBatchedUpdates(); |
254 | 251 |
|
255 | | - await measureFunction(() => { |
256 | | - for (const reportKey of Object.keys(lhnReports)) { |
| 252 | + const inputs = Object.keys(lhnReports) |
| 253 | + .map((reportKey) => { |
257 | 254 | const reportID = reportKey.replace(ONYXKEYS.COLLECTION.REPORT, ''); |
258 | 255 | const report = lhnReports[reportKey]; |
259 | 256 | const nvp = lhnReportNameValuePairs[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${reportID}`]; |
260 | 257 | const actions = lhnReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`]; |
261 | 258 | const isReportArchived = !!nvp?.private_isArchived; |
262 | 259 |
|
263 | | - if (report && actions) { |
264 | | - const lastActorAccountID = report.lastActorAccountID; |
265 | | - let lastActorDetails: Partial<PersonalDetails> | null = null; |
266 | | - if (lastActorAccountID && lhnPersonalDetails[lastActorAccountID]) { |
267 | | - lastActorDetails = lhnPersonalDetails[lastActorAccountID]; |
268 | | - } |
| 260 | + if (!report || !actions) { |
| 261 | + return null; |
| 262 | + } |
269 | 263 |
|
270 | | - const sortedActions = getSortedReportActions(Object.values(actions), true); |
271 | | - const lastReportAction = sortedActions.at(0); |
272 | | - |
273 | | - const movedFromReportID = lastReportAction ? getMovedReportID(lastReportAction, CONST.REPORT.MOVE_TYPE.FROM) : undefined; |
274 | | - const movedToReportID = lastReportAction ? getMovedReportID(lastReportAction, CONST.REPORT.MOVE_TYPE.TO) : undefined; |
275 | | - const movedFromReport = movedFromReportID ? lhnReports[`${ONYXKEYS.COLLECTION.REPORT}${movedFromReportID}`] : undefined; |
276 | | - const movedToReport = movedToReportID ? lhnReports[`${ONYXKEYS.COLLECTION.REPORT}${movedToReportID}`] : undefined; |
277 | | - |
278 | | - const itemPolicy = lhnPolicies[`${ONYXKEYS.COLLECTION.POLICY}${report.policyID}`]; |
279 | | - const itemReportMetadata = lhnReportMetadata[`${ONYXKEYS.COLLECTION.REPORT_METADATA}${reportID}`]; |
280 | | - |
281 | | - getLastMessageTextForReport({ |
282 | | - translate: () => '', |
283 | | - report, |
284 | | - lastActorDetails, |
285 | | - movedFromReport, |
286 | | - movedToReport, |
287 | | - policy: itemPolicy, |
288 | | - isReportArchived, |
289 | | - policyForMovingExpensesID: undefined, |
290 | | - reportMetadata: itemReportMetadata, |
291 | | - currentUserAccountID: 0, |
292 | | - }); |
| 264 | + const lastActorAccountID = report.lastActorAccountID; |
| 265 | + let lastActorDetails: Partial<PersonalDetails> | null = null; |
| 266 | + if (lastActorAccountID && lhnPersonalDetails[lastActorAccountID]) { |
| 267 | + lastActorDetails = lhnPersonalDetails[lastActorAccountID]; |
293 | 268 | } |
294 | | - } |
295 | | - }); |
296 | | - }); |
297 | 269 |
|
298 | | - test('[SidebarUtils LHN] composite renderItem-style simulation for all reports', async () => { |
299 | | - await waitForBatchedUpdates(); |
| 270 | + const sortedActions = getSortedReportActions(Object.values(actions), true); |
| 271 | + const lastReportAction = sortedActions.at(0); |
| 272 | + |
| 273 | + const movedFromReportID = lastReportAction ? getMovedReportID(lastReportAction, CONST.REPORT.MOVE_TYPE.FROM) : undefined; |
| 274 | + const movedToReportID = lastReportAction ? getMovedReportID(lastReportAction, CONST.REPORT.MOVE_TYPE.TO) : undefined; |
| 275 | + const movedFromReport = movedFromReportID ? lhnReports[`${ONYXKEYS.COLLECTION.REPORT}${movedFromReportID}`] : undefined; |
| 276 | + const movedToReport = movedToReportID ? lhnReports[`${ONYXKEYS.COLLECTION.REPORT}${movedToReportID}`] : undefined; |
| 277 | + |
| 278 | + const itemPolicy = lhnPolicies[`${ONYXKEYS.COLLECTION.POLICY}${report.policyID}`]; |
| 279 | + const itemReportMetadata = lhnReportMetadata[`${ONYXKEYS.COLLECTION.REPORT_METADATA}${reportID}`]; |
| 280 | + |
| 281 | + return { |
| 282 | + report, |
| 283 | + lastActorDetails, |
| 284 | + movedFromReport, |
| 285 | + movedToReport, |
| 286 | + itemPolicy, |
| 287 | + isReportArchived, |
| 288 | + itemReportMetadata, |
| 289 | + }; |
| 290 | + }) |
| 291 | + .filter((value): value is NonNullable<typeof value> => value !== null); |
300 | 292 |
|
301 | 293 | await measureFunction(() => { |
302 | | - for (const reportKey of Object.keys(lhnReports)) { |
303 | | - const reportID = reportKey.replace(ONYXKEYS.COLLECTION.REPORT, ''); |
304 | | - const item = lhnReports[reportKey]; |
305 | | - const itemReportActions = lhnReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`]; |
306 | | - const itemReportNameValuePairs = lhnReportNameValuePairs[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${reportID}`]; |
307 | | - const isReportArchived = !!itemReportNameValuePairs?.private_isArchived; |
308 | | - |
309 | | - if (itemReportActions && item) { |
310 | | - const canUserPerformWrite = canUserPerformWriteAction(item, isReportArchived); |
311 | | - const sortedReportActions = getSortedReportActionsForDisplay(itemReportActions, canUserPerformWrite); |
312 | | - const lastReportAction = sortedReportActions.at(0); |
313 | | - |
314 | | - let lastActorDetails: Partial<PersonalDetails> | null = null; |
315 | | - if (item.lastActorAccountID && lhnPersonalDetails[item.lastActorAccountID]) { |
316 | | - lastActorDetails = lhnPersonalDetails[item.lastActorAccountID] ?? null; |
317 | | - } else if (lastReportAction) { |
318 | | - const lastActorDisplayName = lastReportAction?.person?.[0]?.text; |
319 | | - lastActorDetails = lastActorDisplayName |
320 | | - ? { |
321 | | - displayName: lastActorDisplayName, |
322 | | - accountID: item.lastActorAccountID, |
323 | | - } |
324 | | - : null; |
325 | | - } |
326 | | - |
327 | | - const movedFromReportID = lastReportAction ? getMovedReportID(lastReportAction, CONST.REPORT.MOVE_TYPE.FROM) : undefined; |
328 | | - const movedToReportID = lastReportAction ? getMovedReportID(lastReportAction, CONST.REPORT.MOVE_TYPE.TO) : undefined; |
329 | | - const movedFromReport = movedFromReportID ? lhnReports[`${ONYXKEYS.COLLECTION.REPORT}${movedFromReportID}`] : undefined; |
330 | | - const movedToReport = movedToReportID ? lhnReports[`${ONYXKEYS.COLLECTION.REPORT}${movedToReportID}`] : undefined; |
331 | | - |
332 | | - const itemPolicy = lhnPolicies[`${ONYXKEYS.COLLECTION.POLICY}${item.policyID}`]; |
333 | | - const itemReportMetadata = lhnReportMetadata[`${ONYXKEYS.COLLECTION.REPORT_METADATA}${reportID}`]; |
334 | | - |
335 | | - const lastMessageTextFromReport = getLastMessageTextForReport({ |
336 | | - translate: () => '', |
337 | | - report: item, |
338 | | - lastActorDetails, |
339 | | - movedFromReport, |
340 | | - movedToReport, |
341 | | - policy: itemPolicy, |
342 | | - isReportArchived, |
343 | | - policyForMovingExpensesID: undefined, |
344 | | - reportMetadata: itemReportMetadata, |
345 | | - currentUserAccountID: 0, |
346 | | - }); |
347 | | - |
348 | | - const canUserPerformWriteActionForLastAction = canUserPerformWriteAction(item, isReportArchived); |
349 | | - const actionsArray = getSortedReportActions(Object.values(itemReportActions)); |
350 | | - |
351 | | - const reportActionsForDisplay = actionsArray.filter( |
352 | | - (reportAction) => |
353 | | - shouldReportActionBeVisibleAsLastAction(reportAction, canUserPerformWriteActionForLastAction) && |
354 | | - reportAction.actionName !== CONST.REPORT.ACTIONS.TYPE.CREATED, |
355 | | - ); |
356 | | - const lastAction = reportActionsForDisplay.at(-1); |
357 | | - |
358 | | - let lastActionReport: OnyxEntry<Report> | undefined; |
359 | | - if (lastAction && isInviteOrRemovedAction(lastAction)) { |
360 | | - const lastActionOriginalMessage = lastAction?.actionName ? getOriginalMessage(lastAction) : null; |
361 | | - lastActionReport = lastActionOriginalMessage?.reportID |
362 | | - ? lhnReports[`${ONYXKEYS.COLLECTION.REPORT}${lastActionOriginalMessage.reportID}`] |
363 | | - : undefined; |
364 | | - } |
365 | | - |
366 | | - // eslint-disable-next-line @typescript-eslint/no-unused-expressions |
367 | | - [lastMessageTextFromReport, lastAction, lastActionReport]; |
368 | | - } |
| 294 | + for (const input of inputs) { |
| 295 | + getLastMessageTextForReport({ |
| 296 | + translate: () => '', |
| 297 | + report: input.report, |
| 298 | + lastActorDetails: input.lastActorDetails, |
| 299 | + movedFromReport: input.movedFromReport, |
| 300 | + movedToReport: input.movedToReport, |
| 301 | + policy: input.itemPolicy, |
| 302 | + isReportArchived: input.isReportArchived, |
| 303 | + policyForMovingExpensesID: undefined, |
| 304 | + reportMetadata: input.itemReportMetadata, |
| 305 | + currentUserAccountID: 0, |
| 306 | + }); |
369 | 307 | } |
370 | 308 | }); |
371 | 309 | }); |
|
0 commit comments