From 40852c33d96cae46c32deca815ab74fd67b1df11 Mon Sep 17 00:00:00 2001 From: Fred Tingaud <95592999+frederic-tingaud-sonarsource@users.noreply.github.com> Date: Wed, 27 Sep 2023 10:22:15 +0200 Subject: [PATCH] Frontend: Simplify the code a little --- frontend/src/SearchPage.tsx | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/frontend/src/SearchPage.tsx b/frontend/src/SearchPage.tsx index cdcdba809a7..25eb7da1781 100644 --- a/frontend/src/SearchPage.tsx +++ b/frontend/src/SearchPage.tsx @@ -21,13 +21,12 @@ import { IndexedRule, IndexAggregates } from './types/IndexStore'; import { useHistory } from 'react-router-dom'; -function correctResultsOrder(results: IndexedRule[], query: string) { +function correctResultsOrder(results: IndexedRule[], query: string): IndexedRule[] { const upperCaseQuery = query.toLocaleUpperCase(); let reorderedResults: IndexedRule[] = []; - results.forEach(indexedRule => { - if(indexedRule.all_keys.some(key => key === upperCaseQuery)) { - reorderedResults = [indexedRule, ...reorderedResults]; + if (indexedRule.all_keys.includes(upperCaseQuery)) { + reorderedResults.unshift(indexedRule); } else { reorderedResults.push(indexedRule); } @@ -84,16 +83,9 @@ export const SearchPage = () => { if (loading) { resultsDisplay = 'Searching'; } else if (results.length > 0) { - const resultsBoxes: JSX.Element[] = []; - - // making the exact match to appear first in the search results - correctResultsOrder(results, query).forEach(indexedRule => { - const box = + resultsDisplay = correctResultsOrder(results, query).map(indexedRule => - ; - resultsBoxes.push(box); - }); - resultsDisplay = resultsBoxes; + ); } const paramSetters: Record> = {