Skip to content

Commit

Permalink
Merge pull request #741 from cisagov/739-set-filtertags-to-be-in-a-co…
Browse files Browse the repository at this point in the history
…nsistent-order-as-filterdrawer-components

Refactored FilterTags.tsx to sort filters labels in a predefined order (CRASM-931)
  • Loading branch information
schmelz21 authored Jan 22, 2025
2 parents 25e1b3d + 3c32282 commit f6b42c9
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion frontend/src/pages/Search/FilterTags.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,23 @@ type FlatFilters = {
type: 'all' | 'none' | 'any';
}[];

const filterOrder = [
'Region',
'Organization',
'IP',
'Name',
'Root Domain(s)',
'Port',
'CVE',
'Severity'
];

const sortFiltersByOrder = (filters: FlatFilters) => {
return filters.sort((a, b) => {
return filterOrder.indexOf(a.label) - filterOrder.indexOf(b.label);
});
};

export const FilterTags: React.FC<Props> = ({ filters, removeFilter }) => {
const { userLevel } = useUserLevel();

Expand All @@ -188,7 +205,7 @@ export const FilterTags: React.FC<Props> = ({ filters, removeFilter }) => {
}, [userLevel]);

const filtersByColumn: FlatFilters = useMemo(() => {
return filters.reduce((acc, nextFilter) => {
const processedFilters = filters.reduce((acc, nextFilter) => {
const fieldAccessors = FIELD_TO_LABEL_MAP[nextFilter.field] ?? null;
const sortedValues = fieldAccessors
? fieldAccessors.filterValueAccssor(nextFilter.values)
Expand All @@ -211,6 +228,7 @@ export const FilterTags: React.FC<Props> = ({ filters, removeFilter }) => {
}
];
}, []);
return sortFiltersByOrder(processedFilters);
}, [filters]);

return (
Expand Down

0 comments on commit f6b42c9

Please sign in to comment.