import { useMutation, useQuery } from "@tanstack/react-query"; import { redirect, useNavigate } from "react-router"; 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 { getNotificationDestination } from "~/client/api-client/sdk.gen"; import type { Route } from "./+types/notification-details"; import { cn } from "~/client/lib/utils"; import { Card, CardContent, CardHeader, CardTitle } from "~/client/components/ui/card"; import { Bell, Save, TestTube2, Trash2, X } from "lucide-react"; import { Alert, AlertDescription } from "~/client/components/ui/alert"; import { CreateNotificationForm, type NotificationFormValues } from "../components/create-notification-form"; export const handle = { breadcrumb: (match: Route.MetaArgs) => [ { label: "Notifications", href: "/notifications" }, { label: match.params.id }, ], }; export function meta({ params }: Route.MetaArgs) { return [ { title: `Zerobyte - Notification ${params.id}` }, { name: "description", content: "View and edit notification destination settings.", }, ]; } export const clientLoader = async ({ params }: Route.ClientLoaderArgs) => { const destination = await getNotificationDestination({ path: { id: params.id ?? "" } }); if (destination.data) return destination.data; return redirect("/notifications"); }; export default function NotificationDetailsPage({ loaderData }: Route.ComponentProps) { const navigate = useNavigate(); const formId = useId(); const [showDeleteConfirm, setShowDeleteConfirm] = useState(false); const { data } = useQuery({ ...getNotificationDestinationOptions({ path: { id: String(loaderData.id) } }), initialData: loaderData, }); const deleteDestination = useMutation({ ...deleteNotificationDestinationMutation(), onSuccess: () => { toast.success("Notification destination deleted successfully"); navigate("/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 ( <>