diff --git a/src/libs/OptionsListUtils.ts b/src/libs/OptionsListUtils.ts index f9d7c6f69375..98274e829001 100644 --- a/src/libs/OptionsListUtils.ts +++ b/src/libs/OptionsListUtils.ts @@ -221,6 +221,7 @@ type PreviewConfig = {showChatPreviewLine?: boolean; forcePolicyNamePreview?: bo type FilterOptionsConfig = Pick & { preferChatroomsOverThreads?: boolean; includeChatRoomsByParticipants?: boolean; + preferPolicyExpenseChat?: boolean; }; type HasText = { @@ -358,6 +359,12 @@ Onyx.connect({ callback: (value) => (allReportsDraft = value), }); +let activePolicyID: OnyxEntry; +Onyx.connect({ + key: ONYXKEYS.NVP_ACTIVE_POLICY_ID, + callback: (value) => (activePolicyID = value), +}); + /** * Get the report or draft report given a reportID */ @@ -1616,11 +1623,15 @@ function createOptionFromReport(report: Report, personalDetails: OnyxEntry { + if (option.isPolicyExpenseChat && preferPolicyExpenseChat && option.policyID === activePolicyID) { + return 0; + } + if (option.isSelfDM) { return 0; } @@ -2058,11 +2069,14 @@ function getOptions( } // If we are prioritizing 1:1 chats in search, do it only once we started searching - if (sortByReportTypeInSearch && searchValue !== '') { + if (sortByReportTypeInSearch && (searchValue !== '' || !!action)) { // When sortByReportTypeInSearch is true, recentReports will be returned with all the reports including personalDetailsOptions in the correct Order. - recentReportOptions.push(...personalDetailsOptions); - personalDetailsOptions = []; - recentReportOptions = orderOptions(recentReportOptions, searchValue, {preferChatroomsOverThreads: true}); + // If we're in money request flow, we only order the recent report option. + if (!action) { + recentReportOptions.push(...personalDetailsOptions); + personalDetailsOptions = []; + } + recentReportOptions = orderOptions(recentReportOptions, searchValue, {preferChatroomsOverThreads: true, preferPolicyExpenseChat: !!action}); } return { @@ -2171,6 +2185,7 @@ function getFilteredOptions( recentlyUsedPolicyReportFieldOptions: string[] = [], includeInvoiceRooms = false, action: IOUAction | undefined = undefined, + sortByReportTypeInSearch = false, ) { return getOptions( {reports, personalDetails}, @@ -2199,6 +2214,7 @@ function getFilteredOptions( recentlyUsedPolicyReportFieldOptions, includeInvoiceRooms, action, + sortByReportTypeInSearch, }, ); } @@ -2423,6 +2439,7 @@ function filterOptions(options: Options, searchInputValue: string, config?: Filt excludeLogins = [], preferChatroomsOverThreads = false, includeChatRoomsByParticipants = false, + preferPolicyExpenseChat = false, } = config ?? {}; if (searchInputValue.trim() === '' && maxRecentReportsToShow > 0) { return {...options, recentReports: options.recentReports.slice(0, maxRecentReportsToShow)}; @@ -2535,7 +2552,7 @@ function filterOptions(options: Options, searchInputValue: string, config?: Filt return { personalDetails, - recentReports: orderOptions(recentReports, searchValue, {preferChatroomsOverThreads}), + recentReports: orderOptions(recentReports, searchValue, {preferChatroomsOverThreads, preferPolicyExpenseChat}), userToInvite, currentUserOption: matchResults.currentUserOption, categoryOptions: [], diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 89de7a76acf8..9cc844849c89 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -269,10 +269,10 @@ Onyx.connect({ }, }); -let primaryPolicyID: OnyxEntry; +let activePolicyID: OnyxEntry; Onyx.connect({ key: ONYXKEYS.NVP_ACTIVE_POLICY_ID, - callback: (value) => (primaryPolicyID = value), + callback: (value) => (activePolicyID = value), }); /** @@ -6549,8 +6549,8 @@ function getPayMoneyRequestParams( const isInvoiceReport = ReportUtils.isInvoiceReport(iouReport); let chatReport = initialChatReport; - if (ReportUtils.isIndividualInvoiceRoom(chatReport) && payAsBusiness && primaryPolicyID) { - const existingB2BInvoiceRoom = ReportUtils.getInvoiceChatByParticipants(chatReport.policyID ?? '', primaryPolicyID); + if (ReportUtils.isIndividualInvoiceRoom(chatReport) && payAsBusiness && activePolicyID) { + const existingB2BInvoiceRoom = ReportUtils.getInvoiceChatByParticipants(chatReport.policyID ?? '', activePolicyID); if (existingB2BInvoiceRoom) { chatReport = existingB2BInvoiceRoom; } @@ -6596,10 +6596,10 @@ function getPayMoneyRequestParams( lastMessageText: ReportActionsUtils.getReportActionText(optimisticIOUReportAction), lastMessageHtml: ReportActionsUtils.getReportActionHtml(optimisticIOUReportAction), }; - if (ReportUtils.isIndividualInvoiceRoom(chatReport) && payAsBusiness && primaryPolicyID) { + if (ReportUtils.isIndividualInvoiceRoom(chatReport) && payAsBusiness && activePolicyID) { optimisticChatReport.invoiceReceiver = { type: CONST.REPORT.INVOICE_RECEIVER_TYPE.BUSINESS, - policyID: primaryPolicyID, + policyID: activePolicyID, }; } diff --git a/src/pages/iou/request/MoneyRequestParticipantsSelector.tsx b/src/pages/iou/request/MoneyRequestParticipantsSelector.tsx index 89d4a0fc3664..3d2f85ef6c62 100644 --- a/src/pages/iou/request/MoneyRequestParticipantsSelector.tsx +++ b/src/pages/iou/request/MoneyRequestParticipantsSelector.tsx @@ -17,11 +17,13 @@ import useDismissedReferralBanners from '@hooks/useDismissedReferralBanners'; import useLocalize from '@hooks/useLocalize'; import useNetwork from '@hooks/useNetwork'; import usePermissions from '@hooks/usePermissions'; +import usePolicy from '@hooks/usePolicy'; import useScreenWrapperTranstionStatus from '@hooks/useScreenWrapperTransitionStatus'; import useThemeStyles from '@hooks/useThemeStyles'; import * as DeviceCapabilities from '@libs/DeviceCapabilities'; import Navigation from '@libs/Navigation/Navigation'; import * as OptionsListUtils from '@libs/OptionsListUtils'; +import * as PolicyUtils from '@libs/PolicyUtils'; import * as ReportUtils from '@libs/ReportUtils'; import * as SubscriptionUtils from '@libs/SubscriptionUtils'; import * as Policy from '@userActions/Policy/Policy'; @@ -64,6 +66,7 @@ function MoneyRequestParticipantsSelector({participants = CONST.EMPTY_ARRAY, onF const {didScreenTransitionEnd} = useScreenWrapperTranstionStatus(); const [betas] = useOnyx(ONYXKEYS.BETAS); const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID); + const policy = usePolicy(activePolicyID); const [isSearchingForReports] = useOnyx(ONYXKEYS.IS_SEARCHING_FOR_REPORTS, {initWithStoredValues: false}); const {options, areOptionsInitialized} = useOptionsList({ shouldInitialize: didScreenTransitionEnd, @@ -71,6 +74,7 @@ function MoneyRequestParticipantsSelector({participants = CONST.EMPTY_ARRAY, onF const cleanSearchTerm = useMemo(() => debouncedSearchTerm.trim().toLowerCase(), [debouncedSearchTerm]); const offlineMessage: string = isOffline ? `${translate('common.youAppearToBeOffline')} ${translate('search.resultsAreLimited')}` : ''; + const isPaidGroupPolicy = useMemo(() => PolicyUtils.isPaidGroupPolicy(policy), [policy]); const isIOUSplit = iouType === CONST.IOU.TYPE.SPLIT; const isCategorizeOrShareAction = [CONST.IOU.ACTION.CATEGORIZE, CONST.IOU.ACTION.SHARE].some((option) => option === action); @@ -124,6 +128,7 @@ function MoneyRequestParticipantsSelector({participants = CONST.EMPTY_ARRAY, onF undefined, iouType === CONST.IOU.TYPE.INVOICE, action, + isPaidGroupPolicy, ); return optionList; @@ -139,6 +144,7 @@ function MoneyRequestParticipantsSelector({participants = CONST.EMPTY_ARRAY, onF options.personalDetails, options.reports, participants, + isPaidGroupPolicy, ]); const chatOptions = useMemo(() => { @@ -159,9 +165,10 @@ function MoneyRequestParticipantsSelector({participants = CONST.EMPTY_ARRAY, onF selectedOptions: participants as Participant[], excludeLogins: CONST.EXPENSIFY_EMAILS, maxRecentReportsToShow: CONST.IOU.MAX_RECENT_REPORTS_TO_SHOW, + preferPolicyExpenseChat: isPaidGroupPolicy, }); return newOptions; - }, [areOptionsInitialized, defaultOptions, debouncedSearchTerm, participants]); + }, [areOptionsInitialized, defaultOptions, debouncedSearchTerm, participants, isPaidGroupPolicy]); /** * Returns the sections needed for the OptionsSelector