import React, { useEffect, useState } from 'react'; import { createPortal } from 'react-dom'; import { Button, Input } from '../shared/ui/controls'; import editorStyles from '../editor/EditorApp.module.css'; import { useEditorI18n } from '../editor/i18n/EditorI18nContext'; type NpcRelationModalProps = { open: boolean; initialLabel?: string; onClose: () => void; onSave: (label: string) => Promise; }; export function NpcRelationModal({ open, initialLabel = '', onClose, onSave }: NpcRelationModalProps) { const { t } = useEditorI18n(); const [label, setLabel] = useState(initialLabel); const [saving, setSaving] = useState(false); const [error, setError] = useState(null); useEffect(() => { if (!open) return; setLabel(initialLabel); setSaving(false); setError(null); }, [initialLabel, open]); useEffect(() => { if (!open) return; const onKey = (e: KeyboardEvent) => { if (e.key === 'Escape') onClose(); }; window.addEventListener('keydown', onKey); return () => window.removeEventListener('keydown', onKey); }, [onClose, open]); const trimmed = label.trim(); const canSave = trimmed.length >= 1 && !saving; if (!open) return null; return createPortal( <>
{t('npcs.relationLabel')}
{trimmed.length < 1 ? (
{t('npcs.relationLabelRequired')}
) : null}
{error ?
{error}
: null}
, document.body, ); }