fix(store): banner on a refused archive write, rescue download kept alive

This commit is contained in:
bchanot committed 2026-10-14 23:18:33 +02:00
1 parent a37c50b9ee
commit c97b0e7c67
4 files changed
+29 -8

No files matched your search

@@ -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<SessionText, string> = {
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 {
+10 -5
View File
@@ -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