From fdda27e421be6e4ecb03b94178017652e4168297 Mon Sep 17 00:00:00 2001 From: Matt Ketmo Date: Tue, 10 Dec 2024 14:11:16 +0100 Subject: [PATCH] feat: allow to match any of the filters (#23) --- src/components/alerts/template.tsx | 11 +++++------ src/components/alerts/utils.ts | 8 +++++--- src/config/types.ts | 1 + 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/components/alerts/template.tsx b/src/components/alerts/template.tsx index d628c7c..f7253dc 100644 --- a/src/components/alerts/template.tsx +++ b/src/components/alerts/template.tsx @@ -42,6 +42,7 @@ export function AlertsTemplate(props: Props) { const [view, setView] = useState(null) const [alertGroups, setAlertGroups] = useState([]) const [filters] = useQueryState('filters', parseAsArrayOf(parseAsFilter, ';')) + const [filterMatch] = useQueryState('match', { defaultValue: 'all' }) useHotkeys('r', () => refreshAlerts(), []); @@ -56,13 +57,11 @@ export function AlertsTemplate(props: Props) { const groupBy = group !== '' ? group : view.groupBy - // Merge view filters with query filters - const alertFilters = view.filters.concat(filters || []) - // Flatten, filter & sort alerts - const flatAlerts = Object.values(alerts). - reduce((acc, val) => acc.concat(val), []). - filter(alertFilter(alertFilters)) + const flatAlerts = Object.values(alerts) + .reduce((acc, val) => acc.concat(val), []) + .filter(alertFilter(view.filters, view.filtersMatch === 'all')) + .filter(alertFilter(filters || [], filterMatch === 'all')) flatAlerts.sort(alertSort) setFlattenedAlerts(flatAlerts) diff --git a/src/components/alerts/utils.ts b/src/components/alerts/utils.ts index ae457cf..3b91050 100644 --- a/src/components/alerts/utils.ts +++ b/src/components/alerts/utils.ts @@ -52,9 +52,9 @@ export function flattenAlerts(alerts: Record): Alert[] { return Object.values(alerts).reduce((acc, val) => acc.concat(val), []) } -export function alertFilter(filters: LabelFilter[]): (alert: Alert) => boolean { +export function alertFilter(filters: LabelFilter[], matchAll = true): (alert: Alert) => boolean { return (alert: Alert) => { - return filters.map(filter => { + const matches = filters.map(filter => { if (showOnlyActive && alert.status.state !== 'active') { return false } @@ -74,7 +74,9 @@ export function alertFilter(filters: LabelFilter[]): (alert: Alert) => boolean { } return filter.value.includes(value) } - }).every(Boolean) + }) + + return matchAll ? matches.every(Boolean) : matches.some(Boolean) } } diff --git a/src/config/types.ts b/src/config/types.ts index 10e3737..6a43767 100644 --- a/src/config/types.ts +++ b/src/config/types.ts @@ -16,6 +16,7 @@ export const ViewsSchema = z.object({ name: z.string().optional().default(''), groupBy: z.string(), category: z.string().optional().default(''), + filtersMatch: z.enum(['all', 'any']).optional().default('all'), filters: z.array( z.object({ label: z.string(),