Files
faunosteo/apps/app/src/components/editor/findings/FindingDetail.tsx
T

145 lines
4.1 KiB
TypeScript

import type { EditCommand } from '@faunosteo/core-wasm/types'
import ActionButton from '@/components/editor/fields/ActionButton'
import MoveButtons from '@/components/editor/fields/MoveButtons'
import TextField from '@/components/editor/fields/TextField'
import StatusBadge from '@/components/editor/findings/StatusBadge'
import { withName, type FindingItem } from '@/editor/findings/tree'
import { isBlank } from '@/editor/sheetDocument'
import { useEditCommand, type EditCommandApi } from '@/editor/useEditCommand'
import type { Locale } from '@/i18n/types'
import { useT } from '@/i18n/useT'
export interface FindingDetailProps {
item: FindingItem
readOnly: boolean
onDelete: () => void
}
/** The selected element: its names, its review state and the actions on it. */
export default function FindingDetail({
item,
readOnly,
onDelete,
}: FindingDetailProps) {
const t = useT()
const edit = useEditCommand()
return (
<div className="space-y-3 rounded-lg border border-[var(--color-border)] p-3">
<div className="flex flex-wrap items-center gap-2">
<p className="text-sm font-medium">
{t(`editor.findings.type.${item.kind}`)}
</p>
{item.status && <StatusBadge status={item.status} />}
</div>
<Names item={item} readOnly={readOnly} edit={edit} />
<div className="flex flex-wrap items-center justify-end gap-1">
<ReviewActions item={item} readOnly={readOnly} edit={edit} />
<MoveButtons
index={item.index}
count={item.siblings}
readOnly={readOnly}
onMove={(position) =>
edit.run('actions', { op: 'move', key: item.key, position })
}
/>
<ActionButton
readOnly={readOnly}
variant="danger"
size="sm"
onClick={onDelete}
>
{t('editor.action.delete')}
</ActionButton>
</div>
{edit.fieldError('actions') && (
<p role="alert" className="text-sm text-[var(--color-danger-strong)]">
{edit.fieldError('actions')}
</p>
)}
</div>
)
}
function Names({
item,
readOnly,
edit,
}: {
item: FindingItem
readOnly: boolean
edit: EditCommandApi
}) {
const t = useT()
const rename = (locale: Locale) => (value: string) =>
isBlank(value)
? edit.reject(locale, 'editor.field.required')
: edit.run(locale, {
op: 'update',
key: item.key,
element: withName(item, { ...item.name, [locale]: value }),
})
return (
<div className="grid gap-3 md:grid-cols-2">
{(['fr', 'en'] as const).map((locale) => (
<TextField
key={`${item.id}-${locale}`}
id={`finding-name-${locale}`}
label={t(`editor.general.name_${locale}`)}
value={item.name[locale]}
onCommit={rename(locale)}
readOnly={readOnly}
error={edit.fieldError(locale)}
/>
))}
</div>
)
}
function statusCommand(
item: FindingItem,
status: 'reviewed' | 'to_confirm',
): EditCommand {
const wholeBranch = status === 'reviewed' && item.kind !== 'node'
return wholeBranch
? { op: 'mark_reviewed', key: item.key }
: { op: 'set_status', key: item.key, status }
}
function ReviewActions({
item,
readOnly,
edit,
}: {
item: FindingItem
readOnly: boolean
edit: EditCommandApi
}) {
const t = useT()
const canMark = item.kind === 'zone' || item.status !== 'reviewed'
const canReset = item.kind !== 'zone' && item.status === 'reviewed'
return (
<>
{canMark && (
<ActionButton
readOnly={readOnly}
variant="secondary"
size="sm"
onClick={() => edit.run('actions', statusCommand(item, 'reviewed'))}
>
{t('editor.findings.mark_reviewed')}
</ActionButton>
)}
{canReset && (
<ActionButton
readOnly={readOnly}
variant="secondary"
size="sm"
onClick={() => edit.run('actions', statusCommand(item, 'to_confirm'))}
>
{t('editor.findings.reset_to_confirm')}
</ActionButton>
)}
</>
)
}