Skip to content

Commit 0e9a19d

Browse files
authored
Merge pull request Expensify#63550 from software-mansion-labs/korytko/optimize-search-context-a-little-more
[CP Staging] Use ref to check if transactions are empty in SearchContext
2 parents 4db0fc1 + 965d758 commit 0e9a19d

2 files changed

Lines changed: 33 additions & 36 deletions

File tree

src/components/MoneyRequestReportView/MoneyRequestReportTransactionList.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,7 @@ function MoneyRequestReportTransactionList({
162162
}
163163
clearSelectedTransactions(true);
164164
};
165-
// We don't need to run the effect on change of clearSelectedTransactions on every focus.
166-
// eslint-disable-next-line react-compiler/react-compiler
167-
// eslint-disable-next-line react-hooks/exhaustive-deps
168-
}, []),
165+
}, [clearSelectedTransactions]),
169166
);
170167

171168
const handleMouseLeave = (e: React.MouseEvent<Element, MouseEvent>) => {

src/components/Search/SearchContext.tsx

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {useCallback, useContext, useMemo, useState} from 'react';
1+
import React, {useCallback, useContext, useMemo, useRef, useState} from 'react';
22
import {isMoneyRequestReport} from '@libs/ReportUtils';
33
import {isReportListItemType, isTransactionListItemType} from '@libs/SearchUIUtils';
44
import CONST from '@src/CONST';
@@ -37,6 +37,7 @@ function SearchContextProvider({children}: ChildrenProps) {
3737
const [shouldShowFiltersBarLoading, setShouldShowFiltersBarLoading] = useState(false);
3838
const [lastSearchType, setLastSearchType] = useState<string | undefined>(undefined);
3939
const [searchContextData, setSearchContextData] = useState(defaultSearchContextData);
40+
const areTransactionsEmpty = useRef(true);
4041

4142
const setCurrentSearchHash = useCallback((searchHash: number) => {
4243
setSearchContextData((prevState) => ({
@@ -45,42 +46,41 @@ function SearchContextProvider({children}: ChildrenProps) {
4546
}));
4647
}, []);
4748

48-
const setSelectedTransactions: SearchContext['setSelectedTransactions'] = useCallback(
49-
(selectedTransactions, data = []) => {
50-
if (selectedTransactions instanceof Array) {
51-
if (!selectedTransactions.length && !searchContextData.selectedTransactionIDs.length) {
52-
return;
53-
}
54-
return setSearchContextData((prevState) => ({
55-
...prevState,
56-
selectedTransactionIDs: selectedTransactions,
57-
}));
49+
const setSelectedTransactions: SearchContext['setSelectedTransactions'] = useCallback((selectedTransactions, data = []) => {
50+
if (selectedTransactions instanceof Array) {
51+
if (!selectedTransactions.length && areTransactionsEmpty.current) {
52+
areTransactionsEmpty.current = true;
53+
return;
5854
}
55+
areTransactionsEmpty.current = false;
56+
return setSearchContextData((prevState) => ({
57+
...prevState,
58+
selectedTransactionIDs: selectedTransactions,
59+
}));
60+
}
5961

60-
// When selecting transactions, we also need to manage the reports to which these transactions belong. This is done to ensure proper exporting to CSV.
61-
let selectedReports: SearchContext['selectedReports'] = [];
62+
// When selecting transactions, we also need to manage the reports to which these transactions belong. This is done to ensure proper exporting to CSV.
63+
let selectedReports: SearchContext['selectedReports'] = [];
6264

63-
if (data.length && data.every(isReportListItemType)) {
64-
selectedReports = data
65-
.filter((item) => isMoneyRequestReport(item) && item.transactions.every(({keyForList}) => selectedTransactions[keyForList]?.isSelected))
66-
.map(({reportID, action = CONST.SEARCH.ACTION_TYPES.VIEW, total = CONST.DEFAULT_NUMBER_ID, policyID}) => ({reportID, action, total, policyID}));
67-
}
65+
if (data.length && data.every(isReportListItemType)) {
66+
selectedReports = data
67+
.filter((item) => isMoneyRequestReport(item) && item.transactions.every(({keyForList}) => selectedTransactions[keyForList]?.isSelected))
68+
.map(({reportID, action = CONST.SEARCH.ACTION_TYPES.VIEW, total = CONST.DEFAULT_NUMBER_ID, policyID}) => ({reportID, action, total, policyID}));
69+
}
6870

69-
if (data.length && data.every(isTransactionListItemType)) {
70-
selectedReports = data
71-
.filter(({keyForList}) => !!keyForList && selectedTransactions[keyForList]?.isSelected)
72-
.map(({reportID, action = CONST.SEARCH.ACTION_TYPES.VIEW, amount: total = CONST.DEFAULT_NUMBER_ID, policyID}) => ({reportID, action, total, policyID}));
73-
}
71+
if (data.length && data.every(isTransactionListItemType)) {
72+
selectedReports = data
73+
.filter(({keyForList}) => !!keyForList && selectedTransactions[keyForList]?.isSelected)
74+
.map(({reportID, action = CONST.SEARCH.ACTION_TYPES.VIEW, amount: total = CONST.DEFAULT_NUMBER_ID, policyID}) => ({reportID, action, total, policyID}));
75+
}
7476

75-
setSearchContextData((prevState) => ({
76-
...prevState,
77-
selectedTransactions,
78-
shouldTurnOffSelectionMode: false,
79-
selectedReports,
80-
}));
81-
},
82-
[searchContextData.selectedTransactionIDs.length],
83-
);
77+
setSearchContextData((prevState) => ({
78+
...prevState,
79+
selectedTransactions,
80+
shouldTurnOffSelectionMode: false,
81+
selectedReports,
82+
}));
83+
}, []);
8484

8585
const clearSelectedTransactions: SearchContext['clearSelectedTransactions'] = useCallback(
8686
(searchHashOrClearIDsFlag, shouldTurnOffSelectionMode = false) => {

0 commit comments

Comments
 (0)