diff --git a/src/pages/tasks/TaskShareDestinationSelectorModal.tsx b/src/pages/tasks/TaskShareDestinationSelectorModal.tsx index c486e5b294c3..4367f2ccebcb 100644 --- a/src/pages/tasks/TaskShareDestinationSelectorModal.tsx +++ b/src/pages/tasks/TaskShareDestinationSelectorModal.tsx @@ -56,22 +56,53 @@ function TaskShareDestinationSelectorModal() { const textInputHint = useMemo(() => (isOffline ? `${translate('common.youAppearToBeOffline')} ${translate('search.resultsAreLimited')}` : ''), [isOffline, translate]); - const options = useMemo(() => { + const defaultOptions = useMemo(() => { if (!areOptionsInitialized) { return { - sections: [], - headerMessage: '', + recentReports: [], + personalDetails: [], + userToInvite: null, + currentUserOption: null, + categoryOptions: [], + tagOptions: [], + taxRatesOptions: [], + header: '', }; } const filteredReports = reportFilter(optionList.reports); - const {recentReports} = OptionsListUtils.getShareDestinationOptions(filteredReports, optionList.personalDetails, [], debouncedSearchValue.trim(), [], CONST.EXPENSIFY_EMAILS, true); - const headerMessage = OptionsListUtils.getHeaderMessage(recentReports && recentReports.length !== 0, false, debouncedSearchValue); + const {recentReports} = OptionsListUtils.getShareDestinationOptions(filteredReports, optionList.personalDetails, [], '', [], [], true); + const header = OptionsListUtils.getHeaderMessage(recentReports && recentReports.length !== 0, false, ''); + return { + recentReports, + personalDetails: [], + userToInvite: null, + currentUserOption: null, + categoryOptions: [], + tagOptions: [], + taxRatesOptions: [], + header, + }; + }, [areOptionsInitialized, optionList.personalDetails, optionList.reports]); + + const options = useMemo(() => { + if (debouncedSearchValue.trim() === '') { + return defaultOptions; + } + const filteredReports = OptionsListUtils.filterOptions(defaultOptions, debouncedSearchValue.trim(), { + excludeLogins: CONST.EXPENSIFY_EMAILS, + canInviteUser: false, + includeChatRoomsByParticipants: true, + }); + const header = OptionsListUtils.getHeaderMessage(filteredReports.recentReports && filteredReports.recentReports.length !== 0, false, debouncedSearchValue); + return {...filteredReports, header}; + }, [debouncedSearchValue, defaultOptions]); - const sections = - recentReports && recentReports.length > 0 + const sections = useMemo( + () => + options.recentReports && options.recentReports.length > 0 ? [ { - data: recentReports.map((option) => ({ + data: options.recentReports.map((option) => ({ ...option, text: option.text ?? '', alternateText: option.alternateText ?? undefined, @@ -83,10 +114,9 @@ function TaskShareDestinationSelectorModal() { shouldShow: true, }, ] - : []; - - return {sections, headerMessage}; - }, [areOptionsInitialized, optionList.reports, optionList.personalDetails, debouncedSearchValue]); + : [], + [options.recentReports], + ); useEffect(() => { ReportActions.searchInServer(debouncedSearchValue); @@ -106,14 +136,14 @@ function TaskShareDestinationSelectorModal() {