import React, { useEffect, useCallback } from 'react' import { createPortal } from 'react-dom' import { AlertTriangle } from 'lucide-react' import { useTranslation } from '../../i18n' interface ConfirmDialogProps { isOpen: boolean onClose: () => void onConfirm: () => void title?: string message?: string confirmLabel?: string cancelLabel?: string danger?: boolean } // Callers commonly pass an async handler that reports its own failure and then // rethrows. The dialog has already closed at that point, so the rejection would // escape as an unhandled promise — absorb it here. function runConfirm(onConfirm: () => void): void { const result = onConfirm() as unknown if (result && typeof (result as PromiseLike).then === 'function') { void Promise.resolve(result).catch(() => {}) } } export default function ConfirmDialog({ isOpen, onClose, onConfirm, title, message, confirmLabel, cancelLabel, danger = true, }: ConfirmDialogProps) { const { t } = useTranslation() const handleEsc = useCallback((e: KeyboardEvent) => { if (e.key === 'Escape') onClose() }, [onClose]) useEffect(() => { if (isOpen) { document.addEventListener('keydown', handleEsc) } return () => document.removeEventListener('keydown', handleEsc) }, [isOpen, handleEsc]) if (!isOpen) return null return createPortal(
e.stopPropagation()} >
{danger && (
)}

{title || t('common.confirm')}

{message}

, document.body ) }