Skip to content

Commit bbd49f6

Browse files
authored
Merge pull request Expensify#91186 from callstack-internal/perf/total-selectable-items-count
perf:hoist totalSelectableItemsCount out of updateSelectAllM…
2 parents fd6ba76 + 8ae6f9d commit bbd49f6

1 file changed

Lines changed: 25 additions & 20 deletions

File tree

src/components/Search/index.tsx

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -945,26 +945,33 @@ function Search({
945945
isRefreshingSelection.current = false;
946946
}, [selectedTransactions]);
947947

948+
const areItemsGrouped = !!validGroupBy || isExpenseReportType;
949+
const totalSelectableItemsCount = useMemo(() => {
950+
if (!areItemsGrouped) {
951+
return filteredData.length;
952+
}
953+
954+
return (filteredData as TransactionGroupListItemType[]).reduce((count, item) => {
955+
// For empty reports, count the report itself as a selectable item
956+
if (item.transactions.length === 0 && isTransactionReportGroupListItemType(item)) {
957+
if (item.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE) {
958+
return count;
959+
}
960+
961+
return count + 1;
962+
}
963+
// For regular reports, count all transactions except pending delete ones
964+
const selectableTransactions = item.transactions.filter((transaction) => !isTransactionPendingDelete(transaction));
965+
966+
return count + selectableTransactions.length;
967+
}, 0);
968+
}, [areItemsGrouped, filteredData]);
969+
948970
const updateSelectAllMatchingItemsState = useCallback(
949971
(updatedSelectedTransactions: SelectedTransactions) => {
950-
if (!filteredData.length || isRefreshingSelection.current) {
972+
if (!totalSelectableItemsCount || isRefreshingSelection.current) {
951973
return;
952974
}
953-
const areItemsGrouped = !!validGroupBy || type === CONST.SEARCH.DATA_TYPES.EXPENSE_REPORT;
954-
const totalSelectableItemsCount = areItemsGrouped
955-
? (filteredData as TransactionGroupListItemType[]).reduce((count, item) => {
956-
// For empty reports, count the report itself as a selectable item
957-
if (item.transactions.length === 0 && isTransactionReportGroupListItemType(item)) {
958-
if (item.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE) {
959-
return count;
960-
}
961-
return count + 1;
962-
}
963-
// For regular reports, count all transactions except pending delete ones
964-
const selectableTransactions = item.transactions.filter((transaction) => !isTransactionPendingDelete(transaction));
965-
return count + selectableTransactions.length;
966-
}, 0)
967-
: filteredData.length;
968975
const areAllItemsSelected = totalSelectableItemsCount === Object.keys(updatedSelectedTransactions).length;
969976

970977
// If the user has selected all the expenses in their view but there are more expenses matched by the search
@@ -974,7 +981,7 @@ function Search({
974981
selectAllMatchingItems(false);
975982
}
976983
},
977-
[filteredData, validGroupBy, type, searchResults?.search?.hasMoreResults, setShouldShowSelectAllMatchingItems, selectAllMatchingItems],
984+
[totalSelectableItemsCount, searchResults?.search?.hasMoreResults, setShouldShowSelectAllMatchingItems, selectAllMatchingItems],
978985
);
979986

980987
const toggleTransaction = useCallback(
@@ -1335,7 +1342,6 @@ function Search({
13351342
}, [isFocused, searchResults?.search?.hasMoreResults, shouldShowLoadingMoreItems, shouldShowLoadingState, offset, allDataLength]);
13361343

13371344
const toggleAllTransactions = useCallback(() => {
1338-
const areItemsGrouped = !!validGroupBy || isExpenseReportType;
13391345
const totalSelected = Object.keys(selectedTransactions).length;
13401346

13411347
if (totalSelected > 0) {
@@ -1377,8 +1383,7 @@ function Search({
13771383
setSelectedTransactions(updatedTransactions, filteredData);
13781384
updateSelectAllMatchingItemsState(updatedTransactions);
13791385
}, [
1380-
validGroupBy,
1381-
isExpenseReportType,
1386+
areItemsGrouped,
13821387
selectedTransactions,
13831388
setSelectedTransactions,
13841389
filteredData,

0 commit comments

Comments
 (0)