From 3589e0dbe9f41a494e4dfb6657c7875701329854 Mon Sep 17 00:00:00 2001 From: Github Date: Tue, 20 Aug 2024 15:12:11 +0200 Subject: [PATCH 1/2] improve performance of getAllReportErrors --- src/libs/OptionsListUtils.ts | 37 ++++++++++++++++++------------------ src/libs/ReportUtils.ts | 14 ++++++++++---- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/src/libs/OptionsListUtils.ts b/src/libs/OptionsListUtils.ts index 4517ddea1af7..3b3185e67102 100644 --- a/src/libs/OptionsListUtils.ts +++ b/src/libs/OptionsListUtils.ts @@ -471,13 +471,14 @@ function uniqFast(items: string[]): string[] { function getAllReportErrors(report: OnyxEntry, reportActions: OnyxEntry): OnyxCommon.Errors { const reportErrors = report?.errors ?? {}; const reportErrorFields = report?.errorFields ?? {}; - const reportActionErrors: OnyxCommon.ErrorFields = Object.values(reportActions ?? {}).reduce((prevReportActionErrors, action) => { - if (!action || isEmptyObject(action.errors)) { - return prevReportActionErrors; - } + const reportActionsArray = Object.values(reportActions ?? {}); + const reportActionErrors: OnyxCommon.ErrorFields = {}; - return Object.assign(prevReportActionErrors, action.errors); - }, {}); + for (const action of reportActionsArray) { + if (action && !isEmptyObject(action.errors)) { + Object.assign(reportActionErrors, action.errors); + } + } const parentReportAction: OnyxEntry = !report?.parentReportID || !report?.parentReportActionID ? undefined : allReportActions?.[report.parentReportID ?? '-1']?.[report.parentReportActionID ?? '-1']; @@ -491,24 +492,22 @@ function getAllReportErrors(report: OnyxEntry, reportActions: OnyxEntry< if (ReportUtils.shouldShowRBRForMissingSmartscanFields(report?.reportID ?? '-1') && !ReportUtils.isSettled(report?.reportID)) { reportActionErrors.smartscan = ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('iou.error.genericSmartscanFailureMessage'); } - } else if (ReportUtils.hasSmartscanError(Object.values(reportActions ?? {}))) { + } else if (ReportUtils.hasSmartscanError(reportActionsArray)) { reportActionErrors.smartscan = ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('iou.error.genericSmartscanFailureMessage'); } // All error objects related to the report. Each object in the sources contains error messages keyed by microtime - const errorSources = { - reportErrors, - ...reportErrorFields, - ...reportActionErrors, - }; - // Combine all error messages keyed by microtime into one object - const allReportErrors = Object.values(errorSources)?.reduce((prevReportErrors, errors) => { - if (isEmptyObject(errors)) { - return prevReportErrors; - } + // eslint-disable-next-line prefer-object-spread + const errorSources = Object.assign({}, reportErrors, reportErrorFields, reportActionErrors); - return Object.assign(prevReportErrors, errors); - }, {}); + // Combine all error messages keyed by microtime into one object + const errorSourcesArray = Object.values(errorSources ?? {}); + const allReportErrors = {}; + for (const errors of errorSourcesArray) { + if (!isEmptyObject(errors)) { + Object.assign(allReportErrors, errors); + } + } return allReportErrors; } diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index bdc536ac2ad3..302abb6e6764 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -7088,16 +7088,22 @@ function canEditPolicyDescription(policy: OnyxEntry): boolean { */ function hasSmartscanError(reportActions: ReportAction[]) { return reportActions.some((action) => { - if (!ReportActionsUtils.isSplitBillAction(action) && !ReportActionsUtils.isReportPreviewAction(action)) { + const isReportPreview = ReportActionsUtils.isReportPreviewAction(action); + const isSplitReportAction = ReportActionsUtils.isSplitBillAction(action); + if (!isSplitReportAction && !isReportPreview) { return false; } const IOUReportID = ReportActionsUtils.getIOUReportIDFromReportActionPreview(action); - const isReportPreviewError = ReportActionsUtils.isReportPreviewAction(action) && shouldShowRBRForMissingSmartscanFields(IOUReportID) && !isSettled(IOUReportID); + const isReportPreviewError = isReportPreview && shouldShowRBRForMissingSmartscanFields(IOUReportID) && !isSettled(IOUReportID); + if (isReportPreviewError) { + return true; + } + const transactionID = ReportActionsUtils.isMoneyRequestAction(action) ? ReportActionsUtils.getOriginalMessage(action)?.IOUTransactionID ?? '-1' : '-1'; const transaction = allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] ?? {}; - const isSplitBillError = ReportActionsUtils.isSplitBillAction(action) && TransactionUtils.hasMissingSmartscanFields(transaction as Transaction); + const isSplitBillError = isSplitReportAction && TransactionUtils.hasMissingSmartscanFields(transaction as Transaction); - return isReportPreviewError || isSplitBillError; + return isSplitBillError; }); } From 823077878e1d19098846107fc79ca7dc4507e869 Mon Sep 17 00:00:00 2001 From: Github Date: Mon, 9 Sep 2024 17:38:05 +0200 Subject: [PATCH 2/2] add comment --- src/libs/OptionsListUtils.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libs/OptionsListUtils.ts b/src/libs/OptionsListUtils.ts index 3b3185e67102..f191c1d06532 100644 --- a/src/libs/OptionsListUtils.ts +++ b/src/libs/OptionsListUtils.ts @@ -496,6 +496,7 @@ function getAllReportErrors(report: OnyxEntry, reportActions: OnyxEntry< reportActionErrors.smartscan = ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('iou.error.genericSmartscanFailureMessage'); } // All error objects related to the report. Each object in the sources contains error messages keyed by microtime + // Use Object.assign to merge all error objects into one since it is faster and uses less memory than spread operator // eslint-disable-next-line prefer-object-spread const errorSources = Object.assign({}, reportErrors, reportErrorFields, reportActionErrors);