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

Fix FilterTag values sorting #732 (CRASM-908) #733

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
71 changes: 63 additions & 8 deletions frontend/src/pages/Search/FilterTags.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ const FIELD_TO_LABEL_MAP: FieldToLabelMap = {
return 'Region';
},
filterValueAccssor: (t) => {
if (Array.isArray(t)) {
return t.sort((a: string, b: string) => {
return a.localeCompare(b);
});
}
return t;
},
trimAfter: 10
Expand All @@ -48,14 +53,35 @@ const FIELD_TO_LABEL_MAP: FieldToLabelMap = {
return 'Severity';
},
filterValueAccssor(t) {
const severityLevels = [
'N/A',
'Low',
'Medium',
'High',
'Critical',
'Other'
];
if (Array.isArray(t)) {
return t.sort((a: string, b: string) => {
const aValue = severityLevels.indexOf(a);
const bValue = severityLevels.indexOf(b);
return aValue - bValue;
});
}
return t;
}
},
trimAfter: 3
},
ip: {
labelAccessor: (t) => {
return 'IP';
},
filterValueAccssor(t) {
if (Array.isArray(t)) {
return t.sort((a: string, b: string) => {
return a.localeCompare(b);
});
}
return t;
}
},
Expand All @@ -64,6 +90,11 @@ const FIELD_TO_LABEL_MAP: FieldToLabelMap = {
return 'Name';
},
filterValueAccssor(t) {
if (Array.isArray(t)) {
return t.sort((a: string, b: string) => {
return a.localeCompare(b);
});
}
return t;
}
},
Expand All @@ -72,6 +103,11 @@ const FIELD_TO_LABEL_MAP: FieldToLabelMap = {
return 'Root Domain(s)';
},
filterValueAccssor(t) {
if (Array.isArray(t)) {
return t.sort((a: string, b: string) => {
return a.localeCompare(b);
});
}
return t;
}
},
Expand All @@ -80,10 +116,18 @@ const FIELD_TO_LABEL_MAP: FieldToLabelMap = {
return 'Organization';
},
filterValueAccssor: (t) => {
if (Array.isArray(t)) {
return t
.map((org) => org.name)
.sort((a: string, b: string) => {
return a.localeCompare(b);
});
}
return t.name;
},
trimAfter: 2
trimAfter: 3
},

query: {
labelAccessor: (t) => {
return 'Query';
Expand All @@ -96,7 +140,12 @@ const FIELD_TO_LABEL_MAP: FieldToLabelMap = {
labelAccessor: (t) => {
return 'Port';
},
filterValueAccssor(t) {
filterValueAccssor: (t) => {
if (Array.isArray(t)) {
return t.sort((a: number, b: number) => {
return a - b;
});
}
return t;
},
trimAfter: 6
Expand All @@ -106,9 +155,14 @@ const FIELD_TO_LABEL_MAP: FieldToLabelMap = {
return 'CVE';
},
filterValueAccssor(t) {
if (Array.isArray(t)) {
return t.sort((a: string, b: string) => {
return a.localeCompare(b);
});
}
return t;
},
trimAfter: 3
trimAfter: 10
}
};

Expand Down Expand Up @@ -136,14 +190,15 @@ export const FilterTags: React.FC<Props> = ({ filters, removeFilter }) => {
const filtersByColumn: FlatFilters = useMemo(() => {
return filters.reduce((acc, nextFilter) => {
const fieldAccessors = FIELD_TO_LABEL_MAP[nextFilter.field] ?? null;
const sortedValues = fieldAccessors
? fieldAccessors.filterValueAccssor(nextFilter.values)
: nextFilter.values;
const value = fieldAccessors
? ellipsisPastIndex(
nextFilter.values.map((item: any) =>
fieldAccessors.filterValueAccssor(item)
),
sortedValues,
fieldAccessors.trimAfter ? fieldAccessors.trimAfter - 1 : null
).join(', ')
: nextFilter.values.join(', ');
: sortedValues.join(', ');
const label = fieldAccessors
? fieldAccessors.labelAccessor(nextFilter)
: nextFilter.field.split('.').pop();
Expand Down
Loading