Skip to content

Commit b8c759f

Browse files
authored
Merge pull request Expensify#67991 from huult/67295-reset-default-query-on-errors
2 parents 71f02c6 + 4bcf95d commit b8c759f

5 files changed

Lines changed: 38 additions & 3 deletions

File tree

src/components/Search/SearchContext.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const defaultSearchContextData: SearchContextData = {
1515
selectedReports: [],
1616
isOnSearch: false,
1717
shouldTurnOffSelectionMode: false,
18+
shouldResetSearchQuery: false,
1819
};
1920

2021
const defaultSearchContext: SearchContext = {
@@ -31,6 +32,7 @@ const defaultSearchContext: SearchContext = {
3132
setShouldShowFiltersBarLoading: () => {},
3233
shouldShowSelectAllMatchingItems: () => {},
3334
selectAllMatchingItems: () => {},
35+
setShouldResetSearchQuery: () => {},
3436
};
3537

3638
const Context = React.createContext<SearchContext>(defaultSearchContext);
@@ -173,6 +175,13 @@ function SearchContextProvider({children}: ChildrenProps) {
173175
[searchContextData.selectedTransactionIDs, searchContextData.selectedTransactions],
174176
);
175177

178+
const setShouldResetSearchQuery = useCallback((shouldReset: boolean) => {
179+
setSearchContextData((prevState) => ({
180+
...prevState,
181+
shouldResetSearchQuery: shouldReset,
182+
}));
183+
}, []);
184+
176185
const searchContext = useMemo<SearchContext>(
177186
() => ({
178187
...searchContextData,
@@ -188,6 +197,7 @@ function SearchContextProvider({children}: ChildrenProps) {
188197
shouldShowSelectAllMatchingItems,
189198
areAllMatchingItemsSelected,
190199
selectAllMatchingItems,
200+
setShouldResetSearchQuery,
191201
}),
192202
[
193203
searchContextData,
@@ -200,6 +210,7 @@ function SearchContextProvider({children}: ChildrenProps) {
200210
shouldShowSelectAllMatchingItems,
201211
showSelectAllMatchingItems,
202212
areAllMatchingItemsSelected,
213+
setShouldResetSearchQuery,
203214
],
204215
);
205216

src/components/Search/SearchRouter/SearchRouter.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import OptionsListSkeletonView from '@components/OptionsListSkeletonView';
1212
import type {AnimatedTextInputRef} from '@components/RNTextInput';
1313
import type {GetAdditionalSectionsCallback} from '@components/Search/SearchAutocompleteList';
1414
import SearchAutocompleteList from '@components/Search/SearchAutocompleteList';
15+
import {useSearchContext} from '@components/Search/SearchContext';
1516
import SearchInputSelectionWrapper from '@components/Search/SearchInputSelectionWrapper';
1617
import type {SearchFilterKey, SearchQueryString} from '@components/Search/types';
1718
import type {SearchQueryItem} from '@components/SelectionList/Search/SearchQueryListItem';
@@ -84,6 +85,7 @@ type SearchRouterProps = {
8485
function SearchRouter({onRouterClose, shouldHideInputCaret, isSearchRouterDisplayed}: SearchRouterProps, ref: React.Ref<View>) {
8586
const {translate} = useLocalize();
8687
const styles = useThemeStyles();
88+
const {setShouldResetSearchQuery} = useSearchContext();
8789
const [, recentSearchesMetadata] = useOnyx(ONYXKEYS.RECENT_SEARCHES, {canBeMissing: true});
8890
const {areOptionsInitialized} = useOptionsList();
8991
const [isSearchingForReports] = useOnyx(ONYXKEYS.IS_SEARCHING_FOR_REPORTS, {initWithStoredValues: false, canBeMissing: true});
@@ -264,6 +266,9 @@ function SearchRouter({onRouterClose, shouldHideInputCaret, isSearchRouterDispla
264266
return;
265267
}
266268

269+
// Reset the search query flag when performing a new search
270+
setShouldResetSearchQuery(false);
271+
267272
backHistory(() => {
268273
onRouterClose();
269274
Navigation.navigate(ROUTES.SEARCH_ROOT.getRoute({query: updatedQuery}));
@@ -272,7 +277,7 @@ function SearchRouter({onRouterClose, shouldHideInputCaret, isSearchRouterDispla
272277
setTextInputValue('');
273278
setAutocompleteQueryValue('');
274279
},
275-
[autocompleteSubstitutions, onRouterClose, setTextInputValue],
280+
[autocompleteSubstitutions, onRouterClose, setTextInputValue, setShouldResetSearchQuery],
276281
);
277282

278283
const setTextAndUpdateSelection = useCallback(

src/components/Search/index.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import type {PlatformStackNavigationProp} from '@libs/Navigation/PlatformStackNa
2626
import Performance from '@libs/Performance';
2727
import {getIOUActionForTransactionID, isExportIntegrationAction, isIntegrationMessageAction} from '@libs/ReportActionsUtils';
2828
import {canEditFieldOfMoneyRequest, generateReportID, isArchivedReport} from '@libs/ReportUtils';
29-
import {buildSearchQueryString} from '@libs/SearchQueryUtils';
29+
import {buildCannedSearchQuery, buildSearchQueryString} from '@libs/SearchQueryUtils';
3030
import {
3131
getListItem,
3232
getSections,
@@ -185,6 +185,8 @@ function Search({queryJSON, searchResults, onSearchListScroll, contentContainerS
185185
shouldShowSelectAllMatchingItems,
186186
areAllMatchingItemsSelected,
187187
selectAllMatchingItems,
188+
shouldResetSearchQuery,
189+
setShouldResetSearchQuery,
188190
} = useSearchContext();
189191
const [offset, setOffset] = useState(0);
190192

@@ -658,6 +660,19 @@ function Search({queryJSON, searchResults, onSearchListScroll, contentContainerS
658660

659661
const hasErrors = Object.keys(searchResults?.errors ?? {}).length > 0 && !isOffline;
660662

663+
useEffect(() => {
664+
const currentRoute = Navigation.getActiveRouteWithoutParams();
665+
if (hasErrors && (currentRoute === '/' || (shouldResetSearchQuery && currentRoute === '/search'))) {
666+
// Use requestAnimationFrame to safely update navigation params without overriding the current route
667+
requestAnimationFrame(() => {
668+
Navigation.setParams({q: buildCannedSearchQuery()});
669+
});
670+
if (shouldResetSearchQuery) {
671+
setShouldResetSearchQuery(false);
672+
}
673+
}
674+
}, [hasErrors, queryJSON, searchResults, shouldResetSearchQuery, setShouldResetSearchQuery]);
675+
661676
const fetchMoreResults = useCallback(() => {
662677
if (!isFocused || !searchResults?.search?.hasMoreResults || shouldShowLoadingState || shouldShowLoadingMoreItems) {
663678
return;

src/components/Search/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ type SearchContextData = {
7777
selectedReports: SelectedReports[];
7878
isOnSearch: boolean;
7979
shouldTurnOffSelectionMode: boolean;
80+
shouldResetSearchQuery: boolean;
8081
};
8182

8283
type SearchContext = SearchContextData & {
@@ -100,6 +101,7 @@ type SearchContext = SearchContextData & {
100101
shouldShowSelectAllMatchingItems: (shouldShow: boolean) => void;
101102
areAllMatchingItemsSelected: boolean;
102103
selectAllMatchingItems: (on: boolean) => void;
104+
setShouldResetSearchQuery: (shouldReset: boolean) => void;
103105
};
104106

105107
type ASTNode = {

src/pages/settings/Troubleshoot/TroubleshootPage.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import RecordTroubleshootDataToolMenu from '@components/RecordTroubleshootDataTo
1414
import RenderHTML from '@components/RenderHTML';
1515
import ScreenWrapper from '@components/ScreenWrapper';
1616
import ScrollView from '@components/ScrollView';
17+
import {useSearchContext} from '@components/Search/SearchContext';
1718
import Section from '@components/Section';
1819
import Switch from '@components/Switch';
1920
import TestToolMenu from '@components/TestToolMenu';
@@ -51,7 +52,7 @@ function TroubleshootPage() {
5152
const [shouldStoreLogs] = useOnyx(ONYXKEYS.SHOULD_STORE_LOGS, {canBeMissing: true});
5253
const [shouldMaskOnyxState = true] = useOnyx(ONYXKEYS.SHOULD_MASK_ONYX_STATE, {canBeMissing: true});
5354
const {resetOptions} = useOptionsList({shouldInitialize: false});
54-
55+
const {setShouldResetSearchQuery} = useSearchContext();
5556
const exportOnyxState = useCallback(() => {
5657
ExportOnyxState.readFromOnyxDatabase().then((value: Record<string, unknown>) => {
5758
const dataToShare = ExportOnyxState.maskOnyxState(value, shouldMaskOnyxState);
@@ -152,6 +153,7 @@ function TroubleshootPage() {
152153
onConfirm={() => {
153154
setIsConfirmationModalVisible(false);
154155
resetOptions();
156+
setShouldResetSearchQuery(true);
155157
clearOnyxAndResetApp();
156158
}}
157159
onCancel={() => setIsConfirmationModalVisible(false)}

0 commit comments

Comments
 (0)