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

search router polish #50883

Merged
Show file tree
Hide file tree
Changes from 15 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
7 changes: 4 additions & 3 deletions src/components/Search/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ type QueryFilter = {

type AdvancedFiltersKeys = ValueOf<typeof CONST.SEARCH.SYNTAX_FILTER_KEYS>;

type QueryFilters = {
[K in AdvancedFiltersKeys]?: QueryFilter[];
};
type QueryFilters = Array<{
key: AdvancedFiltersKeys;
filters: QueryFilter[];
}>;

type SearchQueryString = string;

Expand Down
4 changes: 2 additions & 2 deletions src/libs/SearchParser/autocompleteParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ function peg$parse(input, options) {

var peg$r0 = /^[:=]/;
var peg$r1 = /^[^"\r\n]/;
var peg$r2 = /^[A-Za-z0-9_@.\/#&+\-\\',;%]/;
var peg$r2 = /^[^ "\t\n\r]/;
var peg$r3 = /^[ \t\r\n]/;

var peg$e0 = peg$otherExpectation("key");
Expand All @@ -219,7 +219,7 @@ function peg$parse(input, options) {
var peg$e19 = peg$literalExpectation("\"", false);
var peg$e20 = peg$classExpectation(["\"", "\r", "\n"], true, false);
var peg$e21 = peg$otherExpectation("word");
var peg$e22 = peg$classExpectation([["A", "Z"], ["a", "z"], ["0", "9"], "_", "@", ".", "/", "#", "&", "+", "-", "\\", "'", ",", ";", "%"], false, false);
var peg$e22 = peg$classExpectation([" ", "\"", "\t", "\n", "\r"], true, false);
var peg$e23 = peg$otherExpectation("whitespace");
var peg$e24 = peg$classExpectation([" ", "\t", "\r", "\n"], false, false);

Expand Down
4 changes: 2 additions & 2 deletions src/libs/SearchParser/baseRules.peggy
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ operator "operator"
quotedString "quote" = "\"" chars:[^"\r\n]* "\"" { return chars.join(""); }

alphanumeric "word"
= chars:[A-Za-z0-9_@./#&+\-\\',;%]+ {
= chars:[^ "\t\n\r]+ {
return chars.join("").trim().split(",").filter(Boolean);
}

logicalAnd = _ { return "and"; }

_ "whitespace" = [ \t\r\n]*
_ "whitespace" = [ \t\r\n]*
289Adam289 marked this conversation as resolved.
Show resolved Hide resolved
13 changes: 2 additions & 11 deletions src/libs/SearchParser/searchParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ function peg$parse(input, options) {

var peg$r0 = /^[:=]/;
var peg$r1 = /^[^"\r\n]/;
var peg$r2 = /^[A-Za-z0-9_@.\/#&+\-\\',;%]/;
var peg$r2 = /^[^ "\t\n\r]/;
var peg$r3 = /^[ \t\r\n]/;

var peg$e0 = peg$otherExpectation("key");
Expand Down Expand Up @@ -245,7 +245,7 @@ function peg$parse(input, options) {
var peg$e30 = peg$literalExpectation("\"", false);
var peg$e31 = peg$classExpectation(["\"", "\r", "\n"], true, false);
var peg$e32 = peg$otherExpectation("word");
var peg$e33 = peg$classExpectation([["A", "Z"], ["a", "z"], ["0", "9"], "_", "@", ".", "/", "#", "&", "+", "-", "\\", "'", ",", ";", "%"], false, false);
var peg$e33 = peg$classExpectation([" ", "\"", "\t", "\n", "\r"], true, false);
var peg$e34 = peg$otherExpectation("whitespace");
var peg$e35 = peg$classExpectation([" ", "\t", "\r", "\n"], false, false);

Expand Down Expand Up @@ -747,15 +747,6 @@ function peg$parse(input, options) {
s1 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e15); }
}
if (s1 === peg$FAILED) {
if (input.substr(peg$currPos, 2) === peg$c6) {
s1 = peg$c6;
peg$currPos += 2;
} else {
s1 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e7); }
}
}
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/libs/SearchParser/searchParser.peggy
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ key "key"
/ "cardID"
/ "from"
/ "expenseType"
/ "in"
)

defaultKey "default key"
Expand Down
115 changes: 65 additions & 50 deletions src/libs/SearchUtils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import cloneDeep from 'lodash/cloneDeep';
import type {OnyxCollection} from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import type {AdvancedFiltersKeys, ASTNode, QueryFilter, QueryFilters, SearchColumnType, SearchQueryJSON, SearchQueryString, SearchStatus, SortOrder} from '@components/Search/types';
import type {ASTNode, QueryFilter, QueryFilters, SearchColumnType, SearchQueryJSON, SearchQueryString, SearchStatus, SortOrder} from '@components/Search/types';
import ChatListItem from '@components/SelectionList/ChatListItem';
import ReportListItem from '@components/SelectionList/Search/ReportListItem';
import TransactionListItem from '@components/SelectionList/Search/TransactionListItem';
Expand All @@ -18,6 +18,7 @@ import type SearchResults from '@src/types/onyx/SearchResults';
import type {ListItemDataType, ListItemType, SearchDataTypes, SearchPersonalDetails, SearchReport, SearchTransaction} from '@src/types/onyx/SearchResults';
import * as CurrencyUtils from './CurrencyUtils';
import DateUtils from './DateUtils';
import localeCompare from './LocaleCompare';
import {translateLocal} from './Localize';
import {validateAmount} from './MoneyRequestUtils';
import Navigation from './Navigation/Navigation';
Expand Down Expand Up @@ -416,18 +417,14 @@ function getQueryHash(query: SearchQueryJSON): number {
orderedQuery += ` ${CONST.SEARCH.SYNTAX_ROOT_KEYS.SORT_BY}:${query.sortBy}`;
orderedQuery += ` ${CONST.SEARCH.SYNTAX_ROOT_KEYS.SORT_ORDER}:${query.sortOrder}`;

Object.keys(query.flatFilters)
query.flatFilters.forEach((filter) => {
filter.filters.sort((a, b) => localeCompare(a.value.toString(), b.value.toString()));
});

query.flatFilters
.map((filter) => buildFilterString(filter.key, filter.filters))
.sort()
.forEach((key) => {
const filterValues = query.flatFilters?.[key as AdvancedFiltersKeys];
const sortedFilterValues = filterValues?.sort((queryFilter1, queryFilter2) => {
if (queryFilter1.value > queryFilter2.value) {
return 1;
}
return -1;
});
orderedQuery += ` ${buildFilterString(key, sortedFilterValues ?? [])}`;
});
.forEach((filterString) => (orderedQuery += ` ${filterString}`));

return UserUtils.hashText(orderedQuery, 2 ** 32);
}
Expand Down Expand Up @@ -511,7 +508,7 @@ function sanitizeString(str: string) {
* traverses the AST and returns filters as a QueryFilters object
*/
function getFilters(queryJSON: SearchQueryJSON) {
const filters = {} as QueryFilters;
const filters = [] as QueryFilters;
const filterKeys = Object.values(CONST.SEARCH.SYNTAX_FILTER_KEYS);

function traverse(node: ASTNode) {
Expand All @@ -532,12 +529,8 @@ function getFilters(queryJSON: SearchQueryJSON) {
return;
}

if (!filters[nodeKey]) {
filters[nodeKey] = [];
}

// the "?? []" is added only for typescript because otherwise TS throws an error, in newer TS versions this should be fixed
289Adam289 marked this conversation as resolved.
Show resolved Hide resolved
const filterArray = filters[nodeKey] ?? [];
const filterArray = [];
if (!Array.isArray(node.right)) {
filterArray.push({
operator: node.operator,
Expand All @@ -551,6 +544,7 @@ function getFilters(queryJSON: SearchQueryJSON) {
});
});
}
filters.push({key: nodeKey, filters: filterArray});
}

if (queryJSON.filters) {
Expand Down Expand Up @@ -594,13 +588,9 @@ function buildSearchQueryString(queryJSON?: SearchQueryJSON) {

const filters = queryJSON.flatFilters;

for (const [, filterKey] of Object.entries(CONST.SEARCH.SYNTAX_FILTER_KEYS)) {
const queryFilter = filters[filterKey];

if (queryFilter) {
const filterValueString = buildFilterString(filterKey, queryFilter);
queryParts.push(filterValueString);
}
for (const filter of filters) {
const filterValueString = buildFilterString(filter.key, filter.filters);
queryParts.push(filterValueString);
}

return queryParts.join(' ');
Expand Down Expand Up @@ -694,32 +684,35 @@ function buildFilterFormValuesFromQuery(
taxRates: Record<string, string[]>,
) {
const filters = queryJSON.flatFilters;
const filterKeys = Object.keys(filters);
const filtersForm = {} as Partial<SearchAdvancedFiltersForm>;
const policyID = queryJSON.policyID;
for (const filterKey of filterKeys) {
for (const queryFilter of filters) {
const filterKey = queryFilter.key;
const filterList = queryFilter.filters;
const filterValues = filterList.map((item) => item.value.toString());
if (filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.REPORT_ID || filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.MERCHANT || filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.DESCRIPTION) {
filtersForm[filterKey] = filters[filterKey]?.[0]?.value.toString();
filtersForm[filterKey] = filterValues.at(0);
}
if (filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.EXPENSE_TYPE) {
filtersForm[filterKey] = filters[filterKey]
?.map((expenseType) => expenseType.value.toString())
.filter((expenseType) => Object.values(CONST.SEARCH.TRANSACTION_TYPE).includes(expenseType as ValueOf<typeof CONST.SEARCH.TRANSACTION_TYPE>));
const validExpenseTypes = new Set(Object.values(CONST.SEARCH.TRANSACTION_TYPE));
filtersForm[filterKey] = filterValues.filter((expenseType) => validExpenseTypes.has(expenseType as ValueOf<typeof CONST.SEARCH.TRANSACTION_TYPE>));
}
if (filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.CARD_ID) {
filtersForm[filterKey] = filters[filterKey]?.map((card) => card.value.toString()).filter((card) => Object.keys(cardList).includes(card));
filtersForm[filterKey] = filterValues.filter((card) => cardList[card]);
}
if (filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.TAX_RATE) {
filtersForm[filterKey] = filters[filterKey]?.map((tax) => tax.value.toString()).filter((tax) => [...Object.values(taxRates)].flat().includes(tax));
const allTaxRates = new Set(Object.values(taxRates).flat());
filtersForm[filterKey] = filterValues.filter((tax) => allTaxRates.has(tax));
}
if (filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.IN) {
filtersForm[filterKey] = filters[filterKey]?.map((report) => report.value.toString()).filter((id) => reports?.[`${ONYXKEYS.COLLECTION.REPORT}${id}`]);
filtersForm[filterKey] = filterValues.filter((id) => reports?.[`${ONYXKEYS.COLLECTION.REPORT}${id}`]);
}
if (filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.FROM || filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.TO) {
filtersForm[filterKey] = filters[filterKey]?.map((id) => id.value.toString()).filter((id) => Object.keys(personalDetails).includes(id));
filtersForm[filterKey] = filterValues.filter((id) => personalDetails[id]);
}
if (filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.CURRENCY) {
filtersForm[filterKey] = filters[filterKey]?.filter((currency) => Object.keys(currencyList).includes(currency.value.toString())).map((currency) => currency.value.toString());
const validCurrency = new Set(Object.keys(currencyList));
filtersForm[filterKey] = filterValues.filter((currency) => validCurrency.has(currency));
}
if (filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.TAG) {
const tags = policyID
Expand All @@ -728,22 +721,23 @@ function buildFilterFormValuesFromQuery(
.filter((item) => !!item)
.map((tagList) => getTagNamesFromTagsLists(tagList ?? {}))
.flat();
tags.push(CONST.SEARCH.EMPTY_VALUE);
filtersForm[filterKey] = filters[filterKey]?.map((tag) => tag.value.toString()).filter((name) => tags.includes(name));
const uniqueTags = new Set(tags);
uniqueTags.add(CONST.SEARCH.EMPTY_VALUE);
filtersForm[filterKey] = filterValues.filter((name) => uniqueTags.has(name));
}
if (filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.CATEGORY) {
const categories = policyID
? Object.values(policyCategories?.[`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${policyID}`] ?? {}).map((category) => category.name)
: Object.values(policyCategories ?? {})
.map((item) => Object.values(item ?? {}).map((category) => category.name))
.flat();
categories.push(CONST.SEARCH.EMPTY_VALUE);
filtersForm[filterKey] = filters[filterKey]?.map((category) => category.value.toString()).filter((name) => categories.includes(name));
const uniqueCategories = new Set(categories);
uniqueCategories.add(CONST.SEARCH.EMPTY_VALUE);
filtersForm[filterKey] = filterValues.filter((name) => uniqueCategories.has(name));
}
if (filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.KEYWORD) {
filtersForm[filterKey] = filters[filterKey]
?.map((filter) => filter.value.toString())
.map((filter) => {
filtersForm[filterKey] = filterValues
?.map((filter) => {
if (filter.includes(' ')) {
return `"${filter}"`;
}
Expand All @@ -752,12 +746,17 @@ function buildFilterFormValuesFromQuery(
.join(' ');
}
if (filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.DATE) {
filtersForm[FILTER_KEYS.DATE_BEFORE] = filters[filterKey]?.find((filter) => filter.operator === 'lt' && ValidationUtils.isValidDate(filter.value.toString()))?.value.toString();
filtersForm[FILTER_KEYS.DATE_AFTER] = filters[filterKey]?.find((filter) => filter.operator === 'gt' && ValidationUtils.isValidDate(filter.value.toString()))?.value.toString();
filtersForm[FILTER_KEYS.DATE_BEFORE] = filterList.find((filter) => filter.operator === 'lt' && ValidationUtils.isValidDate(filter.value.toString()))?.value.toString();
filtersForm[FILTER_KEYS.DATE_AFTER] = filterList.find((filter) => filter.operator === 'gt' && ValidationUtils.isValidDate(filter.value.toString()))?.value.toString();
}
if (filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.AMOUNT) {
filtersForm[FILTER_KEYS.LESS_THAN] = filters[filterKey]?.find((filter) => filter.operator === 'lt' && validateAmount(filter.value.toString(), 2))?.value.toString();
filtersForm[FILTER_KEYS.GREATER_THAN] = filters[filterKey]?.find((filter) => filter.operator === 'gt' && validateAmount(filter.value.toString(), 2))?.value.toString();
// backend amount is an integer and is 2 digit longer than frontend amount
289Adam289 marked this conversation as resolved.
Show resolved Hide resolved
filtersForm[FILTER_KEYS.LESS_THAN] = filterList
.find((filter) => filter.operator === 'lt' && validateAmount(filter.value.toString(), 0, CONST.IOU.AMOUNT_MAX_LENGTH + 2))
289Adam289 marked this conversation as resolved.
Show resolved Hide resolved
?.value.toString();
filtersForm[FILTER_KEYS.GREATER_THAN] = filterList
.find((filter) => filter.operator === 'gt' && validateAmount(filter.value.toString(), 0, CONST.IOU.AMOUNT_MAX_LENGTH + 2))
?.value.toString();
}
}

Expand Down Expand Up @@ -805,6 +804,10 @@ function getDisplayValue(filterName: string, filter: string, personalDetails: On
if (filterName === CONST.SEARCH.SYNTAX_FILTER_KEYS.IN) {
return ReportUtils.getReportName(reports?.[`${ONYXKEYS.COLLECTION.REPORT}${filter}`]) || filter;
}
if (filterName === CONST.SEARCH.SYNTAX_FILTER_KEYS.AMOUNT) {
const frontendAmount = CurrencyUtils.convertToFrontendAmountAsInteger(Number(filter));
return Number.isNaN(frontendAmount) ? filter : frontendAmount.toString();
}
return filter;
}

Expand Down Expand Up @@ -840,8 +843,10 @@ function getSearchHeaderTitle(

let title = `type:${type} status:${status}`;

Object.keys(filters).forEach((key) => {
const queryFilter = filters[key as ValueOf<typeof CONST.SEARCH.SYNTAX_FILTER_KEYS>] ?? [];
for (const filterObject of filters) {
const key = filterObject.key;
const queryFilter = filterObject.filters;

let displayQueryFilters: QueryFilter[] = [];
if (key === CONST.SEARCH.SYNTAX_FILTER_KEYS.TAX_RATE) {
const taxRateIDs = queryFilter.map((filter) => filter.value.toString());
Expand All @@ -865,7 +870,7 @@ function getSearchHeaderTitle(
}));
}
title += buildFilterString(key, displayQueryFilters);
});
}

return title;
}
Expand Down Expand Up @@ -947,6 +952,16 @@ function findIDFromDisplayValue(filterName: ValueOf<typeof CONST.SEARCH.SYNTAX_F
)
.flat();
}
if (filterName === CONST.SEARCH.SYNTAX_FILTER_KEYS.AMOUNT) {
if (typeof filter === 'string') {
const backendAmount = CurrencyUtils.convertToBackendAmount(Number(filter));
return Number.isNaN(backendAmount) ? filter : backendAmount.toString();
}
return filter.map((amount) => {
const backendAmount = CurrencyUtils.convertToBackendAmount(Number(amount));
return Number.isNaN(backendAmount) ? amount : backendAmount.toString();
});
}
return filter;
}

Expand Down
Loading