"use client"; import { zodResolver } from "@hookform/resolvers/zod"; import { Badge } from "@openstatus/ui/components/ui/badge"; import { Button } from "@openstatus/ui/components/ui/button"; import { Checkbox } from "@openstatus/ui/components/ui/checkbox"; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from "@openstatus/ui/components/ui/form"; import { Input } from "@openstatus/ui/components/ui/input"; import { cn } from "@openstatus/ui/lib/utils"; import { isTRPCClientError } from "@trpc/client"; import { useTransition } from "react"; import { useForm } from "react-hook-form"; import { toast } from "sonner"; import { z } from "zod"; import { EmptyStateContainer, EmptyStateTitle, } from "@/components/content/empty-state"; import { FormCard, FormCardContent, FormCardDescription, FormCardFooter, FormCardHeader, FormCardSeparator, FormCardTitle, } from "@/components/forms/form-card"; const schema = z.object({ description: z.string().optional(), externalName: z.string().optional(), statusPages: z.array(z.number()), }); type FormValues = z.infer; export function FormStatusPages({ defaultValues, onSubmit, statusPages, ...props }: Omit, "onSubmit"> & { defaultValues?: FormValues; onSubmit: (values: FormValues) => Promise; statusPages: { id: number; title: string; slug: string }[]; }) { const form = useForm({ resolver: zodResolver(schema), defaultValues: defaultValues ?? { description: "", externalName: "", statusPages: [], }, }); const [isPending, startTransition] = useTransition(); const watchStatusPages = form.watch("statusPages"); 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 (
Status Pages Add status pages to your monitor and configure the external name and description to be shown on the status page. ( External Name External name on the status page, the feed or subscribers notifications. If not provided, monitor's name will be used. )} /> ( Description A tooltip with extra information about the monitor will be displayed. )} /> {statusPages.length > 0 ? ( (
List of Status Pages
{statusPages.map((item) => ( { return ( { return checked ? field.onChange([ ...field.value, item.id, ]) : field.onChange( field.value?.filter( (value) => value !== item.id, ), ); }} /> {item.title}{" "} {item.slug} ); }} /> ))}
)} /> ) : ( No status pages )}
); }