diff --git a/src/libs/ReportActionsUtils.js b/src/libs/ReportActionsUtils.js index cda1f39d3087..19b0513b7e41 100644 --- a/src/libs/ReportActionsUtils.js +++ b/src/libs/ReportActionsUtils.js @@ -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. @@ -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