From c97b0e7c67023cb43c91e16e7d398c33c26be6e5 Mon Sep 17 00:00:00 2001 From: bchanot Date: Wed, 14 Oct 2026 23:18:33 +0200 Subject: [PATCH] fix(store): banner on a refused archive write, rescue download kept alive --- .../src/components/report/HeldSessionPanel.tsx | 13 +++++++++++-- apps/app/src/state/reportStore.ts | 15 ++++++++++----- apps/app/tests/unit/state.heldSession.test.ts | 4 ++++ docs/development/RUST.md | 5 ++++- 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/apps/app/src/components/report/HeldSessionPanel.tsx b/apps/app/src/components/report/HeldSessionPanel.tsx index f9ed1894..e9afc392 100644 --- a/apps/app/src/components/report/HeldSessionPanel.tsx +++ b/apps/app/src/components/report/HeldSessionPanel.tsx @@ -18,12 +18,19 @@ export interface HeldSessionPanelProps { type Pending = { action: 'discard'; text: SessionText } | { action: 'open' } +const REVOKE_DELAY_MS = 30_000 + const FILE_STEM: Record = { draft: 'faunosteo-brouillon', archive: 'faunosteo-archive', } -/** Saves the stored text verbatim (it is JSON.stringify output: ASCII escapes, lossless). */ +/** + * 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. + */ function downloadCopy(text: SessionText): void { const raw = readSessionText(text) if (raw === null) return @@ -31,8 +38,10 @@ function downloadCopy(text: SessionText): void { const a = document.createElement('a') a.href = url a.download = `${FILE_STEM[text]}-${todayIso()}.json` + document.body.appendChild(a) a.click() - URL.revokeObjectURL(url) + a.remove() + setTimeout(() => URL.revokeObjectURL(url), REVOKE_DELAY_MS) } function rowLabelKey(item: HeldText): string { diff --git a/apps/app/src/state/reportStore.ts b/apps/app/src/state/reportStore.ts index c73d94e4..a6040ad8 100644 --- a/apps/app/src/state/reportStore.ts +++ b/apps/app/src/state/reportStore.ts @@ -39,9 +39,11 @@ import { * The per-tab draft is mirrored through the persistence adapter and restored * once the sheet is known; the archive an import brought along is kept * beside the report and written only when the session starts or ends. - * A session text this page has not read, or whose safety copy failed, is - * never removed or overwritten implicitly: restore holds it, and a new - * session (new report, import) is refused until the practitioner resolves it. + * A session text whose safety copy failed is never removed or overwritten + * implicitly: restore holds it, and a new session (new report, import) is + * refused until the practitioner resolves it. A new session reads any draft + * this page never opened first: an unreadable one is set aside, a readable + * one is replaced as it always was. */ export type { NewReportDraft } @@ -265,10 +267,13 @@ export function createReportStore( archives: ArchivePersistence, ) { /** The archive mirror is explicit: these actions are the session's edges. */ + // The archive of this session could not be written: the tab's copy lacks it (banner). + let archiveRefused = false const withArchive = (patch: Patch, archive: ReportArchiveEnvelope | null) => { // A refused write must not leave the replaced session's archive beside the new draft: // the next restore would pair them. That archive belongs to the session being replaced. - if (!archives.save(archive)) archives.save(null) + archiveRefused = !archives.save(archive) + if (archiveRefused) archives.save(null) return { ...patch, archive } } @@ -386,7 +391,7 @@ export function createReportStore( // The mirror follows the report only: an unrelated change never writes or removes the draft. store.subscribe((state, prev) => { if (state.report === prev.report) return - const failed = !persistence.save(state.report) + const failed = !persistence.save(state.report) || archiveRefused if (failed !== state.saveFailed) store.setState({ saveFailed: failed }) }) return store diff --git a/apps/app/tests/unit/state.heldSession.test.ts b/apps/app/tests/unit/state.heldSession.test.ts index 693cb6fe..cd8bb8d3 100644 --- a/apps/app/tests/unit/state.heldSession.test.ts +++ b/apps/app/tests/unit/state.heldSession.test.ts @@ -309,6 +309,10 @@ describe('the draft mirror', () => { store().hydrateImport({ report: second, archive: archiveEnvelope(sheet, second) }, sheet) expect(sessionStorage.getItem(ARCHIVE_KEY)).toBeNull() expect(JSON.parse(sessionStorage.getItem(DRAFT_KEY)!).conclusion).toBe('second') + // The tab's copy lacks the imported archive: the banner says so until a write succeeds. + expect(store().saveFailed).toBe(true) + store().setConclusion('edited') + expect(store().saveFailed).toBe(true) }) it('a deletion request is ignored while a report is open', () => { diff --git a/docs/development/RUST.md b/docs/development/RUST.md index 2aac2477..85f5412f 100644 --- a/docs/development/RUST.md +++ b/docs/development/RUST.md @@ -639,7 +639,10 @@ application itself builds. a refused write keeps the previous text and shows the `editor.save_failed` banner until a write succeeds; a refused archive write at a new session removes the replaced session's archive rather than leave it paired with - the new draft. + the new draft, and shows the same banner (the tab's copy lacks it). The + rescue download keeps its object URL alive 30 s (WebKit cancels a + download whose URL is revoked at once; the PDF export still does that, + follow-up). 4. A core that cannot load leaves the editor on the degraded notice (before R2 a failed sheet chunk left it on "loading"). 5. Home and `/new` no longer load the sheet into memory on mount.