"use client"; import { Button } from "@openstatus/ui/components/ui/button"; import { Label } from "@openstatus/ui/components/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@openstatus/ui/components/ui/select"; import { Separator } from "@openstatus/ui/components/ui/separator"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { useState } from "react"; import { EmptyStateContainer, EmptyStateTitle, } from "@/components/content/empty-state"; import { FormCard, FormCardGroup } from "@/components/forms/form-card"; import { FormSheetContent, FormSheetDescription, FormSheetFooter, FormSheetHeader, FormSheetTitle, FormSheetTrigger, FormSheetWithDirtyProtection, } from "@/components/forms/form-sheet"; import { FormMaintenance } from "@/components/forms/maintenance/form"; import { toCheckboxTreeItems } from "@/components/ui/checkbox-tree"; import { useTRPC } from "@/lib/trpc/client"; /** * Create-maintenance sheet with a status page selector — for surfaces that * are not scoped to a single page (e.g. /overview). `children` is the trigger. */ export function FormSheetMaintenanceCreate({ children, }: { children: React.ReactNode; }) { const [open, setOpen] = useState(false); const [selectedPageId, setSelectedPageId] = useState(null); const trpc = useTRPC(); const queryClient = useQueryClient(); const { data: pages } = useQuery(trpc.page.list.queryOptions()); const pageId = pages?.length === 1 ? pages[0].id : (selectedPageId ?? undefined); const { data: page } = useQuery( trpc.page.get.queryOptions( { id: pageId ?? 0 }, { enabled: pageId !== undefined }, ), ); const sendMaintenanceUpdateMutation = useMutation( trpc.subscriberNotification.maintenance.mutationOptions(), ); const createMaintenanceMutation = useMutation( trpc.maintenance.new.mutationOptions({ onSuccess: (maintenance) => { if (maintenance.notifySubscribers) { sendMaintenanceUpdateMutation.mutate({ id: maintenance.id }); } // no-input prefix key — matches every maintenance.list query queryClient.invalidateQueries({ queryKey: trpc.maintenance.list.queryKey(), }); }, }), ); return ( {children} Maintenance Configure and update the maintenance. {pages && pages.length > 1 ? ( <>
) : null} {pageId !== undefined ? ( { await createMaintenanceMutation.mutateAsync({ pageId, title: values.title, message: values.message, startDate: values.startDate, endDate: values.endDate, pageComponents: values.pageComponents, notifySubscribers: values.notifySubscribers, }); setOpen(false); }} /> ) : ( {pages?.length === 0 ? "No status pages found" : "Select a status page"} )}
); }