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

+46 -4
View File
@@ -15,10 +15,14 @@ import EditInfosDialog, {
} from '@/components/report/EditInfosDialog'
import DisclaimerDialog from '@/components/report/DisclaimerDialog'
import CoreUnavailable from '@/components/report/CoreUnavailable'
import HeldSessionPanel from '@/components/report/HeldSessionPanel'
import Notice from '@/components/ui/Notice'
import Button from '@/components/ui/Button'
import {
useReportStore,
type ActionResult,
type HeldText,
type RestoreOptions,
type RestoreResult,
} from '@/state/reportStore'
import { writeCachedPractitioner } from '@/state/practitionerCache'
@@ -77,6 +81,13 @@ export default function EditorRoute() {
)
// The core failed while reading the draft: stay here, keep the draft, offer a retry.
const [restoreFailed, setRestoreFailed] = useState(false)
// Texts that could not be set aside: the resolution panel replaces the editor.
const [held, setHeld] = useState<HeldText[] | null>(null)
// Each resolution step (retry, deletion, confirmed replacement) runs the restore again.
const [restoreAttempt, setRestoreAttempt] = useState(0)
const restoreOptions = useRef<RestoreOptions>({})
// The attempt that ended held: the effect runs again on every render, the restore must not.
const heldAttempt = useRef<number | null>(null)
const [disclaimer, setDisclaimer] = useState<string | null>(null)
const [exporting, setExporting] = useState(false)
const [editDraft, setEditDraft] = useState<Identity | null>(null)
@@ -91,17 +102,25 @@ export default function EditorRoute() {
// A draft rebased on another sheet is announced with its unresolved count,
// an unreadable one is set aside. While the core cannot run, nothing is
// read or written and the page stays on the degraded notice (no redirect).
// A text that could not be set aside stays in place behind the resolution panel.
useEffect(() => {
if (report) return
if (sheet.status === 'core_unavailable' || sheet.status === 'invalid_sheet')
return
if (sheet.status === 'ready') {
const result = store.restoreDraft(sheet.index)
// Nothing could be read or set aside: stay, keep the draft where it is.
if (result?.kind === 'core_unavailable' || result?.kind === 'unreadable_kept') {
if (heldAttempt.current === restoreAttempt) return
const result = store.restoreDraft(sheet.index, restoreOptions.current)
restoreOptions.current = {}
if (result?.kind === 'core_unavailable') {
setRestoreFailed(true)
return
}
if (result?.kind === 'held') {
heldAttempt.current = restoreAttempt
setHeld(result.held)
return
}
setHeld(null)
const alert = result ? restoreAlert(result, t) : null
if (alert && !rebaseAlerted.current) {
rebaseAlerted.current = true
@@ -110,7 +129,9 @@ export default function EditorRoute() {
}
if (sheet.status !== 'loading' && !useReportStore.getState().report)
navigate('/')
}, [report, sheet, store, navigate, t])
}, [report, sheet, store, navigate, t, restoreAttempt])
const retryRestore = useCallback(() => setRestoreAttempt((n) => n + 1), [])
useEffect(() => {
if (sheet.status !== 'ready' || typeof sessionStorage === 'undefined')
@@ -238,6 +259,22 @@ export default function EditorRoute() {
if (restoreFailed) {
return <CoreUnavailable onRetry={() => window.location.reload()} />
}
if (held && !report) {
return (
<HeldSessionPanel
held={held}
onDiscard={(text) => {
store.discardSessionText(text)
retryRestore()
}}
onOpenAdapted={() => {
restoreOptions.current = { replaceOriginal: true }
retryRestore()
}}
onRetry={retryRestore}
/>
)
}
if (!report) return null
if (sheet.status === 'loading') {
return (
@@ -262,6 +299,11 @@ export default function EditorRoute() {
})
}
/>
{store.saveFailed && (
<Notice tone="error" className="mx-4 sm:mx-6 mt-4">
{t('editor.save_failed')}
</Notice>
)}
<div className="flex-1 grid grid-cols-1 lg:grid-cols-[2fr_3fr] gap-4 sm:gap-6 p-4 sm:p-6">
<section className="border border-[var(--color-border)] rounded-2xl bg-[var(--color-surface)] overflow-hidden max-h-[55vh] lg:max-h-none flex flex-col">
<AnatomyCanvas
+10 -1
View File
@@ -129,7 +129,16 @@ export default function HomeRoute() {
const index = await loadSheetIndex(DEFAULT_SPECIES_ID)
if (!index) throw new ImportError('core_unavailable')
const { read: outcome, rebase } = importAttachment(attachment, index)
hydrateImport({ report: rebase.report, archive: outcome.archive })
const opened = hydrateImport(
{ report: rebase.report, archive: outcome.archive },
index,
)
// A tab text that could not be set aside would be overwritten: resolve it first.
if (!opened.ok) {
window.alert(t('session.held_blocked'))
navigate('/editor')
return
}
const lines = importMessages(outcome, rebase, t)
if (lines.length > 0) window.alert(lines.join('\n\n'))
navigate('/editor')
+9 -4
View File
@@ -13,8 +13,7 @@ import {
writeCachedPractitioner,
} from '@/state/practitionerCache'
import { useReportStore } from '@/state/reportStore'
import { useSheetAvailability } from '@/sheets/useSheet'
import { coreRuntime } from '@/core/runtime'
import { loadSheetIndex, useSheetAvailability } from '@/sheets/useSheet'
import { useT } from '@/i18n/useT'
/** The one species this build ships a sheet for. */
@@ -49,7 +48,9 @@ export default function NewReportRoute() {
async function submit() {
if (!canSubmit || sheet.status !== 'ready') return
writeCachedPractitioner(practitioner)
if (!(await coreRuntime().ready())) {
// The loaded sheet lets the store read a draft this page never opened before replacing it.
const index = await loadSheetIndex(DEFAULT_SPECIES_ID)
if (!index) {
window.alert(t('sheet.core_unavailable'))
return
}
@@ -64,9 +65,13 @@ export default function NewReportRoute() {
recommendations: '',
},
sheet.sheet.ref,
index,
)
if (created.ok) navigate('/editor')
else window.alert(t('core.action_failed'))
else if (created.reason === 'held') {
window.alert(t('session.held_blocked'))
navigate('/editor')
} else window.alert(t('core.action_failed'))
}
return (