Skip to content

Commit

Permalink
Merge pull request #41644 from janicduplessis/@janic/fix-41573
Browse files Browse the repository at this point in the history
Fix getContinuousReportActionChain with optimistic actions
  • Loading branch information
roryabraham committed May 6, 2024
2 parents 19c0687 + ad8ca1d commit df96271
Showing 1 changed file with 31 additions and 24 deletions.
55 changes: 31 additions & 24 deletions src/libs/ReportActionsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,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 a sorted and filtered list of report actions from a report and it's associated child
* transaction thread report in order to correctly display reportActions from both reports in the one-transaction report view.
Expand Down Expand Up @@ -295,12 +318,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) {
Expand All @@ -312,32 +330,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--;
}
Expand Down

0 comments on commit df96271

Please sign in to comment.