Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

order options by the last expense request time in "Submit Expense" flow #49568

Merged
Merged
45 changes: 41 additions & 4 deletions src/libs/OptionsListUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ type PreviewConfig = {showChatPreviewLine?: boolean; forcePolicyNamePreview?: bo
type FilterOptionsConfig = Pick<GetOptionsConfig, 'sortByReportTypeInSearch' | 'canInviteUser' | 'selectedOptions' | 'excludeUnknownUsers' | 'excludeLogins' | 'maxRecentReportsToShow'> & {
preferChatroomsOverThreads?: boolean;
preferPolicyExpenseChat?: boolean;
preferRecentExpenseReports?: boolean;
};

/**
Expand Down Expand Up @@ -1571,7 +1572,11 @@ function createOptionFromReport(report: Report, personalDetails: OnyxEntry<Perso
* @param searchValue - search string
* @returns a sorted list of options
*/
function orderOptions(options: ReportUtils.OptionData[], searchValue: string | undefined, {preferChatroomsOverThreads = false, preferPolicyExpenseChat = false} = {}) {
function orderOptions(
options: ReportUtils.OptionData[],
searchValue: string | undefined,
{preferChatroomsOverThreads = false, preferPolicyExpenseChat = false, preferRecentExpenseReports = false} = {},
) {
return lodashOrderBy(
options,
[
Expand All @@ -1583,6 +1588,12 @@ function orderOptions(options: ReportUtils.OptionData[], searchValue: string | u
if (option.isSelfDM) {
return 0;
}
if (preferRecentExpenseReports && !!option?.lastIOUCreationDate) {
return 1;
}
if (preferRecentExpenseReports && option.isPolicyExpenseChat) {
return 1;
}
if (preferChatroomsOverThreads && option.isThread) {
return 4;
}
Expand All @@ -1599,8 +1610,11 @@ function orderOptions(options: ReportUtils.OptionData[], searchValue: string | u
// When option.login is an exact match with the search value, returning 0 puts it at the top of the option list
return 0;
},
// For Submit Expense flow, prioritize the most recent expense reports and then policy expense chats (without expense requests)
preferRecentExpenseReports ? (option) => option?.lastIOUCreationDate ?? '' : '',
preferRecentExpenseReports ? (option) => option?.isPolicyExpenseChat : 0,
],
['asc'],
['asc', 'desc', 'desc'],
);
}

Expand Down Expand Up @@ -1921,6 +1935,8 @@ function getOptions(
let recentReportOptions = [];
let personalDetailsOptions: ReportUtils.OptionData[] = [];

const preferRecentExpenseReports = action === CONST.IOU.ACTION.CREATE;

if (includeRecentReports) {
for (const reportOption of allReportOptions) {
/**
Expand Down Expand Up @@ -1979,6 +1995,22 @@ function getOptions(
recentReportOptions.push(reportOption);
}

// Add a field to sort the recent reports by the time of last IOU request for create actions
if (preferRecentExpenseReports) {
const reportPreviewAction = allSortedReportActions[reportOption.reportID]?.find((reportAction) =>
ReportActionUtils.isActionOfType(reportAction, CONST.REPORT.ACTIONS.TYPE.REPORT_PREVIEW),
);

if (reportPreviewAction) {
const iouReportID = ReportActionUtils.getIOUReportIDFromReportActionPreview(reportPreviewAction);
const iouReportActions = allSortedReportActions[iouReportID] ?? [];
const lastIOUAction = iouReportActions.find((iouAction) => iouAction.actionName === CONST.REPORT.ACTIONS.TYPE.IOU);
if (lastIOUAction) {
reportOption.lastIOUCreationDate = lastIOUAction.lastModified;
}
}
}

// Add this login to the exclude list so it won't appear when we process the personal details
if (reportOption.login) {
optionsToExclude.push({login: reportOption.login});
Expand Down Expand Up @@ -2027,7 +2059,11 @@ function getOptions(
recentReportOptions.push(...personalDetailsOptions);
personalDetailsOptions = [];
}
recentReportOptions = orderOptions(recentReportOptions, searchValue, {preferChatroomsOverThreads: true, preferPolicyExpenseChat: !!action});
recentReportOptions = orderOptions(recentReportOptions, searchValue, {
preferChatroomsOverThreads: true,
preferPolicyExpenseChat: !!action,
preferRecentExpenseReports,
});
}

return {
Expand Down Expand Up @@ -2391,6 +2427,7 @@ function filterOptions(options: Options, searchInputValue: string, config?: Filt
excludeLogins = [],
preferChatroomsOverThreads = false,
preferPolicyExpenseChat = false,
preferRecentExpenseReports = false,
} = config ?? {};
if (searchInputValue.trim() === '' && maxRecentReportsToShow > 0) {
return {...options, recentReports: options.recentReports.slice(0, maxRecentReportsToShow)};
Expand Down Expand Up @@ -2472,7 +2509,7 @@ function filterOptions(options: Options, searchInputValue: string, config?: Filt

return {
personalDetails,
recentReports: orderOptions(recentReports, searchValue, {preferChatroomsOverThreads, preferPolicyExpenseChat}),
recentReports: orderOptions(recentReports, searchValue, {preferChatroomsOverThreads, preferPolicyExpenseChat, preferRecentExpenseReports}),
userToInvite,
currentUserOption: matchResults.currentUserOption,
categoryOptions: [],
Expand Down
1 change: 1 addition & 0 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ type OptionData = {
tabIndex?: 0 | -1;
isConciergeChat?: boolean;
isBold?: boolean;
lastIOUCreationDate?: string;
} & Report;

type OnyxDataTaskAssigneeChat = {
Expand Down
1 change: 1 addition & 0 deletions src/pages/home/ReportScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,7 @@ function ReportScreen({route, currentReportID = '', navigation}: ReportScreenPro

const lastRoute = usePrevious(route);
const lastReportActionIDFromRoute = usePrevious(reportActionIDFromRoute);

// Define here because reportActions are recalculated before mount, allowing data to display faster than useEffect can trigger.
// If we have cached reportActions, they will be shown immediately.
// We aim to display a loader first, then fetch relevant reportActions, and finally show them.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,10 @@ function MoneyRequestParticipantsSelector({participants = CONST.EMPTY_ARRAY, onF
excludeLogins: CONST.EXPENSIFY_EMAILS,
maxRecentReportsToShow: CONST.IOU.MAX_RECENT_REPORTS_TO_SHOW,
preferPolicyExpenseChat: isPaidGroupPolicy,
preferRecentExpenseReports: action === CONST.IOU.ACTION.CREATE,
});
return newOptions;
}, [areOptionsInitialized, defaultOptions, debouncedSearchTerm, participants, isPaidGroupPolicy, canUseP2PDistanceRequests, iouRequestType, isCategorizeOrShareAction]);
}, [areOptionsInitialized, defaultOptions, debouncedSearchTerm, participants, isPaidGroupPolicy, canUseP2PDistanceRequests, iouRequestType, isCategorizeOrShareAction, action]);

/**
* Returns the sections needed for the OptionsSelector
Expand Down
Loading