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

perf: only calculate participants for autocomplete when needed #55460

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
81 changes: 41 additions & 40 deletions src/components/Search/SearchRouter/SearchRouterList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useThemeStyles from '@hooks/useThemeStyles';
import {searchInServer} from '@libs/actions/Report';
import {getCardDescription, isCard, isCardHiddenFromSearch, mergeCardListWithWorkspaceFeeds} from '@libs/CardUtils';
import {combineOrderingOfReportsAndPersonalDetails, getSearchOptions, getValidOptions} from '@libs/OptionsListUtils';
import memoize from '@libs/memoize';
import {combineOrderingOfReportsAndPersonalDetails, getSearchOptions, getValidPersonalDetailOptions} from '@libs/OptionsListUtils';
import type {Options, SearchOption} from '@libs/OptionsListUtils';
import Performance from '@libs/Performance';
import {getAllTaxRates, getCleanedTagName} from '@libs/PolicyUtils';
Expand Down Expand Up @@ -153,44 +154,44 @@ function SearchRouterList(
const allCards = useMemo(() => mergeCardListWithWorkspaceFeeds(workspaceCardFeeds, userCardList), [userCardList, workspaceCardFeeds]);
const cardAutocompleteList = Object.values(allCards);

const participantsAutocompleteList = useMemo(() => {
if (!areOptionsInitialized) {
return [];
}

const filteredOptions = getValidOptions(
{
reports: options.reports,
personalDetails: options.personalDetails,
},
{
excludeLogins: CONST.EXPENSIFY_EMAILS_OBJECT,
includeSelfDM: true,
showChatPreviewLine: true,
shouldBoldTitleByDefault: false,
},
);
const getParticipantsAutocompleteList = useMemo(
() =>
memoize(() => {
if (!areOptionsInitialized) {
return [];
}

// This cast is needed as something is incorrect in types OptionsListUtils.getOptions around l1490 and includeRecentReports types
const personalDetailsFromOptions = filteredOptions.personalDetails.map((option) => (option as SearchOption<PersonalDetails>).item);
const autocompleteOptions = Object.values(personalDetailsFromOptions)
.filter((details): details is NonNullable<PersonalDetails> => !!(details && details?.login))
.map((details) => {
return {
name: details.displayName ?? Str.removeSMSDomain(details.login ?? ''),
accountID: details.accountID.toString(),
const currentUserRef = {
current: undefined as OptionData | undefined,
};
});
const currentUser = filteredOptions.currentUserOption ? (filteredOptions.currentUserOption as SearchOption<PersonalDetails>).item : undefined;
if (currentUser) {
autocompleteOptions.push({
name: currentUser.displayName ?? Str.removeSMSDomain(currentUser.login ?? ''),
accountID: currentUser.accountID?.toString(),
});
}

return autocompleteOptions;
}, [areOptionsInitialized, options.personalDetails, options.reports]);
const filteredOptions = getValidPersonalDetailOptions(options.personalDetails, {
loginsToExclude: CONST.EXPENSIFY_EMAILS_OBJECT,
shouldBoldTitleByDefault: false,
currentUserRef,
});

// This cast is needed as something is incorrect in types OptionsListUtils.getOptions around l1490 and includeRecentReports types
const personalDetailsFromOptions = filteredOptions.map((option) => (option as SearchOption<PersonalDetails>).item);
const autocompleteOptions = Object.values(personalDetailsFromOptions)
.filter((details): details is NonNullable<PersonalDetails> => !!details?.login)
.map((details) => {
return {
name: details.displayName ?? Str.removeSMSDomain(details.login ?? ''),
accountID: details.accountID.toString(),
};
});
const currentUser = currentUserRef.current;
if (currentUser && currentUser.accountID) {
autocompleteOptions.push({
name: currentUser.displayName ?? Str.removeSMSDomain(currentUser.login ?? ''),
accountID: currentUser.accountID.toString(),
});
}

return autocompleteOptions;
}),
[areOptionsInitialized, options.personalDetails],
);

const taxAutocompleteList = useMemo(() => getAutocompleteTaxList(taxRates, policy), [policy, taxRates]);

Expand Down Expand Up @@ -280,7 +281,7 @@ function SearchRouterList(
}));
}
case CONST.SEARCH.SYNTAX_FILTER_KEYS.FROM: {
const filteredParticipants = participantsAutocompleteList
const filteredParticipants = getParticipantsAutocompleteList()
.filter((participant) => participant.name.toLowerCase().includes(autocompleteValue.toLowerCase()) && !alreadyAutocompletedKeys.includes(participant.name.toLowerCase()))
.slice(0, 10);

Expand All @@ -292,7 +293,7 @@ function SearchRouterList(
}));
}
case CONST.SEARCH.SYNTAX_FILTER_KEYS.TO: {
const filteredParticipants = participantsAutocompleteList
const filteredParticipants = getParticipantsAutocompleteList()
.filter((participant) => participant.name.toLowerCase().includes(autocompleteValue.toLowerCase()) && !alreadyAutocompletedKeys.includes(participant.name.toLowerCase()))
.slice(0, 10);

Expand Down Expand Up @@ -367,7 +368,7 @@ function SearchRouterList(
currencyAutocompleteList,
recentCurrencyAutocompleteList,
taxAutocompleteList,
participantsAutocompleteList,
getParticipantsAutocompleteList,
searchOptions.recentReports,
typeAutocompleteList,
statusAutocompleteList,
Expand Down
75 changes: 54 additions & 21 deletions src/libs/OptionsListUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1404,6 +1404,48 @@ function getValidReports(
return validReportOptions;
}

function getValidPersonalDetailOptions(
options: OptionList['personalDetails'],
{
loginsToExclude = {},
includeDomainEmail = false,
shouldBoldTitleByDefault = false,
currentUserRef,
}: {
loginsToExclude?: Record<string, boolean>;
includeDomainEmail?: boolean;
shouldBoldTitleByDefault: boolean;
// If the current user is found in the options and you pass an object ref, it will be assigned
currentUserRef?: {
current?: OptionData;
};
},
) {
const personalDetailsOptions: OptionData[] = [];
for (let i = 0; i < options.length; i++) {
// eslint-disable-next-line rulesdir/prefer-at
const detail = options[i];
if (!detail?.login || !detail.accountID || !!detail?.isOptimisticPersonalDetail || (!includeDomainEmail && Str.isDomainEmail(detail.login))) {
continue;
}

if (currentUserRef && !!currentUserLogin && detail.login === currentUserLogin) {
// eslint-disable-next-line no-param-reassign
currentUserRef.current = detail;
}

if (loginsToExclude[detail.login]) {
continue;
}

detail.isBold = shouldBoldTitleByDefault;

personalDetailsOptions.push(detail);
}

return personalDetailsOptions;
}

/**
* Options are reports and personal details. This function filters out the options that are not valid to be displayed.
*/
Expand Down Expand Up @@ -1463,8 +1505,10 @@ function getValidOptions(
}

// Get valid personal details and check if we can find the current user:
const personalDetailsOptions: OptionData[] = [];
let currentUserOption: OptionData | undefined;
let personalDetailsOptions: OptionData[] = [];
const currentUserRef = {
current: undefined as OptionData | undefined,
};
if (includeP2P) {
let personalDetailLoginsToExclude = loginsToExclude;
if (currentUserLogin) {
Expand All @@ -1473,25 +1517,13 @@ function getValidOptions(
[currentUserLogin]: true,
};
}
for (let i = 0; i < options.personalDetails.length; i++) {
// eslint-disable-next-line rulesdir/prefer-at
const detail = options.personalDetails[i];
if (!detail?.login || !detail.accountID || !!detail?.isOptimisticPersonalDetail || (!includeDomainEmail && Str.isDomainEmail(detail.login))) {
continue;
}

if (!!currentUserLogin && detail.login === currentUserLogin) {
currentUserOption = detail;
}

if (personalDetailLoginsToExclude[detail.login]) {
continue;
}

detail.isBold = shouldBoldTitleByDefault;

personalDetailsOptions.push(detail);
}
personalDetailsOptions = getValidPersonalDetailOptions(options.personalDetails, {
loginsToExclude: personalDetailLoginsToExclude,
shouldBoldTitleByDefault,
includeDomainEmail,
currentUserRef,
});
}

let workspaceChats: OptionData[] = [];
Expand All @@ -1513,7 +1545,7 @@ function getValidOptions(
return {
personalDetails: personalDetailsOptions,
recentReports: recentReportOptions,
currentUserOption,
currentUserOption: currentUserRef.current,
// User to invite is generated by the search input of a user.
// As this function isn't concerned with any search input yet, this is null (will be set when using filterOptions).
userToInvite: null,
Expand Down Expand Up @@ -2076,6 +2108,7 @@ export {
isCurrentUser,
isPersonalDetailsReady,
getValidOptions,
getValidPersonalDetailOptions,
getSearchOptions,
getShareDestinationOptions,
getMemberInviteOptions,
Expand Down
Loading