import { useState } from 'react' import type { ReferenceType } from '@faunosteo/core-wasm/types' import ActionButton from '@/components/editor/fields/ActionButton' import DraftField from '@/components/editor/fields/DraftField' import SelectField from '@/components/editor/fields/SelectField' import { isBlank, nullable } from '@/editor/sheetDocument' import { useEditCommand } from '@/editor/useEditCommand' import { useT } from '@/i18n/useT' import { REFERENCE_TYPES, type ReferenceHost } from './referenceTypes' const EMPTY = { type: '', citation: '', url: '', doi: '' } /** * Adds a reference: the type and the citation are required, URL and DOI optional. * `onAdded` runs after a successful add, so the list can show the new reference. */ export default function ReferenceForm({ host, onAdded, }: { host: ReferenceHost onAdded?: () => void }) { const t = useT() const edit = useEditCommand() const [form, setForm] = useState(EMPTY) const set = (patch: Partial) => setForm({ ...form, ...patch }) const options = [ { value: '', label: t('editor.field.choose') }, ...REFERENCE_TYPES.map((type) => ({ value: type, label: t(`editor.reference.type.${type}`), })), ] const create = () => { if (form.type === '' || isBlank(form.citation)) { edit.reject('form', 'editor.reference.required') return } const added = edit.runNew('form', 'ref', (id) => ({ op: 'insert', element: { kind: 'reference', value: { id, type: form.type as ReferenceType, citation: form.citation, url: nullable(form.url), doi: nullable(form.doi), }, }, })) if (!added) return setForm(EMPTY) onAdded?.() } return (

{t('editor.reference.new')}

set({ type })} /> set({ citation })} multiline /> set({ url })} /> set({ doi })} />
{edit.fieldError('form') && (

{edit.fieldError('form')}

)} {t('editor.reference.add')}
) }