0279953 chose the stopped message from CoreRuntime.stopped alone: it
promised the report was kept even when the tab's storage refused the
draft (export, the banner's advice, needs the core), four surfaces still
offered a retry that could not work, and a failed recovery below the cap
was a dead end since nothing called retry().
The runtime restarts a failed recovery on the next call (compiled module
reused, cap still counts traps) and notifies status listeners. One
CoreStoppedNotice on the editor, Home, New report and the full-page
notice: reload when saved, else core.stopped_unsaved with a core-free
copy download (also on the storage-full banner) and a beforeunload
guard. Export, import and new report say the stopped message; a core
failure reading an unread draft is FAILED, not held. ESLint census of
the failure keys; core.stopped says "plusieurs erreurs".
170 lines
5.9 KiB
TypeScript
170 lines
5.9 KiB
TypeScript
import { useCallback, useEffect, useRef, useState } from 'react'
|
|
import { buildViewSvg, type ViewFailure, type ViewStates } from '@/anatomy/drawing'
|
|
import type { SheetIndex } from '@/services/sheetIndex'
|
|
import { coreRuntime, useCoreStopped } from '@/core/runtime'
|
|
import { stoppedMessageKey } from '@/core/failure'
|
|
import { useLocaleStore } from '@/state/localeStore'
|
|
import { isUnsaved, useReportStore } from '@/state/reportStore'
|
|
import { pickLocaleLabel } from '@/i18n/pickLocaleLabel'
|
|
import Button from '@/components/ui/Button'
|
|
import Notice from '@/components/ui/Notice'
|
|
import { useT } from '@/i18n/useT'
|
|
|
|
export interface AnatomyCanvasProps {
|
|
index: SheetIndex
|
|
viewId: string
|
|
states: ViewStates
|
|
onZoneClick: (annotationId: string, viewId: string) => void
|
|
className?: string
|
|
}
|
|
|
|
const ANNOTATED = '[data-anno-id]'
|
|
|
|
/** `engine_stopped` is replaced at render by the stopped text that fits the save state. */
|
|
const FAILURE_MESSAGE: Record<ViewFailure, string> = {
|
|
engine: 'sheet.core_unavailable',
|
|
engine_stopped: 'core.stopped',
|
|
view: 'canvas.view_unavailable',
|
|
}
|
|
|
|
function annotatedTarget(event: Event): Element | null {
|
|
const target = event.target
|
|
return target instanceof Element ? target.closest(ANNOTATED) : null
|
|
}
|
|
|
|
/**
|
|
* One view of the sheet: the drawing plus the annotation layer, composed by
|
|
* the core (R3), rebuilt when the view changes and restyled in place when the
|
|
* selection changes (the restyle runs right after every build). Clicks are
|
|
* delegated on `data-anno-id`. When the engine cannot run, the canvas shows the
|
|
* degraded notice with a retry (none once the engine stopped for good: the page's stopped
|
|
* notice holds the buttons); when the view itself cannot be drawn (a drawing
|
|
* refused or impossible to compose), a notice without retry: nothing would change.
|
|
*/
|
|
export default function AnatomyCanvas({
|
|
index,
|
|
viewId,
|
|
states,
|
|
onZoneClick,
|
|
className = '',
|
|
}: AnatomyCanvasProps) {
|
|
const wrapperRef = useRef<HTMLDivElement | null>(null)
|
|
const svgRef = useRef<HTMLDivElement | null>(null)
|
|
const tooltipRef = useRef<HTMLDivElement | null>(null)
|
|
const locale = useLocaleStore((s) => s.locale)
|
|
const t = useT()
|
|
// Each retry builds again; a failed build leaves the canvas empty behind the notice.
|
|
const [attempt, setAttempt] = useState(0)
|
|
const [failure, setFailure] = useState<ViewFailure | null>(null)
|
|
// Derived at render: an engine failure shown while the engine stops becomes the stopped text.
|
|
const stopped = useCoreStopped()
|
|
const unsaved = useReportStore(isUnsaved)
|
|
const engineStopped = failure === 'engine_stopped' || (failure === 'engine' && stopped)
|
|
|
|
// A failed recovery below the cap needs the runtime restarted before the view is built again.
|
|
const retryEngine = useCallback(() => {
|
|
void coreRuntime()
|
|
.retry()
|
|
.finally(() => setAttempt((n) => n + 1))
|
|
}, [])
|
|
|
|
useEffect(() => {
|
|
const host = svgRef.current
|
|
if (!host) return
|
|
const build = buildViewSvg(index, viewId)
|
|
setFailure(build.ok ? null : build.failure)
|
|
host.replaceChildren(...(build.ok ? [build.svg] : []))
|
|
}, [index, viewId, attempt])
|
|
|
|
useEffect(() => {
|
|
const host = svgRef.current
|
|
if (!host) return
|
|
host.querySelectorAll<Element>(ANNOTATED).forEach((el) => {
|
|
const state = states.get(el.getAttribute('data-anno-id') ?? '')
|
|
if (state) el.setAttribute('data-state', state)
|
|
else el.removeAttribute('data-state')
|
|
})
|
|
}, [states, index, viewId, attempt])
|
|
|
|
useEffect(() => {
|
|
const host = svgRef.current
|
|
if (!host) return
|
|
const onClick = (e: Event) => {
|
|
const id = annotatedTarget(e)?.getAttribute('data-anno-id')
|
|
if (id) onZoneClick(id, viewId)
|
|
}
|
|
host.addEventListener('click', onClick)
|
|
return () => host.removeEventListener('click', onClick)
|
|
}, [onZoneClick, viewId])
|
|
|
|
const showTooltip = useCallback(
|
|
(el: Element, x: number, y: number) => {
|
|
const tip = tooltipRef.current
|
|
if (!tip) return
|
|
const label = pickLocaleLabel(
|
|
el.getAttribute('data-anno-name-fr') ?? '',
|
|
el.getAttribute('data-anno-name-en') ?? '',
|
|
locale,
|
|
)
|
|
if (!label) {
|
|
tip.style.opacity = '0'
|
|
return
|
|
}
|
|
tip.textContent = label
|
|
tip.style.opacity = '1'
|
|
tip.style.left = `${x + 12}px`
|
|
tip.style.top = `${y - 8}px`
|
|
},
|
|
[locale],
|
|
)
|
|
|
|
useEffect(() => {
|
|
const host = svgRef.current
|
|
if (!host) return
|
|
const hide = () => {
|
|
if (tooltipRef.current) tooltipRef.current.style.opacity = '0'
|
|
}
|
|
const onMove = (e: MouseEvent) => {
|
|
const el = annotatedTarget(e)
|
|
const wrapper = wrapperRef.current
|
|
if (!el || !wrapper || el.hasAttribute('data-anno-opens-view'))
|
|
return hide()
|
|
const rect = wrapper.getBoundingClientRect()
|
|
showTooltip(el, e.clientX - rect.left, e.clientY - rect.top)
|
|
}
|
|
host.addEventListener('mousemove', onMove)
|
|
host.addEventListener('mouseleave', hide)
|
|
return () => {
|
|
host.removeEventListener('mousemove', onMove)
|
|
host.removeEventListener('mouseleave', hide)
|
|
}
|
|
}, [showTooltip])
|
|
|
|
return (
|
|
<div
|
|
data-testid="anatomy-canvas"
|
|
data-view-id={viewId}
|
|
ref={wrapperRef}
|
|
className={`svg-canvas relative w-full h-full ${className}`}
|
|
>
|
|
<div
|
|
ref={svgRef}
|
|
className="w-full h-full [&>svg]:max-w-full [&>svg]:max-h-full [&>svg]:mx-auto"
|
|
/>
|
|
<div ref={tooltipRef} className="zone-tooltip" style={{ opacity: 0 }} />
|
|
{failure && (
|
|
<div className="absolute inset-0 grid place-items-center p-4">
|
|
<div className="space-y-3 max-w-sm">
|
|
<Notice tone="error">
|
|
{t(engineStopped ? stoppedMessageKey(unsaved) : FAILURE_MESSAGE[failure])}
|
|
</Notice>
|
|
{failure === 'engine' && !engineStopped && (
|
|
<Button onClick={retryEngine}>{t('sheet.core_retry')}</Button>
|
|
)}
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
)
|
|
}
|