"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, open: controlledOpen, onOpenChange, defaultPageId, }: { children?: React.ReactNode; open?: boolean; onOpenChange?: (open: boolean) => void; /** Pre-scopes the sheet to a page and hides the selector. */ defaultPageId?: number; }) { const [internalOpen, setInternalOpen] = useState(false); const open = controlledOpen ?? internalOpen; const setOpen = onOpenChange ?? setInternalOpen; const [selectedPageId, setSelectedPageId] = useState(null); const trpc = useTRPC(); const queryClient = useQueryClient(); const { data: pages } = useQuery(trpc.page.list.queryOptions()); const pageId = defaultPageId ?? (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 ? ( {children} ) : null} Maintenance Configure and update the maintenance. {defaultPageId === undefined && 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"} )}
); }