From eb896c68d2d36fccb0fb7a5ecef5360f62bd000b Mon Sep 17 00:00:00 2001 From: 289Adam289 Date: Mon, 9 Dec 2024 15:55:14 +0100 Subject: [PATCH] fix substitution map to work on old keys --- src/components/Search/SearchPageHeaderInput.tsx | 8 ++++---- src/components/Search/SearchRouter/SearchRouter.tsx | 8 ++++---- .../Search/SearchRouter/SearchRouterList.tsx | 11 +++++++++-- .../Search/SearchRouter/getQueryWithSubstitutions.ts | 4 +--- .../SelectionList/Search/SearchQueryListItem.tsx | 1 + 5 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/components/Search/SearchPageHeaderInput.tsx b/src/components/Search/SearchPageHeaderInput.tsx index 9394d491244d..f725d0f56267 100644 --- a/src/components/Search/SearchPageHeaderInput.tsx +++ b/src/components/Search/SearchPageHeaderInput.tsx @@ -156,8 +156,8 @@ function SearchPageHeaderInput({queryJSON, children}: SearchPageHeaderInputProps const trimmedUserSearchQuery = SearchAutocompleteUtils.getQueryWithoutAutocompletedPart(textInputValue); onSearchQueryChange(`${trimmedUserSearchQuery}${SearchQueryUtils.sanitizeSearchValue(item.searchQuery)} `); - if (item.text && item.autocompleteID) { - const substitutions = {...autocompleteSubstitutions, [item.text]: item.autocompleteID}; + if (item.mapKey && item.autocompleteID) { + const substitutions = {...autocompleteSubstitutions, [item.mapKey]: item.autocompleteID}; setAutocompleteSubstitutions(substitutions); } @@ -175,11 +175,11 @@ function SearchPageHeaderInput({queryJSON, children}: SearchPageHeaderInputProps const updateAutocompleteSubstitutions = useCallback( (item: SearchQueryItem) => { - if (!item.autocompleteID || !item.text) { + if (!item.autocompleteID || !item.mapKey) { return; } - const substitutions = {...autocompleteSubstitutions, [item.text]: item.autocompleteID}; + const substitutions = {...autocompleteSubstitutions, [item.mapKey]: item.autocompleteID}; setAutocompleteSubstitutions(substitutions); }, [autocompleteSubstitutions], diff --git a/src/components/Search/SearchRouter/SearchRouter.tsx b/src/components/Search/SearchRouter/SearchRouter.tsx index 7d7c81539c4d..4b800b637712 100644 --- a/src/components/Search/SearchRouter/SearchRouter.tsx +++ b/src/components/Search/SearchRouter/SearchRouter.tsx @@ -223,8 +223,8 @@ function SearchRouter({onRouterClose, shouldHideInputCaret}: SearchRouterProps) const trimmedUserSearchQuery = SearchAutocompleteUtils.getQueryWithoutAutocompletedPart(textInputValue); onSearchQueryChange(`${trimmedUserSearchQuery}${SearchQueryUtils.sanitizeSearchValue(item.searchQuery)} `); - if (item.text && item.autocompleteID) { - const substitutions = {...autocompleteSubstitutions, [item.text]: item.autocompleteID}; + if (item.mapKey && item.autocompleteID) { + const substitutions = {...autocompleteSubstitutions, [item.mapKey]: item.autocompleteID}; setAutocompleteSubstitutions(substitutions); } @@ -248,11 +248,11 @@ function SearchRouter({onRouterClose, shouldHideInputCaret}: SearchRouterProps) const updateAutocompleteSubstitutions = useCallback( (item: SearchQueryItem) => { - if (!item.autocompleteID || !item.text) { + if (!item.autocompleteID || !item.mapKey) { return; } - const substitutions = {...autocompleteSubstitutions, [item.text]: item.autocompleteID}; + const substitutions = {...autocompleteSubstitutions, [item.mapKey]: item.autocompleteID}; setAutocompleteSubstitutions(substitutions); }, [autocompleteSubstitutions], diff --git a/src/components/Search/SearchRouter/SearchRouterList.tsx b/src/components/Search/SearchRouter/SearchRouterList.tsx index fa3f25be8704..8ace6d3b90ad 100644 --- a/src/components/Search/SearchRouter/SearchRouterList.tsx +++ b/src/components/Search/SearchRouter/SearchRouterList.tsx @@ -5,7 +5,7 @@ import {useOnyx} from 'react-native-onyx'; import * as Expensicons from '@components/Icon/Expensicons'; import {usePersonalDetails} from '@components/OnyxProvider'; import {useOptionsList} from '@components/OptionListContextProvider'; -import type {UserFriendlyKey} from '@components/Search/types'; +import type {SearchFilterKey, UserFriendlyKey} from '@components/Search/types'; import SelectionList from '@components/SelectionList'; import SearchQueryListItem, {isSearchQueryItem} from '@components/SelectionList/Search/SearchQueryListItem'; import type {SearchQueryItem, SearchQueryListItemProps} from '@components/SelectionList/Search/SearchQueryListItem'; @@ -43,6 +43,7 @@ type AutocompleteItemData = { filterKey: UserFriendlyKey; text: string; autocompleteID?: string; + substitutionMapKey?: SearchFilterKey; }; type SearchRouterListProps = { @@ -261,6 +262,7 @@ function SearchRouterList( filterKey: CONST.SEARCH.SEARCH_USER_FRIENDLY_KEYS.TAX_RATE, text: tax.taxRateName, autocompleteID: tax.taxRateIds.join(','), + substitutionMapKey: CONST.SEARCH.SYNTAX_FILTER_KEYS.TAX_RATE, })); } case CONST.SEARCH.SYNTAX_FILTER_KEYS.FROM: { @@ -272,6 +274,7 @@ function SearchRouterList( filterKey: CONST.SEARCH.SEARCH_USER_FRIENDLY_KEYS.FROM, text: participant.name, autocompleteID: participant.accountID, + substitutionMapKey: CONST.SEARCH.SYNTAX_FILTER_KEYS.FROM, })); } case CONST.SEARCH.SYNTAX_FILTER_KEYS.TO: { @@ -283,6 +286,7 @@ function SearchRouterList( filterKey: CONST.SEARCH.SEARCH_USER_FRIENDLY_KEYS.TO, text: participant.name, autocompleteID: participant.accountID, + substitutionMapKey: CONST.SEARCH.SYNTAX_FILTER_KEYS.TO, })); } case CONST.SEARCH.SYNTAX_FILTER_KEYS.IN: { @@ -294,6 +298,7 @@ function SearchRouterList( filterKey: CONST.SEARCH.SEARCH_USER_FRIENDLY_KEYS.IN, text: chat.text ?? '', autocompleteID: chat.reportID, + substitutionMapKey: CONST.SEARCH.SYNTAX_FILTER_KEYS.IN, })); } case CONST.SEARCH.SYNTAX_ROOT_KEYS.TYPE: { @@ -334,6 +339,7 @@ function SearchRouterList( filterKey: CONST.SEARCH.SEARCH_USER_FRIENDLY_KEYS.CARD_ID, text: CardUtils.getCardDescription(card.cardID), autocompleteID: card.cardID.toString(), + substitutionMapKey: CONST.SEARCH.SYNTAX_FILTER_KEYS.CARD_ID, })); } default: { @@ -411,9 +417,10 @@ function SearchRouterList( sections.push({title: translate('search.recentChats'), data: styledRecentReports}); if (autocompleteSuggestions.length > 0) { - const autocompleteData = autocompleteSuggestions.map(({filterKey, text, autocompleteID}) => { + const autocompleteData = autocompleteSuggestions.map(({filterKey, text, autocompleteID, substitutionMapKey}) => { return { text: getSubstitutionMapKey(filterKey, text), + mapKey: substitutionMapKey ? getSubstitutionMapKey(substitutionMapKey, text) : undefined, singleIcon: Expensicons.MagnifyingGlass, searchQuery: text, autocompleteID, diff --git a/src/components/Search/SearchRouter/getQueryWithSubstitutions.ts b/src/components/Search/SearchRouter/getQueryWithSubstitutions.ts index 5a39159f52ff..66e01f5c1ada 100644 --- a/src/components/Search/SearchRouter/getQueryWithSubstitutions.ts +++ b/src/components/Search/SearchRouter/getQueryWithSubstitutions.ts @@ -1,6 +1,5 @@ import type {SearchAutocompleteQueryRange, SearchFilterKey, UserFriendlyKey} from '@components/Search/types'; import * as parser from '@libs/SearchParser/autocompleteParser'; -import {getUserFriendlyKey} from '@libs/SearchQueryUtils'; type SubstitutionMap = Record; @@ -32,8 +31,7 @@ function getQueryWithSubstitutions(changedQuery: string, substitutions: Substitu for (const range of searchAutocompleteQueryRanges) { const itemKey = getSubstitutionMapKey(range.key, range.value); - const alternativeItemKey = getSubstitutionMapKey(getUserFriendlyKey(range.key), range.value); - const substitutionEntry = substitutions[itemKey] ?? substitutions[alternativeItemKey]; + const substitutionEntry = substitutions[itemKey]; if (substitutionEntry) { const substitutionStart = range.start + lengthDiff; diff --git a/src/components/SelectionList/Search/SearchQueryListItem.tsx b/src/components/SelectionList/Search/SearchQueryListItem.tsx index b5631e03c3d7..770bad4faa31 100644 --- a/src/components/SelectionList/Search/SearchQueryListItem.tsx +++ b/src/components/SelectionList/Search/SearchQueryListItem.tsx @@ -17,6 +17,7 @@ type SearchQueryItem = ListItem & { searchQuery?: string; autocompleteID?: string; roomType?: ValueOf; + mapKey?: string; }; type SearchQueryListItemProps = {