"use client"; import { EmptyStateContainer, EmptyStateTitle, } from "@/components/content/empty-state"; import { FormCard, FormCardContent, FormCardDescription, FormCardFooter, FormCardHeader, FormCardTitle, } from "@/components/forms/form-card"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Checkbox } from "@/components/ui/checkbox"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { config } from "@/data/notifications.client"; import { cn } from "@/lib/utils"; import { zodResolver } from "@hookform/resolvers/zod"; import type { NotificationProvider } from "@openstatus/db/src/schema"; import { isTRPCClientError } from "@trpc/client"; import { useTransition } from "react"; import { useForm } from "react-hook-form"; import { toast } from "sonner"; import { z } from "zod"; const schema = z.object({ notifiers: z.array(z.number()), }); type FormValues = z.infer; export function FormNotifiers({ defaultValues, onSubmit, notifiers, ...props }: Omit, "onSubmit"> & { defaultValues?: FormValues; onSubmit: (values: FormValues) => Promise; notifiers: { id: number; name: string; provider: NotificationProvider }[]; }) { const form = useForm({ resolver: zodResolver(schema), defaultValues: defaultValues ?? { notifiers: [], }, }); const watchNotifiers = form.watch("notifiers"); const [isPending, startTransition] = useTransition(); function submitAction(values: FormValues) { if (isPending) return; startTransition(async () => { try { const promise = onSubmit(values); toast.promise(promise, { loading: "Saving...", success: () => "Saved", error: (error) => { if (isTRPCClientError(error)) { return error.message; } return "Failed to save"; }, }); await promise; } catch (error) { console.error(error); } }); } return (
Notifications Get notified when your monitor is degraded or down. {notifiers.length > 0 ? ( (
List of Notifications
{notifiers.map((item) => ( { const Icon = config[item.provider].icon; const label = config[item.provider].label; return ( { return checked ? field.onChange([ ...field.value, item.id, ]) : field.onChange( field.value?.filter( (value) => value !== item.id, ), ); }} /> {item.name}{" "} {label} ); }} /> ))}
)} /> ) : ( No notifications )}
); }