From 90e3851bd66eb1e032413114d4ba3107a5299455 Mon Sep 17 00:00:00 2001 From: Mateusz Titz Date: Mon, 2 Sep 2024 16:58:16 +0200 Subject: [PATCH] remove isCustomQueryParam and add checking if query is canned --- src/ROUTES.ts | 2 +- src/components/Search/SearchPageHeader.tsx | 14 ++++++++------ src/components/Search/index.tsx | 6 +----- .../Navigation/AppNavigator/AuthScreens.tsx | 2 +- src/libs/Navigation/linkingConfig/config.ts | 3 --- src/libs/Navigation/types.ts | 2 -- src/libs/SearchUtils.ts | 11 +++++++++++ src/pages/Search/AdvancedSearchFilters.tsx | 1 - src/pages/Search/SearchPage.tsx | 9 ++------- src/pages/Search/SearchPageBottomTab.tsx | 17 ++++------------- src/pages/Search/SearchTypeMenu.tsx | 9 +++++---- 11 files changed, 33 insertions(+), 43 deletions(-) diff --git a/src/ROUTES.ts b/src/ROUTES.ts index 48076178bcb9..3ad0563f6f1f 100644 --- a/src/ROUTES.ts +++ b/src/ROUTES.ts @@ -35,7 +35,7 @@ const ROUTES = { SEARCH_CENTRAL_PANE: { route: 'search', - getRoute: ({query, isCustomQuery = false}: {query: SearchQueryString; isCustomQuery?: boolean}) => `search?q=${query}&isCustomQuery=${isCustomQuery}` as const, + getRoute: ({query}: {query: SearchQueryString}) => `search?q=${query}` as const, }, SEARCH_ADVANCED_FILTERS: 'search/filters', SEARCH_ADVANCED_FILTERS_DATE: 'search/filters/date', diff --git a/src/components/Search/SearchPageHeader.tsx b/src/components/Search/SearchPageHeader.tsx index c28eed688aaa..48fd53391cf7 100644 --- a/src/components/Search/SearchPageHeader.tsx +++ b/src/components/Search/SearchPageHeader.tsx @@ -93,7 +93,6 @@ type SearchPageHeaderProps = { queryJSON: SearchQueryJSON; hash: number; onSelectDeleteOption?: (itemsToDelete: string[]) => void; - isCustomQuery: boolean; setOfflineModalOpen?: () => void; setDownloadErrorModalOpen?: () => void; data?: TransactionListItemType[] | ReportListItemType[]; @@ -118,7 +117,7 @@ function getHeaderContent(type: SearchDataTypes): HeaderContent { } } -function SearchPageHeader({queryJSON, hash, onSelectDeleteOption, setOfflineModalOpen, setDownloadErrorModalOpen, isCustomQuery, data}: SearchPageHeaderProps) { +function SearchPageHeader({queryJSON, hash, onSelectDeleteOption, setOfflineModalOpen, setDownloadErrorModalOpen, data}: SearchPageHeaderProps) { const {translate} = useLocalize(); const theme = useTheme(); const styles = useThemeStyles(); @@ -143,11 +142,14 @@ function SearchPageHeader({queryJSON, hash, onSelectDeleteOption, setOfflineModa [data, selectedTransactions], ); const {status, type} = queryJSON; - const headerSubtitle = isCustomQuery ? SearchUtils.getSearchHeaderTitle(queryJSON) : translate(getHeaderContent(type).titleText); - const headerTitle = isCustomQuery ? translate('search.filtersHeader') : ''; - const headerIcon = isCustomQuery ? Illustrations.Filters : getHeaderContent(type).icon; - const subtitleStyles = isCustomQuery ? {} : styles.textHeadlineH2; + const isCannedQuery = SearchUtils.isCannedSearchQuery(queryJSON); + + const headerSubtitle = isCannedQuery ? translate(getHeaderContent(type).titleText) : SearchUtils.getSearchHeaderTitle(queryJSON); + const headerTitle = isCannedQuery ? '' : translate('search.filtersHeader'); + const headerIcon = isCannedQuery ? getHeaderContent(type).icon : Illustrations.Filters; + + const subtitleStyles = isCannedQuery ? styles.textHeadlineH2 : {}; const headerButtonsOptions = useMemo(() => { if (selectedTransactionsKeys.length === 0) { diff --git a/src/components/Search/index.tsx b/src/components/Search/index.tsx index 958e238d4598..63664556bd2b 100644 --- a/src/components/Search/index.tsx +++ b/src/components/Search/index.tsx @@ -38,7 +38,6 @@ import type {SearchColumnType, SearchQueryJSON, SearchStatus, SelectedTransactio type SearchProps = { queryJSON: SearchQueryJSON; - isCustomQuery: boolean; }; const transactionItemMobileHeight = 100; @@ -75,7 +74,7 @@ function prepareTransactionsList(item: TransactionListItemType, selectedTransact return {...selectedTransactions, [item.keyForList]: {isSelected: true, canDelete: item.canDelete, canHold: item.canHold, canUnhold: item.canUnhold, action: item.action}}; } -function Search({queryJSON, isCustomQuery}: SearchProps) { +function Search({queryJSON}: SearchProps) { const {isOffline} = useNetwork(); const {translate} = useLocalize(); const styles = useThemeStyles(); @@ -197,7 +196,6 @@ function Search({queryJSON, isCustomQuery}: SearchProps) { return ( <> @@ -232,7 +230,6 @@ function Search({queryJSON, isCustomQuery}: SearchProps) { return ( <> @@ -325,7 +322,6 @@ function Search({queryJSON, isCustomQuery}: SearchProps) { return ( <> > { if (screenName === SCREENS.SEARCH.CENTRAL_PANE) { // Generate default query string with buildSearchQueryString without argument. - return {q: buildSearchQueryString(), isCustomQuery: false}; + return {q: buildSearchQueryString()}; } if (screenName === SCREENS.REPORT) { diff --git a/src/libs/Navigation/linkingConfig/config.ts b/src/libs/Navigation/linkingConfig/config.ts index e7d02b3f65dc..6b723cb305ce 100644 --- a/src/libs/Navigation/linkingConfig/config.ts +++ b/src/libs/Navigation/linkingConfig/config.ts @@ -57,9 +57,6 @@ const config: LinkingOptions['config'] = { [SCREENS.SETTINGS.WORKSPACES]: ROUTES.SETTINGS_WORKSPACES, [SCREENS.SEARCH.CENTRAL_PANE]: { path: ROUTES.SEARCH_CENTRAL_PANE.route, - parse: { - isCustomQuery: (isCustomQuery) => isCustomQuery.toLowerCase() === 'true', - }, }, [SCREENS.SETTINGS.SAVE_THE_WORLD]: ROUTES.SETTINGS_SAVE_THE_WORLD, [SCREENS.SETTINGS.SUBSCRIPTION.ROOT]: ROUTES.SETTINGS_SUBSCRIPTION, diff --git a/src/libs/Navigation/types.ts b/src/libs/Navigation/types.ts index 68cef98ff59f..b0c44a7797cc 100644 --- a/src/libs/Navigation/types.ts +++ b/src/libs/Navigation/types.ts @@ -65,10 +65,8 @@ type CentralPaneScreensParamList = { [SCREENS.SETTINGS.TROUBLESHOOT]: undefined; [SCREENS.SETTINGS.WORKSPACES]: undefined; - // Param types of the search central pane are also used for the search bottom tab screen. [SCREENS.SEARCH.CENTRAL_PANE]: { q: SearchQueryString; - isCustomQuery: boolean; }; [SCREENS.SETTINGS.SAVE_THE_WORLD]: undefined; [SCREENS.SETTINGS.SUBSCRIPTION.ROOT]: undefined; diff --git a/src/libs/SearchUtils.ts b/src/libs/SearchUtils.ts index b30ff55ef5ae..a411c83cb6b8 100644 --- a/src/libs/SearchUtils.ts +++ b/src/libs/SearchUtils.ts @@ -581,6 +581,16 @@ function buildCannedSearchQuery(type: SearchDataTypes = CONST.SEARCH.DATA_TYPES. return normalizeQuery(`type:${type} status:${status}`); } +/** + * Returns whether a given search query is a Canned query. + * + * Canned queries are simple predefined queries, that are defined only using type and status and no additional filters. + * For example: "type:trip status:all" is a canned query. + */ +function isCannedSearchQuery(queryJSON: SearchQueryJSON) { + return !queryJSON.filters; +} + export { buildQueryStringFromFilters, buildSearchQueryJSON, @@ -599,6 +609,7 @@ export { normalizeQuery, shouldShowYear, buildCannedSearchQuery, + isCannedSearchQuery, getExpenseTypeTranslationKey, getChatFiltersTranslationKey, }; diff --git a/src/pages/Search/AdvancedSearchFilters.tsx b/src/pages/Search/AdvancedSearchFilters.tsx index aec2b8e93363..1d625fe5aa10 100644 --- a/src/pages/Search/AdvancedSearchFilters.tsx +++ b/src/pages/Search/AdvancedSearchFilters.tsx @@ -334,7 +334,6 @@ function AdvancedSearchFilters() { Navigation.navigate( ROUTES.SEARCH_CENTRAL_PANE.getRoute({ query, - isCustomQuery: true, }), ); }; diff --git a/src/pages/Search/SearchPage.tsx b/src/pages/Search/SearchPage.tsx index 37735cddce3f..c3607d4a5947 100644 --- a/src/pages/Search/SearchPage.tsx +++ b/src/pages/Search/SearchPage.tsx @@ -16,7 +16,7 @@ type SearchPageProps = StackScreenProps SearchUtils.buildSearchQueryJSON(q), [q]); const handleOnBackButtonPress = () => Navigation.goBack(ROUTES.SEARCH_CENTRAL_PANE.getRoute({query: SearchUtils.buildCannedSearchQuery()})); @@ -39,12 +39,7 @@ function SearchPage({route}: SearchPageProps) { onBackButtonPress={handleOnBackButtonPress} shouldShowLink={false} > - {queryJSON && ( - - )} + {queryJSON && } ); diff --git a/src/pages/Search/SearchPageBottomTab.tsx b/src/pages/Search/SearchPageBottomTab.tsx index 8b03da81456e..9c46588bb68e 100644 --- a/src/pages/Search/SearchPageBottomTab.tsx +++ b/src/pages/Search/SearchPageBottomTab.tsx @@ -27,9 +27,9 @@ function SearchPageBottomTab() { const {clearSelectedTransactions} = useSearchContext(); const [selectionMode] = useOnyx(ONYXKEYS.MOBILE_SELECTION_MODE); - const {queryJSON, policyID, isCustomQuery} = useMemo(() => { + const {queryJSON, policyID} = useMemo(() => { if (activeCentralPaneRoute?.name !== SCREENS.SEARCH.CENTRAL_PANE) { - return {queryJSON: undefined, policyID: undefined, isCustomQuery: undefined}; + return {queryJSON: undefined, policyID: undefined}; } const searchParams = activeCentralPaneRoute?.params as AuthScreensParamList[typeof SCREENS.SEARCH.CENTRAL_PANE]; @@ -38,7 +38,6 @@ function SearchPageBottomTab() { return { queryJSON: parsedQuery, policyID: parsedQuery && SearchUtils.getPolicyIDFromSearchQuery(parsedQuery), - isCustomQuery: searchParams.isCustomQuery, }; }, [activeCentralPaneRoute]); @@ -62,10 +61,7 @@ function SearchPageBottomTab() { breadcrumbLabel={translate('common.search')} shouldDisplaySearch={false} /> - + ) : ( )} - {shouldUseNarrowLayout && queryJSON && ( - - )} + {shouldUseNarrowLayout && queryJSON && } ); diff --git a/src/pages/Search/SearchTypeMenu.tsx b/src/pages/Search/SearchTypeMenu.tsx index c434bf346592..810e7140060f 100644 --- a/src/pages/Search/SearchTypeMenu.tsx +++ b/src/pages/Search/SearchTypeMenu.tsx @@ -19,7 +19,6 @@ import SearchTypeMenuNarrow from './SearchTypeMenuNarrow'; type SearchTypeMenuProps = { queryJSON: SearchQueryJSON; - isCustomQuery: boolean; }; type SearchTypeMenuItem = { @@ -29,7 +28,7 @@ type SearchTypeMenuItem = { route?: Route; }; -function SearchTypeMenu({queryJSON, isCustomQuery}: SearchTypeMenuProps) { +function SearchTypeMenu({queryJSON}: SearchTypeMenuProps) { const {type} = queryJSON; const styles = useThemeStyles(); const {shouldUseNarrowLayout} = useResponsiveLayout(); @@ -56,10 +55,12 @@ function SearchTypeMenu({queryJSON, isCustomQuery}: SearchTypeMenuProps) { route: ROUTES.SEARCH_CENTRAL_PANE.getRoute({query: SearchUtils.buildCannedSearchQuery(CONST.SEARCH.DATA_TYPES.TRIP, CONST.SEARCH.STATUS.TRIP.ALL)}), }, ]; - const activeItemIndex = isCustomQuery ? -1 : typeMenuItems.findIndex((item) => item.type === type); + + const isCannedQuery = SearchUtils.isCannedSearchQuery(queryJSON); + const activeItemIndex = isCannedQuery ? typeMenuItems.findIndex((item) => item.type === type) : -1; if (shouldUseNarrowLayout) { - const title = isCustomQuery ? SearchUtils.getSearchHeaderTitle(queryJSON) : undefined; + const title = isCannedQuery ? undefined : SearchUtils.getSearchHeaderTitle(queryJSON); return (