Skip to content

Commit

Permalink
Merge pull request Expensify#48414 from software-mansion-labs/kicu/48…
Browse files Browse the repository at this point in the history
…331-remove-customquery

remove isCustomQuery param and add checking if query is canned
  • Loading branch information
luacmartins authored Sep 4, 2024
2 parents 6e500c2 + 90e3851 commit 8f0f140
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/ROUTES.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
14 changes: 8 additions & 6 deletions src/components/Search/SearchPageHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ type SearchPageHeaderProps = {
queryJSON: SearchQueryJSON;
hash: number;
onSelectDeleteOption?: (itemsToDelete: string[]) => void;
isCustomQuery: boolean;
setOfflineModalOpen?: () => void;
setDownloadErrorModalOpen?: () => void;
data?: TransactionListItemType[] | ReportListItemType[];
Expand All @@ -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();
Expand All @@ -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) {
Expand Down
6 changes: 1 addition & 5 deletions src/components/Search/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import type {SearchColumnType, SearchQueryJSON, SearchStatus, SelectedTransactio

type SearchProps = {
queryJSON: SearchQueryJSON;
isCustomQuery: boolean;
};

const transactionItemMobileHeight = 100;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -197,7 +196,6 @@ function Search({queryJSON, isCustomQuery}: SearchProps) {
return (
<>
<SearchPageHeader
isCustomQuery={isCustomQuery}
queryJSON={queryJSON}
hash={hash}
/>
Expand Down Expand Up @@ -232,7 +230,6 @@ function Search({queryJSON, isCustomQuery}: SearchProps) {
return (
<>
<SearchPageHeader
isCustomQuery={isCustomQuery}
queryJSON={queryJSON}
hash={hash}
/>
Expand Down Expand Up @@ -325,7 +322,6 @@ function Search({queryJSON, isCustomQuery}: SearchProps) {
return (
<>
<SearchPageHeader
isCustomQuery={isCustomQuery}
queryJSON={queryJSON}
hash={hash}
onSelectDeleteOption={handleOnSelectDeleteOption}
Expand Down
2 changes: 1 addition & 1 deletion src/libs/Navigation/AppNavigator/AuthScreens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function shouldOpenOnAdminRoom() {
function getCentralPaneScreenInitialParams(screenName: CentralPaneName, initialReportID?: string): Partial<ValueOf<CentralPaneScreensParamList>> {
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) {
Expand Down
3 changes: 0 additions & 3 deletions src/libs/Navigation/linkingConfig/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ const config: LinkingOptions<RootStackParamList>['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,
Expand Down
2 changes: 0 additions & 2 deletions src/libs/Navigation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions src/libs/SearchUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,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,
Expand All @@ -592,6 +602,7 @@ export {
normalizeQuery,
shouldShowYear,
buildCannedSearchQuery,
isCannedSearchQuery,
getExpenseTypeTranslationKey,
getChatFiltersTranslationKey,
};
1 change: 0 additions & 1 deletion src/pages/Search/AdvancedSearchFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@ function AdvancedSearchFilters() {
Navigation.navigate(
ROUTES.SEARCH_CENTRAL_PANE.getRoute({
query,
isCustomQuery: true,
}),
);
};
Expand Down
9 changes: 2 additions & 7 deletions src/pages/Search/SearchPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type SearchPageProps = StackScreenProps<AuthScreensParamList, typeof SCREENS.SEA
function SearchPage({route}: SearchPageProps) {
const {shouldUseNarrowLayout} = useResponsiveLayout();
const styles = useThemeStyles();
const {q, isCustomQuery} = route.params;
const {q} = route.params;

const queryJSON = useMemo(() => SearchUtils.buildSearchQueryJSON(q), [q]);
const handleOnBackButtonPress = () => Navigation.goBack(ROUTES.SEARCH_CENTRAL_PANE.getRoute({query: SearchUtils.buildCannedSearchQuery()}));
Expand All @@ -39,12 +39,7 @@ function SearchPage({route}: SearchPageProps) {
onBackButtonPress={handleOnBackButtonPress}
shouldShowLink={false}
>
{queryJSON && (
<Search
isCustomQuery={isCustomQuery}
queryJSON={queryJSON}
/>
)}
{queryJSON && <Search queryJSON={queryJSON} />}
</FullPageNotFoundView>
</ScreenWrapper>
);
Expand Down
17 changes: 4 additions & 13 deletions src/pages/Search/SearchPageBottomTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -38,7 +38,6 @@ function SearchPageBottomTab() {
return {
queryJSON: parsedQuery,
policyID: parsedQuery && SearchUtils.getPolicyIDFromSearchQuery(parsedQuery),
isCustomQuery: searchParams.isCustomQuery,
};
}, [activeCentralPaneRoute]);

Expand All @@ -62,10 +61,7 @@ function SearchPageBottomTab() {
breadcrumbLabel={translate('common.search')}
shouldDisplaySearch={false}
/>
<SearchTypeMenu
isCustomQuery={isCustomQuery}
queryJSON={queryJSON}
/>
<SearchTypeMenu queryJSON={queryJSON} />
</>
) : (
<HeaderWithBackButton
Expand All @@ -76,12 +72,7 @@ function SearchPageBottomTab() {
}}
/>
)}
{shouldUseNarrowLayout && queryJSON && (
<Search
queryJSON={queryJSON}
isCustomQuery={isCustomQuery}
/>
)}
{shouldUseNarrowLayout && queryJSON && <Search queryJSON={queryJSON} />}
</FullPageNotFoundView>
</ScreenWrapper>
);
Expand Down
9 changes: 5 additions & 4 deletions src/pages/Search/SearchTypeMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import SearchTypeMenuNarrow from './SearchTypeMenuNarrow';

type SearchTypeMenuProps = {
queryJSON: SearchQueryJSON;
isCustomQuery: boolean;
};

type SearchTypeMenuItem = {
Expand All @@ -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();
Expand All @@ -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 (
<SearchTypeMenuNarrow
Expand Down

0 comments on commit 8f0f140

Please sign in to comment.