diff --git a/src/libs/OptionsListUtils.ts b/src/libs/OptionsListUtils.ts index 4517ddea1af7..f191c1d06532 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,23 @@ 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; - } + // 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); - 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; }); }