import {FormEvent, useEffect, useState} from 'react' import {X} from 'lucide-react' interface JsonRecordModalProps { open: boolean title: string description: string initialValue: string isPending: boolean submitError?: string onClose: () => void onSubmit: (document: Record) => void } export function JsonRecordModal({ open, title, description, initialValue, isPending, submitError, onClose, onSubmit, }: JsonRecordModalProps) { const [value, setValue] = useState(initialValue) const [validationError, setValidationError] = useState() useEffect(() => { if (!open) return setValue(initialValue) setValidationError(undefined) }, [initialValue, open]) useEffect(() => { if (!open) return const closeOnEscape = (event: KeyboardEvent) => { if (event.key === 'Escape' && !isPending) onClose() } window.addEventListener('keydown', closeOnEscape) return () => window.removeEventListener('keydown', closeOnEscape) }, [isPending, onClose, open]) if (!open) return null function submit(event: FormEvent) { event.preventDefault() try { const parsed = JSON.parse(value) as unknown if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) { setValidationError('Record must be a JSON object.') return } setValidationError(undefined) onSubmit(parsed as Record) } catch (error) { setValidationError(error instanceof Error ? error.message : 'Invalid JSON.') } } return (
event.target === event.currentTarget && !isPending && onClose()}>

{title}

{description}