-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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: remove getSearchText function #46409
Merged
neil-marcellini
merged 9 commits into
Expensify:main
from
callstack-internal:perf/getsearchtext
Aug 5, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4981a04
remove usage of searchText for personal details
TMisiukiewicz dad7d56
remove usage of searchtext in current user option
TMisiukiewicz a5e171e
remove getSearchText function
TMisiukiewicz 28aa5a4
remove unused search code
TMisiukiewicz 5c333f3
migrate OptionListUtils search tests to filterOptions
TMisiukiewicz fc0599f
Merge remote-tracking branch 'upstream/main' into perf/getsearchtext
TMisiukiewicz b4997e2
link issue for email regex
TMisiukiewicz c3885ac
Merge remote-tracking branch 'upstream/main' into perf/getsearchtext
TMisiukiewicz 23fb1e9
move normalizing search term out of loops
TMisiukiewicz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -512,50 +512,6 @@ function uniqFast(items: string[]): string[] { | |
return result; | ||
} | ||
|
||
/** | ||
* Returns a string with all relevant search terms. | ||
* | ||
* This method must be incredibly performant. It was found to be a big performance bottleneck | ||
* when dealing with accounts that have thousands of reports. For loops are more efficient than _.each | ||
* Array.prototype.push.apply is faster than using the spread operator. | ||
*/ | ||
function getSearchText( | ||
report: OnyxInputOrEntry<Report>, | ||
reportName: string, | ||
personalDetailList: Array<Partial<PersonalDetails>>, | ||
isChatRoomOrPolicyExpenseChat: boolean, | ||
isThread: boolean, | ||
): string { | ||
const searchTerms: string[] = []; | ||
|
||
for (const personalDetail of personalDetailList) { | ||
if (personalDetail.login) { | ||
// The regex below is used to remove dots only from the local part of the user email (local-part@domain) | ||
// so that we can match emails that have dots without explicitly writing the dots (e.g: fistlast@domain will match first.last@domain) | ||
// More info https://github.com/Expensify/App/issues/8007 | ||
searchTerms.push(PersonalDetailsUtils.getDisplayNameOrDefault(personalDetail, '', false), personalDetail.login, personalDetail.login.replace(/\.(?=[^\s@]*@)/g, '')); | ||
} | ||
} | ||
|
||
if (report) { | ||
Array.prototype.push.apply(searchTerms, reportName.split(/[,\s]/)); | ||
|
||
if (isThread) { | ||
const title = ReportUtils.getReportName(report); | ||
const chatRoomSubtitle = ReportUtils.getChatRoomSubtitle(report); | ||
|
||
Array.prototype.push.apply(searchTerms, title.split(/[,\s]/)); | ||
Array.prototype.push.apply(searchTerms, chatRoomSubtitle?.split(/[,\s]/) ?? ['']); | ||
} else if (isChatRoomOrPolicyExpenseChat) { | ||
const chatRoomSubtitle = ReportUtils.getChatRoomSubtitle(report); | ||
|
||
Array.prototype.push.apply(searchTerms, chatRoomSubtitle?.split(/[,\s]/) ?? ['']); | ||
} | ||
Comment on lines
-543
to
-553
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We had made threads searchable in #19117. |
||
} | ||
|
||
return uniqFast(searchTerms).join(' '); | ||
} | ||
|
||
/** | ||
* Get an object of error messages keyed by microtime by combining all error objects related to the report. | ||
*/ | ||
|
@@ -790,7 +746,6 @@ function createOption( | |
phoneNumber: undefined, | ||
hasDraftComment: false, | ||
keyForList: undefined, | ||
searchText: undefined, | ||
isDefaultRoom: false, | ||
isPinned: false, | ||
isWaitingOnBankAccount: false, | ||
|
@@ -887,9 +842,6 @@ function createOption( | |
} | ||
|
||
result.text = reportName; | ||
// Disabling this line for safeness as nullish coalescing works only if the value is undefined or null | ||
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing | ||
result.searchText = getSearchText(report, reportName, personalDetailList, !!result.isChatRoom || !!result.isPolicyExpenseChat, !!result.isThread); | ||
result.icons = ReportUtils.getIcons(report, personalDetails, personalDetail?.avatar, personalDetail?.login, personalDetail?.accountID, null); | ||
result.subtitle = subtitle; | ||
|
||
|
@@ -2053,22 +2005,6 @@ function getOptions( | |
continue; | ||
} | ||
|
||
// Finally check to see if this option is a match for the provided search string if we have one | ||
const {searchText, participantsList, isChatRoom} = reportOption; | ||
const participantNames = getParticipantNames(participantsList); | ||
|
||
if (searchValue) { | ||
// Determine if the search is happening within a chat room and starts with the report ID | ||
const isReportIdSearch = isChatRoom && Str.startsWith(reportOption.reportID ?? '-1', searchValue); | ||
|
||
// Check if the search string matches the search text or participant names considering the type of the room | ||
const isSearchMatch = isSearchStringMatch(searchValue, searchText, participantNames, isChatRoom); | ||
|
||
if (!isReportIdSearch && !isSearchMatch) { | ||
continue; | ||
} | ||
} | ||
|
||
reportOption.isSelected = isReportSelected(reportOption, selectedOptions); | ||
|
||
if (action === CONST.IOU.ACTION.CATEGORIZE) { | ||
|
@@ -2093,19 +2029,11 @@ function getOptions( | |
if (personalDetailsOptionsToExclude.some((optionToExclude) => optionToExclude.login === personalDetailOption.login)) { | ||
return; | ||
} | ||
const {searchText, participantsList, isChatRoom} = personalDetailOption; | ||
const participantNames = getParticipantNames(participantsList); | ||
if (searchValue && !isSearchStringMatch(searchValue, searchText, participantNames, isChatRoom)) { | ||
return; | ||
} | ||
|
||
personalDetailsOptions.push(personalDetailOption); | ||
}); | ||
|
||
let currentUserOption = allPersonalDetailsOptions.find((personalDetailsOption) => personalDetailsOption.login === currentUserLogin); | ||
if (searchValue && currentUserOption && !isSearchStringMatch(searchValue, currentUserOption.searchText)) { | ||
currentUserOption = undefined; | ||
} | ||
const currentUserOption = allPersonalDetailsOptions.find((personalDetailsOption) => personalDetailsOption.login === currentUserLogin); | ||
|
||
let userToInvite: ReportUtils.OptionData | null = null; | ||
if ( | ||
|
@@ -2438,11 +2366,12 @@ function formatSectionsFromSearchTerm( | |
}; | ||
} | ||
|
||
const cleanSearchTerm = searchTerm.trim().toLowerCase(); | ||
// If you select a new user you don't have a contact for, they won't get returned as part of a recent report or personal details | ||
// This will add them to the list of options, deduping them if they already exist in the other lists | ||
const selectedParticipantsWithoutDetails = selectedOptions.filter((participant) => { | ||
const accountID = participant.accountID ?? null; | ||
const isPartOfSearchTerm = participant.searchText?.toLowerCase().includes(searchTerm.trim().toLowerCase()); | ||
const isPartOfSearchTerm = getPersonalDetailSearchTerms(participant).join(' ').toLowerCase().includes(cleanSearchTerm); | ||
const isReportInRecentReports = filteredRecentReports.some((report) => report.accountID === accountID); | ||
const isReportInPersonalDetails = filteredPersonalDetails.some((personalDetail) => personalDetail.accountID === accountID); | ||
return isPartOfSearchTerm && !isReportInRecentReports && !isReportInPersonalDetails; | ||
|
@@ -2474,6 +2403,14 @@ function getFirstKeyForList(data?: Option[] | null) { | |
|
||
return firstNonEmptyDataObj.keyForList ? firstNonEmptyDataObj.keyForList : ''; | ||
} | ||
|
||
function getPersonalDetailSearchTerms(item: Partial<ReportUtils.OptionData>) { | ||
return [item.participantsList?.[0]?.displayName ?? '', item.login ?? '', item.login?.replace(CONST.EMAIL_SEARCH_REGEX, '') ?? '']; | ||
} | ||
|
||
function getCurrentUserSearchTerms(item: ReportUtils.OptionData) { | ||
return [item.text ?? '', item.login ?? '', item.login?.replace(CONST.EMAIL_SEARCH_REGEX, '') ?? '']; | ||
} | ||
/** | ||
* Filters options based on the search input value | ||
*/ | ||
|
@@ -2495,10 +2432,6 @@ function filterOptions(options: Options, searchInputValue: string, config?: Filt | |
const searchValue = parsedPhoneNumber.possible && parsedPhoneNumber.number?.e164 ? parsedPhoneNumber.number.e164 : searchInputValue.toLowerCase(); | ||
const searchTerms = searchValue ? searchValue.split(' ') : []; | ||
|
||
// The regex below is used to remove dots only from the local part of the user email (local-part@domain) | ||
// so that we can match emails that have dots without explicitly writing the dots (e.g: fistlast@domain will match first.last@domain) | ||
const emailRegex = /\.(?=[^\s@]*@)/g; | ||
|
||
const optionsToExclude: Option[] = [{login: CONST.EMAIL.NOTIFICATIONS}]; | ||
|
||
excludeLogins.forEach((login) => { | ||
|
@@ -2518,7 +2451,7 @@ function filterOptions(options: Options, searchInputValue: string, config?: Filt | |
|
||
if (login) { | ||
keys.push(login); | ||
keys.push(login.replace(emailRegex, '')); | ||
keys.push(login.replace(CONST.EMAIL_SEARCH_REGEX, '')); | ||
} | ||
}); | ||
} | ||
|
@@ -2534,7 +2467,7 @@ function filterOptions(options: Options, searchInputValue: string, config?: Filt | |
|
||
if (item.login) { | ||
values.push(item.login); | ||
values.push(item.login.replace(emailRegex, '')); | ||
values.push(item.login.replace(CONST.EMAIL_SEARCH_REGEX, '')); | ||
} | ||
|
||
if (item.isThread) { | ||
|
@@ -2560,15 +2493,9 @@ function filterOptions(options: Options, searchInputValue: string, config?: Filt | |
|
||
return uniqFast(values); | ||
}); | ||
const personalDetails = filterArrayByMatch(items.personalDetails, term, (item) => | ||
uniqFast([item.participantsList?.[0]?.displayName ?? '', item.login ?? '', item.login?.replace(emailRegex, '') ?? '']), | ||
); | ||
const personalDetails = filterArrayByMatch(items.personalDetails, term, (item) => uniqFast(getPersonalDetailSearchTerms(item))); | ||
|
||
const currentUserOptionSearchText = uniqFast([ | ||
items.currentUserOption?.text ?? '', | ||
items.currentUserOption?.login ?? '', | ||
items.currentUserOption?.login?.replace(emailRegex, '') ?? '', | ||
]).join(' '); | ||
const currentUserOptionSearchText = items.currentUserOption ? uniqFast(getCurrentUserSearchTerms(items.currentUserOption)).join(' ') : ''; | ||
|
||
const currentUserOption = isSearchStringMatch(term, currentUserOptionSearchText) ? items.currentUserOption : null; | ||
|
||
|
@@ -2635,7 +2562,6 @@ export { | |
getSearchValueForPhoneOrEmail, | ||
getPersonalDetailsForAccountIDs, | ||
getIOUConfirmationOptionsFromPayeePersonalDetail, | ||
getSearchText, | ||
isSearchStringMatchUserDetails, | ||
getAllReportErrors, | ||
getPolicyExpenseReportOption, | ||
|
@@ -2664,6 +2590,8 @@ export { | |
canCreateOptimisticPersonalDetailOption, | ||
getUserToInviteOption, | ||
shouldShowViolations, | ||
getPersonalDetailSearchTerms, | ||
getCurrentUserSearchTerms, | ||
}; | ||
|
||
export type {MemberForList, CategorySection, CategoryTreeSection, Options, OptionList, SearchOption, PayeePersonalDetails, Category, Tax, TaxRatesOption, Option, OptionTree}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need to link this old issue anymore?