'use client' import { useState } from 'react' import { useRouter } from 'next/navigation' import { toast } from 'sonner' import { Button } from '@/components/ui/button' import { AlertDialog, AlertDialogTrigger, AlertDialogContent, AlertDialogHeader, AlertDialogFooter, AlertDialogTitle, AlertDialogDescription, AlertDialogAction, AlertDialogCancel, } from '@/components/ui/alert-dialog' // Confirm-then-run wrapper for a self-serve cancel. The action returns the // project-standard { error } | { data/success } shape. export function CancelAction({ title, description, action, successMessage, }: { title: string description: string action: () => Promise<{ error?: string } | Record> successMessage: string }) { const router = useRouter() const [busy, setBusy] = useState(false) async function run() { setBusy(true) const res = await action() setBusy(false) if (res && 'error' in res && res.error) return toast.error(res.error as string) toast.success(successMessage) router.refresh() } return ( {title} {description} Keep it Cancel it ) }