Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/components/AvatarWithDisplayName.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ function AvatarWithDisplayName({

const derivedParentReportName = getReportNameFromNames(derivedReportNames, report?.parentReportID);
const title = getReportName(report, derivedReportName);
const subtitle = getChatRoomSubtitle(report, policy, conciergeReportID, translate, rules, true, isReportArchived);
const subtitle = getChatRoomSubtitle(report, policy, conciergeReportID, translate, undefined, isReportArchived);
const parentNavigationSubtitleData = getParentNavigationSubtitle({
report,
policy,
Expand Down
27 changes: 25 additions & 2 deletions src/components/PersonalDetailsByLoginProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,36 @@ function buildPersonalDetailsByLogin(personalDetailsList: OnyxEntry<PersonalDeta
return personalDetailsByLogin;
}

// Onyx hands out a new list object on every change, so the list's reference identifies the map built from it.
// The WeakMap lets an old list and its map be garbage collected once nothing reads that list anymore.
const personalDetailsByLoginCache = new WeakMap<PersonalDetailsList, PersonalDetailsByLogin>();

/**
* Returns the login map of a personal details list, building it only the first time that list is seen. The provider
* and anything that receives the same list, like the option list builder, then share one map instead of each building it.
*/
function getPersonalDetailsByLogin(personalDetailsList: OnyxEntry<PersonalDetailsList>): PersonalDetailsByLogin {
if (!personalDetailsList) {
return EMPTY_PERSONAL_DETAILS_BY_LOGIN;
}

const cachedPersonalDetailsByLogin = personalDetailsByLoginCache.get(personalDetailsList);
if (cachedPersonalDetailsByLogin) {
return cachedPersonalDetailsByLogin;
}

const personalDetailsByLogin = buildPersonalDetailsByLogin(personalDetailsList);
personalDetailsByLoginCache.set(personalDetailsList, personalDetailsByLogin);
return personalDetailsByLogin;
}

/**
* Builds the login -> personal details map once for the whole app, so that consumers don't each have to
* subscribe to the entire personal details list to look somebody up by their login.
*/
function PersonalDetailsByLoginProvider({children}: ChildrenProps) {
const [personalDetailsList] = useAllPersonalDetails();
const personalDetailsByLogin = buildPersonalDetailsByLogin(personalDetailsList);
const personalDetailsByLogin = getPersonalDetailsByLogin(personalDetailsList);

const storeRef = useRef({snapshot: personalDetailsByLogin, listeners: new Set<Listener>()});

Expand All @@ -82,5 +105,5 @@ function PersonalDetailsByLoginProvider({children}: ChildrenProps) {
}

export default PersonalDetailsByLoginProvider;
export {buildPersonalDetailsByLogin, EMPTY_PERSONAL_DETAILS_BY_LOGIN, PersonalDetailsByLoginContext};
export {buildPersonalDetailsByLogin, EMPTY_PERSONAL_DETAILS_BY_LOGIN, getPersonalDetailsByLogin, PersonalDetailsByLoginContext};
export type {PersonalDetailsByLogin};
6 changes: 6 additions & 0 deletions src/components/Search/FilterComponents/InSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {useActivePolicyContext} from '@components/ActivePolicyProvider';
import {usePersonalDetails} from '@components/OnyxListItemProvider';
import {getPersonalDetailsByLogin} from '@components/PersonalDetailsByLoginProvider';
import type {SearchFilterCommonProps} from '@components/Search/types';
import InviteMemberListItem from '@components/SelectionList/ListItem/InviteMemberListItem';
import SelectionListWithSections from '@components/SelectionList/SelectionListWithSections';
Expand Down Expand Up @@ -120,6 +121,11 @@ function InSelector({value = [], selectionListTextInputStyle, selectionListStyle
isTrackIntentUser,
pendingDeleteMemberAccountIDs: pendingDeleteMemberAccountIDsByReportID?.[id],
formatPhoneNumber,
// Derived from personalDetails instead of read from the provider's store, which publishes a render later,
// so the option always gets a login map that matches the list it's built from. It's derived here, where it's
// used, because React Compiler treats the returned map as mutable: computed at the top of the component, its
// mutable range would span every buildReportOption call and the hooks in between, dropping memoization.
personalDetailsByLogin: getPersonalDetailsByLogin(personalDetails),
}),
),
isSelected,
Expand Down
2 changes: 2 additions & 0 deletions src/components/Search/SearchRouter/SearchRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ function SearchRouter({onRouterClose, shouldHideInputCaret, isSearchRouterDispla
isTrackIntentUser,
pendingDeleteMemberAccountIDs: contextualReportPendingDeleteMemberAccountIDs,
formatPhoneNumber,
// Passing personalDetailsByLogin as undefined is intentional, the contextual suggestion only shows the option's text, never its subtitle.
personalDetailsByLogin: undefined,
});
reportForContextualSearch = option;
}
Expand Down
10 changes: 9 additions & 1 deletion src/hooks/usePersonalDetailByLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,13 @@ function usePersonalDetailsByLogins<TReturn>(logins: Array<string | undefined>,
return useSyncExternalStore(subscribe, getSelectedPersonalDetails, getSelectedPersonalDetails);
}

/**
* Returns the whole map of personal details keyed by login.
*/
function useAllPersonalDetailsByLogin(): PersonalDetailsByLogin {
const {subscribe, getSnapshot} = useContext(PersonalDetailsByLoginContext);
return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
}

export default usePersonalDetailByLogin;
export {useGetPersonalDetailsByLogin, usePersonalDetailsByLogins};
export {useAllPersonalDetailsByLogin, useGetPersonalDetailsByLogin, usePersonalDetailsByLogins};
28 changes: 27 additions & 1 deletion src/libs/OptionsListUtils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import FallbackAvatar from '@assets/images/avatars/fallback-avatar.svg';

import type {LocaleContextProps, LocalizedTranslate} from '@components/LocaleContextProvider';
import {getPersonalDetailsByLogin} from '@components/PersonalDetailsByLoginProvider';
import type {PersonalDetailsByLogin} from '@components/PersonalDetailsByLoginProvider';

import type {CurrencyListActionsContextType} from '@hooks/useCurrencyList';
import type {PrivateIsArchivedMap} from '@hooks/usePrivateIsArchivedMap';
Expand Down Expand Up @@ -411,6 +413,7 @@ type CreateOptionParams = {
pendingDeleteMemberAccountIDs?: string[];
formatPhoneNumber: LocaleContextProps['formatPhoneNumber'];
currentUserLogin?: string;
personalDetailsByLogin: PersonalDetailsByLogin | undefined;
};

/** Shared by createOption and shells so filtering uses the final display text. */
Expand Down Expand Up @@ -459,6 +462,7 @@ function createOption({
pendingDeleteMemberAccountIDs,
formatPhoneNumber,
currentUserLogin,
personalDetailsByLogin,
}: CreateOptionParams): SearchOptionData {
const {showChatPreviewLine = false, forcePolicyNamePreview = false, showPersonalDetails = false, selected, isSelected, isDisabled} = config ?? {};
const translateFn = translate ?? translateLocal;
Expand Down Expand Up @@ -531,7 +535,8 @@ function createOption({
result.brickRoadIndicator = !isEmptyObject(result.allReportErrors) ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : (reportAttribute?.brickRoadStatus ?? '');

hasMultipleParticipants = personalDetailList.length > 1 || result.isChatRoom || result.isPolicyExpenseChat || reportUtilsIsGroupChat(report);
subtitle = getChatRoomSubtitle(report, policy, conciergeReportID, translateFn, rules, true, result.private_isArchived);
const approverLookup = personalDetailsByLogin ? {personalDetailsByLogin, rules} : undefined;
subtitle = getChatRoomSubtitle(report, policy, conciergeReportID, translateFn, approverLookup, result.private_isArchived);

result.alternateText =
showPersonalDetails && personalDetail?.login
Expand Down Expand Up @@ -682,6 +687,8 @@ function getReportOption({
currentUserAccountID,
formatPhoneNumber,
pendingDeleteMemberAccountIDs,
// Passing personalDetailsByLogin as undefined is intentional, the confirmation list rows compute the workspace chat subtitle themselves (see useWorkspaceChatSubtitle).
personalDetailsByLogin: undefined,
});

// Update text because createOption returns workspace name only if report is owned by the user
Expand Down Expand Up @@ -767,6 +774,8 @@ function getReportDisplayOption({
currentUserAccountID,
formatPhoneNumber,
pendingDeleteMemberAccountIDs,
// Passing personalDetailsByLogin as undefined is intentional, the alternateText of a workspace chat is replaced with "Workspace" below.
personalDetailsByLogin: undefined,
});

// Update text & alternateText because createOption returns workspace name only if report is owned by the user
Expand Down Expand Up @@ -843,6 +852,8 @@ function getPolicyExpenseReportOption(
visibleReportActionsData,
currentUserAccountID,
formatPhoneNumber,
// Passing personalDetailsByLogin as undefined is intentional, the alternateText is replaced with "Workspace" below and the picker rows compute their own (see useWorkspaceChatSubtitle).
personalDetailsByLogin: undefined,
});

// Update text & alternateText because createOption returns workspace name only if report is owned by the user
Expand Down Expand Up @@ -997,6 +1008,7 @@ function processReport(
pendingDeleteMemberAccountIDs,
currentUserLogin,
convertToDisplayString,
personalDetailsByLogin,
}: {
currentUserAccountID: number;
pendingDeleteMemberAccountIDs?: string[];
Expand All @@ -1010,6 +1022,7 @@ function processReport(
transactionThreadIDs?: Record<string, string | undefined>;
lastActions: Record<string, ReportAction> | undefined;
currentUserLogin?: string;
personalDetailsByLogin: PersonalDetailsByLogin | undefined;
},
rules: OnyxCollection<Rule>,
formatPhoneNumber: LocaleContextProps['formatPhoneNumber'],
Expand Down Expand Up @@ -1057,6 +1070,7 @@ function processReport(
pendingDeleteMemberAccountIDs,
formatPhoneNumber,
currentUserLogin,
personalDetailsByLogin,
}),
},
};
Expand Down Expand Up @@ -1183,6 +1197,8 @@ function buildFullOption(
// Passing transactionThreadIDs as undefined is intentional, contact options show personal details and never build the chat preview line that reads it.
transactionThreadIDs: undefined,
formatPhoneNumber,
// Passing personalDetailsByLogin as undefined is intentional, `report` here is always a 1:1 DM, never a workspace chat.
personalDetailsByLogin: undefined,
}),
isHydrated: true,
};
Expand Down Expand Up @@ -1350,6 +1366,10 @@ function createFilteredOptionList(
return cloneOptionList(cachedEntry.result);
}

// Derived from personalDetails, which is already a cache input, so the map always matches the list the options are built
// from. A login map handed in separately could arrive a render later and rebuild the whole option list a second time.
const personalDetailsByLogin = getPersonalDetailsByLogin(personalDetails);

const reportMapForAccountIDs: Record<number, Report> = {};

// Step 1: Pre-filter reports to avoid processing thousands
Expand Down Expand Up @@ -1415,6 +1435,7 @@ function createFilteredOptionList(
currentUserAccountID,
pendingDeleteMemberAccountIDs: pendingDeleteMemberAccountIDsByReportID?.[report.reportID],
currentUserLogin,
personalDetailsByLogin,
},
rules,
formatPhoneNumber,
Expand Down Expand Up @@ -1510,6 +1531,7 @@ type CreateOptionFromReportParams = {
convertToDisplayStringWithoutCurrency?: CurrencyListActionsContextType['convertToDisplayStringWithoutCurrency'];
pendingDeleteMemberAccountIDs: string[] | undefined;
formatPhoneNumber: LocaleContextProps['formatPhoneNumber'];
personalDetailsByLogin: PersonalDetailsByLogin | undefined;
};

function createOptionFromReport({
Expand All @@ -1534,6 +1556,7 @@ function createOptionFromReport({
convertToDisplayStringWithoutCurrency,
pendingDeleteMemberAccountIDs,
formatPhoneNumber,
personalDetailsByLogin,
}: CreateOptionFromReportParams) {
const accountIDs = getParticipantsAccountIDsForDisplay(report, undefined);

Expand Down Expand Up @@ -1562,6 +1585,7 @@ function createOptionFromReport({
isTrackIntentUser,
pendingDeleteMemberAccountIDs,
formatPhoneNumber,
personalDetailsByLogin,
}),
};
}
Expand Down Expand Up @@ -1945,6 +1969,8 @@ function getUserToInviteOption({
visibleReportActionsData,
currentUserAccountID,
formatPhoneNumber,
// Passing personalDetailsByLogin as undefined is intentional, the invite option has no report, so it has no subtitle.
personalDetailsByLogin: undefined,
});
userToInvite.isOptimisticAccount = true;
userToInvite.login = searchValue;
Expand Down
24 changes: 11 additions & 13 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7819,6 +7819,11 @@ function getReportSubtitlePrefix(report: OnyxEntry<Report>, currentUserEmail?: s
return `${policyName} ${CONST.DOT_SEPARATOR} `;
}

type ChatRoomSubtitleApproverLookup = {
personalDetailsByLogin: PersonalDetailsByLogin;
rules: OnyxCollection<Rule>;
};

/**
* Get either the policyName or domainName the chat is tied to
*/
Expand All @@ -7827,8 +7832,7 @@ function getChatRoomSubtitle(
policy: OnyxEntry<Policy>,
conciergeReportID: string | undefined,
translate: LocalizedTranslate,
rules: OnyxCollection<Rule>,
isPolicyNamePreferred = false,
approverLookup: ChatRoomSubtitleApproverLookup | undefined,
isReportArchived = false,
): string | undefined {
if (isChatThread(report)) {
Expand All @@ -7851,19 +7855,13 @@ function getChatRoomSubtitle(
return report?.reportName?.substring(1) ?? '';
}
if ((isPolicyExpenseChat(report) && !!report?.isOwnPolicyExpenseChat) || isExpenseReport(report)) {
// Finding the approver scans every approval rule, so skip it when the workspace name is shown anyway
if (!isPolicyNamePreferred) {
return getPolicyName({report, policy, unavailableTranslation: translate('workspace.common.unavailable')});
}

const submitToAccountID = getSubmitToAccountID(policy, report, getLoginByAccountID(report?.ownerAccountID, getAllPersonalDetails()), rules);
const submitsToAccountDetails = getPersonalDetail(submitToAccountID);
const subtitle = submitsToAccountDetails?.displayName ?? submitsToAccountDetails?.login;

if (!subtitle) {
const submitToLogin = approverLookup ? getSubmitToEmail(policy, report, getLoginByAccountID(report?.ownerAccountID, getAllPersonalDetails()), approverLookup.rules) : undefined;
const submitToDetails = submitToLogin ? approverLookup?.personalDetailsByLogin[submitToLogin] : undefined;
Comment thread
bernhardoj marked this conversation as resolved.
Resolved
const submitToName = submitToDetails?.displayName ?? submitToDetails?.login;
if (!submitToName) {
return getPolicyName({report, policy, unavailableTranslation: translate('workspace.common.unavailable')});
}
return `${getReportSubtitlePrefix(report)}${translate('iou.submitsTo', subtitle ?? '')}`;
return `${getReportSubtitlePrefix(report)}${translate('iou.submitsTo', submitToName)}`;
}

if (isReportArchived) {
Expand Down
2 changes: 1 addition & 1 deletion src/libs/SidebarUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@ function getOptionData({

const isExpense = isExpenseOrBillReport(report);
const hasMultipleParticipants = participantPersonalDetailList.length > 1 || result.isChatRoom || result.isPolicyExpenseChat || isExpense;
const subtitle = getChatRoomSubtitle(report, policy, conciergeReportID, translate, rules, false, isReportArchived);
const subtitle = getChatRoomSubtitle(report, policy, conciergeReportID, translate, undefined, isReportArchived);

const status = personalDetail?.status ?? '';

Expand Down
5 changes: 2 additions & 3 deletions src/libs/actions/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import type {OnyxData} from '@src/types/onyx/Request';
import type {SearchResultDataType} from '@src/types/onyx/SearchResults';
import {isEmptyObject} from '@src/types/utils/EmptyObject';

import type {NullishDeep, OnyxCollection, OnyxEntry, OnyxKey, OnyxUpdate} from 'react-native-onyx';
import type {NullishDeep, OnyxEntry, OnyxKey, OnyxUpdate} from 'react-native-onyx';

import {Str} from 'expensify-common';
import Onyx from 'react-native-onyx';
Expand Down Expand Up @@ -1331,7 +1331,6 @@ function getShareDestination(
policy: OnyxEntry<OnyxTypes.Policy>,
conciergeReportID: string | undefined,
translate: LocalizedTranslate,
rules: OnyxCollection<OnyxTypes.Rule>,
reportName: string | undefined,
pendingDeleteMemberAccountIDs?: string[],
): ShareDestination {
Expand All @@ -1356,7 +1355,7 @@ function getShareDestination(
const login = personalDetails?.[participantAccountID]?.login ?? '';
subtitle = formatPhoneNumber(login || displayName);
} else {
subtitle = ReportUtils.getChatRoomSubtitle(report, policy, conciergeReportID, translate, rules) ?? '';
subtitle = ReportUtils.getChatRoomSubtitle(report, policy, conciergeReportID, translate, undefined) ?? '';
}
return {
icons: ReportUtils.getIcons({report, formatPhoneNumber, translate, personalDetails, defaultIcon: FallbackAvatar, pendingDeleteMemberAccountIDs, conciergeReportID}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,11 @@ function CenteredReportNameContent({report, name}: CenteredReportNameContentProp
const isReportArchived = useReportIsArchived(report.reportID);
const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${report.policyID}`);
const [conciergeReportID] = useOnyx(ONYXKEYS.CONCIERGE_REPORT_ID);
const [rules] = useOnyx(ONYXKEYS.COLLECTION.RULE);

const isWorkspaceChat = isWorkspaceChatUtil(report.chatType ?? '');
let subtitle = '';
if (!isGroupChatUtil(report)) {
const chatRoomSubtitle = getChatRoomSubtitle(report, policy, conciergeReportID, translate, rules, false, isReportArchived) ?? '';
const chatRoomSubtitle = getChatRoomSubtitle(report, policy, conciergeReportID, translate, undefined, isReportArchived) ?? '';
const isSubtitleBare = isExpenseReportUtil(report) || isPolicyExpenseChatUtil(report) || isInvoiceRoomUtil(report);
subtitle = chatRoomSubtitle && (isSubtitleBare ? chatRoomSubtitle : `${translate('threads.in')} ${chatRoomSubtitle}`);
}
Expand Down
3 changes: 1 addition & 2 deletions src/pages/ShareCodePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ function ShareCodePage({report, policy, backTo, shouldShowQRCodeOnly = false}: S

const currentUserPersonalDetails = useCurrentUserPersonalDetails();
const [conciergeReportID] = useOnyx(ONYXKEYS.CONCIERGE_REPORT_ID);
const [rules] = useOnyx(ONYXKEYS.COLLECTION.RULE);
const [parentReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${report?.parentReportID}`);
const invoiceReceiverPolicy = usePolicy(getInvoiceReceiverPolicyID(parentReport));
const reportForTitle = getReportForHeader(report, parentReport);
Expand Down Expand Up @@ -154,7 +153,7 @@ function ShareCodePage({report, policy, backTo, shouldShowQRCodeOnly = false}: S
parentReportName: derivedParentReportName,
isParentReportArchived,
currentUserAccountID: currentUserPersonalDetails.accountID,
}).workspaceName ?? getChatRoomSubtitle(report, policy, conciergeReportID, translate, rules, false, isReportArchived);
}).workspaceName ?? getChatRoomSubtitle(report, policy, conciergeReportID, translate, undefined, isReportArchived);
}
} else {
subtitle = currentUserPersonalDetails.login;
Expand Down
Loading
Loading