@@ -2,6 +2,7 @@ import {useFocusEffect} from '@react-navigation/native';
22import { useEffect } from 'react' ;
33import { useSearchActionsContext , useSearchStateContext } from '@components/Search/SearchContext' ;
44import type { SearchQueryJSON } from '@components/Search/types' ;
5+ import { saveLastSearchParams } from '@libs/actions/ReportNavigation' ;
56import { openSearch , search } from '@libs/actions/Search' ;
67import { hasDeferredWrite } from '@libs/deferredLayoutWrite' ;
78import { isSearchDataLoaded } from '@libs/SearchUIUtils' ;
@@ -10,6 +11,10 @@ import useNetwork from './useNetwork';
1011import usePrevious from './usePrevious' ;
1112import useSearchShouldCalculateTotals from './useSearchShouldCalculateTotals' ;
1213
14+ // Gates the save below to real hash changes so snapshot-loading re-fires don't wipe fields
15+ // (hasMoreResults, previousLengthOfResults) maintained by report-browsing callers.
16+ let lastSavedSearchHash : number | undefined ;
17+
1318/**
1419 * Handles page-level setup for Search that must happen before the Search component mounts:
1520 * - Clears selected transactions when the query changes
@@ -51,6 +56,14 @@ function useSearchPageSetup(queryJSON: Readonly<SearchQueryJSON> | undefined) {
5156 if ( ! queryJSON || hash === undefined || shouldUseLiveData || isOffline ) {
5257 return ;
5358 }
59+
60+ // Must run even on cached snapshots, else SearchTabButton's Onyx fallback restores
61+ // a stale query after a tab switch (e.g. filter reappears after Reset).
62+ if ( lastSavedSearchHash !== hash ) {
63+ saveLastSearchParams ( { queryJSON, offset : 0 , searchKey : currentSearchKey , hasMoreResults : false , allowPostSearchRecount : false } ) ;
64+ lastSavedSearchHash = hash ;
65+ }
66+
5467 if ( isSnapshotDataLoaded || isSnapshotSearchLoading ) {
5568 return ;
5669 }
0 commit comments