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

Improve performance of isConsecutiveActionMadeByPreviousActor #27956

Merged
merged 1 commit into from
Sep 25, 2023
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
21 changes: 18 additions & 3 deletions src/libs/ReportActionsUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,23 @@ function extractLinksFromMessageHtml(reportAction) {
return _.map([...htmlContent.matchAll(regex)], (match) => match[1]);
}

/**
* Returns the report action immediately before the specified index.
* @param {Array} reportActions - all actions
* @param {Number} actionIndex - index of the action
* @returns {Object|null}
*/
function findPreviousAction(reportActions, actionIndex) {
for (let i = actionIndex + 1; i < reportActions.length; i++) {
// Find the next non-pending deletion report action, as the pending delete action means that it is not displayed in the UI, but still is in the report actions list.
// If we are offline, all actions are pending but shown in the UI, so we take the previous action, even if it is a delete.
if (isNetworkOffline || reportActions[i].pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE) {
return reportActions[i];
}
}
return null;
}

/**
* Returns true when the report action immediately before the specified index is a comment made by the same actor who who is leaving a comment in the action at the specified index.
* Also checks to ensure that the comment is not too old to be shown as a grouped comment.
Expand All @@ -258,9 +275,7 @@ function extractLinksFromMessageHtml(reportAction) {
* @returns {Boolean}
*/
function isConsecutiveActionMadeByPreviousActor(reportActions, actionIndex) {
// Find the next non-pending deletion report action, as the pending delete action means that it is not displayed in the UI, but still is in the report actions list.
// If we are offline, all actions are pending but shown in the UI, so we take the previous action, even if it is a delete.
const previousAction = _.find(_.drop(reportActions, actionIndex + 1), (action) => isNetworkOffline || action.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE);
const previousAction = findPreviousAction(reportActions, actionIndex);
const currentAction = reportActions[actionIndex];

// It's OK for there to be no previous action, and in that case, false will be returned
Expand Down
Loading