"use client"; import { zodResolver } from "@hookform/resolvers/zod"; import { FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from "@openstatus/ui/components/ui/form"; import { Form } from "@openstatus/ui/components/ui/form"; import { Input } from "@openstatus/ui/components/ui/input"; import { cn } from "@openstatus/ui/lib/utils"; import { useTransition } from "react"; import { useForm } from "react-hook-form"; import { toast } from "sonner"; import { z } from "zod"; import { CheckboxTree } from "@/components/ui/checkbox-tree"; const schema = z.object({ name: z.string(), provider: z.enum([ "slack", "ms-teams", "discord", "email", "sms", "webhook", "opsgenie", "pagerduty", "ntfy", "telegram", "whatsapp", "google-chat", "grafana-oncall", ]), data: z.record(z.string(), z.any()).or(z.string()), monitors: z.array(z.number()), }); export type FormValues = z.infer; export function NotifierForm({ defaultValues, className, onSubmit, monitors, ...props }: Omit, "onSubmit"> & { defaultValues?: FormValues; onSubmit?: (values: FormValues) => Promise | void; monitors: { id: number; name: string }[]; }) { const form = useForm({ resolver: zodResolver(schema), defaultValues: defaultValues ?? { name: "", data: { webhook: "", }, monitors: [], }, }); const [isPending, startTransition] = useTransition(); function submitAction(values: FormValues) { if (isPending) return; startTransition(async () => { try { const promise = new Promise((resolve) => setTimeout(resolve, 1000)); toast.promise(promise, { loading: "Saving...", success: () => JSON.stringify(values), error: "Failed to save", }); await promise; onSubmit?.(values); } catch (error) { console.error(error); } }); } return (
( Name Enter a descriptive name for your notifier. )} /> ( Webhook URL )} /> ( Monitors Select the monitors you want to notify. ({ id: m.id, label: m.name, })), }, ]} value={field.value ?? []} onValueChange={field.onChange} /> )} /> ); }