Skip to content

Commit

Permalink
Merge pull request #14213 from Expensify/Rory-ImproveReportActionsSor…
Browse files Browse the repository at this point in the history
…ting
  • Loading branch information
roryabraham authored Jan 11, 2023
2 parents 8133a76 + c913abd commit ea6c52c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/libs/ReportActionsUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,19 @@ function getSortedReportActions(reportActions, shouldSortInDescendingOrder = fal
}
const invertedMultiplier = shouldSortInDescendingOrder ? -1 : 1;
reportActions.sort((first, second) => {
// If only one action is a report created action, return the created action first.
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;
}

// First 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;
}

// Then fallback on reportActionID as the final sorting criteria. It is a random number,
// but using this will ensure that the order of reportActions with the same created time and action type
// will be consistent across all users and devices
return (first.reportActionID < second.reportActionID ? -1 : 1) * invertedMultiplier;
});
return reportActions;
Expand Down
48 changes: 48 additions & 0 deletions tests/unit/ReportActionsUtilsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,94 @@ describe('ReportActionsUtils', () => {
{
created: '2022-11-09 22:27:01.825',
reportActionID: '8401445780099176',
actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT,
},
{
created: '2022-11-09 22:27:01.600',
reportActionID: '6401435781022176',
actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT,
},

// These reportActions were created in the same millisecond so should appear ordered by reportActionID
{
created: '2022-11-09 22:26:48.789',
reportActionID: '2962390724708756',
actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT,
},
{
created: '2022-11-09 22:26:48.789',
reportActionID: '1609646094152486',
actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT,
},
{
created: '2022-11-09 22:26:48.789',
reportActionID: '1661970171066218',
actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT,
},
],
[
{
created: '2022-11-09 22:26:48.789',
reportActionID: '1609646094152486',
actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT,
},
{
created: '2022-11-09 22:26:48.789',
reportActionID: '1661970171066218',
actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT,
},
{
created: '2022-11-09 22:26:48.789',
reportActionID: '2962390724708756',
actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT,
},
{
created: '2022-11-09 22:27:01.600',
reportActionID: '6401435781022176',
actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT,
},
{
created: '2022-11-09 22:27:01.825',
reportActionID: '8401445780099176',
actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT,
},
],
],
[
[
// Given three reportActions with the same timestamp
{
created: '2023-01-10 22:25:47.132',
reportActionID: '3',
actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT,
},
{
created: '2023-01-10 22:25:47.132',
reportActionID: '2',
actionName: CONST.REPORT.ACTIONS.TYPE.CREATED,
},
{
created: '2023-01-10 22:25:47.132',
reportActionID: '1',
actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT,
},
],
[
// The CREATED action should appear first, then we should sort by reportActionID
{
created: '2023-01-10 22:25:47.132',
reportActionID: '2',
actionName: CONST.REPORT.ACTIONS.TYPE.CREATED,
},
{
created: '2023-01-10 22:25:47.132',
reportActionID: '1',
actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT,
},
{
created: '2023-01-10 22:25:47.132',
reportActionID: '3',
actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT,
},
],
],
Expand Down

0 comments on commit ea6c52c

Please sign in to comment.