From 2dd0e87fe2b97bacd6d5a35ce64ff62bd849adce Mon Sep 17 00:00:00 2001 From: 289Adam289 Date: Thu, 26 Sep 2024 16:54:50 +0200 Subject: [PATCH] add display name replacement --- .../Search/SearchRouter/SearchRouter.tsx | 14 ++++- src/libs/SearchUtils.ts | 59 +++++++++++++++++++ 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/src/components/Search/SearchRouter/SearchRouter.tsx b/src/components/Search/SearchRouter/SearchRouter.tsx index dfe2cbbe16c6..d9e8b8bd37df 100644 --- a/src/components/Search/SearchRouter/SearchRouter.tsx +++ b/src/components/Search/SearchRouter/SearchRouter.tsx @@ -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'; @@ -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(undefined); const clearUserQuery = () => { @@ -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(); diff --git a/src/libs/SearchUtils.ts b/src/libs/SearchUtils.ts index e178c7dcb77b..13cad8edc401 100644 --- a/src/libs/SearchUtils.ts +++ b/src/libs/SearchUtils.ts @@ -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'; @@ -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, taxRates: Record) { + 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, taxRates: Record) { + 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. * @@ -839,4 +897,5 @@ export { getExpenseTypeTranslationKey, getOverflowMenu, isCorrectSearchUserName, + standardizeQueryJSON, };