/** * File : frontend/src/components/ImportBackupPanel.tsx (new) * Purpose : load a backup file: choose file -> preview -> confirm. * Consumers: App.tsx (protocol list view). onRestored refreshes the list. * Safety : nothing is written until the person clicks Restore. Restoring * into a project that already exists needs an explicit tick-box. * Styles : existing classes from index.css only. */ import { useState } from "react"; import { BackupApiError, readBackupFile, previewBackup, restoreBackup, type BackupDocument, type RestorePlan, } from "../api/backup"; interface Props { onRestored: () => void | Promise; } const SECTION_LABELS: Record = { screening: "Screening decisions", extraction: "Extraction records", rob: "Risk-of-bias assessments", confidence: "Confidence ratings", }; function totalNew(plan: RestorePlan): number { return Object.values(plan.sections).reduce((sum, s) => sum + s.new, 0); } export function ImportBackupPanel({ onRestored }: Props) { const [doc, setDoc] = useState(null); const [fileName, setFileName] = useState(""); const [plan, setPlan] = useState(null); const [merge, setMerge] = useState(false); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const [message, setMessage] = useState(null); function reset() { setDoc(null); setFileName(""); setPlan(null); setMerge(false); } function fail(err: unknown) { setError(err instanceof BackupApiError ? err.message : "Something went wrong. Nothing was changed."); } async function handleFile(file: File | undefined) { reset(); setError(null); setMessage(null); if (!file) return; setBusy(true); try { const parsed = await readBackupFile(file); const result = await previewBackup(parsed); // writes nothing setDoc(parsed); setFileName(file.name); setPlan(result); } catch (err) { fail(err); } finally { setBusy(false); } } async function handleRestore() { if (!doc || !plan) return; setBusy(true); setError(null); try { const result = await restoreBackup(doc, merge); setMessage( result.protocol_exists ? `Merged: ${totalNew(result)} new entries added. Nothing existing was changed.` : `Project restored with ${totalNew(result)} log entries.`, ); reset(); await onRestored(); } catch (err) { fail(err); } finally { setBusy(false); } } const needsMergeTick = plan?.protocol_exists === true; const nothingToAdd = plan !== null && plan.protocol_exists && totalNew(plan) === 0; const canRestore = plan !== null && !busy && !nothingToAdd && (!needsMergeTick || merge); return (

Load a project backup

Choose a backup file you exported earlier. You will see what it contains before anything is loaded.

{ void handleFile(e.target.files?.[0]); e.target.value = ""; // lets the same file be chosen again }} /> {busy &&

Working…

} {error &&
{error}
} {message &&
{message}
} {plan && (

{fileName} —{" "} {plan.protocol_exists ? "this project already exists here." : "a new project that will be added."}

{Object.entries(plan.sections).map(([key, s]) => ( ))}
SectionIn fileNewAlready here
{SECTION_LABELS[key] ?? key} {s.total} {s.new} {s.duplicate}

Search run: {plan.search_run}. Synthesis: {plan.synthesis}.

{plan.warnings.map((w) => (
{w}
))} {needsMergeTick && !nothingToAdd && ( )} {nothingToAdd &&

Everything in this file is already here, so there is nothing to add.

}
)}
); }