Consumers now fed by the core (crates/core through @faunosteo/core-wasm):
- the sheet: the bundled payload is parsed and indexed by the core
(SheetHandle, SheetIndexData); SheetIndex reads precomputed tables only;
- the store: new report, add / remove / move observation, draft restore
(v2 migration + rebase) through the core, actions answering { ok };
- the editor click chain (zone.click), laterality and vertebra pickers
(zone.add_sides, vertebra.initial / sync / select), the findings picker
(finding.toggle by clicked leaf position), zone removal;
- the import: engine byte scan (TypeScript until R4), then the core's
layered reading and rebase in one call; the export: the core builds the
sheet snapshot, the frozen TypeScript engine writes the PDF (R4).
Lazy core: Home and the new-report form read the sheet availability from
the bundle and load the module on import / submit only; the auth shell
never references it. Degraded states (core_unavailable, invalid_sheet)
show the validated notice with a retry and never redirect or touch the
draft; an unreadable draft or archive is set aside under
osteo:quarantine:* (only content kinds; a failed write keeps it in place
and the editor stays), a rebase that flags items keeps the raw pre-rebase
draft. Four strings, wording validated by the owner.
Deleted: services/zoneInteraction.ts and its suite (13 cases ported to
cargo), the TypeScript Finding builder and vertebra selection rules of the
pickers, store rules, persistence parsing, hydrateReport. ESLint forbids
@faunosteo/domain in apps/app/src; the report:compare CLI keeps the frozen
domain until R4 (scripts/compare/sheet.ts). App suite 313/313 on the real
core (24 new tests: quarantine, degraded editor, core failures, N-1
reader); step6 goldens and the frozen envelopes unchanged.
214 lines
7.3 KiB
TypeScript
214 lines
7.3 KiB
TypeScript
import { useNavigate } from 'react-router-dom'
|
|
import { useRef, useState } from 'react'
|
|
import type {
|
|
ArchiveReadOutcome,
|
|
RebaseOutcome,
|
|
} from '@faunosteo/core-wasm/types'
|
|
import AppHeader from '@/components/brand/AppHeader'
|
|
import HomeSpaceLink from '@/components/brand/HomeSpaceLink'
|
|
import Button from '@/components/ui/Button'
|
|
import Card from '@/components/ui/Card'
|
|
import {
|
|
importAttachment,
|
|
ImportError,
|
|
MAX_PDF_BYTES,
|
|
readAttachment,
|
|
} from '@/pdf/import'
|
|
import { useReportStore } from '@/state/reportStore'
|
|
import { loadSheetIndex, useSheetAvailability } from '@/sheets/useSheet'
|
|
import { DEFAULT_SPECIES_ID } from './NewReportRoute'
|
|
import { useT } from '@/i18n/useT'
|
|
|
|
type Translate = ReturnType<typeof useT>
|
|
|
|
/**
|
|
* The detail lines of the import alert (STEP6 §16), in a fixed order: a
|
|
* lossy v2 migration, the archive kept, the snapshot dropped with its
|
|
* reason, the rebase onto the current sheet. Empty when nothing applies.
|
|
*/
|
|
function importDetailLines(
|
|
outcome: ArchiveReadOutcome,
|
|
rebase: RebaseOutcome,
|
|
t: Translate,
|
|
): string[] {
|
|
const lines: string[] = []
|
|
const lost = outcome.unresolvedFindings + outcome.unresolvedZones
|
|
if (outcome.migrated && lost > 0) {
|
|
lines.push(t('home.import.migrated').replace('{count}', String(lost)))
|
|
}
|
|
if (outcome.archive) lines.push(t('home.import.archive_ok'))
|
|
if (outcome.snapshotRejected) {
|
|
lines.push(
|
|
t('home.import.snapshot_rejected').replace(
|
|
'{reason}',
|
|
t(`home.import.snapshot_reason.${outcome.snapshotRejected}`),
|
|
),
|
|
)
|
|
}
|
|
if (rebase.rebased) {
|
|
const count = rebase.unresolvedZones + rebase.unresolvedFindings
|
|
lines.push(t('home.import.rebased').replace('{count}', String(count)))
|
|
}
|
|
return lines
|
|
}
|
|
|
|
/**
|
|
* Degraded recovery: something the practitioner must check. A rejected
|
|
* snapshot, a migration or a rebase that lost items. A rebase that resolved
|
|
* everything is a complete restoration on a different sheet.
|
|
*/
|
|
function isDegradedImport(
|
|
outcome: ArchiveReadOutcome,
|
|
rebase: RebaseOutcome,
|
|
): boolean {
|
|
const lost = outcome.unresolvedFindings + outcome.unresolvedZones
|
|
const rebaseLost = rebase.unresolvedZones + rebase.unresolvedFindings
|
|
return (
|
|
outcome.snapshotRejected !== undefined ||
|
|
(outcome.migrated && lost > 0) ||
|
|
(rebase.rebased && rebaseLost > 0)
|
|
)
|
|
}
|
|
|
|
/**
|
|
* The one alert shown after a successful import: a headline that says
|
|
* whether the restoration is complete or partial, then the detail lines.
|
|
* Empty (no alert) when there is nothing to say: a bare v3 on the same sheet.
|
|
*/
|
|
function importMessages(
|
|
outcome: ArchiveReadOutcome,
|
|
rebase: RebaseOutcome,
|
|
t: Translate,
|
|
): string[] {
|
|
const details = importDetailLines(outcome, rebase, t)
|
|
if (details.length === 0) return []
|
|
const headline = isDegradedImport(outcome, rebase)
|
|
? t('home.import.degraded')
|
|
: t('home.import.complete')
|
|
return [headline, ...details]
|
|
}
|
|
|
|
/** The error line under the import button, by ImportError kind. */
|
|
function importErrorMessage(err: unknown, t: Translate): string {
|
|
if (!(err instanceof ImportError)) return t('home.import.error_generic')
|
|
switch (err.kind) {
|
|
case 'incompatible_version':
|
|
return t('home.import.error_incompatible_version').replace(
|
|
'{version}',
|
|
String(err.foundVersion ?? '?'),
|
|
)
|
|
case 'invalid_pdf':
|
|
return t('home.import.error_invalid_pdf')
|
|
case 'attachment_stripped':
|
|
return t('home.import.error_attachment_stripped')
|
|
case 'no_attachment':
|
|
return t('home.import.error_no_attachment')
|
|
case 'core_unavailable':
|
|
return t('home.import.error_core_unavailable')
|
|
}
|
|
}
|
|
|
|
export default function HomeRoute() {
|
|
const navigate = useNavigate()
|
|
const fileRef = useRef<HTMLInputElement | null>(null)
|
|
const hydrateImport = useReportStore((s) => s.hydrateImport)
|
|
// The page only needs to know a sheet exists; the core loads when a file is picked.
|
|
const sheet = useSheetAvailability(DEFAULT_SPECIES_ID)
|
|
const [error, setError] = useState<string | null>(null)
|
|
const [busy, setBusy] = useState(false)
|
|
const t = useT()
|
|
|
|
/** Read, rebase onto the loaded sheet, open; the editor always works on that sheet. */
|
|
async function importFile(file: File) {
|
|
// The engine refuses the same size; checking first keeps a huge file out of memory.
|
|
if (file.size > MAX_PDF_BYTES) throw new ImportError('invalid_pdf')
|
|
const bytes = new Uint8Array(await file.arrayBuffer())
|
|
const attachment = await readAttachment(bytes)
|
|
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 lines = importMessages(outcome, rebase, t)
|
|
if (lines.length > 0) window.alert(lines.join('\n\n'))
|
|
navigate('/editor')
|
|
}
|
|
|
|
async function onPickFile(e: React.ChangeEvent<HTMLInputElement>) {
|
|
const file = e.target.files?.[0]
|
|
if (!file || sheet.status !== 'ready') return
|
|
setBusy(true)
|
|
setError(null)
|
|
try {
|
|
await importFile(file)
|
|
} catch (err) {
|
|
setError(importErrorMessage(err, t))
|
|
} finally {
|
|
setBusy(false)
|
|
if (fileRef.current) fileRef.current.value = ''
|
|
}
|
|
}
|
|
|
|
const noSheet = sheet.status === 'none'
|
|
|
|
return (
|
|
<main className="flex-1 flex flex-col">
|
|
<AppHeader>
|
|
<HomeSpaceLink />
|
|
</AppHeader>
|
|
<section className="flex-1 grid place-items-center px-4 sm:px-6">
|
|
<div className="grid gap-4 md:grid-cols-2 max-w-3xl w-full">
|
|
{noSheet && (
|
|
<Card className="p-4 sm:p-6 space-y-2 md:col-span-2">
|
|
<h2 className="font-serif text-xl sm:text-2xl">
|
|
{t('home.no_sheet.title')}
|
|
</h2>
|
|
<p className="text-sm text-red-700">
|
|
{t('home.no_sheet.description')}
|
|
</p>
|
|
</Card>
|
|
)}
|
|
<Card className="p-4 sm:p-6 space-y-3 sm:space-y-4">
|
|
<h2 className="font-serif text-xl sm:text-2xl">
|
|
{t('home.new_report.title')}
|
|
</h2>
|
|
<p className="text-sm text-[var(--color-ink-muted)]">
|
|
{t('home.new_report.description')}
|
|
</p>
|
|
<Button
|
|
onClick={() => navigate('/new')}
|
|
size="lg"
|
|
disabled={noSheet}
|
|
>
|
|
{t('home.new_report.cta')}
|
|
</Button>
|
|
</Card>
|
|
<Card className="p-4 sm:p-6 space-y-3 sm:space-y-4">
|
|
<h2 className="font-serif text-xl sm:text-2xl">
|
|
{t('home.import.title')}
|
|
</h2>
|
|
<p className="text-sm text-[var(--color-ink-muted)]">
|
|
{t('home.import.description')}
|
|
</p>
|
|
<input
|
|
ref={fileRef}
|
|
type="file"
|
|
accept="application/pdf,.pdf"
|
|
className="hidden"
|
|
onChange={onPickFile}
|
|
/>
|
|
<Button
|
|
variant="secondary"
|
|
size="lg"
|
|
onClick={() => fileRef.current?.click()}
|
|
disabled={busy || sheet.status !== 'ready'}
|
|
>
|
|
{busy ? t('home.import.loading') : t('home.import.cta')}
|
|
</Button>
|
|
{error && <p className="text-sm text-red-700">{error}</p>}
|
|
</Card>
|
|
</div>
|
|
</section>
|
|
</main>
|
|
)
|
|
}
|