diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index e98acb5bafbe4..49367a50e6f6b 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -10044,6 +10044,22 @@ function getOutstandingReportsForUser( .sort((a, b) => a?.reportName?.localeCompare(b?.reportName?.toLowerCase() ?? '') ?? 0); } +/** + * Sort outstanding reports by their name, while keeping the selected one at the beginning. + * @param report1 Details of the first report to be compared. + * @param report2 Details of the second report to be compared. + * @param selectedReportID ID of the selected report which needs to be at the beginning. + */ +function sortOutstandingReportsBySelected(report1: OnyxEntry, report2: OnyxEntry, selectedReportID: string | undefined): number { + if (report1?.reportID === selectedReportID) { + return -1; + } + if (report2?.reportID === selectedReportID) { + return 1; + } + return report1?.reportName?.localeCompare(report2?.reportName?.toLowerCase() ?? '') ?? 0; +} + /** * @returns the object to update `report.hasOutstandingChildRequest` */ @@ -11435,6 +11451,7 @@ export { shouldReportBeInOptionList, shouldReportShowSubscript, shouldShowFlagComment, + sortOutstandingReportsBySelected, getReportActionWithMissingSmartscanFields, shouldShowRBRForMissingSmartscanFields, shouldUseFullTitleToDisplay, diff --git a/src/pages/iou/request/step/IOURequestEditReportCommon.tsx b/src/pages/iou/request/step/IOURequestEditReportCommon.tsx index f2107c4db7e07..cab6c66b31362 100644 --- a/src/pages/iou/request/step/IOURequestEditReportCommon.tsx +++ b/src/pages/iou/request/step/IOURequestEditReportCommon.tsx @@ -11,7 +11,7 @@ import useDebouncedState from '@hooks/useDebouncedState'; import useLocalize from '@hooks/useLocalize'; import useOnyx from '@hooks/useOnyx'; import Navigation from '@libs/Navigation/Navigation'; -import {getOutstandingReportsForUser, getPolicyName, isIOUReport} from '@libs/ReportUtils'; +import {getOutstandingReportsForUser, getPolicyName, isIOUReport, sortOutstandingReportsBySelected} from '@libs/ReportUtils'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type {Route} from '@src/ROUTES'; @@ -86,7 +86,7 @@ function IOURequestEditReportCommon({backTo, transactionsReports, selectReport, } return expenseReports - .sort((a, b) => a?.reportName?.localeCompare(b?.reportName?.toLowerCase() ?? '') ?? 0) + .sort((report1, report2) => sortOutstandingReportsBySelected(report1, report2, onlyReport?.reportID)) .filter((report) => !debouncedSearchValue || report?.reportName?.toLowerCase().includes(debouncedSearchValue.toLowerCase())) .filter((report): report is NonNullable => report !== undefined) .map((report) => { diff --git a/tests/unit/ReportUtilsTest.ts b/tests/unit/ReportUtilsTest.ts index 0587e769c17da..f9b2f9202c80c 100644 --- a/tests/unit/ReportUtilsTest.ts +++ b/tests/unit/ReportUtilsTest.ts @@ -65,6 +65,7 @@ import { shouldReportBeInOptionList, shouldReportShowSubscript, shouldShowFlagComment, + sortOutstandingReportsBySelected, temporary_getMoneyRequestOptions, } from '@libs/ReportUtils'; import type {OptionData} from '@libs/ReportUtils'; @@ -444,6 +445,21 @@ describe('ReportUtils', () => { }); }); + describe('sortOutstandingReportsBySelected', () => { + it('should return -1 when report1 is selected and report2 is not', () => { + const report1 = LHNTestUtils.getFakeReport(); + const report2 = LHNTestUtils.getFakeReport(); + const selectedReportID = report1.reportID; + expect(sortOutstandingReportsBySelected(report1, report2, selectedReportID)).toBe(-1); + }); + it('should return 1 when report2 is selected and report1 is not', () => { + const report1 = LHNTestUtils.getFakeReport(); + const report2 = LHNTestUtils.getFakeReport(); + const selectedReportID = report2.reportID; + expect(sortOutstandingReportsBySelected(report1, report2, selectedReportID)).toBe(1); + }); + }); + describe('getDisplayNamesWithTooltips', () => { test('withSingleParticipantReport', () => { const participants = getDisplayNamesWithTooltips(participantsPersonalDetails, false);