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

Show primary workspace chat first when creating expense #47437

Merged
merged 6 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions src/libs/OptionsListUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ type PreviewConfig = {showChatPreviewLine?: boolean; forcePolicyNamePreview?: bo
type FilterOptionsConfig = Pick<GetOptionsConfig, 'sortByReportTypeInSearch' | 'canInviteUser' | 'selectedOptions' | 'excludeUnknownUsers' | 'excludeLogins' | 'maxRecentReportsToShow'> & {
preferChatroomsOverThreads?: boolean;
includeChatRoomsByParticipants?: boolean;
preferPolicyExpenseChat?: boolean;
};

type HasText = {
Expand Down Expand Up @@ -358,6 +359,12 @@ Onyx.connect({
callback: (value) => (allReportsDraft = value),
});

let activePolicyID: OnyxEntry<string>;
mountiny marked this conversation as resolved.
Show resolved Hide resolved
Onyx.connect({
key: ONYXKEYS.NVP_ACTIVE_POLICY_ID,
callback: (value) => (activePolicyID = value),
});

/**
* Get the report or draft report given a reportID
*/
Expand Down Expand Up @@ -1616,11 +1623,15 @@ 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} = {}) {
function orderOptions(options: ReportUtils.OptionData[], searchValue: string | undefined, {preferChatroomsOverThreads = false, preferPolicyExpenseChat = false} = {}) {
return lodashOrderBy(
options,
[
(option) => {
if (option.isPolicyExpenseChat && preferPolicyExpenseChat && option.policyID === activePolicyID) {
return 0;
}

if (option.isSelfDM) {
return 0;
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -2171,6 +2185,7 @@ function getFilteredOptions(
recentlyUsedPolicyReportFieldOptions: string[] = [],
includeInvoiceRooms = false,
action: IOUAction | undefined = undefined,
sortByReportTypeInSearch = false,
) {
return getOptions(
{reports, personalDetails},
Expand Down Expand Up @@ -2199,6 +2214,7 @@ function getFilteredOptions(
recentlyUsedPolicyReportFieldOptions,
includeInvoiceRooms,
action,
sortByReportTypeInSearch,
},
);
}
Expand Down Expand Up @@ -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)};
Expand Down Expand Up @@ -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: [],
Expand Down
12 changes: 6 additions & 6 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,10 @@ Onyx.connect({
},
});

let primaryPolicyID: OnyxEntry<string>;
let activePolicyID: OnyxEntry<string>;
Onyx.connect({
key: ONYXKEYS.NVP_ACTIVE_POLICY_ID,
callback: (value) => (primaryPolicyID = value),
callback: (value) => (activePolicyID = value),
});

/**
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -64,13 +66,15 @@ 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,
});
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);

Expand Down Expand Up @@ -124,6 +128,7 @@ function MoneyRequestParticipantsSelector({participants = CONST.EMPTY_ARRAY, onF
undefined,
iouType === CONST.IOU.TYPE.INVOICE,
action,
isPaidGroupPolicy,
);

return optionList;
Expand All @@ -139,6 +144,7 @@ function MoneyRequestParticipantsSelector({participants = CONST.EMPTY_ARRAY, onF
options.personalDetails,
options.reports,
participants,
isPaidGroupPolicy,
]);

const chatOptions = useMemo(() => {
Expand All @@ -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
Expand Down
Loading