Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update grammar filters #53688

Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6092,6 +6092,33 @@ const CONST = {
AUTOCOMPLETE_SUGGESTION: 'autocompleteSuggestion',
SEARCH: 'searchItem',
},
SEARCH_USER_FRIENDLY_KEYS: {
TYPE: 'type',
STATUS: 'status',
SORT_BY: 'sort-by',
SORT_ORDER: 'sort-order',
POLICY_ID: 'workspace',
DATE: 'date',
AMOUNT: 'amount',
EXPENSE_TYPE: 'expense-type',
CURRENCY: 'currency',
MERCHANT: 'merchant',
DESCRIPTION: 'description',
FROM: 'from',
TO: 'to',
CATEGORY: 'category',
TAG: 'tag',
TAX_RATE: 'tax-rate',
CARD_ID: 'card',
REPORT_ID: 'reportid',
KEYWORD: 'keyword',
IN: 'in',
SUBMITTED: 'submitted',
APPROVED: 'approved',
PAID: 'paid',
EXPORTED: 'exported',
POSTED: 'posted',
},
DATE_MODIFIERS: {
BEFORE: 'Before',
AFTER: 'After',
Expand Down
8 changes: 4 additions & 4 deletions src/components/Search/SearchPageHeaderInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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],
Expand Down
16 changes: 8 additions & 8 deletions src/components/Search/SearchRouter/SearchRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,20 @@ function getContextualSearchAutocompleteKey(item: SearchQueryItem) {
}

function getContextualSearchQuery(item: SearchQueryItem) {
const baseQuery = `${CONST.SEARCH.SYNTAX_ROOT_KEYS.TYPE}:${item.roomType}`;
const baseQuery = `${CONST.SEARCH.SEARCH_USER_FRIENDLY_KEYS.TYPE}:${item.roomType}`;
let additionalQuery = '';

switch (item.roomType) {
case CONST.SEARCH.DATA_TYPES.EXPENSE:
case CONST.SEARCH.DATA_TYPES.INVOICE:
additionalQuery += ` ${CONST.SEARCH.SYNTAX_ROOT_KEYS.POLICY_ID}:${item.policyID}`;
additionalQuery += ` ${CONST.SEARCH.SEARCH_USER_FRIENDLY_KEYS.POLICY_ID}:${item.policyID}`;
if (item.roomType === CONST.SEARCH.DATA_TYPES.INVOICE && item.autocompleteID) {
additionalQuery += ` ${CONST.SEARCH.SYNTAX_FILTER_KEYS.TO}:${SearchQueryUtils.sanitizeSearchValue(item.searchQuery ?? '')}`;
additionalQuery += ` ${CONST.SEARCH.SEARCH_USER_FRIENDLY_KEYS.TO}:${SearchQueryUtils.sanitizeSearchValue(item.searchQuery ?? '')}`;
}
break;
case CONST.SEARCH.DATA_TYPES.CHAT:
default:
additionalQuery = ` ${CONST.SEARCH.SYNTAX_FILTER_KEYS.IN}:${SearchQueryUtils.sanitizeSearchValue(item.searchQuery ?? '')}`;
additionalQuery = ` ${CONST.SEARCH.SEARCH_USER_FRIENDLY_KEYS.IN}:${SearchQueryUtils.sanitizeSearchValue(item.searchQuery ?? '')}`;
break;
}
return baseQuery + additionalQuery;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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],
Expand Down
35 changes: 21 additions & 14 deletions src/components/Search/SearchRouter/SearchRouterList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {SearchFilterKey} 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';
Expand Down Expand Up @@ -40,9 +40,10 @@ import type PersonalDetails from '@src/types/onyx/PersonalDetails';
import {getSubstitutionMapKey} from './getQueryWithSubstitutions';

type AutocompleteItemData = {
filterKey: SearchFilterKey;
filterKey: UserFriendlyKey;
text: string;
autocompleteID?: string;
substitutionMapKey?: SearchFilterKey;
};

