Skip to content

Commit

Permalink
Frontend: Simplify the code a little
Browse files Browse the repository at this point in the history
  • Loading branch information
frederic-tingaud-sonarsource authored Sep 27, 2023
1 parent 077b76f commit 40852c3
Showing 1 changed file with 5 additions and 13 deletions.
18 changes: 5 additions & 13 deletions frontend/src/SearchPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 = <Box key={indexedRule.id} className={classes.searchHitBox}>
resultsDisplay = correctResultsOrder(results, query).map(indexedRule => <Box key={indexedRule.id} className={classes.searchHitBox}>
<SearchHit key={indexedRule.id} data={indexedRule}/>
</Box>;
resultsBoxes.push(box);
});
resultsDisplay = resultsBoxes;
</Box>);
}

const paramSetters: Record<string, SearchParamSetter<any>> = {
Expand Down

0 comments on commit 40852c3

Please sign in to comment.