fix(store): hold session texts whose set-aside fails, never overwrite them

This commit is contained in:
bchanot committed 2026-10-14 23:11:59 +02:00
1 parent 1312b366b1
commit e34b7f282e
19 files changed
+1093 -86

No files matched your search

@@ -0,0 +1,145 @@
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 { readSessionText, type SessionText } from '@/state/persistence'
import type { HeldText } from '@/state/reportStore'
import { useT } from '@/i18n/useT'
export interface HeldSessionPanelProps {
held: readonly HeldText[]
/** Removes the text from the tab; called only after the practitioner confirmed. */
onDiscard: (text: SessionText) => void
/** Opens the rebased draft, replacing its original; called only after confirmation. */
onOpenAdapted: () => void
onRetry: () => void
}
type Pending = { action: 'discard'; text: SessionText } | { action: 'open' }
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). */
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`
a.click()
URL.revokeObjectURL(url)
}
function rowLabelKey(item: HeldText): string {
return `session.held.${item.text}_${item.reason}`
}
/**
* Resolution of the session texts the store holds in place because their safety copy
* failed (STEP R2 maintenance B): one row per text, a copy to download, an explicit
* deletion or replacement behind a confirmation, a retry. Nothing happens on its own.
*/
export default function HeldSessionPanel({
held,
onDiscard,
onOpenAdapted,
onRetry,
}: HeldSessionPanelProps) {
const t = useT()
const [pending, setPending] = useState<Pending | null>(null)
function confirm() {
if (!pending) return
if (pending.action === 'discard') onDiscard(pending.text)
else onOpenAdapted()
setPending(null)
}
return (
<main className="p-4 sm:p-6 space-y-4 max-w-2xl">
<Notice tone="error">{t('session.held.notice')}</Notice>
<ul className="space-y-3">
{held.map((item) => (
<li
key={item.text}
className="rounded-lg border border-[var(--color-border)] p-3 space-y-2"
data-testid={`held-${item.text}`}
>
<p className="text-sm">
{t(rowLabelKey(item)).replace(
'{size}',
t('session.held.size_kb').replace(
'{n}',
String(Math.max(1, Math.ceil(item.bytes / 1024))),
),
)}
</p>
<div className="flex flex-wrap gap-2">
<Button variant="secondary" onClick={() => downloadCopy(item.text)}>
{t(
item.reason === 'pre_rebase'
? 'session.held.download_original'
: 'session.held.download',
)}
</Button>
{item.reason === 'pre_rebase' ? (
<Button variant="ghost" onClick={() => setPending({ action: 'open' })}>
{t('session.held.open_adapted')}
</Button>
) : (
<Button
variant="ghost"
onClick={() => setPending({ action: 'discard', text: item.text })}
>
{t('session.held.discard')}
</Button>
)}
</div>
</li>
))}
</ul>
<div className="flex justify-end">
<Button onClick={onRetry}>{t('session.held.retry')}</Button>
</div>
<Dialog
open={pending !== null}
onClose={() => setPending(null)}
title={t(
pending?.action === 'open'
? 'session.held.open_title'
: `session.held.discard_title_${pending?.action === 'discard' ? pending.text : 'draft'}`,
)}
size="sm"
>
<p className="text-sm text-[var(--color-ink-muted)]">
{t(
pending?.action === 'open'
? 'session.held.open_body'
: 'session.held.discard_body',
)}
</p>
<div className="mt-4 flex justify-end gap-2">
<Button type="button" variant="ghost" onClick={() => setPending(null)}>
{t('common.cancel')}
</Button>
<Button
type="button"
variant={pending?.action === 'open' ? 'primary' : 'danger'}
onClick={confirm}
>
{t(
pending?.action === 'open'
? 'session.held.open_confirm'
: 'session.held.discard_confirm',
)}
</Button>
</div>
</Dialog>
</main>
)
}