130 lines
3.8 KiB
TypeScript
130 lines
3.8 KiB
TypeScript
import { useState } from 'react'
|
|
import type {
|
|
AnatomicalSheetDocument,
|
|
Attribution,
|
|
} from '@faunosteo/core-wasm/types'
|
|
import ActionButton from '@/components/editor/fields/ActionButton'
|
|
import DraftField from '@/components/editor/fields/DraftField'
|
|
import TextField from '@/components/editor/fields/TextField'
|
|
import { isBlank, nullable, textsOf } from '@/editor/sheetDocument'
|
|
import { useEditCommand } from '@/editor/useEditCommand'
|
|
import { useT } from '@/i18n/useT'
|
|
|
|
/** Authors (add / remove), licence and notes. */
|
|
export default function AttributionSection({
|
|
doc,
|
|
readOnly,
|
|
}: {
|
|
doc: AnatomicalSheetDocument
|
|
readOnly: boolean
|
|
}) {
|
|
const t = useT()
|
|
const edit = useEditCommand()
|
|
const attribution = doc.attribution
|
|
const send = (field: string, patch: Partial<Attribution>) =>
|
|
edit.run(field, {
|
|
op: 'update',
|
|
key: { kind: 'sheet' },
|
|
element: {
|
|
kind: 'sheet',
|
|
value: { ...textsOf(doc), attribution: { ...attribution, ...patch } },
|
|
},
|
|
})
|
|
|
|
return (
|
|
<section aria-labelledby="general-attribution" className="space-y-3">
|
|
<h3 id="general-attribution" className="text-base font-semibold">
|
|
{t('editor.general.attribution')}
|
|
</h3>
|
|
<Authors
|
|
authors={attribution.authors}
|
|
readOnly={readOnly}
|
|
error={edit.fieldError('authors')}
|
|
onChange={(authors) => send('authors', { authors })}
|
|
/>
|
|
<TextField
|
|
id="general-license"
|
|
label={t('editor.general.license')}
|
|
value={attribution.license ?? ''}
|
|
onCommit={(value) => send('license', { license: nullable(value) })}
|
|
readOnly={readOnly}
|
|
error={edit.fieldError('license')}
|
|
/>
|
|
<TextField
|
|
id="general-notes"
|
|
label={t('editor.general.notes')}
|
|
value={attribution.notes ?? ''}
|
|
onCommit={(value) => send('notes', { notes: nullable(value) })}
|
|
readOnly={readOnly}
|
|
multiline
|
|
error={edit.fieldError('notes')}
|
|
/>
|
|
</section>
|
|
)
|
|
}
|
|
|
|
function Authors({
|
|
authors,
|
|
readOnly,
|
|
error,
|
|
onChange,
|
|
}: {
|
|
authors: readonly string[]
|
|
readOnly: boolean
|
|
error?: string
|
|
onChange: (authors: string[]) => boolean
|
|
}) {
|
|
const t = useT()
|
|
const [draft, setDraft] = useState('')
|
|
const add = () => {
|
|
if (isBlank(draft)) return
|
|
if (onChange([...authors, draft])) setDraft('')
|
|
}
|
|
return (
|
|
<div className="space-y-2">
|
|
<p className="text-sm font-medium">{t('editor.general.authors')}</p>
|
|
{authors.length === 0 && (
|
|
<p className="text-sm text-[var(--color-ink-muted)]">—</p>
|
|
)}
|
|
<ul className="space-y-1">
|
|
{authors.map((author, index) => (
|
|
<li
|
|
key={`${index}-${author}`}
|
|
className="flex items-center gap-2 text-sm"
|
|
>
|
|
<span className="min-w-0 flex-1 break-words">{author}</span>
|
|
<ActionButton
|
|
readOnly={readOnly}
|
|
variant="ghost"
|
|
size="sm"
|
|
aria-label={t('editor.general.author_remove').replace(
|
|
'{name}',
|
|
author,
|
|
)}
|
|
onClick={() => onChange(authors.filter((_, i) => i !== index))}
|
|
>
|
|
{t('editor.action.remove')}
|
|
</ActionButton>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
{!readOnly && (
|
|
<div className="flex items-end gap-2">
|
|
<div className="min-w-0 flex-1">
|
|
<DraftField
|
|
id="general-author-new"
|
|
label={t('editor.general.author_new')}
|
|
value={draft}
|
|
onChange={setDraft}
|
|
error={error}
|
|
/>
|
|
</div>
|
|
<ActionButton readOnly={readOnly} variant="secondary" onClick={add}>
|
|
{t('editor.general.author_add')}
|
|
</ActionButton>
|
|
</div>
|
|
)}
|
|
</div>
|
|
)
|
|
}
|