Skip to content

Commit 49afa4b

Browse files
authored
Merge pull request Expensify#65734 from nkdengineer/fix/65716-1
fix: app crashes when group-by:reports is manually
2 parents 8e5924e + d290524 commit 49afa4b

2 files changed

Lines changed: 31 additions & 12 deletions

File tree

src/components/Search/SearchList.tsx

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ type SearchListProps = Pick<FlashListProps<SearchListItem>, 'onScroll' | 'conten
9696

9797
const 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+
99107
function 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

src/components/Search/index.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,13 @@ function Search({queryJSON, searchResults, onSearchListScroll, contentContainerS
272272
return [];
273273
}
274274

275+
// Group-by option cannot be used for chats or tasks
276+
const isChat = type === CONST.SEARCH.DATA_TYPES.CHAT;
277+
const isTask = type === CONST.SEARCH.DATA_TYPES.TASK;
278+
if (groupBy && (isChat || isTask)) {
279+
return [];
280+
}
281+
275282
return getSections(type, searchResults.data, searchResults.search, groupBy, exportReportActions, currentSearchKey);
276283
}, [currentSearchKey, exportReportActions, groupBy, isDataLoaded, searchResults, type]);
277284

0 commit comments

Comments
 (0)