108 lines
3.5 KiB
TypeScript
108 lines
3.5 KiB
TypeScript
import type { Reference, ReferenceType } from '@faunosteo/core-wasm/types'
|
|
import ActionButton from '@/components/editor/fields/ActionButton'
|
|
import MoveButtons from '@/components/editor/fields/MoveButtons'
|
|
import SelectField from '@/components/editor/fields/SelectField'
|
|
import TextField from '@/components/editor/fields/TextField'
|
|
import { FOCUS_CLASS, focusAttrs } from '@/editor/focusRequest'
|
|
import { isBlank, nullable } from '@/editor/sheetDocument'
|
|
import { useEditCommand } from '@/editor/useEditCommand'
|
|
import { useT } from '@/i18n/useT'
|
|
import { REFERENCE_TYPES } from './referenceTypes'
|
|
|
|
const CARD = 'space-y-3 rounded-lg border border-[var(--color-border)] p-3'
|
|
|
|
/** One reference: fields commit one by one, ↑ / ↓ reorder, delete asks the core. */
|
|
export default function ReferenceRow({
|
|
reference,
|
|
index,
|
|
count,
|
|
readOnly,
|
|
onDelete,
|
|
}: {
|
|
reference: Reference
|
|
index: number
|
|
count: number
|
|
readOnly: boolean
|
|
onDelete: () => void
|
|
}) {
|
|
const t = useT()
|
|
const edit = useEditCommand()
|
|
const key = { kind: 'reference', id: reference.id } as const
|
|
const send = (field: string, patch: Partial<Reference>) =>
|
|
edit.run(field, {
|
|
op: 'update',
|
|
key,
|
|
element: { kind: 'reference', value: { ...reference, ...patch } },
|
|
})
|
|
const options = REFERENCE_TYPES.map((type) => ({
|
|
value: type,
|
|
label: t(`editor.reference.type.${type}`),
|
|
}))
|
|
const prefix = `reference-${reference.id}`
|
|
return (
|
|
<li {...focusAttrs(key)} className={`${CARD} ${FOCUS_CLASS}`}>
|
|
<div className="grid gap-3 md:grid-cols-2">
|
|
<SelectField
|
|
id={`${prefix}-type`}
|
|
label={t('editor.reference.type')}
|
|
value={reference.type}
|
|
options={options}
|
|
onCommit={(type) => send('type', { type: type as ReferenceType })}
|
|
readOnly={readOnly}
|
|
error={edit.fieldError('type')}
|
|
/>
|
|
<TextField
|
|
id={`${prefix}-citation`}
|
|
label={t('editor.reference.citation')}
|
|
value={reference.citation}
|
|
onCommit={(value) =>
|
|
isBlank(value)
|
|
? edit.reject('citation', 'editor.field.required')
|
|
: send('citation', { citation: value })
|
|
}
|
|
readOnly={readOnly}
|
|
multiline
|
|
error={edit.fieldError('citation')}
|
|
/>
|
|
<TextField
|
|
id={`${prefix}-url`}
|
|
label={t('editor.reference.url')}
|
|
value={reference.url ?? ''}
|
|
onCommit={(value) => send('url', { url: nullable(value) })}
|
|
readOnly={readOnly}
|
|
error={edit.fieldError('url')}
|
|
/>
|
|
<TextField
|
|
id={`${prefix}-doi`}
|
|
label={t('editor.reference.doi')}
|
|
value={reference.doi ?? ''}
|
|
onCommit={(value) => send('doi', { doi: nullable(value) })}
|
|
readOnly={readOnly}
|
|
error={edit.fieldError('doi')}
|
|
/>
|
|
</div>
|
|
<div className="flex flex-wrap items-center justify-end gap-1">
|
|
<MoveButtons
|
|
index={index}
|
|
count={count}
|
|
readOnly={readOnly}
|
|
onMove={(position) => edit.run('move', { op: 'move', key, position })}
|
|
/>
|
|
<ActionButton
|
|
readOnly={readOnly}
|
|
variant="danger"
|
|
size="sm"
|
|
onClick={onDelete}
|
|
>
|
|
{t('editor.action.delete')}
|
|
</ActionButton>
|
|
</div>
|
|
{edit.fieldError('move') && (
|
|
<p role="alert" className="text-sm text-[var(--color-danger-strong)]">
|
|
{edit.fieldError('move')}
|
|
</p>
|
|
)}
|
|
</li>
|
|
)
|
|
}
|