Skip to content

Commit

Permalink
Merge pull request #51023 from c3024/sort-to-have-the-created-action-…
Browse files Browse the repository at this point in the history
…always-on-top

Sort to ensure the `CREATED` action is always on top
  • Loading branch information
stitesExpensify authored Oct 21, 2024
2 parents bab932c + 0ccaa8e commit 3edf81f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
11 changes: 6 additions & 5 deletions src/libs/ReportActionsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,15 +384,16 @@ function getSortedReportActions(reportActions: ReportAction[] | null, shouldSort
const invertedMultiplier = shouldSortInDescendingOrder ? -1 : 1;

const sortedActions = reportActions?.filter(Boolean).sort((first, second) => {
// First sort by timestamp
// First sort by action type, ensuring that `CREATED` actions always come first if they have the same or even a later timestamp as another action type
if ((first.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED || second.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED) && first.actionName !== second.actionName) {
return (first.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED ? -1 : 1) * invertedMultiplier;
}

// Then sort by timestamp
if (first.created !== second.created) {
return (first.created < second.created ? -1 : 1) * invertedMultiplier;
}

// Then by action type, ensuring that `CREATED` actions always come first if they have the same timestamp as another action type
if ((first.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED || second.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED) && first.actionName !== second.actionName) {
return (first.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED ? -1 : 1) * invertedMultiplier;
}
// Ensure that `REPORT_PREVIEW` actions always come after if they have the same timestamp as another action type
if ((first.actionName === CONST.REPORT.ACTIONS.TYPE.REPORT_PREVIEW || second.actionName === CONST.REPORT.ACTIONS.TYPE.REPORT_PREVIEW) && first.actionName !== second.actionName) {
return (first.actionName === CONST.REPORT.ACTIONS.TYPE.REPORT_PREVIEW ? 1 : -1) * invertedMultiplier;
Expand Down
14 changes: 11 additions & 3 deletions tests/unit/ReportActionsUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,12 @@ describe('ReportActionsUtils', () => {
},
];

// Expected output should have the `CREATED` action at last
// eslint-disable-next-line rulesdir/prefer-at
const expectedOutput: ReportAction[] = [...input.slice(0, 1), ...input.slice(2), input[1]];

const result = ReportActionsUtils.getSortedReportActionsForDisplay(input);
expect(result).toStrictEqual(input);
expect(result).toStrictEqual(expectedOutput);
});

it('should filter out closed actions', () => {
Expand Down Expand Up @@ -392,9 +396,13 @@ describe('ReportActionsUtils', () => {
],
},
];

// Expected output should have the `CREATED` action at last and `CLOSED` action removed
// eslint-disable-next-line rulesdir/prefer-at
const expectedOutput: ReportAction[] = [...input.slice(0, 1), ...input.slice(2, -1), input[1]];

const result = ReportActionsUtils.getSortedReportActionsForDisplay(input);
input.pop();
expect(result).toStrictEqual(input);
expect(result).toStrictEqual(expectedOutput);
});

it('should filter out deleted, non-pending comments', () => {
Expand Down

0 comments on commit 3edf81f

Please sign in to comment.