feat(app): export and import through @faunosteo/pdf — snapshot, archived branding and labels, browser rasterizer, imported archive kept in session, explicit rebase with messages; legacy PDF modules removed
This commit is contained in:
1 parent
20dafc0b00
commit
9bd5173a9c
43 files changed
+1492
-1989
No files matched your search
@@ -57,8 +57,7 @@ export default function EditorRoute() {
|
||||
// No report in memory: restore the per-tab draft once the sheet is known.
|
||||
useEffect(() => {
|
||||
if (report) return
|
||||
if (sheet.status === 'ready')
|
||||
store.restoreDraft(sheet.index.sheet.migrationMap)
|
||||
if (sheet.status === 'ready') store.restoreDraft(sheet.index)
|
||||
if (sheet.status !== 'loading' && !useReportStore.getState().report)
|
||||
navigate('/')
|
||||
}, [report, sheet, store, navigate])
|
||||
|
||||
@@ -1,64 +1,104 @@
|
||||
import { useNavigate } from 'react-router-dom'
|
||||
import { useRef, useState } from 'react'
|
||||
import {
|
||||
rebaseReportOnSheet,
|
||||
type ArchiveReadOutcome,
|
||||
type RebaseOutcome,
|
||||
} from '@faunosteo/domain'
|
||||
import AppHeader from '@/components/brand/AppHeader'
|
||||
import Button from '@/components/ui/Button'
|
||||
import Card from '@/components/ui/Card'
|
||||
import { importReportFromPdf, ImportError } from '@/pdf/import'
|
||||
import { useReportStore } from '@/state/reportStore'
|
||||
import { useSheet } from '@/sheets/useSheet'
|
||||
import type { SheetIndex } from '@/services/sheetIndex'
|
||||
import { DEFAULT_SPECIES_ID } from './NewReportRoute'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
type Translate = ReturnType<typeof useT>
|
||||
|
||||
/**
|
||||
* The lines of the one alert shown after a successful import (STEP6 §16):
|
||||
* a lossy v2 migration, the archive kept, the snapshot dropped with its
|
||||
* reason, the rebase onto the current sheet. Empty when nothing applies.
|
||||
*/
|
||||
function importMessages(
|
||||
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
|
||||
}
|
||||
|
||||
/** 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')
|
||||
}
|
||||
}
|
||||
|
||||
export default function HomeRoute() {
|
||||
const navigate = useNavigate()
|
||||
const fileRef = useRef<HTMLInputElement | null>(null)
|
||||
const hydrate = useReportStore((s) => s.hydrateReport)
|
||||
const hydrateImport = useReportStore((s) => s.hydrateImport)
|
||||
const sheet = useSheet(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, index: SheetIndex) {
|
||||
const bytes = new Uint8Array(await file.arrayBuffer())
|
||||
const outcome = await importReportFromPdf(bytes, index.sheet.migrationMap)
|
||||
const rebase = rebaseReportOnSheet(
|
||||
outcome.report,
|
||||
index,
|
||||
outcome.archive?.sheetSnapshot ?? null,
|
||||
)
|
||||
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 {
|
||||
const bytes = new Uint8Array(await file.arrayBuffer())
|
||||
const outcome = await importReportFromPdf(
|
||||
bytes,
|
||||
sheet.index.sheet.migrationMap,
|
||||
)
|
||||
if (
|
||||
outcome.migrated &&
|
||||
outcome.unresolvedFindings + outcome.unresolvedZones > 0
|
||||
) {
|
||||
window.alert(
|
||||
t('home.import.migrated').replace(
|
||||
'{count}',
|
||||
String(outcome.unresolvedFindings + outcome.unresolvedZones),
|
||||
),
|
||||
)
|
||||
}
|
||||
hydrate(outcome.report)
|
||||
navigate('/editor')
|
||||
await importFile(file, sheet.index)
|
||||
} catch (err) {
|
||||
if (err instanceof ImportError) {
|
||||
if (err.kind === 'incompatible_version') {
|
||||
setError(
|
||||
t('home.import.error_incompatible_version').replace(
|
||||
'{version}',
|
||||
String(err.foundVersion ?? '?'),
|
||||
),
|
||||
)
|
||||
} else if (err.kind === 'invalid_pdf') {
|
||||
setError(t('home.import.error_invalid_pdf'))
|
||||
} else {
|
||||
setError(t('home.import.error_no_attachment'))
|
||||
}
|
||||
} else {
|
||||
setError(t('home.import.error_generic'))
|
||||
}
|
||||
setError(importErrorMessage(err, t))
|
||||
} finally {
|
||||
setBusy(false)
|
||||
if (fileRef.current) fileRef.current.value = ''
|
||||
|
||||
Reference in new issue
Block a user