import { createPortal } from 'preact/compat'; import { useEffect, useState } from 'preact/hooks'; import type { ComponentChildren } from 'preact'; import { TriangleAlert } from 'lucide-preact'; import { t } from '@/lib/i18n'; interface ConfirmDialogProps { open: boolean; title: string; message: string; variant?: 'default' | 'warning'; showIcon?: boolean; confirmText?: string; cancelText?: string; danger?: boolean; hideCancel?: boolean; confirmDisabled?: boolean; cancelDisabled?: boolean; onConfirm: () => void; onCancel: () => void; children?: ComponentChildren; afterActions?: ComponentChildren; } function incrementDialogBodyLock() { if (typeof document === 'undefined') return; const body = document.body; const nextCount = Number(body.dataset.dialogCount || '0') + 1; body.dataset.dialogCount = String(nextCount); body.classList.add('dialog-open'); } function decrementDialogBodyLock() { if (typeof document === 'undefined') return; const body = document.body; const nextCount = Math.max(0, Number(body.dataset.dialogCount || '0') - 1); if (nextCount === 0) { delete body.dataset.dialogCount; body.classList.remove('dialog-open'); return; } body.dataset.dialogCount = String(nextCount); } export function useDialogLifecycle(active: boolean, onCancel?: (() => void) | null) { useEffect(() => { if (!active) return; incrementDialogBodyLock(); return () => decrementDialogBodyLock(); }, [active]); useEffect(() => { if (!active || !onCancel || typeof window === 'undefined') return; const handleKeyDown = (event: KeyboardEvent) => { if (event.key !== 'Escape') return; event.preventDefault(); onCancel(); }; window.addEventListener('keydown', handleKeyDown); return () => window.removeEventListener('keydown', handleKeyDown); }, [active, onCancel]); } export default function ConfirmDialog(props: ConfirmDialogProps) { const [present, setPresent] = useState(props.open); const [closing, setClosing] = useState(false); const canDismiss = !props.cancelDisabled && !closing && !props.hideCancel; useEffect(() => { if (props.open) { setPresent(true); setClosing(false); return; } if (!present) return; setClosing(true); const timer = window.setTimeout(() => { setPresent(false); setClosing(false); }, 240); return () => window.clearTimeout(timer); }, [props.open, present]); useDialogLifecycle(present, canDismiss ? props.onCancel : null); if (!present || typeof document === 'undefined') return null; return createPortal((