feat(app): editor Annotations tab, core-decided placement, repair rules
This commit is contained in:
1 parent
92c595caa1
commit
26097852ff
37 files changed
+5001
-6
No files matched your search
@@ -0,0 +1,244 @@
|
||||
import { useState, type Dispatch } from 'react'
|
||||
import type {
|
||||
AnatomicalSheetDocument,
|
||||
Annotation,
|
||||
Geometry,
|
||||
Marking,
|
||||
} from '@faunosteo/core-wasm/types'
|
||||
import ActionButton from '@/components/editor/fields/ActionButton'
|
||||
import DeleteDialog from '@/components/editor/DeleteDialog'
|
||||
import Button from '@/components/ui/Button'
|
||||
import {
|
||||
updateGeometry,
|
||||
type CommitContext,
|
||||
} from '@/editor/annotations/annotationCommit'
|
||||
import { isEditable } from '@/editor/annotations/geometryEdit'
|
||||
import type { UiAction } from '@/editor/annotations/uiState'
|
||||
import { geometryErrorsById } from '@/editor/issues'
|
||||
import type { EditorTabId } from '@/editor/tabs'
|
||||
import { useEditCommand } from '@/editor/useEditCommand'
|
||||
import { useEditorStore } from '@/editor/state/editorStore'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
import AnnotationFields from './AnnotationFields'
|
||||
import type { ApplyResult } from './GeometryGroup'
|
||||
import { annotationName } from './AnnotationList'
|
||||
import GeometryGroup from './GeometryGroup'
|
||||
import ReviewBadge from './ReviewBadge'
|
||||
|
||||
export interface PropertiesPanelProps {
|
||||
annotation: Annotation
|
||||
doc: AnatomicalSheetDocument
|
||||
/** Not editable now: every field shows as text and no action is offered. */
|
||||
readOnly: boolean
|
||||
/** The view is not measured: geometry is shown as text only, the rest stays editable. */
|
||||
geometryLocked: boolean
|
||||
dispatch: Dispatch<UiAction>
|
||||
context: CommitContext
|
||||
onSelectTab: (tab: EditorTabId) => void
|
||||
}
|
||||
|
||||
/** What still names this annotation as invalid: never hidden, never shown as valid. */
|
||||
function useGeometryIssues(id: string): string[] {
|
||||
const state = useEditorStore((s) => s.previewState)
|
||||
const issues = state ? geometryErrorsById(state).get(id) : undefined
|
||||
return [...new Set((issues ?? []).map((issue) => issue.message))]
|
||||
}
|
||||
|
||||
export default function PropertiesPanel(props: PropertiesPanelProps) {
|
||||
const t = useT()
|
||||
const locale = useLocaleStore((s) => s.locale)
|
||||
const { annotation, doc, readOnly } = props
|
||||
const edit = useEditCommand()
|
||||
const name = annotationName(annotation, doc.views, locale)
|
||||
return (
|
||||
<section
|
||||
aria-label={t('editor.annotations.properties')}
|
||||
data-testid="annotation-properties"
|
||||
className="space-y-4 rounded-xl border border-[var(--color-border)] p-4"
|
||||
>
|
||||
<header className="flex items-start justify-between gap-2">
|
||||
<h3 className="text-base font-semibold">
|
||||
{t(`editor.annotations.kind.${annotation.kind}`)}
|
||||
</h3>
|
||||
<ReviewBadge status={annotation.review_status} />
|
||||
</header>
|
||||
<AnnotationFields
|
||||
annotation={annotation}
|
||||
doc={doc}
|
||||
readOnly={readOnly}
|
||||
edit={edit}
|
||||
/>
|
||||
<GeometrySection {...props} />
|
||||
<StatusActions annotation={annotation} readOnly={readOnly} edit={edit} />
|
||||
<Deletion id={annotation.id} name={name} readOnly={readOnly} />
|
||||
</section>
|
||||
)
|
||||
}
|
||||
|
||||
function GeometrySection(props: PropertiesPanelProps) {
|
||||
const t = useT()
|
||||
const { annotation, readOnly, geometryLocked } = props
|
||||
const locked = readOnly || geometryLocked
|
||||
const { geometry } = annotation
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
{geometryLocked && !readOnly && <UnmeasuredHint {...props} />}
|
||||
{isEditable(geometry) ? (
|
||||
<GeometryGroup
|
||||
key={annotation.id}
|
||||
idPrefix="annotation"
|
||||
geometry={geometry}
|
||||
locked={locked}
|
||||
onApply={(next) => applyGeometry(annotation, next, props.context)}
|
||||
/>
|
||||
) : (
|
||||
<p className="text-sm text-[var(--color-ink-muted)]">
|
||||
{t(
|
||||
geometry === null
|
||||
? 'editor.annotations.no_geometry'
|
||||
: 'editor.annotations.path',
|
||||
)}
|
||||
</p>
|
||||
)}
|
||||
{!locked && !isEditable(geometry) && (
|
||||
<TraceReplacement id={annotation.id} dispatch={props.dispatch} />
|
||||
)}
|
||||
<GeometryIssues id={annotation.id} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function applyGeometry(
|
||||
annotation: Annotation,
|
||||
next: Geometry,
|
||||
context: CommitContext,
|
||||
): ApplyResult {
|
||||
const result = updateGeometry(annotation, next, context)
|
||||
return result.ok ? { ok: true } : { ok: false, messages: result.messages }
|
||||
}
|
||||
|
||||
function TraceReplacement({
|
||||
id,
|
||||
dispatch,
|
||||
}: {
|
||||
id: string
|
||||
dispatch: Dispatch<UiAction>
|
||||
}) {
|
||||
const t = useT()
|
||||
return (
|
||||
<Button
|
||||
type="button"
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
onClick={() =>
|
||||
dispatch({ type: 'trace_start', target: 'replace', annotationId: id })
|
||||
}
|
||||
>
|
||||
{t('editor.annotations.replace_polygon')}
|
||||
</Button>
|
||||
)
|
||||
}
|
||||
|
||||
function GeometryIssues({ id }: { id: string }) {
|
||||
const issues = useGeometryIssues(id)
|
||||
if (issues.length === 0) return null
|
||||
return (
|
||||
<ul
|
||||
role="alert"
|
||||
data-testid="annotation-geometry-issues"
|
||||
className="list-disc pl-5 text-sm text-[var(--color-danger-strong)]"
|
||||
>
|
||||
{issues.map((issue) => (
|
||||
<li key={issue}>{issue}</li>
|
||||
))}
|
||||
</ul>
|
||||
)
|
||||
}
|
||||
|
||||
export function UnmeasuredHint({
|
||||
onSelectTab,
|
||||
}: Pick<PropertiesPanelProps, 'onSelectTab'>) {
|
||||
const t = useT()
|
||||
return (
|
||||
<p className="text-sm text-[var(--color-ink-muted)]">
|
||||
{t('editor.annotations.unmeasured.hint')}{' '}
|
||||
<button
|
||||
type="button"
|
||||
className="underline"
|
||||
onClick={() => onSelectTab('views')}
|
||||
>
|
||||
{t('editor.tab.views')}
|
||||
</button>
|
||||
</p>
|
||||
)
|
||||
}
|
||||
|
||||
function StatusActions({
|
||||
annotation,
|
||||
readOnly,
|
||||
edit,
|
||||
}: {
|
||||
annotation: Annotation
|
||||
readOnly: boolean
|
||||
edit: ReturnType<typeof useEditCommand>
|
||||
}) {
|
||||
const t = useT()
|
||||
const mark = (status: Marking) =>
|
||||
edit.run('status', {
|
||||
op: 'set_status',
|
||||
key: { kind: 'annotation', id: annotation.id },
|
||||
status,
|
||||
})
|
||||
const reviewed = annotation.review_status === 'reviewed'
|
||||
return (
|
||||
<div className="space-y-1">
|
||||
<ActionButton
|
||||
readOnly={readOnly}
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
onClick={() => mark(reviewed ? 'to_confirm' : 'reviewed')}
|
||||
>
|
||||
{t(
|
||||
reviewed
|
||||
? 'editor.annotations.to_confirm'
|
||||
: 'editor.annotations.mark_reviewed',
|
||||
)}
|
||||
</ActionButton>
|
||||
{edit.fieldError('status') && (
|
||||
<p role="alert" className="text-sm text-[var(--color-danger-strong)]">
|
||||
{edit.fieldError('status')}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function Deletion({
|
||||
id,
|
||||
name,
|
||||
readOnly,
|
||||
}: {
|
||||
id: string
|
||||
name: string
|
||||
readOnly: boolean
|
||||
}) {
|
||||
const t = useT()
|
||||
const [open, setOpen] = useState(false)
|
||||
return (
|
||||
<>
|
||||
<ActionButton
|
||||
readOnly={readOnly}
|
||||
variant="danger"
|
||||
size="sm"
|
||||
onClick={() => setOpen(true)}
|
||||
>
|
||||
{t('editor.action.delete')}
|
||||
</ActionButton>
|
||||
<DeleteDialog
|
||||
target={open ? { key: { kind: 'annotation', id }, label: name } : null}
|
||||
onClose={() => setOpen(false)}
|
||||
/>
|
||||
</>
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user