From c3c2b597684645d8cbf08afb338960cdb0688f53 Mon Sep 17 00:00:00 2001 From: Rory Abraham <47436092+roryabraham@users.noreply.github.com> Date: Mon, 6 May 2024 16:16:13 -0700 Subject: [PATCH] Merge pull request #41644 from janicduplessis/@janic/fix-41573 Fix getContinuousReportActionChain with optimistic actions (cherry picked from commit df96271d5fafff03c2637d965209dd0b5ade8855) --- src/libs/ReportActionsUtils.ts | 55 +++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/src/libs/ReportActionsUtils.ts b/src/libs/ReportActionsUtils.ts index 71d66a5d2a31..ed9b2a7fcc50 100644 --- a/src/libs/ReportActionsUtils.ts +++ b/src/libs/ReportActionsUtils.ts @@ -299,6 +299,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. @@ -310,12 +333,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) { @@ -327,32 +345,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--; }