diff --git a/src/libs/ReportActionsUtils.ts b/src/libs/ReportActionsUtils.ts index dc07f31c1067..46354b4b8774 100644 --- a/src/libs/ReportActionsUtils.ts +++ b/src/libs/ReportActionsUtils.ts @@ -301,6 +301,29 @@ function getSortedReportActions(reportActions: ReportAction[] | null, shouldSort return sortedActions; } +function isOptimisticAction(reportAction: ReportAction) { + return ( + !!reportAction.isOptimisticAction || + reportAction.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD || + reportAction.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE + ); +} + +function shouldIgnoreGap(currentReportAction: ReportAction | undefined, nextReportAction: ReportAction | undefined) { + if (!currentReportAction || !nextReportAction) { + return false; + } + return ( + isOptimisticAction(currentReportAction) || + isOptimisticAction(nextReportAction) || + !!currentReportAction.whisperedToAccountIDs?.length || + !!nextReportAction.whisperedToAccountIDs?.length || + currentReportAction.actionName === CONST.REPORT.ACTIONS.TYPE.ROOM_CHANGE_LOG.INVITE_TO_ROOM || + nextReportAction.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED || + nextReportAction.actionName === CONST.REPORT.ACTIONS.TYPE.CLOSED + ); +} + /** * Returns the largest gapless range of reportActions including a the provided reportActionID, where a "gap" is defined as a reportAction's `previousReportActionID` not matching the previous reportAction in the sortedReportActions array. * See unit tests for example of inputs and expected outputs. @@ -312,12 +335,7 @@ function getContinuousReportActionChain(sortedReportActions: ReportAction[], id? if (id) { index = sortedReportActions.findIndex((reportAction) => reportAction.reportActionID === id); } else { - index = sortedReportActions.findIndex( - (reportAction) => - reportAction.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD && - reportAction.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE && - !reportAction.isOptimisticAction, - ); + index = sortedReportActions.findIndex((reportAction) => !isOptimisticAction(reportAction)); } if (index === -1) { @@ -329,32 +347,21 @@ function getContinuousReportActionChain(sortedReportActions: ReportAction[], id? let startIndex = index; let endIndex = index; - // Iterate forwards through the array, starting from endIndex. This loop checks the continuity of actions by: - // 1. Comparing the current item's previousReportActionID with the next item's reportActionID. - // This ensures that we are moving in a sequence of related actions from newer to older. + // Iterate forwards through the array, starting from endIndex. i.e: newer to older + // This loop checks the continuity of actions by comparing the current item's previousReportActionID with the next item's reportActionID. + // It ignores optimistic actions, whispers and InviteToRoom actions while ( (endIndex < sortedReportActions.length - 1 && sortedReportActions[endIndex].previousReportActionID === sortedReportActions[endIndex + 1].reportActionID) || - !!sortedReportActions[endIndex + 1]?.whisperedToAccountIDs?.length || - !!sortedReportActions[endIndex]?.whisperedToAccountIDs?.length || - sortedReportActions[endIndex]?.actionName === CONST.REPORT.ACTIONS.TYPE.ROOM_CHANGE_LOG.INVITE_TO_ROOM || - sortedReportActions[endIndex + 1]?.actionName === CONST.REPORT.ACTIONS.TYPE.CLOSED || - sortedReportActions[endIndex + 1]?.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED + shouldIgnoreGap(sortedReportActions[endIndex], sortedReportActions[endIndex + 1]) ) { endIndex++; } - // Iterate backwards through the sortedReportActions, starting from startIndex. This loop has two main checks: - // 1. It compares the current item's reportActionID with the previous item's previousReportActionID. - // This is to ensure continuity in a sequence of actions. - // 2. If the first condition fails, it then checks if the previous item has a pendingAction of 'add'. - // This additional check is to include recently sent messages that might not yet be part of the established sequence. + // Iterate backwards through the sortedReportActions, starting from startIndex. (older to newer) + // This loop ensuress continuity in a sequence of actions by comparing the current item's reportActionID with the previous item's previousReportActionID. while ( (startIndex > 0 && sortedReportActions[startIndex].reportActionID === sortedReportActions[startIndex - 1].previousReportActionID) || - sortedReportActions[startIndex - 1]?.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD || - sortedReportActions[startIndex - 1]?.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE || - // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing - sortedReportActions[startIndex - 1]?.isOptimisticAction || - sortedReportActions[startIndex - 1]?.actionName === CONST.REPORT.ACTIONS.TYPE.ROOM_CHANGE_LOG.INVITE_TO_ROOM + shouldIgnoreGap(sortedReportActions[startIndex], sortedReportActions[startIndex - 1]) ) { startIndex--; }