Skip to content

Commit

Permalink
Merge pull request Expensify#47780 from callstack-internal/fix/improv…
Browse files Browse the repository at this point in the history
…e-getAllReportErrors-performance

improve performance of getAllReportErrors
  • Loading branch information
flodnv authored Sep 10, 2024
2 parents 1725041 + 8230778 commit e96b038
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
38 changes: 19 additions & 19 deletions src/libs/OptionsListUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,13 +471,14 @@ function uniqFast(items: string[]): string[] {
function getAllReportErrors(report: OnyxEntry<Report>, reportActions: OnyxEntry<ReportActions>): 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<ReportAction> =
!report?.parentReportID || !report?.parentReportActionID ? undefined : allReportActions?.[report.parentReportID ?? '-1']?.[report.parentReportActionID ?? '-1'];

Expand All @@ -491,24 +492,23 @@ function getAllReportErrors(report: OnyxEntry<Report>, 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;
}

Expand Down
14 changes: 10 additions & 4 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7088,16 +7088,22 @@ function canEditPolicyDescription(policy: OnyxEntry<Policy>): 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;
});
}

Expand Down

0 comments on commit e96b038

Please sign in to comment.