Systematic-tool-review / frontend /src /components /ImportBackupPanel.tsx
Sadum1's picture
Upload 2 files
834b620 verified
Raw History Blame Contribute Delete
5.63 kB
/**
* 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<void>;
}
const SECTION_LABELS: Record<string, string> = {
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<BackupDocument | null>(null);
const [fileName, setFileName] = useState("");
const [plan, setPlan] = useState<RestorePlan | null>(null);
const [merge, setMerge] = useState(false);
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
const [message, setMessage] = useState<string | null>(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 (
<section className="phase4-section" aria-label="Load a project backup">
<h2>Load a project backup</h2>
<p className="form-hint">
Choose a backup file you exported earlier. You will see what it contains before anything is loaded.
</p>
<input
type="file"
accept=".json,application/json"
disabled={busy}
onChange={(e) => {
void handleFile(e.target.files?.[0]);
e.target.value = ""; // lets the same file be chosen again
}}
/>
{busy && <p className="form-hint" role="status">Working…</p>}
{error && <div className="form-error" role="alert" style={{ whiteSpace: "pre-line" }}>{error}</div>}
{message && <div className="locked-note" role="status">{message}</div>}
{plan && (
<div style={{ marginTop: 16 }}>
<p>
<strong>{fileName}</strong> —{" "}
{plan.protocol_exists ? "this project already exists here." : "a new project that will be added."}
</p>
<div className="evidence-table-wrap">
<table className="evidence-table">
<thead>
<tr><th>Section</th><th>In file</th><th>New</th><th>Already here</th></tr>
</thead>
<tbody>
{Object.entries(plan.sections).map(([key, s]) => (
<tr key={key}>
<td>{SECTION_LABELS[key] ?? key}</td>
<td>{s.total}</td>
<td>{s.new}</td>
<td>{s.duplicate}</td>
</tr>
))}
</tbody>
</table>
</div>
<p className="form-hint" style={{ marginTop: 10 }}>
Search run: {plan.search_run}. Synthesis: {plan.synthesis}.
</p>
{plan.warnings.map((w) => (
<div key={w} className="form-error" role="alert">{w}</div>
))}
{needsMergeTick && !nothingToAdd && (
<label className="source-checkbox" style={{ marginTop: 12 }}>
<input type="checkbox" checked={merge} onChange={(e) => setMerge(e.target.checked)} />
I understand: add the new entries to the existing project.
</label>
)}
{nothingToAdd && <p className="form-hint">Everything in this file is already here, so there is nothing to add.</p>}
<div className="form-actions">
<button type="button" className="secondary-btn" onClick={reset} disabled={busy}>Cancel</button>
<button type="button" className="primary-btn" onClick={handleRestore} disabled={!canRestore}>
Restore
</button>
</div>
</div>
)}
</section>
);
}