Skip to content

Commit d77cb2d

Browse files
authored
feat: Allow to add webhooks from Create Alert modal (#767)
![Screenshot 2025-04-19 at 11 34 19 AM](https://github.com/user-attachments/assets/9a3a87eb-9011-474c-ae8e-c26b13e17026)
1 parent 66eca90 commit d77cb2d

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

packages/app/src/TeamPage.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -679,12 +679,12 @@ type WebhookForm = {
679679
body?: string;
680680
};
681681

682-
function CreateWebhookForm({
682+
export function CreateWebhookForm({
683683
onClose,
684684
onSuccess,
685685
}: {
686686
onClose: VoidFunction;
687-
onSuccess: VoidFunction;
687+
onSuccess: (webhookId?: string) => void;
688688
}) {
689689
const saveWebhook = api.useSaveWebhook();
690690

@@ -697,7 +697,7 @@ function CreateWebhookForm({
697697
const onSubmit: SubmitHandler<WebhookForm> = async values => {
698698
const { service, name, url, description, body } = values;
699699
try {
700-
await saveWebhook.mutateAsync({
700+
const response = await saveWebhook.mutateAsync({
701701
service,
702702
name,
703703
url,
@@ -711,7 +711,7 @@ function CreateWebhookForm({
711711
color: 'green',
712712
message: `Webhook created successfully`,
713713
});
714-
onSuccess();
714+
onSuccess(response.data?._id);
715715
onClose();
716716
} catch (e) {
717717
console.error(e);

packages/app/src/components/Alerts.tsx

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { useMemo } from 'react';
2-
import Link from 'next/link';
3-
import { Control } from 'react-hook-form';
2+
import { Control, useController } from 'react-hook-form';
43
import { Select, SelectProps } from 'react-hook-form-mantine';
54
import { Label, ReferenceArea, ReferenceLine } from 'recharts';
6-
import type { Alert, AlertChannelType } from '@hyperdx/common-utils/dist/types';
7-
import { Button, ComboboxData, Group } from '@mantine/core';
5+
import type { AlertChannelType } from '@hyperdx/common-utils/dist/types';
6+
import { Button, ComboboxData, Group, Modal } from '@mantine/core';
7+
import { useDisclosure } from '@mantine/hooks';
88

99
import api from '@/api';
1010

11+
import { CreateWebhookForm } from '../TeamPage';
12+
1113
type Webhook = {
1214
_id: string;
1315
name: string;
@@ -16,7 +18,11 @@ type Webhook = {
1618
const WebhookChannelForm = <T extends object>(
1719
props: Partial<SelectProps<T>>,
1820
) => {
19-
const { data: webhooks } = api.useWebhooks(['slack', 'generic']);
21+
const { data: webhooks, refetch: refetchWebhooks } = api.useWebhooks([
22+
'slack',
23+
'generic',
24+
]);
25+
const [opened, { open, close }] = useDisclosure(false);
2026

2127
const hasWebhooks = Array.isArray(webhooks?.data) && webhooks.data.length > 0;
2228

@@ -37,6 +43,20 @@ const WebhookChannelForm = <T extends object>(
3743
];
3844
}, [webhooks]);
3945

46+
const { field } = useController({
47+
control: props.control,
48+
name: props.name!,
49+
});
50+
51+
const handleWebhookCreated = async (webhookId?: string) => {
52+
await refetchWebhooks();
53+
if (webhookId) {
54+
field.onChange(webhookId);
55+
field.onBlur();
56+
}
57+
close();
58+
};
59+
4060
return (
4161
<div>
4262
<Group gap="md" justify="space-between">
@@ -55,12 +75,21 @@ const WebhookChannelForm = <T extends object>(
5575
control={props.control}
5676
{...props}
5777
/>
58-
<Link href="/team" passHref>
59-
<Button size="xs" variant="subtle" color="gray">
60-
Add New Incoming Webhook
61-
</Button>
62-
</Link>
78+
<Button size="xs" variant="subtle" color="gray" onClick={open}>
79+
Add New Incoming Webhook
80+
</Button>
6381
</Group>
82+
83+
<Modal
84+
opened={opened}
85+
onClose={close}
86+
title="Add New Webhook"
87+
centered
88+
zIndex={9999}
89+
size="lg"
90+
>
91+
<CreateWebhookForm onClose={close} onSuccess={handleWebhookCreated} />
92+
</Modal>
6493
</div>
6594
);
6695
};

0 commit comments

Comments
 (0)