type SearchRouterListProps = {
Expand Down Expand Up @@ -223,7 +224,7 @@ function SearchRouterList(
.slice(0, 10);

return filteredTags.map((tagName) => ({
filterKey: CONST.SEARCH.SYNTAX_FILTER_KEYS.TAG,
filterKey: CONST.SEARCH.SEARCH_USER_FRIENDLY_KEYS.TAG,
text: tagName,
}));
}
Expand All @@ -235,7 +236,7 @@ function SearchRouterList(
.slice(0, 10);

return filteredCategories.map((categoryName) => ({
filterKey: CONST.SEARCH.SYNTAX_FILTER_KEYS.CATEGORY,
filterKey: CONST.SEARCH.SEARCH_USER_FRIENDLY_KEYS.CATEGORY,
text: categoryName,
}));
}
Expand All @@ -247,7 +248,7 @@ function SearchRouterList(
.slice(0, 10);

return filteredCurrencies.map((currencyName) => ({
filterKey: CONST.SEARCH.SYNTAX_FILTER_KEYS.CURRENCY,
filterKey: CONST.SEARCH.SEARCH_USER_FRIENDLY_KEYS.CURRENCY,
text: currencyName,
}));
}
Expand All @@ -258,9 +259,10 @@ function SearchRouterList(
.slice(0, 10);

return filteredTaxRates.map((tax) => ({
filterKey: CONST.SEARCH.SYNTAX_FILTER_KEYS.TAX_RATE,
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: {
Expand All @@ -269,9 +271,10 @@ function SearchRouterList(
.slice(0, 10);

return filteredParticipants.map((participant) => ({
filterKey: CONST.SEARCH.SYNTAX_FILTER_KEYS.FROM,
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: {
Expand All @@ -280,9 +283,10 @@ function SearchRouterList(
.slice(0, 10);

return filteredParticipants.map((participant) => ({
filterKey: CONST.SEARCH.SYNTAX_FILTER_KEYS.TO,
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: {
Expand All @@ -291,33 +295,34 @@ function SearchRouterList(
.slice(0, 10);

return filteredChats.map((chat) => ({
filterKey: CONST.SEARCH.SYNTAX_FILTER_KEYS.IN,
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: {
const filteredTypes = typeAutocompleteList
.filter((type) => type.toLowerCase().includes(autocompleteValue.toLowerCase()) && !alreadyAutocompletedKeys.includes(type.toLowerCase()))
.sort();

return filteredTypes.map((type) => ({filterKey: CONST.SEARCH.SYNTAX_ROOT_KEYS.TYPE, text: type}));
return filteredTypes.map((type) => ({filterKey: CONST.SEARCH.SEARCH_USER_FRIENDLY_KEYS.TYPE, text: type}));
}
case CONST.SEARCH.SYNTAX_ROOT_KEYS.STATUS: {
const filteredStatuses = statusAutocompleteList
.filter((status) => status.includes(autocompleteValue.toLowerCase()) && !alreadyAutocompletedKeys.includes(status))
.sort()
.slice(0, 10);

return filteredStatuses.map((status) => ({filterKey: CONST.SEARCH.SYNTAX_ROOT_KEYS.STATUS, text: status}));
return filteredStatuses.map((status) => ({filterKey: CONST.SEARCH.SEARCH_USER_FRIENDLY_KEYS.STATUS, text: status}));
}
case CONST.SEARCH.SYNTAX_FILTER_KEYS.EXPENSE_TYPE: {
const filteredExpenseTypes = expenseTypes
.filter((expenseType) => expenseType.includes(autocompleteValue.toLowerCase()) && !alreadyAutocompletedKeys.includes(expenseType))
.sort();

return filteredExpenseTypes.map((expenseType) => ({
filterKey: CONST.SEARCH.SYNTAX_FILTER_KEYS.EXPENSE_TYPE,
filterKey: CONST.SEARCH.SEARCH_USER_FRIENDLY_KEYS.EXPENSE_TYPE,
text: expenseType,
}));
}
Expand All @@ -331,9 +336,10 @@ function SearchRouterList(
.slice(0, 10);

return filteredCards.map((card) => ({
filterKey: CONST.SEARCH.SYNTAX_FILTER_KEYS.CARD_ID,
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: {
Expand Down Expand Up @@ -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,
289Adam289 marked this conversation as resolved.
Show resolved Hide resolved
singleIcon: Expensicons.MagnifyingGlass,
searchQuery: text,
autocompleteID,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type {SearchAutocompleteQueryRange, SearchFilterKey} from '@components/Search/types';
import type {SearchAutocompleteQueryRange, SearchFilterKey, UserFriendlyKey} from '@components/Search/types';
import * as parser from '@libs/SearchParser/autocompleteParser';

type SubstitutionMap = Record<string, string>;

const getSubstitutionMapKey = (filterKey: SearchFilterKey, value: string) => `${filterKey}:${value}`;
const getSubstitutionMapKey = (filterKey: SearchFilterKey | UserFriendlyKey, value: string) => `${filterKey}:${value}`;

289Adam289 marked this conversation as resolved.
Show resolved Hide resolved
/**
* Given a plaintext query and a SubstitutionMap object, this function will return a transformed query where:
Expand Down
3 changes: 3 additions & 0 deletions src/components/Search/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ type SearchFilterKey =
| typeof CONST.SEARCH.SYNTAX_ROOT_KEYS.STATUS
| typeof CONST.SEARCH.SYNTAX_ROOT_KEYS.POLICY_ID;

type UserFriendlyKey = ValueOf<typeof CONST.SEARCH.SEARCH_USER_FRIENDLY_KEYS>;

type QueryFilters = Array<{
key: SearchFilterKey;
filters: QueryFilter[];
Expand Down Expand Up @@ -148,6 +150,7 @@ export type {
QueryFilter,
QueryFilters,
SearchFilterKey,
UserFriendlyKey,
ExpenseSearchStatus,
InvoiceSearchStatus,
TripSearchStatus,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type SearchQueryItem = ListItem & {
searchQuery?: string;
autocompleteID?: string;
roomType?: ValueOf<typeof CONST.SEARCH.DATA_TYPES>;
mapKey?: string;
};

type SearchQueryListItemProps = {
Expand Down
Loading
Loading