import { useState, useRef, useEffect } from 'react'; import { useTranslation } from 'react-i18next'; import { cn } from '../lib/utils'; import { Input } from './ui/input'; interface AddLevelDialogProps { open: boolean; onClose: () => void; onConfirm: (name: string, elevation: number) => void; defaultName: string; defaultElevation: number; title?: string; confirmLabel?: string; } export default function AddLevelDialog({ open, onClose, onConfirm, defaultName, defaultElevation, title, confirmLabel }: AddLevelDialogProps) { const { t } = useTranslation(); const [name, setName] = useState(defaultName); const [elevation, setElevation] = useState(String(defaultElevation)); const nameRef = useRef(null); const dialogTitle = title ?? t('dialog.addLevel'); const dialogConfirm = confirmLabel ?? t('dialog.add'); useEffect(() => { if (open) { setName(defaultName); setElevation(String(defaultElevation)); setTimeout(() => nameRef.current?.select(), 0); } }, [open, defaultName, defaultElevation]); // Esc closes from anywhere (capture phase, before the canvas shortcuts). useEffect(() => { if (!open) return; const onKey = (e: KeyboardEvent) => { if (e.key !== 'Escape') return; e.preventDefault(); e.stopPropagation(); onClose(); }; window.addEventListener('keydown', onKey, true); return () => window.removeEventListener('keydown', onKey, true); }, [open, onClose]); if (!open) return null; const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); const elev = parseFloat(elevation); if (isNaN(elev)) return; onConfirm(name.trim() || defaultName, elev); onClose(); }; return (
{ if (e.target === e.currentTarget) onClose(); }} >
{dialogTitle}
setName(e.target.value)} autoFocus /> setElevation(e.target.value)} />
); }