import React, { useEffect } from "react"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from "@/components/ui/dialog"; import { Button } from "@/components/ui/button"; import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"; import { AlertConfiguration, alertConfigService } from "@/services/alertConfigService"; import { useForm } from "react-hook-form"; import { z } from "zod"; import { zodResolver } from "@hookform/resolvers/zod"; import { Input } from "@/components/ui/input"; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Switch } from "@/components/ui/switch"; import { Loader2 } from "lucide-react"; interface NotificationChannelDialogProps { open: boolean; onClose: (refreshList: boolean) => void; editingConfig: AlertConfiguration | null; } const baseSchema = z.object({ notify_name: z.string().min(1, "Name is required"), notification_type: z.enum(["telegram", "discord", "slack", "signal", "email"]), enabled: z.boolean().default(true), service_id: z.string().default("global"), // Assuming global for now, could be linked to specific services template_id: z.string().optional(), }); const telegramSchema = baseSchema.extend({ notification_type: z.literal("telegram"), telegram_chat_id: z.string().min(1, "Chat ID is required"), bot_token: z.string().min(1, "Bot token is required"), }); const discordSchema = baseSchema.extend({ notification_type: z.literal("discord"), discord_webhook_url: z.string().url("Must be a valid URL"), }); const slackSchema = baseSchema.extend({ notification_type: z.literal("slack"), slack_webhook_url: z.string().url("Must be a valid URL"), }); const signalSchema = baseSchema.extend({ notification_type: z.literal("signal"), signal_number: z.string().min(1, "Signal number is required"), }); const emailSchema = baseSchema.extend({ notification_type: z.literal("email"), // Email specific fields could be added here }); const formSchema = z.discriminatedUnion("notification_type", [ telegramSchema, discordSchema, slackSchema, signalSchema, emailSchema ]); type FormValues = z.infer; export const NotificationChannelDialog = ({ open, onClose, editingConfig }: NotificationChannelDialogProps) => { const isEditing = !!editingConfig; const form = useForm({ resolver: zodResolver(formSchema), defaultValues: { notify_name: "", notification_type: "telegram" as const, enabled: true, service_id: "global", template_id: "", }, }); const { watch, reset } = form; const notificationType = watch("notification_type"); const [isSubmitting, setIsSubmitting] = React.useState(false); useEffect(() => { if (editingConfig) { // Handle string vs boolean for enabled field const enabled = typeof editingConfig.enabled === 'string' ? editingConfig.enabled === "true" : !!editingConfig.enabled; reset({ ...editingConfig, enabled }); } else if (open) { reset({ notify_name: "", notification_type: "telegram" as const, enabled: true, service_id: "global", template_id: "", }); } }, [editingConfig, open, reset]); const handleClose = () => { onClose(false); }; const onSubmit = async (values: FormValues) => { setIsSubmitting(true); try { // Ensure service_id is always present const configData = { ...values, service_id: values.service_id || "global", }; if (isEditing && editingConfig?.id) { await alertConfigService.updateAlertConfiguration(editingConfig.id, configData); } else { await alertConfigService.createAlertConfiguration(configData as any); } onClose(true); // Close with refresh } finally { setIsSubmitting(false); } }; return ( handleClose()}> {isEditing ? "Edit Notification Channel" : "Add Notification Channel"}
( Name A name to identify this notification channel )} /> ( Channel Type Telegram Discord Slack Signal Email )} /> {/* Show different fields based on notification type */} {notificationType === "telegram" && ( <> ( Chat ID )} /> ( Bot Token )} /> )} {notificationType === "discord" && ( ( Webhook URL )} /> )} {notificationType === "slack" && ( ( Webhook URL )} /> )} {notificationType === "signal" && ( ( Signal Number )} /> )} (
Enabled Turn notifications on or off
)} />
); };