import { useMutation, useSuspenseQuery } from "@tanstack/react-query"; import { toast } from "sonner"; import { useState, useId } from "react"; import { deleteNotificationDestinationMutation, getNotificationDestinationOptions, testNotificationDestinationMutation, updateNotificationDestinationMutation, } from "~/client/api-client/@tanstack/react-query.gen"; import { Button } from "~/client/components/ui/button"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "~/client/components/ui/alert-dialog"; import { parseError } from "~/client/lib/errors"; import { cn } from "~/client/lib/utils"; import { Card, CardContent, CardHeader, CardTitle } from "~/client/components/ui/card"; import { Bell, Save, TestTube2, Trash2 } from "lucide-react"; import { Alert, AlertDescription } from "~/client/components/ui/alert"; import { CreateNotificationForm, type NotificationFormValues } from "../components/create-notification-form"; import { useNavigate } from "@tanstack/react-router"; export function NotificationDetailsPage({ notificationId }: { notificationId: string }) { const navigate = useNavigate(); const formId = useId(); const [showDeleteConfirm, setShowDeleteConfirm] = useState(false); const { data } = useSuspenseQuery({ ...getNotificationDestinationOptions({ path: { id: notificationId } }), }); const deleteDestination = useMutation({ ...deleteNotificationDestinationMutation(), onSuccess: () => { toast.success("Notification destination deleted successfully"); void navigate({ to: "/notifications" }); }, onError: (error) => { toast.error("Failed to delete notification destination", { description: parseError(error)?.message, }); }, }); const updateDestination = useMutation({ ...updateNotificationDestinationMutation(), onSuccess: () => { toast.success("Notification destination updated successfully"); }, onError: (error) => { toast.error("Failed to update notification destination", { description: parseError(error)?.message, }); }, }); const testDestination = useMutation({ ...testNotificationDestinationMutation(), onSuccess: () => { toast.success("Test notification sent successfully"); }, onError: (error) => { toast.error("Failed to send test notification", { description: parseError(error)?.message, }); }, }); const handleConfirmDelete = () => { setShowDeleteConfirm(false); deleteDestination.mutate({ path: { id: String(data.id) } }); }; const handleSubmit = (values: NotificationFormValues) => { updateDestination.mutate({ path: { id: String(data.id) }, body: { name: values.name, config: values, }, }); }; const handleTest = () => { testDestination.mutate({ path: { id: String(data.id) } }); }; return ( <>
{data.enabled ? "Enabled" : "Disabled"} {data.type}
{data.name}
{updateDestination.isError && ( Failed to update notification destination:
{parseError(updateDestination.error)?.message}
)}
Delete Notification Destination Are you sure you want to delete the notification destination "{data.name}"? This action cannot be undone and will remove this destination from all backup schedules. Cancel Delete ); }