Skip to content

Commit 9df5b91

Browse files
Revert to single-pass O(n) loop for finding earliest report action
Replace getSortedReportActions (O(n log n)) with a single-pass for-loop that tracks the earliest matching action in both getIOUReportActionWithBadge and getReasonAndReportActionThatRequiresAttention. Keeps the unit test. Co-authored-by: Aimane Chnaif <aimane-chnaif@users.noreply.github.com>
1 parent 989cd82 commit 9df5b91

2 files changed

Lines changed: 34 additions & 20 deletions

File tree

src/libs/ReportUtils.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4279,19 +4279,25 @@ function getReasonAndReportActionThatRequiresAttention(
42794279
}
42804280

42814281
if (isInvoiceRoom(optionOrReport)) {
4282-
const sortedActions = getSortedReportActions(Object.values(reportActions));
4283-
const reportAction = sortedActions.find(
4284-
(action) =>
4285-
action.actionName === CONST.REPORT.ACTIONS.TYPE.REPORT_PREVIEW &&
4286-
action.childReportID &&
4287-
hasMissingInvoiceBankAccount(action.childReportID) &&
4288-
!isSettled(action.childReportID),
4289-
);
4282+
let earliestAction: ReportAction | undefined;
4283+
for (const action of Object.values(reportActions)) {
4284+
if (
4285+
action.actionName !== CONST.REPORT.ACTIONS.TYPE.REPORT_PREVIEW ||
4286+
!action.childReportID ||
4287+
!hasMissingInvoiceBankAccount(action.childReportID) ||
4288+
isSettled(action.childReportID)
4289+
) {
4290+
continue;
4291+
}
4292+
if (!earliestAction?.created || (action.created && action.created < earliestAction.created)) {
4293+
earliestAction = action;
4294+
}
4295+
}
42904296

4291-
return reportAction
4297+
return earliestAction
42924298
? {
42934299
reason: CONST.REQUIRES_ATTENTION_REASONS.HAS_MISSING_INVOICE_BANK_ACCOUNT,
4294-
reportAction,
4300+
reportAction: earliestAction,
42954301
}
42964302
: null;
42974303
}

src/libs/actions/IOU/ReportWorkflow.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import Navigation from '@libs/Navigation/Navigation';
1717
import {getIsOffline} from '@libs/NetworkState';
1818
import {buildNextStepNew, buildOptimisticNextStep} from '@libs/NextStepUtils';
1919
import {arePaymentsEnabled, getSubmitReportManagerAccountID, hasDynamicExternalWorkflow, isPaidGroupPolicy, isPolicyAdmin, isSubmitAndClose} from '@libs/PolicyUtils';
20-
import {getAllReportActions, getReportActionHtml, getReportActionText, getSortedReportActions, hasPendingDEWApprove, isCreatedAction, isDeletedAction} from '@libs/ReportActionsUtils';
20+
import {getAllReportActions, getReportActionHtml, getReportActionText, hasPendingDEWApprove, isCreatedAction, isDeletedAction} from '@libs/ReportActionsUtils';
2121
import {
2222
buildOptimisticApprovedReportAction,
2323
buildOptimisticChangeApproverReportAction,
@@ -308,21 +308,29 @@ function getIOUReportActionWithBadge(
308308
const chatReportActions = getAllReportActionsFromIOU()?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport?.reportID}`] ?? {};
309309

310310
let actionBadge: ValueOf<typeof CONST.REPORT.ACTION_BADGE> | undefined;
311-
const sortedActions = getSortedReportActions(Object.values(chatReportActions));
312-
const reportAction = sortedActions.find((action) => {
311+
let earliestAction: ReportAction | undefined;
312+
let earliestBadge: ValueOf<typeof CONST.REPORT.ACTION_BADGE> | undefined;
313+
314+
for (const action of Object.values(chatReportActions)) {
313315
if (action?.actionName !== CONST.REPORT.ACTIONS.TYPE.REPORT_PREVIEW || isDeletedAction(action)) {
314-
return false;
316+
continue;
315317
}
316318
const iouReport = getReportOrDraftReport(action.childReportID);
317319
const badge = getBadgeFromIOUReport(iouReport, chatReport, policy, reportMetadata, invoiceReceiverPolicy, currentUserLogin, currentUserAccountID);
318-
if (badge) {
319-
actionBadge = badge;
320-
return true;
320+
if (!badge) {
321+
continue;
321322
}
322-
return false;
323-
});
323+
if (!earliestAction?.created || (action.created && action.created < earliestAction.created)) {
324+
earliestAction = action;
325+
earliestBadge = badge;
326+
}
327+
}
328+
329+
if (earliestAction) {
330+
actionBadge = earliestBadge;
331+
}
324332

325-
return {reportAction, actionBadge};
333+
return {reportAction: earliestAction, actionBadge};
326334
}
327335

328336
/**

0 commit comments

Comments
 (0)