Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve getOneTransactionThreadReportID performance #46187

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions src/libs/ReportActionsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,13 @@ function isTaskAction(reportAction: OnyxEntry<ReportAction>): boolean {
);
}

// Get all IOU report actions for the report.
const iouRequestTypes = new Set<ValueOf<typeof CONST.IOU.REPORT_ACTION_TYPE>>([
CONST.IOU.REPORT_ACTION_TYPE.CREATE,
CONST.IOU.REPORT_ACTION_TYPE.SPLIT,
CONST.IOU.REPORT_ACTION_TYPE.PAY,
CONST.IOU.REPORT_ACTION_TYPE.TRACK,
]);
/**
* Gets the reportID for the transaction thread associated with a report by iterating over the reportActions and identifying the IOU report actions.
* Returns a reportID if there is exactly one transaction thread for the report, and null otherwise.
Expand All @@ -966,28 +973,23 @@ function getOneTransactionThreadReportID(reportID: string, reportActions: OnyxEn
return;
}

const reportActionsArray = Object.values(reportActions ?? {});
const reportActionsArray = Array.isArray(reportActions) ? reportActions : Object.values(reportActions ?? {});
if (!reportActionsArray.length) {
return;
}

// Get all IOU report actions for the report.
const iouRequestTypes: Array<ValueOf<typeof CONST.IOU.REPORT_ACTION_TYPE>> = [
CONST.IOU.REPORT_ACTION_TYPE.CREATE,
CONST.IOU.REPORT_ACTION_TYPE.SPLIT,
CONST.IOU.REPORT_ACTION_TYPE.PAY,
CONST.IOU.REPORT_ACTION_TYPE.TRACK,
];

const iouRequestActions = reportActionsArray.filter((action) => {
const iouRequestActions = [];
for (const action of reportActionsArray) {
if (!isMoneyRequestAction(action)) {
return false;
// eslint-disable-next-line no-continue
continue;
}

const originalMessage = getOriginalMessage(action);
const actionType = originalMessage?.type;
return (
if (
actionType &&
(iouRequestTypes.includes(actionType) ?? []) &&
iouRequestTypes.has(actionType) &&
action.childReportID &&
// Include deleted IOU reportActions if:
// - they have an assocaited IOU transaction ID or
Expand All @@ -997,22 +999,27 @@ function getOneTransactionThreadReportID(reportID: string, reportActions: OnyxEn
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
(isMessageDeleted(action) && action.childVisibleActionCount) ||
(action.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE && (isOffline ?? isNetworkOffline)))
);
});
) {
iouRequestActions.push(action);
}
}

// If we don't have any IOU request actions, or we have more than one IOU request actions, this isn't a oneTransaction report
if (!iouRequestActions.length || iouRequestActions.length > 1) {
return;
}

const singleAction = iouRequestActions[0];
const originalMessage = getOriginalMessage(singleAction);

// If there's only one IOU request action associated with the report but it's been deleted, then we don't consider this a oneTransaction report
// and want to display it using the standard view
if (isMoneyRequestAction(iouRequestActions[0]) && (getOriginalMessage(iouRequestActions[0])?.deleted ?? '') !== '') {
if ((originalMessage?.deleted ?? '') !== '' && isMoneyRequestAction(singleAction)) {
return;
}

// Ensure we have a childReportID associated with the IOU report action
return iouRequestActions[0].childReportID;
return singleAction.childReportID;
}

/**
Expand Down
Loading