Systematic-tool-review / frontend /src /components /PdfLibraryPanel.tsx
Sadum1's picture
Upload PdfLibraryPanel.tsx
88bb107 verified
Raw History Blame Contribute Delete
6.11 kB
/**
* File : frontend/src/components/PdfLibraryPanel.tsx (new)
* Purpose : list a project's included studies and let people attach, open,
* replace and remove each study's full-text PDF.
* Consumers: App.tsx (project detail view, below the protocol).
* Styles : existing classes from index.css only.
*/
import { useCallback, useEffect, useState } from "react";
import {
PdfApiError,
attachPdf,
listStudyPdfs,
pdfUrl,
removePdf,
type StudyPdfRow,
} from "../api/pdfs";
interface Props {
protocolId: string;
}
function formatSize(bytes: number): string {
return bytes >= 1024 * 1024 ? `${(bytes / (1024 * 1024)).toFixed(1)} MB` : `${Math.max(1, Math.round(bytes / 1024))} KB`;
}
export function PdfLibraryPanel({ protocolId }: Props) {
const [rows, setRows] = useState<StudyPdfRow[] | null>(null);
const [initials, setInitials] = useState("");
const [busyId, setBusyId] = useState<string | null>(null);
const [error, setError] = useState<string | null>(null);
const [message, setMessage] = useState<string | null>(null);
const load = useCallback(async () => {
try {
setRows(await listStudyPdfs(protocolId));
setError(null);
} catch (err) {
setError(err instanceof PdfApiError ? err.message : "Could not load the study list.");
}
}, [protocolId]);
useEffect(() => {
void load();
}, [load]);
async function handleFile(row: StudyPdfRow, file: File | undefined) {
if (!file) return;
setBusyId(row.paper_id);
setError(null);
setMessage(null);
try {
await attachPdf(protocolId, row.paper_id, file, initials.trim(), row.pdf !== null);
setMessage(`PDF ${row.pdf ? "replaced" : "attached"}: ${file.name}`);
await load();
} catch (err) {
setError(err instanceof PdfApiError ? err.message : "Upload failed. Nothing was changed.");
} finally {
setBusyId(null);
}
}
async function handleRemove(row: StudyPdfRow) {
if (!window.confirm(`Remove the PDF attached to "${row.title}"? The study stays in the project.`)) return;
setBusyId(row.paper_id);
setError(null);
setMessage(null);
try {
await removePdf(protocolId, row.paper_id);
setMessage("PDF removed.");
await load();
} catch (err) {
setError(err instanceof PdfApiError ? err.message : "Could not remove the PDF.");
} finally {
setBusyId(null);
}
}
const canUpload = initials.trim().length > 0;
return (
<section className="phase4-section" aria-label="Full-text PDFs">
<h2>Full-text PDFs</h2>
<p className="form-hint">
Attach the full text of each included study (PDF, up to 25 MB). Files are kept in your project storage.
</p>
<input
className="reviewer-input"
type="text"
placeholder="Your initials (required to upload)"
value={initials}
onChange={(e) => setInitials(e.target.value)}
aria-label="Your initials"
/>
{error && <div className="form-error" role="alert">{error}</div>}
{message && <div className="locked-note" role="status">{message}</div>}
{rows === null && !error && <p className="form-hint">Loading…</p>}
{rows !== null && rows.length === 0 && (
<p className="empty-state">No studies are included yet. Studies appear here once they pass screening.</p>
)}
{rows !== null && rows.length > 0 && (
<div className="evidence-table-wrap">
<table className="evidence-table">
<thead>
<tr><th>Study</th><th>Full-text PDF</th><th>Actions</th></tr>
</thead>
<tbody>
{rows.map((row) => (
<tr key={row.paper_id}>
<td className="evidence-study-cell">
{row.title}
<div className="paper-meta">{[row.authors, row.year].filter(Boolean).join(" · ")}</div>
</td>
<td>
{row.pdf ? (
<>
<a href={pdfUrl(protocolId, row.paper_id)} target="_blank" rel="noopener noreferrer">
{row.pdf.filename}
</a>
<div className="paper-meta">
{formatSize(row.pdf.size_bytes)}
{row.pdf.uploaded_by ? ` · added by ${row.pdf.uploaded_by}` : ""}
</div>
</>
) : (
"Not attached"
)}
</td>
<td>
<label
className="secondary-btn"
style={{
display: "inline-block",
cursor: canUpload && busyId === null ? "pointer" : "not-allowed",
opacity: canUpload && busyId === null ? 1 : 0.5,
marginRight: 8,
}}
>
{busyId === row.paper_id ? "Working…" : row.pdf ? "Replace" : "Attach PDF"}
<input
type="file"
accept="application/pdf,.pdf"
disabled={!canUpload || busyId !== null}
style={{ display: "none" }}
onChange={(e) => {
void handleFile(row, e.target.files?.[0]);
e.target.value = "";
}}
/>
</label>
{row.pdf && (
<button
type="button"
className="secondary-btn"
disabled={busyId !== null}
onClick={() => void handleRemove(row)}
>
Remove
</button>
)}
</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</section>
);
}