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:
bchanot committed 2026-10-16 18:26:10 +02:00
1 parent 0279953e9e
commit 6371523f8c
24 files changed
+839 -83

No files matched your search

@@ -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 {