Skip to content

Commit

Permalink
Merge pull request #51314 from software-mansion-labs/@szymczak/fix-re…
Browse files Browse the repository at this point in the history
…cent-searches-list

Fix "same search query appears twice in Recent searches"
  • Loading branch information
luacmartins authored Nov 5, 2024
2 parents e5d732b + df04107 commit 74994dc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/components/Search/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ type SearchQueryAST = {
type SearchQueryJSON = {
inputQuery: SearchQueryString;
hash: number;
/** Hash used for putting queries in recent searches list. It ignores sortOrder and sortBy, because we want to treat queries differing only in sort params as the same query */
recentSearchHash: number;
flatFilters: QueryFilters;
} & SearchQueryAST;

Expand Down
22 changes: 14 additions & 8 deletions src/libs/SearchQueryUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,10 @@ function findIDFromDisplayValue(filterName: ValueOf<typeof CONST.SEARCH.SYNTAX_F
* Computes and returns a numerical hash for a given queryJSON.
* Sorts the query keys and values to ensure that hashes stay consistent.
*/
function getQueryHash(query: SearchQueryJSON): number {
function getQueryHashes(query: SearchQueryJSON): {primaryHash: number; recentSearchHash: number} {
let orderedQuery = '';
if (query.policyID) {
orderedQuery += `${CONST.SEARCH.SYNTAX_ROOT_KEYS.POLICY_ID}:${query.policyID} `;
}
orderedQuery += `${CONST.SEARCH.SYNTAX_ROOT_KEYS.TYPE}:${query.type}`;
orderedQuery += ` ${CONST.SEARCH.SYNTAX_ROOT_KEYS.STATUS}:${query.status}`;
orderedQuery += ` ${CONST.SEARCH.SYNTAX_ROOT_KEYS.SORT_BY}:${query.sortBy}`;
orderedQuery += ` ${CONST.SEARCH.SYNTAX_ROOT_KEYS.SORT_ORDER}:${query.sortOrder}`;

query.flatFilters.forEach((filter) => {
filter.filters.sort((a, b) => localeCompare(a.value.toString(), b.value.toString()));
Expand All @@ -235,7 +230,16 @@ function getQueryHash(query: SearchQueryJSON): number {
.sort()
.forEach((filterString) => (orderedQuery += ` ${filterString}`));

return UserUtils.hashText(orderedQuery, 2 ** 32);
const recentSearchHash = UserUtils.hashText(orderedQuery, 2 ** 32);

orderedQuery += ` ${CONST.SEARCH.SYNTAX_ROOT_KEYS.SORT_BY}:${query.sortBy}`;
orderedQuery += ` ${CONST.SEARCH.SYNTAX_ROOT_KEYS.SORT_ORDER}:${query.sortOrder}`;
if (query.policyID) {
orderedQuery += ` ${CONST.SEARCH.SYNTAX_ROOT_KEYS.POLICY_ID}:${query.policyID} `;
}
const primaryHash = UserUtils.hashText(orderedQuery, 2 ** 32);

return {primaryHash, recentSearchHash};
}

/**
Expand All @@ -252,7 +256,9 @@ function buildSearchQueryJSON(query: SearchQueryString) {
// Add the full input and hash to the results
result.inputQuery = query;
result.flatFilters = flatFilters;
result.hash = getQueryHash(result);
const {primaryHash, recentSearchHash} = getQueryHashes(result);
result.hash = primaryHash;
result.recentSearchHash = recentSearchHash;

return result;
} catch (e) {
Expand Down

0 comments on commit 74994dc

Please sign in to comment.