Skip to content

Commit

Permalink
add display name replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
289Adam289 committed Sep 26, 2024
1 parent 2d0d4e9 commit 2dd0e87
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/components/Search/SearchRouter/SearchRouter.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import debounce from 'lodash/debounce';
import React, {useCallback, useState} from 'react';
import {View} from 'react-native';
import {useOnyx} from 'react-native-onyx';
import FocusTrapForModal from '@components/FocusTrap/FocusTrapForModal';
import Modal from '@components/Modal';
import type {SearchQueryJSON} from '@components/Search/types';
import useKeyboardShortcut from '@hooks/useKeyboardShortcut';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useThemeStyles from '@hooks/useThemeStyles';
import {getAllTaxRates} from '@libs/PolicyUtils';
import * as SearchUtils from '@libs/SearchUtils';
import Navigation from '@navigation/Navigation';
import * as SearchActions from '@userActions/Search';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import {useSearchRouterContext} from './SearchRouterContext';
import SearchRouterInput from './SearchRouterInput';
Expand All @@ -22,6 +26,10 @@ function SearchRouter() {
const {isSmallScreenWidth} = useResponsiveLayout();
const {isSearchRouterDisplayed, closeSearchRouter} = useSearchRouterContext();

const [reports] = useOnyx(ONYXKEYS.COLLECTION.REPORT);
const taxRates = getAllTaxRates();
const [cardList = {}] = useOnyx(ONYXKEYS.CARD_LIST);

const [userSearchQuery, setUserSearchQuery] = useState<SearchQueryJSON | undefined>(undefined);

const clearUserQuery = () => {
Expand Down Expand Up @@ -53,11 +61,13 @@ function SearchRouter() {

closeSearchRouter();

const query = SearchUtils.buildSearchQueryString(userSearchQuery);
const standardQuery = SearchUtils.standardizeQueryJSON(userSearchQuery, cardList, reports, taxRates);
const query = SearchUtils.buildSearchQueryString(standardQuery);
SearchActions.clearAllFilters();
Navigation.navigate(ROUTES.SEARCH_CENTRAL_PANE.getRoute({query}));

clearUserQuery();
}, [closeSearchRouter, userSearchQuery]);
}, [closeSearchRouter, userSearchQuery, cardList, reports, taxRates]);

useKeyboardShortcut(CONST.KEYBOARD_SHORTCUTS.ESCAPE, () => {
closeSearchRouter();
Expand Down
59 changes: 59 additions & 0 deletions src/libs/SearchUtils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import cloneDeep from 'lodash/cloneDeep';
import type {OnyxCollection} from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import type {ASTNode, QueryFilter, QueryFilters, SearchColumnType, SearchQueryJSON, SearchQueryString, SearchStatus, SortOrder} from '@components/Search/types';
Expand Down Expand Up @@ -803,6 +804,63 @@ function getOverflowMenu(itemName: string, hash: number, inputQuery: string, sho
];
}

function getIDFromDisplayValue(filterName: string, filter: string | string[], cardList: OnyxTypes.CardList, reports: OnyxCollection<OnyxTypes.Report>, taxRates: Record<string, string[]>) {
if (filterName === CONST.SEARCH.SYNTAX_FILTER_KEYS.FROM || filterName === CONST.SEARCH.SYNTAX_FILTER_KEYS.TO) {
if (typeof filter === 'string') {
const email = filter;
return PersonalDetailsUtils.getPersonalDetailByEmail(email)?.accountID.toString() ?? filter;
}
const emails = filter;
return emails.map((email) => PersonalDetailsUtils.getPersonalDetailByEmail(email)?.accountID.toString() ?? email);
}
if (filterName === CONST.SEARCH.SYNTAX_FILTER_KEYS.TAX_RATE) {
const names = Array.isArray(filter) ? filter : ([filter] as string[]);
return names.map((name) => taxRates[name] ?? name).flat();
}
if (filterName === CONST.SEARCH.SYNTAX_FILTER_KEYS.CARD_ID) {
if (typeof filter === 'string') {
const bank = filter;
return Object.values(cardList)
.filter((card) => card.bank === bank)
.map((card) => card.cardID.toString());
}
const banks = filter;
return Object.values(cardList)
.filter((card) => banks.includes(card.bank))
.map((card) => card.cardID.toString());
}
if (filterName === CONST.SEARCH.SYNTAX_FILTER_KEYS.IN) {
const names = Array.isArray(filter) ? filter : ([filter] as string[]);

return Object.values(reports ?? {})
.filter((report) => names.includes(ReportUtils.getReportName(report)))
.map((report) => report?.reportID.toString() ?? '');
}
return filter;
}

function standardizeQueryJSON(queryJSON: SearchQueryJSON, cardList: OnyxTypes.CardList, reports: OnyxCollection<OnyxTypes.Report>, taxRates: Record<string, string[]>) {
const standardQuery = cloneDeep(queryJSON);
const filters = standardQuery.filters;
const traverse = (node: ASTNode) => {
if (!node.operator) {
return;
}
if (typeof node.left === 'object' && node.left) {
traverse(node.left);
}
if (typeof node.right === 'object' && node.right && !Array.isArray(node.right)) {
traverse(node.right);
}
// eslint-disable-next-line no-param-reassign
node.right = getIDFromDisplayValue(node.left as string, node.right as string | string[], cardList, reports, taxRates);
};

traverse(filters);
standardQuery.flatFilters = getFilters(standardQuery);
return standardQuery;
}

/**
* Returns whether a given search query is a Canned query.
*
Expand Down Expand Up @@ -839,4 +897,5 @@ export {
getExpenseTypeTranslationKey,
getOverflowMenu,
isCorrectSearchUserName,
standardizeQueryJSON,
};

0 comments on commit 2dd0e87

Please sign in to comment.