import { useState, type Dispatch } from 'react' import { flushSync } from 'react-dom' 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 { useDeselect } from '@/editor/annotations/deselect' import EditorGlyph from '@/components/editor/EditorGlyph' import { isEditable } from '@/editor/annotations/geometryEdit' import type { UiAction } from '@/editor/annotations/uiState' import { geometryErrorsById } from '@/editor/issues' import { focusRow } from '@/editor/focusRequest' import { targetOfKey } from '@/editor/navigation' import type { EditorPlaceProps } 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 context: CommitContext navigate: EditorPlaceProps['navigate'] /** Clears the selection: the overview shows again. */ onShowAll: () => void /** A tracing runs: the selection is not left from here. */ busy?: boolean /** Put before every id, for a twin of the panel that lives beside another one. */ idPrefix?: string } /** What still names this annotation as invalid: never hidden, never shown as valid. */ export 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 (

{t(`editor.annotations.kind.${annotation.kind}`)}

) } /** Back to the overview, focus on the row of the annotation that was open. */ function ShowAll({ id, onShowAll }: { id: string; onShowAll: () => void }) { const t = useT() return ( ) } /** Leaves the annotation open: the overview shows again, the focus on its row. */ function DeselectButton({ annotationId, onShowAll, busy, }: { annotationId: string } & Pick< PropertiesPanelProps, 'onShowAll' | 'busy' >) { const t = useT() const { byButton } = useDeselect(annotationId, onShowAll) if (busy) return null return ( ) } function GeometrySection(props: PropertiesPanelProps) { const t = useT() const { annotation, readOnly, geometryLocked } = props const locked = readOnly || geometryLocked const { geometry } = annotation return (
{geometryLocked && !readOnly && ( )} {isEditable(geometry) ? ( applyGeometry(annotation, next, props.context)} /> ) : (

{t( geometry === null ? 'editor.annotations.no_geometry' : 'editor.annotations.path', )}

)} {!locked && !isEditable(geometry) && ( )}
) } 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 }) { const t = useT() return ( ) } function GeometryIssues({ id }: { id: string }) { const issues = useGeometryIssues(id) if (issues.length === 0) return null return ( ) } /** Points to the views section, on the view whose drawing cannot be measured. */ export function UnmeasuredHint({ navigate, viewId, }: Pick & { viewId: string }) { const t = useT() return (

{t('editor.annotations.unmeasured.hint')}{' '}

) } function StatusActions({ annotation, readOnly, edit, }: { annotation: Annotation readOnly: boolean edit: ReturnType }) { 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 (
mark(reviewed ? 'to_confirm' : 'reviewed')} > {t( reviewed ? 'editor.annotations.to_confirm' : 'editor.annotations.mark_reviewed', )} {edit.fieldError('status') && (

{edit.fieldError('status')}

)}
) } function Deletion({ id, name, readOnly, }: { id: string name: string readOnly: boolean }) { const t = useT() const [open, setOpen] = useState(false) return ( <> setOpen(true)} > {t('editor.action.delete')} setOpen(false)} /> ) }