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 all 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 @@ -6102,6 +6102,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 @@ -160,8 +160,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 @@ -179,11 +179,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
41 changes: 26 additions & 15 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;
mapKey?: SearchFilterKey;
};

type SearchRouterListProps = {
Expand Down Expand Up @@ -82,6 +83,10 @@ function isSearchQueryListItem(listItem: UserListItemProps<OptionData> | SearchQ
return isSearchQueryItem(listItem.item);
}

function getAutocompleteDisplayText(filterKey: UserFriendlyKey, value: string) {
return `${filterKey}:${value}`;
}

function getItemHeight(item: OptionData | SearchQueryItem) {
if (isSearchQueryItem(item)) {
return 44;
Expand Down Expand Up @@ -223,7 +228,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 +240,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 +252,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 +263,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(','),
mapKey: CONST.SEARCH.SYNTAX_FILTER_KEYS.TAX_RATE,
}));
}
case CONST.SEARCH.SYNTAX_FILTER_KEYS.FROM: {
Expand All @@ -269,9 +275,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,
mapKey: CONST.SEARCH.SYNTAX_FILTER_KEYS.FROM,
}));
}
case CONST.SEARCH.SYNTAX_FILTER_KEYS.TO: {
Expand All @@ -280,9 +287,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,
mapKey: CONST.SEARCH.SYNTAX_FILTER_KEYS.TO,
}));
}
case CONST.SEARCH.SYNTAX_FILTER_KEYS.IN: {
Expand All @@ -291,33 +299,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,
mapKey: 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 +340,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(),
mapKey: CONST.SEARCH.SYNTAX_FILTER_KEYS.CARD_ID,
}));
}
default: {
Expand Down Expand Up @@ -411,9 +421,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, mapKey}) => {
return {
text: getSubstitutionMapKey(filterKey, text),
text: getAutocompleteDisplayText(filterKey, text),
mapKey: mapKey ? getSubstitutionMapKey(mapKey, text) : undefined,
singleIcon: Expensicons.MagnifyingGlass,
searchQuery: text,
autocompleteID,
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