import React, { useState, useEffect } from "react"; export type DialogType = | "info" | "warning" | "error" | "success" | "delete" | "confirm"; interface DialogConfig { title: string; icon: React.ReactNode; confirmButtonColor: "red" | "orange" | "blue" | "green"; headerBgColor: string; iconBgColor: string; iconColor: string; subtitle?: string; } interface ConfirmDialogProps { isOpen: boolean; onClose: () => void; onConfirm: () => void; type?: DialogType; title?: string; message: string | React.ReactNode; confirmText?: string; cancelText?: string; icon?: React.ReactNode; showWarning?: boolean; } /** * ConfirmDialog Component * Componente reutilizável para exibir diálogos de confirmação customizados * Mantém o tema do projeto com cores e estilos consistentes * Suporta diferentes tipos: info, warning, error, success, delete, confirm * * @param isOpen - Controla se o dialog está aberto * @param onClose - Callback chamado ao fechar/cancelar * @param onConfirm - Callback chamado ao confirmar * @param type - Tipo do diálogo (info, warning, error, success, delete, confirm) * @param title - Título do dialog (opcional, será definido pelo tipo se não fornecido) * @param message - Mensagem principal do dialog * @param confirmText - Texto do botão de confirmação (opcional, será definido pelo tipo se não fornecido) * @param cancelText - Texto do botão de cancelamento (padrão: "Cancelar") * @param icon - Ícone customizado (opcional, será definido pelo tipo se não fornecido) * @param showWarning - Se deve mostrar subtítulo de atenção (padrão: true para warning/error/delete) */ const ConfirmDialog: React.FC = ({ isOpen, onClose, onConfirm, type = "confirm", title, message, confirmText, cancelText = "Cancelar", icon, showWarning, }) => { const [isAnimating, setIsAnimating] = useState(false); const [shouldRender, setShouldRender] = useState(false); // Configurações por tipo de diálogo const getDialogConfig = (dialogType: DialogType): DialogConfig => { const configs: Record = { info: { title: "Informação", icon: ( ), confirmButtonColor: "blue", headerBgColor: "bg-[#002147]", iconBgColor: "bg-blue-500/20", iconColor: "text-blue-400", subtitle: "Informação", }, warning: { title: "Atenção", icon: ( ), confirmButtonColor: "orange", headerBgColor: "bg-[#002147]", iconBgColor: "bg-orange-500/20", iconColor: "text-orange-400", subtitle: "Atenção", }, error: { title: "Erro", icon: ( ), confirmButtonColor: "red", headerBgColor: "bg-[#002147]", iconBgColor: "bg-red-500/20", iconColor: "text-red-400", subtitle: "Erro", }, success: { title: "Sucesso", icon: ( ), confirmButtonColor: "green", headerBgColor: "bg-[#002147]", iconBgColor: "bg-green-500/20", iconColor: "text-green-400", subtitle: "Sucesso", }, delete: { title: "Confirmar Exclusão", icon: ( ), confirmButtonColor: "red", headerBgColor: "bg-[#002147]", iconBgColor: "bg-red-500/20", iconColor: "text-red-400", subtitle: "Atenção", }, confirm: { title: "Confirmar Ação", icon: ( ), confirmButtonColor: "orange", headerBgColor: "bg-[#002147]", iconBgColor: "bg-orange-500/20", iconColor: "text-orange-400", subtitle: "Confirmação", }, }; return configs[dialogType]; }; // Obter configuração do tipo const config = getDialogConfig(type as DialogType); const finalTitle = title || config.title; const finalIcon = icon || config.icon; const finalConfirmText = confirmText || (type === "delete" ? "Excluir" : type === "success" || type === "error" || type === "info" ? "OK" : "Confirmar"); const shouldShowWarning = showWarning !== undefined ? showWarning : ["warning", "error", "delete"].includes(type); useEffect(() => { if (isOpen) { setShouldRender(true); // Pequeno delay para garantir que o DOM está pronto antes de iniciar a animação setTimeout(() => setIsAnimating(true), 10); } else { // Iniciar animação de saída setIsAnimating(false); // Remover do DOM após a animação terminar const timer = setTimeout(() => setShouldRender(false), 300); return () => clearTimeout(timer); } }, [isOpen]); // Não renderizar se não deve estar visível if (!shouldRender) return null; const handleConfirm = () => { setIsAnimating(false); setTimeout(() => { onConfirm(); onClose(); }, 300); }; const handleCancel = () => { setIsAnimating(false); setTimeout(() => { onClose(); }, 300); }; // Cores do botão de confirmação const confirmButtonClasses = { red: "bg-red-500 hover:bg-red-600 shadow-red-500/20", orange: "bg-orange-500 hover:bg-orange-600 shadow-orange-500/20", blue: "bg-blue-500 hover:bg-blue-600 shadow-blue-500/20", green: "bg-green-500 hover:bg-green-600 shadow-green-500/20", }; return (
{/* Overlay */}
{/* Dialog - Altura consistente em todos os dispositivos */}
{/* Header */}
{finalIcon && (
{finalIcon}
)}

{finalTitle}

{shouldShowWarning && config.subtitle && (

{config.subtitle}

)}
{/* Content */}
{typeof message === "string" ? (

{message}

) : (
{message}
)}
{/* Actions */}
{/* Mostrar botão de cancelar se: - For tipo info (permite dois botões) OU - Não for tipo success/error E finalConfirmText não é "OK" */} {type === "info" || (type !== "success" && type !== "error" && finalConfirmText !== "OK") ? ( ) : null}
); }; export default ConfirmDialog;