diff --git a/src/components/Search/index.tsx b/src/components/Search/index.tsx index 0603fd4896e6..7283a36e9f59 100644 --- a/src/components/Search/index.tsx +++ b/src/components/Search/index.tsx @@ -188,33 +188,6 @@ function Search({queryJSON, policyIDs, isCustomQuery}: SearchProps) { turnOffMobileSelectionMode(); }, [isSearchResultsEmpty, prevIsSearchResultEmpty]); - const toggleTransaction = (item: TransactionListItemType | ReportListItemType) => { - if (SearchUtils.isTransactionListItemType(item)) { - if (!item.keyForList) { - return; - } - - setSelectedTransactions(prepareTransactionsList(item, selectedTransactions)); - return; - } - - if (item.transactions.every((transaction) => selectedTransactions[transaction.keyForList]?.isSelected)) { - const reducedSelectedTransactions: SelectedTransactions = {...selectedTransactions}; - - item.transactions.forEach((transaction) => { - delete reducedSelectedTransactions[transaction.keyForList]; - }); - - setSelectedTransactions(reducedSelectedTransactions); - return; - } - - setSelectedTransactions({ - ...selectedTransactions, - ...Object.fromEntries(item.transactions.map(mapTransactionItemToSelectedEntry)), - }); - }; - if (shouldShowLoadingState) { return ( <> @@ -228,7 +201,19 @@ function Search({queryJSON, policyIDs, isCustomQuery}: SearchProps) { ); } - const shouldShowEmptyState = !isDataLoaded || SearchUtils.isSearchResultsEmpty(searchResults); + const type = SearchUtils.getSearchType(searchResults?.search); + + if (searchResults === undefined || type === undefined) { + Log.alert('[Search] Undefined search type'); + return null; + } + + const ListItem = SearchUtils.getListItem(type); + const data = SearchUtils.getSections(searchResults.data, searchResults.search, type); + const sortedData = SearchUtils.getSortedSections(type, data, sortBy, sortOrder); + const sortedSelectedData = sortedData.map((item) => mapToItemWithSelectionInfo(item, selectedTransactions)); + + const shouldShowEmptyState = !isDataLoaded || data.length === 0; if (shouldShowEmptyState) { return ( @@ -243,6 +228,33 @@ function Search({queryJSON, policyIDs, isCustomQuery}: SearchProps) { ); } + const toggleTransaction = (item: TransactionListItemType | ReportListItemType) => { + if (SearchUtils.isTransactionListItemType(item)) { + if (!item.keyForList) { + return; + } + + setSelectedTransactions(prepareTransactionsList(item, selectedTransactions)); + return; + } + + if (item.transactions.every((transaction) => selectedTransactions[transaction.keyForList]?.isSelected)) { + const reducedSelectedTransactions: SelectedTransactions = {...selectedTransactions}; + + item.transactions.forEach((transaction) => { + delete reducedSelectedTransactions[transaction.keyForList]; + }); + + setSelectedTransactions(reducedSelectedTransactions); + return; + } + + setSelectedTransactions({ + ...selectedTransactions, + ...Object.fromEntries(item.transactions.map(mapTransactionItemToSelectedEntry)), + }); + }; + const openReport = (item: TransactionListItemType | ReportListItemType) => { let reportID = SearchUtils.isTransactionListItemType(item) ? item.transactionThreadReportID : item.reportID; @@ -266,19 +278,6 @@ function Search({queryJSON, policyIDs, isCustomQuery}: SearchProps) { setOffset(offset + CONST.SEARCH.RESULTS_PAGE_SIZE); }; - const type = SearchUtils.getSearchType(searchResults?.search); - - if (type === undefined) { - Log.alert('[Search] Undefined search type'); - return null; - } - - const ListItem = SearchUtils.getListItem(type); - - const data = SearchUtils.getSections(searchResults?.data ?? {}, searchResults?.search ?? {}, type); - const sortedData = SearchUtils.getSortedSections(type, data, sortBy, sortOrder); - const sortedSelectedData = sortedData.map((item) => mapToItemWithSelectionInfo(item, selectedTransactions)); - const toggleAllTransactions = () => { const areItemsOfReportType = searchResults?.search.type === CONST.SEARCH.DATA_TYPES.REPORT; const flattenedItems = areItemsOfReportType ? (data as ReportListItemType[]).flatMap((item) => item.transactions) : data; diff --git a/src/libs/SearchUtils.ts b/src/libs/SearchUtils.ts index f778cca36f62..bdb4e7c8e005 100644 --- a/src/libs/SearchUtils.ts +++ b/src/libs/SearchUtils.ts @@ -76,7 +76,11 @@ function isSearchDataType(type: string): type is SearchDataTypes { return searchDataTypes.includes(type); } -function getSearchType(search: OnyxTypes.SearchResults['search']): SearchDataTypes | undefined { +function getSearchType(search: OnyxTypes.SearchResults['search'] | undefined): SearchDataTypes | undefined { + if (!search) { + return undefined; + } + if (!isSearchDataType(search.type)) { return undefined; } @@ -220,7 +224,7 @@ function getReportSections(data: OnyxTypes.SearchResults['data'], metadata: Onyx }; if (reportIDToTransactions[reportKey]?.transactions) { reportIDToTransactions[reportKey].transactions.push(transaction); - } else { + } else if (reportIDToTransactions[reportKey]) { reportIDToTransactions[reportKey].transactions = [transaction]; } }