@@ -8,6 +8,7 @@ import FullPageOfflineBlockingView from '@components/BlockingViews/FullPageOffli
88import SearchTableHeader from '@components/SelectionList/SearchTableHeader' ;
99import type { ReportActionListItemType , SearchListItem , SelectionListHandle , TransactionGroupListItemType , TransactionListItemType } from '@components/SelectionList/types' ;
1010import SearchRowSkeleton from '@components/Skeletons/SearchRowSkeleton' ;
11+ import useCardFeedsForDisplay from '@hooks/useCardFeedsForDisplay' ;
1112import useLocalize from '@hooks/useLocalize' ;
1213import useNetwork from '@hooks/useNetwork' ;
1314import useOnyx from '@hooks/useOnyx' ;
@@ -30,6 +31,7 @@ import {
3031 getListItem ,
3132 getSections ,
3233 getSortedSections ,
34+ getSuggestedSearches ,
3335 getWideAmountIndicators ,
3436 isReportActionListItemType ,
3537 isSearchDataLoaded ,
@@ -40,6 +42,7 @@ import {
4042 shouldShowEmptyState ,
4143 shouldShowYear as shouldShowYearUtil ,
4244} from '@libs/SearchUIUtils' ;
45+ import type { SearchKey } from '@libs/SearchUIUtils' ;
4346import { isOnHold , isTransactionPendingDelete } from '@libs/TransactionUtils' ;
4447import Navigation from '@navigation/Navigation' ;
4548import type { SearchFullscreenNavigatorParamList } from '@navigation/types' ;
@@ -147,8 +150,7 @@ function Search({queryJSON, searchResults, onSearchListScroll, contentContainerS
147150 const navigation = useNavigation < PlatformStackNavigationProp < SearchFullscreenNavigatorParamList > > ( ) ;
148151 const isFocused = useIsFocused ( ) ;
149152 const {
150- currentSearchKey,
151- setCurrentSearchHash,
153+ setCurrentSearchHashAndKey,
152154 setSelectedTransactions,
153155 selectedTransactions,
154156 clearSelectedTransactions,
@@ -161,8 +163,6 @@ function Search({queryJSON, searchResults, onSearchListScroll, contentContainerS
161163 } = useSearchContext ( ) ;
162164 const [ offset , setOffset ] = useState ( 0 ) ;
163165
164- const { type, status, sortBy, sortOrder, hash, groupBy} = queryJSON ;
165-
166166 const [ transactions ] = useOnyx ( ONYXKEYS . COLLECTION . TRANSACTION , { canBeMissing : true } ) ;
167167 const previousTransactions = usePrevious ( transactions ) ;
168168 const [ reportActions ] = useOnyx ( ONYXKEYS . COLLECTION . REPORT_ACTIONS , { canBeMissing : true } ) ;
@@ -179,6 +179,23 @@ function Search({queryJSON, searchResults, onSearchListScroll, contentContainerS
179179 ) ;
180180 } ,
181181 } ) ;
182+ const { defaultCardFeed} = useCardFeedsForDisplay ( ) ;
183+ const [ accountID ] = useOnyx ( ONYXKEYS . SESSION , { canBeMissing : false , selector : ( s ) => s ?. accountID } ) ;
184+ const suggestedSearches = useMemo ( ( ) => getSuggestedSearches ( defaultCardFeed ?. id , accountID ) , [ defaultCardFeed ?. id , accountID ] ) ;
185+
186+ const { type, status, sortBy, sortOrder, hash, groupBy} = queryJSON ;
187+ const searchKey = useMemo ( ( ) => Object . values ( suggestedSearches ) . find ( ( search ) => search . hash === hash ) ?. key , [ suggestedSearches , hash ] ) ;
188+
189+ const shouldCalculateTotals = useMemo ( ( ) => {
190+ if ( offset !== 0 ) {
191+ return false ;
192+ }
193+ if ( ! searchKey ) {
194+ return false ;
195+ }
196+ const eligibleSearchKeys : Partial < SearchKey [ ] > = [ CONST . SEARCH . SEARCH_KEYS . STATEMENTS , CONST . SEARCH . SEARCH_KEYS . UNAPPROVED_CASH , CONST . SEARCH . SEARCH_KEYS . UNAPPROVED_COMPANY_CARDS ] ;
197+ return eligibleSearchKeys . includes ( searchKey ) ;
198+ } , [ offset , searchKey ] ) ;
182199
183200 const previousReportActions = usePrevious ( reportActions ) ;
184201 const reportActionsArray = useMemo (
@@ -194,8 +211,8 @@ function Search({queryJSON, searchResults, onSearchListScroll, contentContainerS
194211 useFocusEffect (
195212 useCallback ( ( ) => {
196213 clearSelectedTransactions ( hash ) ;
197- setCurrentSearchHash ( hash ) ;
198- } , [ hash , clearSelectedTransactions , setCurrentSearchHash ] ) ,
214+ setCurrentSearchHashAndKey ( hash , searchKey ) ;
215+ } , [ hash , searchKey , clearSelectedTransactions , setCurrentSearchHashAndKey ] ) ,
199216 ) ;
200217
201218 const isSearchResultsEmpty = ! searchResults ?. data || isSearchResultsEmptyUtil ( searchResults ) ;
@@ -205,7 +222,7 @@ function Search({queryJSON, searchResults, onSearchListScroll, contentContainerS
205222 return ;
206223 }
207224
208- const selectedKeys = Object . keys ( selectedTransactions ) . filter ( ( key ) => selectedTransactions [ key ] ) ;
225+ const selectedKeys = Object . keys ( selectedTransactions ) . filter ( ( transactionKey ) => selectedTransactions [ transactionKey ] ) ;
209226 if ( selectedKeys . length === 0 && isMobileSelectionModeEnabled && shouldTurnOffSelectionMode ) {
210227 turnOffMobileSelectionMode ( ) ;
211228 }
@@ -220,7 +237,7 @@ function Search({queryJSON, searchResults, onSearchListScroll, contentContainerS
220237 return ;
221238 }
222239
223- const selectedKeys = Object . keys ( selectedTransactions ) . filter ( ( key ) => selectedTransactions [ key ] ) ;
240+ const selectedKeys = Object . keys ( selectedTransactions ) . filter ( ( transactionKey ) => selectedTransactions [ transactionKey ] ) ;
224241 if ( ! isSmallScreenWidth ) {
225242 if ( selectedKeys . length === 0 && isMobileSelectionModeEnabled ) {
226243 turnOffMobileSelectionMode ( ) ;
@@ -239,11 +256,11 @@ function Search({queryJSON, searchResults, onSearchListScroll, contentContainerS
239256 return ;
240257 }
241258
242- handleSearch ( { queryJSON, offset} ) ;
259+ handleSearch ( { queryJSON, searchKey , offset, shouldCalculateTotals } ) ;
243260 // We don't need to run the effect on change of isFocused.
244261 // eslint-disable-next-line react-compiler/react-compiler
245262 // eslint-disable-next-line react-hooks/exhaustive-deps
246- } , [ handleSearch , isOffline , offset , queryJSON ] ) ;
263+ } , [ handleSearch , isOffline , offset , queryJSON , searchKey , shouldCalculateTotals ] ) ;
247264
248265 useEffect ( ( ) => {
249266 openSearch ( ) ;
@@ -254,7 +271,9 @@ function Search({queryJSON, searchResults, onSearchListScroll, contentContainerS
254271 transactions,
255272 previousTransactions,
256273 queryJSON,
274+ searchKey,
257275 offset,
276+ shouldCalculateTotals,
258277 reportActions,
259278 previousReportActions,
260279 } ) ;
@@ -279,8 +298,8 @@ function Search({queryJSON, searchResults, onSearchListScroll, contentContainerS
279298 return [ ] ;
280299 }
281300
282- return getSections ( type , searchResults . data , searchResults . search , groupBy , exportReportActions , currentSearchKey ) ;
283- } , [ currentSearchKey , exportReportActions , groupBy , isDataLoaded , searchResults , type ] ) ;
301+ return getSections ( type , searchResults . data , searchResults . search , groupBy , exportReportActions , searchKey ) ;
302+ } , [ searchKey , exportReportActions , groupBy , isDataLoaded , searchResults , type ] ) ;
284303
285304 useEffect ( ( ) => {
286305 /** We only want to display the skeleton for the status filters the first time we load them for a specific data type */
0 commit comments