feat(editor): the canvas tells an engine failure (retry) from a view it cannot draw (no retry)
This commit is contained in:
1 parent
6882c6da4a
commit
c0a5c8c7ce
8 files changed
+182
-36
No files matched your search
@@ -4,17 +4,36 @@ import type { SheetIndex } from '@/services/sheetIndex'
|
||||
* The inline SVG of one view, as the core composed it (R3): the sheet's drawing, read and
|
||||
* sanitised by the core's allowlist at load, framed on the view, with the annotation layer
|
||||
* last (docs/ANNOTATION-RENDERING.md). The browser only parses the text (`DOMParser` never
|
||||
* runs scripts) and imports the tree into the page. `null` when the core or the parse
|
||||
* fails: the canvas shows its degraded notice.
|
||||
* runs scripts) and imports the tree into the page.
|
||||
*/
|
||||
|
||||
export type ViewStates = ReadonlyMap<string, 'current'>
|
||||
|
||||
export function buildViewSvg(index: SheetIndex, viewId: string): SVGSVGElement | null {
|
||||
/**
|
||||
* Why a view cannot be drawn: the engine did not run (a retry may succeed), or the view
|
||||
* itself cannot be drawn (a refused drawing, a view the core cannot compose: retrying
|
||||
* changes nothing). The technical kind stays in the core's outcome and the DEV console.
|
||||
*/
|
||||
export type ViewFailure = 'engine' | 'view'
|
||||
|
||||
export type ViewBuild = { ok: true; svg: SVGSVGElement } | { ok: false; failure: ViewFailure }
|
||||
|
||||
const ENGINE_KINDS: ReadonlySet<string> = new Set(['core_unavailable', 'internal'])
|
||||
|
||||
function failed(failure: ViewFailure, kind: string, detail?: string): ViewBuild {
|
||||
if (import.meta.env.DEV) console.warn(`canvas: ${kind}`, detail ?? '')
|
||||
return { ok: false, failure }
|
||||
}
|
||||
|
||||
export function buildViewSvg(index: SheetIndex, viewId: string): ViewBuild {
|
||||
const text = index.canvasSvg(viewId)
|
||||
if (!text.ok) return null
|
||||
if (!text.ok) {
|
||||
return failed(ENGINE_KINDS.has(text.kind) ? 'engine' : 'view', text.kind, text.detail)
|
||||
}
|
||||
const parsed = new DOMParser().parseFromString(text.value, 'image/svg+xml')
|
||||
const root = parsed.documentElement
|
||||
if (parsed.querySelector('parsererror') || root.localName !== 'svg') return null
|
||||
return document.importNode(root, true) as unknown as SVGSVGElement
|
||||
if (parsed.querySelector('parsererror') || root.localName !== 'svg') {
|
||||
return failed('view', 'parse')
|
||||
}
|
||||
return { ok: true, svg: document.importNode(root, true) as unknown as SVGSVGElement }
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useCallback, useEffect, useRef, useState } from 'react'
|
||||
import { buildViewSvg, type ViewStates } from '@/anatomy/drawing'
|
||||
import { buildViewSvg, type ViewFailure, type ViewStates } from '@/anatomy/drawing'
|
||||
import type { SheetIndex } from '@/services/sheetIndex'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
import { pickLocaleLabel } from '@/i18n/pickLocaleLabel'
|
||||
@@ -26,8 +26,9 @@ function annotatedTarget(event: Event): Element | 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 core cannot compose the view, the
|
||||
* canvas shows the degraded notice with a retry.
|
||||
* delegated on `data-anno-id`. When the engine cannot run, the canvas shows the
|
||||
* degraded notice with a retry; 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,
|
||||
@@ -43,14 +44,14 @@ export default function AnatomyCanvas({
|
||||
const t = useT()
|
||||
// Each retry builds again; a failed build leaves the canvas empty behind the notice.
|
||||
const [attempt, setAttempt] = useState(0)
|
||||
const [failed, setFailed] = useState(false)
|
||||
const [failure, setFailure] = useState<ViewFailure | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
const host = svgRef.current
|
||||
if (!host) return
|
||||
const svg = buildViewSvg(index, viewId)
|
||||
setFailed(svg === null)
|
||||
host.replaceChildren(...(svg ? [svg] : []))
|
||||
const build = buildViewSvg(index, viewId)
|
||||
setFailure(build.ok ? null : build.failure)
|
||||
host.replaceChildren(...(build.ok ? [build.svg] : []))
|
||||
}, [index, viewId, attempt])
|
||||
|
||||
useEffect(() => {
|
||||
@@ -129,13 +130,17 @@ export default function AnatomyCanvas({
|
||||
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 }} />
|
||||
{failed && (
|
||||
{failure && (
|
||||
<div className="absolute inset-0 grid place-items-center p-4">
|
||||
<div className="space-y-3 max-w-sm">
|
||||
<Notice tone="error">{t('sheet.core_unavailable')}</Notice>
|
||||
<Button onClick={() => setAttempt((n) => n + 1)}>
|
||||
{t('sheet.core_retry')}
|
||||
</Button>
|
||||
<Notice tone="error">
|
||||
{t(failure === 'engine' ? 'sheet.core_unavailable' : 'canvas.view_unavailable')}
|
||||
</Notice>
|
||||
{failure === 'engine' && (
|
||||
<Button onClick={() => setAttempt((n) => n + 1)}>
|
||||
{t('sheet.core_retry')}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
@@ -201,6 +201,8 @@ export const en: Dict = {
|
||||
'sheet.core_unavailable':
|
||||
"Faunosteo's computation engine could not be loaded. Your session data is kept.",
|
||||
'sheet.core_retry': 'Retry',
|
||||
'canvas.view_unavailable':
|
||||
"This anatomical view cannot be displayed: its drawing was refused by the safety checks or could not be composed. Your report is not changed.",
|
||||
'core.action_failed':
|
||||
'This action could not be applied. Try again; your report was not changed.',
|
||||
|
||||
|
||||
@@ -215,6 +215,8 @@ export const fr: Dict = {
|
||||
'sheet.core_unavailable':
|
||||
"Le moteur de calcul de Faunosteo n'a pas pu être chargé. Vos données de session sont conservées.",
|
||||
'sheet.core_retry': 'Réessayer',
|
||||
'canvas.view_unavailable':
|
||||
"Cette vue anatomique ne peut pas être affichée : son dessin a été refusé par les contrôles de sécurité ou n'a pas pu être composé. Votre bilan n'est pas modifié.",
|
||||
'core.action_failed':
|
||||
"Cette action n'a pas pu être appliquée. Réessayez ; votre bilan n'a pas été modifié.",
|
||||
|
||||
|
||||
Reference in new issue
Block a user