@@ -96,6 +96,14 @@ type SearchListProps = Pick<FlashListProps<SearchListItem>, 'onScroll' | 'conten
9696
9797const keyExtractor = ( item : SearchListItem , index : number ) => item . keyForList ?? `${ index } ` ;
9898
99+ function isTransactionGroupListItemArray ( data : SearchListItem [ ] ) : data is TransactionGroupListItemType [ ] {
100+ if ( data . length <= 0 ) {
101+ return false ;
102+ }
103+ const firstElement = data . at ( 0 ) ;
104+ return typeof firstElement === 'object' && 'transactions' in firstElement ;
105+ }
106+
99107function SearchList (
100108 {
101109 data,
@@ -125,19 +133,23 @@ function SearchList(
125133
126134 const { initialHeight, initialWidth} = useInitialWindowDimensions ( ) ;
127135 const { hash, groupBy, type} = queryJSON ;
128- const flattenedTransactions = groupBy ? ( data as TransactionGroupListItemType [ ] ) . flatMap ( ( item ) => item . transactions ) : data ;
129-
130- const flattenedTransactionWithoutPendingDelete = useMemo (
131- ( ) => flattenedTransactions . filter ( ( t ) => t ?. pendingAction !== CONST . RED_BRICK_ROAD_PENDING_ACTION . DELETE ) ,
132- [ flattenedTransactions ] ,
133- ) ;
136+ const flattenedItems = useMemo ( ( ) => {
137+ if ( groupBy ) {
138+ if ( ! isTransactionGroupListItemArray ( data ) ) {
139+ return data ;
140+ }
141+ return data . flatMap ( ( item ) => item . transactions ) ;
142+ }
143+ return data ;
144+ } , [ data , groupBy ] ) ;
145+ const flattenedItemsWithoutPendingDelete = useMemo ( ( ) => flattenedItems . filter ( ( t ) => t ?. pendingAction !== CONST . RED_BRICK_ROAD_PENDING_ACTION . DELETE ) , [ flattenedItems ] ) ;
134146
135147 const selectedItemsLength = useMemo (
136148 ( ) =>
137- flattenedTransactions . reduce ( ( acc , item ) => {
149+ flattenedItems . reduce ( ( acc , item ) => {
138150 return item ?. isSelected ? acc + 1 : acc ;
139151 } , 0 ) ,
140- [ flattenedTransactions ] ,
152+ [ flattenedItems ] ,
141153 ) ;
142154
143155 const { translate} = useLocalize ( ) ;
@@ -222,7 +234,7 @@ function SearchList(
222234
223235 const [ focusedIndex , setFocusedIndex ] = useArrowKeyFocusManager ( {
224236 initialFocusedIndex : - 1 ,
225- maxIndex : flattenedTransactions . length - 1 ,
237+ maxIndex : flattenedItems . length - 1 ,
226238 isActive : isFocused ,
227239 onFocusedIndexChange : ( index : number ) => {
228240 scrollToIndex ( index ) ;
@@ -352,7 +364,7 @@ function SearchList(
352364
353365 const tableHeaderVisible = canSelectMultiple || ! ! SearchTableHeader ;
354366 const selectAllButtonVisible = canSelectMultiple && ! SearchTableHeader ;
355- const isSelectAllChecked = selectedItemsLength > 0 && selectedItemsLength === flattenedTransactionWithoutPendingDelete . length ;
367+ const isSelectAllChecked = selectedItemsLength > 0 && selectedItemsLength === flattenedItemsWithoutPendingDelete . length ;
356368
357369 const getItemHeight = useMemo (
358370 ( ) =>
@@ -407,11 +419,11 @@ function SearchList(
407419 < Checkbox
408420 accessibilityLabel = { translate ( 'workspace.people.selectAll' ) }
409421 isChecked = { isSelectAllChecked }
410- isIndeterminate = { selectedItemsLength > 0 && selectedItemsLength !== flattenedTransactionWithoutPendingDelete . length }
422+ isIndeterminate = { selectedItemsLength > 0 && selectedItemsLength !== flattenedItemsWithoutPendingDelete . length }
411423 onPress = { ( ) => {
412424 onAllCheckboxPress ( ) ;
413425 } }
414- disabled = { flattenedTransactions . length === 0 }
426+ disabled = { flattenedItems . length === 0 }
415427 />
416428 ) }
417429
0 commit comments