'use client'; import { useState, type ReactNode } from 'react'; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from '@/components/ui/alert-dialog'; import { AlertTriangle, AlertCircle, Info, CheckCircle } from 'lucide-react'; import { cn } from '@/lib/utils'; export type ConfirmDialogVariant = 'danger' | 'warning' | 'info' | 'success'; interface ConfirmDialogProps { /** Whether the dialog is open */ open: boolean; /** Callback when open state changes */ onOpenChange: (open: boolean) => void; /** Dialog title */ title: string; /** Dialog description/content */ description: ReactNode; /** Cancel button text (default: "Cancel") */ cancelText?: string; /** Confirm button text (default: "Confirm") */ confirmText?: string; /** Dialog variant/type */ variant?: ConfirmDialogVariant; /** Callback when user confirms */ onConfirm: () => void | Promise; /** Callback when user cancels */ onCancel?: () => void; /** Whether the confirm action is destructive */ destructive?: boolean; } const VARIANT_ICONS = { danger: AlertTriangle, warning: AlertCircle, info: Info, success: CheckCircle, }; const VARIANT_TITLE_COLORS = { danger: 'text-[var(--color-danger)]', warning: 'text-[var(--color-warning)]', info: 'text-[var(--color-info)]', success: 'text-[var(--color-success)]', }; const VARIANT_ACTION_COLORS = { danger: 'bg-[var(--color-danger)] hover:bg-[var(--interactive-danger-hover)] text-white', warning: 'bg-[var(--color-warning)] hover:opacity-90 text-black', info: 'bg-[var(--color-info)] hover:opacity-90 text-white', success: 'bg-[var(--color-success)] hover:opacity-90 text-white', }; /** * Reusable confirmation dialog component. * Uses the existing shadcn/ui AlertDialog. * * @example * */ export function ConfirmDialog({ open, onOpenChange, title, description, cancelText = 'Cancel', confirmText = 'Confirm', variant = 'warning', onConfirm, onCancel, destructive = false, }: ConfirmDialogProps) { const [isLoading, setIsLoading] = useState(false); const Icon = VARIANT_ICONS[variant]; const titleColor = VARIANT_TITLE_COLORS[variant]; const actionClass = destructive ? VARIANT_ACTION_COLORS.danger : VARIANT_ACTION_COLORS[variant]; const handleConfirm = async () => { setIsLoading(true); try { await onConfirm(); onOpenChange(false); } finally { setIsLoading(false); } }; const handleCancel = () => { onCancel?.(); onOpenChange(false); }; return ( {title} {description} {cancelText} {isLoading ? 'Processing...' : confirmText} ); } /** * Hook to easily manage a confirmation dialog state. * * @example * const { dialogProps, showConfirm } = useConfirmDialog(); * * showConfirm({ * title: "Delete Item", * description: "Are you sure?", * onConfirm: () => deleteItem(), * }); */ export function useConfirmDialog() { const [dialogState, setDialogState] = useState<{ open: boolean; props: Omit; }>({ open: false, props: { title: '', description: '', onConfirm: () => {}, }, }); const showConfirm = (props: Omit) => { setDialogState({ open: true, props }); }; const dialogProps: ConfirmDialogProps = { open: dialogState.open, onOpenChange: (open: boolean) => setDialogState(prev => ({ ...prev, open })), ...dialogState.props, }; return { dialogProps, showConfirm, ConfirmDialogComponent: , }; } export default ConfirmDialog;