import { useState, type ReactNode } from 'react'; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from '@/components/ui/alert-dialog'; import { buttonVariants } from '@/components/ui/button'; import { cn } from '@/lib/utils'; interface ConfirmDialogProps { open: boolean; onOpenChange: (open: boolean) => void; title: string; description?: ReactNode; confirmText?: string; cancelText?: string; destructive?: boolean; onConfirm: () => void | Promise; } export function ConfirmDialog({ open, onOpenChange, title, description, confirmText = '确认', cancelText = '取消', destructive = false, onConfirm, }: ConfirmDialogProps) { const [busy, setBusy] = useState(false); const handleConfirm = async () => { setBusy(true); try { await onConfirm(); onOpenChange(false); } finally { setBusy(false); } }; return ( {title} {description && {description}} {cancelText} { e.preventDefault(); void handleConfirm(); }} className={cn(destructive && buttonVariants({ variant: 'destructive' }))} disabled={busy} > {busy ? '处理中…' : confirmText} ); }