Skip to content

Commit

Permalink
Merge pull request #49568 from c3024/improve-order-of-options-in-subm…
Browse files Browse the repository at this point in the history
…it-expense-flow

order options by the last expense request time in "Submit Expense" flow
  • Loading branch information
MariaHCD committed Sep 26, 2024
2 parents b930641 + b76fb45 commit da37289
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
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 @@ -1573,7 +1574,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 @@ -1585,6 +1590,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 @@ -1601,8 +1612,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 @@ -1923,6 +1937,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 @@ -1983,6 +1999,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 @@ -2031,7 +2063,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 @@ -2395,6 +2431,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 @@ -2476,7 +2513,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 @@ -694,6 +694,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
3 changes: 2 additions & 1 deletion src/pages/iou/request/MoneyRequestParticipantsSelector.tsx
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

0 comments on commit da37289

Please sign in to comment.