diff --git a/application/src/components/settings/notification-settings/NotificationChannelDialog.tsx b/application/src/components/settings/notification-settings/NotificationChannelDialog.tsx index 61fcb6a..cddf07b 100644 --- a/application/src/components/settings/notification-settings/NotificationChannelDialog.tsx +++ b/application/src/components/settings/notification-settings/NotificationChannelDialog.tsx @@ -1,3 +1,4 @@ + import React, { useEffect } from "react"; import { Dialog, @@ -14,6 +15,7 @@ import { useForm } from "react-hook-form"; import { z } from "zod"; import { zodResolver } from "@hookform/resolvers/zod"; import { Input } from "@/components/ui/input"; +import { Textarea } from "@/components/ui/textarea"; import { Form, FormControl, @@ -24,7 +26,9 @@ import { FormMessage, } from "@/components/ui/form"; import { Switch } from "@/components/ui/switch"; -import { Loader2 } from "lucide-react"; +import { Loader2, Copy } from "lucide-react"; +import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; +import { toast } from "@/hooks/use-toast"; interface NotificationChannelDialogProps { open: boolean; @@ -76,6 +80,9 @@ const webhookSchema = baseSchema.extend({ webhook_secret: z.string().optional(), webhook_headers: z.string().optional(), webhook_description: z.string().optional(), + webhook_payload_template: z.string().optional(), + webhook_retry_count: z.string().optional(), + webhook_trigger_events: z.string().optional(), }); const formSchema = z.discriminatedUnion("notification_type", [ @@ -98,6 +105,53 @@ const notificationTypeOptions = [ { value: "webhook", label: "Webhook", description: "Send notifications to custom webhook" }, ]; +const webhookPayloadTemplates = { + server: `{ + "alert_type": "server", + "server_name": "\${server_name}", + "status": "\${status}", + "cpu_usage": "\${cpu_usage}", + "ram_usage": "\${ram_usage}", + "disk_usage": "\${disk_usage}", + "network_usage": "\${network_usage}", + "cpu_temp": "\${cpu_temp}", + "disk_io": "\${disk_io}", + "threshold": "\${threshold}", + "timestamp": "\${time}", + "message": "Server \${server_name} alert: \${status}" +}`, + service: `{ + "alert_type": "service", + "service_name": "\${service_name}", + "status": "\${status}", + "response_time": "\${response_time}", + "url": "\${url}", + "uptime": "\${uptime}", + "downtime": "\${downtime}", + "timestamp": "\${time}", + "message": "Service \${service_name} is \${status}" +}`, + ssl: `{ + "alert_type": "ssl", + "domain": "\${domain}", + "certificate_name": "\${certificate_name}", + "expiry_date": "\${expiry_date}", + "days_left": "\${days_left}", + "issuer": "\${issuer}", + "serial_number": "\${serial_number}", + "timestamp": "\${time}", + "message": "SSL certificate for \${domain} expires in \${days_left} days" +}` +}; + +const defaultPayloadTemplate = `{ + "alert_type": "general", + "service_name": "\${service_name}", + "status": "\${status}", + "message": "\${message}", + "timestamp": "\${time}" +}`; + export const NotificationChannelDialog = ({ open, onClose, @@ -115,7 +169,7 @@ export const NotificationChannelDialog = ({ }, }); - const { watch, reset } = form; + const { watch, reset, setValue } = form; const notificationType = watch("notification_type"); const [isSubmitting, setIsSubmitting] = React.useState(false); @@ -145,6 +199,18 @@ export const NotificationChannelDialog = ({ onClose(false); }; + const copyToClipboard = (text: string) => { + navigator.clipboard.writeText(text); + toast({ + title: "Copied", + description: "Template copied to clipboard", + }); + }; + + const insertTemplate = (template: string) => { + setValue("webhook_payload_template", template); + }; + const onSubmit = async (values: FormValues) => { setIsSubmitting(true); try { @@ -158,6 +224,9 @@ export const NotificationChannelDialog = ({ secret: values.webhook_secret || "", headers: values.webhook_headers || "", description: values.webhook_description || "", + payload_template: values.webhook_payload_template || "", + retry_count: values.webhook_retry_count || "3", + trigger_events: values.webhook_trigger_events || "all", user_id: "global", // or get current user id }; @@ -187,7 +256,7 @@ export const NotificationChannelDialog = ({ return ( handleClose()}> - + {isEditing ? "Edit Notification Channel" : "Add Notification Channel"} @@ -433,6 +502,7 @@ export const NotificationChannelDialog = ({ + GET POST PUT PATCH @@ -456,6 +526,42 @@ export const NotificationChannelDialog = ({ )} /> + +
+ ( + + Retry Count + + + + + Number of retry attempts on failure + + + + )} + /> + ( + + Trigger Events + + + + + Events that trigger this webhook (comma-separated) + + + + )} + /> +
+ Custom Headers (Optional) - +