import { useEffect, useState } from 'react'; import { Button, Divider, Drawer, NumberInput, Stack, Text, TextInput } from '@mantine/core'; import { useTranslation } from 'react-i18next'; import { BilingualInput, ModalFooter } from '@ema-platform/ui'; import type { FormSchemaPalette, FormSectionConfig } from '@ema-platform/api'; import { ConditionBuilder, type ConditionValue } from './ConditionBuilder'; import type { ConditionTarget } from '../config/schema-paths'; const KEY_PATTERN = /^[A-Za-z][A-Za-z0-9_]*$/; function emptySection(): FormSectionConfig { return { key: '', title: { en: '', am: '' }, fields: [] }; } /** Adds/edits one section's own metadata — its fields are managed on the list, not here. */ export function SectionEditorDrawer({ opened, onClose, section, onSave, palette, conditionTargets, }: { opened: boolean; onClose: () => void; /** Null = adding a new section. */ section: FormSectionConfig | null; onSave: (section: Omit) => void; palette: FormSchemaPalette | undefined; conditionTargets: ConditionTarget[]; }) { const { t } = useTranslation(); const [draft, setDraft] = useState(emptySection()); const [keyError, setKeyError] = useState(null); const isNew = !section; useEffect(() => { if (opened) { setDraft(section ? { ...section, title: { ...section.title } } : emptySection()); setKeyError(null); } }, [opened, section]); function save() { if (!draft.key.trim() || !KEY_PATTERN.test(draft.key.trim())) { setKeyError(t('certReq.section.keyInvalid', 'Key must start with a letter and contain only letters, numbers, underscores')); return; } if (!draft.title.en?.trim()) return; const { fields: _fields, ...meta } = draft; onSave({ ...meta, key: draft.key.trim() }); } return ( {isNew ? t('certReq.section.add', 'Add section') : t('certReq.section.edit', 'Edit section')}} > { const { value } = e.currentTarget; setDraft((d) => ({ ...d, key: value })); }} /> setDraft((d) => ({ ...d, title: v }))} /> setDraft((d) => ({ ...d, description: v }))} /> { const { value } = e.currentTarget; setDraft((d) => ({ ...d, group: value || undefined })); }} /> setDraft((d) => ({ ...d, groupOrder: typeof v === 'number' ? v : undefined }))} /> setDraft((d) => ({ ...d, sortOrder: typeof v === 'number' ? v : undefined }))} /> setDraft((d) => ({ ...d, showWhen: v ?? undefined }))} targets={conditionTargets} palette={palette} /> ); }