fix(editor): honest stopped state when the draft is unsaved; no dead retry
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".
This commit is contained in:
1 parent
0279953e9e
commit
6371523f8c
24 files changed
+839
-83
No files matched your search
@@ -1,7 +1,10 @@
|
||||
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'
|
||||
@@ -17,7 +20,7 @@ export interface AnatomyCanvasProps {
|
||||
|
||||
const ANNOTATED = '[data-anno-id]'
|
||||
|
||||
/** Only an engine that can still recover offers a retry. */
|
||||
/** `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',
|
||||
@@ -34,7 +37,8 @@ function annotatedTarget(event: Event): Element | null {
|
||||
* 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; when the view itself cannot be drawn (a drawing
|
||||
* 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({
|
||||
@@ -52,6 +56,17 @@ export default function AnatomyCanvas({
|
||||
// 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
|
||||
@@ -140,11 +155,11 @@ export default function AnatomyCanvas({
|
||||
{failure && (
|
||||
<div className="absolute inset-0 grid place-items-center p-4">
|
||||
<div className="space-y-3 max-w-sm">
|
||||
<Notice tone="error">{t(FAILURE_MESSAGE[failure])}</Notice>
|
||||
{failure === 'engine' && (
|
||||
<Button onClick={() => setAttempt((n) => n + 1)}>
|
||||
{t('sheet.core_retry')}
|
||||
</Button>
|
||||
<Notice tone="error">
|
||||
{t(engineStopped ? stoppedMessageKey(unsaved) : FAILURE_MESSAGE[failure])}
|
||||
</Notice>
|
||||
{failure === 'engine' && !engineStopped && (
|
||||
<Button onClick={retryEngine}>{t('sheet.core_retry')}</Button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
import { useCoreStopped } from '@/core/runtime'
|
||||
import { downloadJson } from '@/lib/download'
|
||||
import { isUnsaved, useReportStore } from '@/state/reportStore'
|
||||
import Button from '@/components/ui/Button'
|
||||
import Notice from '@/components/ui/Notice'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
/**
|
||||
* Saves the in-memory report as JSON. It needs no engine: this is the rescue copy when the
|
||||
* tab's storage refused the last write and the PDF export cannot run.
|
||||
*/
|
||||
export function CopyReportButton() {
|
||||
const t = useT()
|
||||
const report = useReportStore((s) => s.report)
|
||||
if (!report) return null
|
||||
return (
|
||||
<Button
|
||||
variant="secondary"
|
||||
onClick={() => downloadJson(JSON.stringify(report), 'faunosteo-brouillon')}
|
||||
>
|
||||
{t('session.held.download')}
|
||||
</Button>
|
||||
)
|
||||
}
|
||||
|
||||
export interface CoreStoppedNoticeProps {
|
||||
className?: string
|
||||
}
|
||||
|
||||
/**
|
||||
* The final state of the engine (three recoveries spent), never a retry: reload the page
|
||||
* when the tab's copy of the report is saved; when it is not, the text says so and offers
|
||||
* the copy instead of the reload (a reload would lose the latest edits).
|
||||
*/
|
||||
export default function CoreStoppedNotice({ className = '' }: CoreStoppedNoticeProps) {
|
||||
const t = useT()
|
||||
const unsaved = useReportStore(isUnsaved)
|
||||
return (
|
||||
<Notice tone="error" className={className}>
|
||||
<p>{t(unsaved ? 'core.stopped_unsaved' : 'core.stopped')}</p>
|
||||
<div className="mt-2">
|
||||
{unsaved ? (
|
||||
<CopyReportButton />
|
||||
) : (
|
||||
<Button variant="secondary" onClick={() => window.location.reload()}>
|
||||
{t('core.reload')}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</Notice>
|
||||
)
|
||||
}
|
||||
|
||||
/** The notice on the pages that run the core, shown only once the engine stopped. */
|
||||
export function CoreStoppedBanner({ className = '' }: CoreStoppedNoticeProps) {
|
||||
return useCoreStopped() ? <CoreStoppedNotice className={className} /> : null
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
import Button from '@/components/ui/Button'
|
||||
import Notice from '@/components/ui/Notice'
|
||||
import CoreStoppedNotice from '@/components/report/CoreStoppedNotice'
|
||||
import { useCoreStopped } from '@/core/runtime'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
export interface CoreUnavailableProps {
|
||||
@@ -8,10 +10,19 @@ export interface CoreUnavailableProps {
|
||||
|
||||
/**
|
||||
* Degraded state of the sheet pages when the core module cannot run (CSP, network, an old
|
||||
* browser, a refused sheet): the session data stays where it is, the practitioner retries.
|
||||
* browser, a refused sheet): the session data stays where it is, the practitioner retries. Once the engine
|
||||
* stopped for good, the stopped notice replaces the retry, which could not work.
|
||||
*/
|
||||
export default function CoreUnavailable({ onRetry }: CoreUnavailableProps) {
|
||||
const t = useT()
|
||||
const stopped = useCoreStopped()
|
||||
if (stopped) {
|
||||
return (
|
||||
<main className="p-6 max-w-xl">
|
||||
<CoreStoppedNotice />
|
||||
</main>
|
||||
)
|
||||
}
|
||||
return (
|
||||
<main className="p-6 space-y-3 max-w-xl">
|
||||
<Notice tone="error">{t('sheet.core_unavailable')}</Notice>
|
||||
|
||||
@@ -2,7 +2,7 @@ import { useState } from 'react'
|
||||
import Button from '@/components/ui/Button'
|
||||
import Dialog from '@/components/ui/Dialog'
|
||||
import Notice from '@/components/ui/Notice'
|
||||
import { todayIso } from '@/lib/dates'
|
||||
import { downloadJson } from '@/lib/download'
|
||||
import { readSessionText, type SessionText } from '@/state/persistence'
|
||||
import type { HeldText } from '@/state/reportStore'
|
||||
import { useT } from '@/i18n/useT'
|
||||
@@ -18,30 +18,15 @@ export interface HeldSessionPanelProps {
|
||||
|
||||
type Pending = { action: 'discard'; text: SessionText } | { action: 'open' }
|
||||
|
||||
const REVOKE_DELAY_MS = 30_000
|
||||
|
||||
const FILE_STEM: Record<SessionText, string> = {
|
||||
draft: 'faunosteo-brouillon',
|
||||
archive: 'faunosteo-archive',
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the stored text verbatim. It is JSON.stringify output, whose lone surrogates are
|
||||
* `\uXXXX` escapes, so the UTF-8 Blob is lossless. This copy may be the last one before
|
||||
* a deletion: the anchor is attached while clicked and the URL revoked later, since some
|
||||
* engines (WebKit) cancel a download whose URL is revoked at once.
|
||||
*/
|
||||
/** Saves the stored text verbatim (it is JSON.stringify output). */
|
||||
function downloadCopy(text: SessionText): void {
|
||||
const raw = readSessionText(text)
|
||||
if (raw === null) return
|
||||
const url = URL.createObjectURL(new Blob([raw], { type: 'application/json' }))
|
||||
const a = document.createElement('a')
|
||||
a.href = url
|
||||
a.download = `${FILE_STEM[text]}-${todayIso()}.json`
|
||||
document.body.appendChild(a)
|
||||
a.click()
|
||||
a.remove()
|
||||
setTimeout(() => URL.revokeObjectURL(url), REVOKE_DELAY_MS)
|
||||
if (raw !== null) downloadJson(raw, FILE_STEM[text])
|
||||
}
|
||||
|
||||
function rowLabelKey(item: HeldText): string {
|
||||
|
||||
Reference in new issue
Block a user