From 02ef09ac36a467aac726d86fd4b00cca3aa86fbb Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Wed, 14 Aug 2024 22:44:55 +0700 Subject: [PATCH 1/5] Show primary workspace chat first when creating exepnse --- src/libs/OptionsListUtils.ts | 34 +++++++++++++++---- .../MoneyRequestParticipantsSelector.tsx | 9 ++++- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/libs/OptionsListUtils.ts b/src/libs/OptionsListUtils.ts index 7d2312052ed9..5ecda7c89944 100644 --- a/src/libs/OptionsListUtils.ts +++ b/src/libs/OptionsListUtils.ts @@ -222,7 +222,7 @@ type PreviewConfig = {showChatPreviewLine?: boolean; forcePolicyNamePreview?: bo type FilterOptionsConfig = Pick< GetOptionsConfig, 'sortByReportTypeInSearch' | 'canInviteUser' | 'betas' | 'selectedOptions' | 'excludeUnknownUsers' | 'excludeLogins' | 'maxRecentReportsToShow' -> & {preferChatroomsOverThreads?: boolean; includeChatRoomsByParticipants?: boolean}; +> & {preferChatroomsOverThreads?: boolean; includeChatRoomsByParticipants?: boolean; preferPolicyExpenseChat?: boolean}; type HasText = { text?: string; @@ -359,6 +359,12 @@ Onyx.connect({ callback: (value) => (allReportsDraft = value), }); +let primaryPolicyID: OnyxEntry; +Onyx.connect({ + key: ONYXKEYS.NVP_ACTIVE_POLICY_ID, + callback: (value) => (primaryPolicyID = value), +}); + /** * Get the report or draft report given a reportID */ @@ -1617,11 +1623,19 @@ function createOptionFromReport(report: Report, personalDetails: OnyxEntry { + if (option.isPolicyExpenseChat && preferPolicyExpenseChat && option.policyID === primaryPolicyID) { + return 0; + } + + if (option.isPolicyExpenseChat && preferPolicyExpenseChat) { + return 1; + } + if (option.isSelfDM) { return 0; } @@ -2060,11 +2074,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 { @@ -2173,6 +2190,7 @@ function getFilteredOptions( recentlyUsedPolicyReportFieldOptions: string[] = [], includeInvoiceRooms = false, action: IOUAction | undefined = undefined, + sortByReportTypeInSearch = false, ) { return getOptions( {reports, personalDetails}, @@ -2201,6 +2219,7 @@ function getFilteredOptions( recentlyUsedPolicyReportFieldOptions, includeInvoiceRooms, action, + sortByReportTypeInSearch, }, ); } @@ -2426,6 +2445,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)}; @@ -2539,7 +2559,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/pages/iou/request/MoneyRequestParticipantsSelector.tsx b/src/pages/iou/request/MoneyRequestParticipantsSelector.tsx index 6ff64128cbbc..b611ef78c179 100644 --- a/src/pages/iou/request/MoneyRequestParticipantsSelector.tsx +++ b/src/pages/iou/request/MoneyRequestParticipantsSelector.tsx @@ -16,11 +16,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'; @@ -63,6 +65,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); @@ -126,6 +130,7 @@ function MoneyRequestParticipantsSelector({participants = CONST.EMPTY_ARRAY, onF undefined, iouType === CONST.IOU.TYPE.INVOICE, action, + isPaidGroupPolicy, ); return optionList; @@ -141,6 +146,7 @@ function MoneyRequestParticipantsSelector({participants = CONST.EMPTY_ARRAY, onF options.personalDetails, options.reports, participants, + isPaidGroupPolicy, ]); const chatOptions = useMemo(() => { @@ -162,9 +168,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, betas, defaultOptions, debouncedSearchTerm, participants]); + }, [areOptionsInitialized, betas, defaultOptions, debouncedSearchTerm, participants, isPaidGroupPolicy]); /** * Returns the sections needed for the OptionsSelector From 7f3878b1ab9355f52432a23c1e189fc8e1c79105 Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Thu, 15 Aug 2024 01:40:46 +0700 Subject: [PATCH 2/5] rename primaryPolicyID --- src/libs/OptionsListUtils.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libs/OptionsListUtils.ts b/src/libs/OptionsListUtils.ts index 5ecda7c89944..ff1774ff2a28 100644 --- a/src/libs/OptionsListUtils.ts +++ b/src/libs/OptionsListUtils.ts @@ -359,10 +359,10 @@ Onyx.connect({ callback: (value) => (allReportsDraft = value), }); -let primaryPolicyID: OnyxEntry; +let activePolicyID: OnyxEntry; Onyx.connect({ key: ONYXKEYS.NVP_ACTIVE_POLICY_ID, - callback: (value) => (primaryPolicyID = value), + callback: (value) => (activePolicyID = value), }); /** @@ -1628,7 +1628,7 @@ function orderOptions(options: ReportUtils.OptionData[], searchValue: string | u options, [ (option) => { - if (option.isPolicyExpenseChat && preferPolicyExpenseChat && option.policyID === primaryPolicyID) { + if (option.isPolicyExpenseChat && preferPolicyExpenseChat && option.policyID === activePolicyID) { return 0; } From caffcd15580c484203f8fd76c67c3bbe559096e0 Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Thu, 15 Aug 2024 02:02:31 +0700 Subject: [PATCH 3/5] revert change name --- src/libs/OptionsListUtils.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libs/OptionsListUtils.ts b/src/libs/OptionsListUtils.ts index ff1774ff2a28..5ecda7c89944 100644 --- a/src/libs/OptionsListUtils.ts +++ b/src/libs/OptionsListUtils.ts @@ -359,10 +359,10 @@ Onyx.connect({ callback: (value) => (allReportsDraft = value), }); -let activePolicyID: OnyxEntry; +let primaryPolicyID: OnyxEntry; Onyx.connect({ key: ONYXKEYS.NVP_ACTIVE_POLICY_ID, - callback: (value) => (activePolicyID = value), + callback: (value) => (primaryPolicyID = value), }); /** @@ -1628,7 +1628,7 @@ function orderOptions(options: ReportUtils.OptionData[], searchValue: string | u options, [ (option) => { - if (option.isPolicyExpenseChat && preferPolicyExpenseChat && option.policyID === activePolicyID) { + if (option.isPolicyExpenseChat && preferPolicyExpenseChat && option.policyID === primaryPolicyID) { return 0; } From 38d3d8eee566cf50e17b54ef300cda9c4eff212c Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Thu, 15 Aug 2024 14:58:43 +0700 Subject: [PATCH 4/5] rename variable --- src/libs/OptionsListUtils.ts | 6 +++--- src/libs/actions/IOU.ts | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/libs/OptionsListUtils.ts b/src/libs/OptionsListUtils.ts index b6c926ca8745..81f544e6a35a 100644 --- a/src/libs/OptionsListUtils.ts +++ b/src/libs/OptionsListUtils.ts @@ -359,10 +359,10 @@ Onyx.connect({ callback: (value) => (allReportsDraft = value), }); -let primaryPolicyID: OnyxEntry; +let activePolicyID: OnyxEntry; Onyx.connect({ key: ONYXKEYS.NVP_ACTIVE_POLICY_ID, - callback: (value) => (primaryPolicyID = value), + callback: (value) => (activePolicyID = value), }); /** @@ -1628,7 +1628,7 @@ function orderOptions(options: ReportUtils.OptionData[], searchValue: string | u options, [ (option) => { - if (option.isPolicyExpenseChat && preferPolicyExpenseChat && option.policyID === primaryPolicyID) { + if (option.isPolicyExpenseChat && preferPolicyExpenseChat && option.policyID === activePolicyID) { return 0; } 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, }; } From 883666112f169c905dacd455eba9648a18653db7 Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Thu, 15 Aug 2024 17:46:12 +0700 Subject: [PATCH 5/5] only display the active policy expense chat at the top --- src/libs/OptionsListUtils.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/libs/OptionsListUtils.ts b/src/libs/OptionsListUtils.ts index 81f544e6a35a..98274e829001 100644 --- a/src/libs/OptionsListUtils.ts +++ b/src/libs/OptionsListUtils.ts @@ -1632,10 +1632,6 @@ function orderOptions(options: ReportUtils.OptionData[], searchValue: string | u return 0; } - if (option.isPolicyExpenseChat && preferPolicyExpenseChat) { - return 1; - } - if (option.isSelfDM) { return 0; }