diff --git a/src/components/Search/SearchList.tsx b/src/components/Search/SearchList.tsx index 51128bbe5c8e3..bfb3104712704 100644 --- a/src/components/Search/SearchList.tsx +++ b/src/components/Search/SearchList.tsx @@ -96,6 +96,14 @@ type SearchListProps = Pick, 'onScroll' | 'conten const keyExtractor = (item: SearchListItem, index: number) => item.keyForList ?? `${index}`; +function isTransactionGroupListItemArray(data: SearchListItem[]): data is TransactionGroupListItemType[] { + if (data.length <= 0) { + return false; + } + const firstElement = data.at(0); + return typeof firstElement === 'object' && 'transactions' in firstElement; +} + function SearchList( { data, @@ -125,19 +133,23 @@ function SearchList( const {initialHeight, initialWidth} = useInitialWindowDimensions(); const {hash, groupBy, type} = queryJSON; - const flattenedTransactions = groupBy ? (data as TransactionGroupListItemType[]).flatMap((item) => item.transactions) : data; - - const flattenedTransactionWithoutPendingDelete = useMemo( - () => flattenedTransactions.filter((t) => t?.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE), - [flattenedTransactions], - ); + const flattenedItems = useMemo(() => { + if (groupBy) { + if (!isTransactionGroupListItemArray(data)) { + return data; + } + return data.flatMap((item) => item.transactions); + } + return data; + }, [data, groupBy]); + const flattenedItemsWithoutPendingDelete = useMemo(() => flattenedItems.filter((t) => t?.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE), [flattenedItems]); const selectedItemsLength = useMemo( () => - flattenedTransactions.reduce((acc, item) => { + flattenedItems.reduce((acc, item) => { return item?.isSelected ? acc + 1 : acc; }, 0), - [flattenedTransactions], + [flattenedItems], ); const {translate} = useLocalize(); @@ -222,7 +234,7 @@ function SearchList( const [focusedIndex, setFocusedIndex] = useArrowKeyFocusManager({ initialFocusedIndex: -1, - maxIndex: flattenedTransactions.length - 1, + maxIndex: flattenedItems.length - 1, isActive: isFocused, onFocusedIndexChange: (index: number) => { scrollToIndex(index); @@ -352,7 +364,7 @@ function SearchList( const tableHeaderVisible = canSelectMultiple || !!SearchTableHeader; const selectAllButtonVisible = canSelectMultiple && !SearchTableHeader; - const isSelectAllChecked = selectedItemsLength > 0 && selectedItemsLength === flattenedTransactionWithoutPendingDelete.length; + const isSelectAllChecked = selectedItemsLength > 0 && selectedItemsLength === flattenedItemsWithoutPendingDelete.length; const getItemHeight = useMemo( () => @@ -407,11 +419,11 @@ function SearchList( 0 && selectedItemsLength !== flattenedTransactionWithoutPendingDelete.length} + isIndeterminate={selectedItemsLength > 0 && selectedItemsLength !== flattenedItemsWithoutPendingDelete.length} onPress={() => { onAllCheckboxPress(); }} - disabled={flattenedTransactions.length === 0} + disabled={flattenedItems.length === 0} /> )} diff --git a/src/components/Search/index.tsx b/src/components/Search/index.tsx index c3164a157e32f..3ea88a1beeda8 100644 --- a/src/components/Search/index.tsx +++ b/src/components/Search/index.tsx @@ -272,6 +272,13 @@ function Search({queryJSON, searchResults, onSearchListScroll, contentContainerS return []; } + // Group-by option cannot be used for chats or tasks + const isChat = type === CONST.SEARCH.DATA_TYPES.CHAT; + const isTask = type === CONST.SEARCH.DATA_TYPES.TASK; + if (groupBy && (isChat || isTask)) { + return []; + } + return getSections(type, searchResults.data, searchResults.search, groupBy, exportReportActions, currentSearchKey); }, [currentSearchKey, exportReportActions, groupBy, isDataLoaded, searchResults, type]);