Skip to content

Commit e1b9c66

Browse files
committed
feat: revamp notification silences ui
1 parent cee1c27 commit e1b9c66

File tree

3 files changed

+434
-48
lines changed

3 files changed

+434
-48
lines changed

src/api/services/notifications.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,73 @@ export const getNotificationSilencesByID = async (id: string) => {
271271
return res.data?.[0] ?? undefined;
272272
};
273273

274+
export const getNotificationSilencesHistory = async ({
275+
pageIndex = 0,
276+
pageSize = 10,
277+
component_id,
278+
config_id,
279+
check_id,
280+
canary_id,
281+
filter,
282+
selectors
283+
}: {
284+
pageIndex?: number;
285+
pageSize?: number;
286+
component_id?: string;
287+
config_id?: string;
288+
check_id?: string;
289+
canary_id?: string;
290+
filter?: string;
291+
selectors?: string;
292+
}) => {
293+
const pagingParams = getPagingParams({ pageIndex, pageSize });
294+
295+
const selectColumns = [
296+
"*",
297+
"checks:check_id(id,name,type,status)",
298+
"catalog:config_id(id,name,type,config_class)",
299+
"component:component_id(id,name,icon)",
300+
`createdBy:created_by(${AVATAR_INFO})`
301+
].join(",");
302+
303+
// Filter for past 15 days
304+
const fifteenDaysAgo = new Date();
305+
fifteenDaysAgo.setDate(fifteenDaysAgo.getDate() - 15);
306+
const dateFilter = `&created_at=gte.${fifteenDaysAgo.toISOString()}`;
307+
308+
// Build filters based on provided criteria
309+
let filters = "";
310+
if (component_id) {
311+
filters += `&component_id=eq.${component_id}`;
312+
}
313+
if (config_id) {
314+
filters += `&config_id=eq.${config_id}`;
315+
}
316+
if (check_id) {
317+
filters += `&check_id=eq.${check_id}`;
318+
}
319+
if (canary_id) {
320+
filters += `&canary_id=eq.${canary_id}`;
321+
}
322+
if (filter) {
323+
filters += `&filter=eq.${encodeURIComponent(filter)}`;
324+
}
325+
if (selectors) {
326+
filters += `&selectors=eq.${encodeURIComponent(selectors)}`;
327+
}
328+
329+
return resolvePostGrestRequestWithPagination(
330+
IncidentCommander.get<NotificationSilenceItemApiResponse[] | null>(
331+
`/notification_silences?select=${selectColumns}&order=created_at.desc${pagingParams}&deleted_at=is.null${dateFilter}${filters}`,
332+
{
333+
headers: {
334+
Prefer: "count=exact"
335+
}
336+
}
337+
)
338+
);
339+
};
340+
274341
export const deleteNotificationSilence = async (id: string) => {
275342
return IncidentCommander.patch<NotificationSilenceItem>(
276343
`/notification_silences?id=eq.${id}`,

src/components/Notifications/SilenceNotificationForm/FormikNotificationResourceField.tsx

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@ import FormikResourceSelectorDropdown from "@flanksource-ui/components/Forms/For
22
import { Label } from "@flanksource-ui/ui/FormControls/Label";
33
import { Switch } from "@flanksource-ui/ui/FormControls/Switch";
44
import { useFormikContext } from "formik";
5-
import { useMemo, useState } from "react";
5+
import { useMemo, useState, useEffect } from "react";
66

7-
export default function FormikNotificationResourceField() {
8-
const { values } = useFormikContext<Record<string, any>>();
7+
type FormikNotificationResourceFieldProps = {
8+
disabled?: boolean;
9+
onFieldChange?: (hasValue: boolean) => void;
10+
};
11+
12+
export default function FormikNotificationResourceField({
13+
disabled = false,
14+
onFieldChange
15+
}: FormikNotificationResourceFieldProps = {}) {
16+
const { values, setFieldValue } = useFormikContext<Record<string, any>>();
917

1018
const component_id = values.component_id;
1119
const config_id = values.config_id;
@@ -55,6 +63,12 @@ export default function FormikNotificationResourceField() {
5563
}
5664
}, [switchOption]);
5765

66+
// Monitor field values and call onFieldChange when they change
67+
useEffect(() => {
68+
const hasValue = !!(component_id || config_id || check_id || canary_id);
69+
onFieldChange?.(hasValue);
70+
}, [component_id, config_id, check_id, canary_id, onFieldChange]);
71+
5872
return (
5973
<div className="flex flex-col gap-2">
6074
<Label label="Resource" required={true} />
@@ -66,38 +80,40 @@ export default function FormikNotificationResourceField() {
6680
itemsClassName=""
6781
defaultValue="Go Template"
6882
value={switchOption}
83+
disabled={disabled}
6984
onChange={(v) => {
7085
setSwitchOption(v);
7186
// clear the other fields if the user selects a different option
7287
if (v === "Component") {
73-
values.config_id = null;
74-
values.check_id = null;
75-
values.canary_id = null;
88+
setFieldValue('config_id', null);
89+
setFieldValue('check_id', null);
90+
setFieldValue('canary_id', null);
7691
}
7792

7893
if (v === "Catalog") {
79-
values.component_id = null;
80-
values.check_id = null;
81-
values.canary_id = null;
94+
setFieldValue('component_id', null);
95+
setFieldValue('check_id', null);
96+
setFieldValue('canary_id', null);
8297
}
8398

8499
if (v === "Check") {
85-
values.component_id = null;
86-
values.config_id = null;
87-
values.canary_id = null;
100+
setFieldValue('component_id', null);
101+
setFieldValue('config_id', null);
102+
setFieldValue('canary_id', null);
88103
}
89104

90105
if (v === "Canary") {
91-
values.component_id = null;
92-
values.config_id = null;
93-
values.check_id = null;
106+
setFieldValue('component_id', null);
107+
setFieldValue('config_id', null);
108+
setFieldValue('check_id', null);
94109
}
95110
}}
96111
/>
97112
</div>
98113

99114
<FormikResourceSelectorDropdown
100115
name={fieldName.name}
116+
disabled={disabled}
101117
checkResourceSelector={
102118
switchOption === "Check" ? [{ id: check_id }] : undefined
103119
}

0 commit comments

Comments
 (0)