/** * 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(null); const [initials, setInitials] = useState(""); const [busyId, setBusyId] = useState(null); const [error, setError] = useState(null); const [message, setMessage] = useState(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 (

Full-text PDFs

Attach the full text of each included study (PDF, up to 25 MB). Files are kept in your project storage.

setInitials(e.target.value)} aria-label="Your initials" /> {error &&
{error}
} {message &&
{message}
} {rows === null && !error &&

Loading…

} {rows !== null && rows.length === 0 && (

No studies are included yet. Studies appear here once they pass screening.

)} {rows !== null && rows.length > 0 && (
{rows.map((row) => ( ))}
StudyFull-text PDFActions
{row.title}
{[row.authors, row.year].filter(Boolean).join(" · ")}
{row.pdf ? ( <> {row.pdf.filename}
{formatSize(row.pdf.size_bytes)} {row.pdf.uploaded_by ? ` · added by ${row.pdf.uploaded_by}` : ""}
) : ( "Not attached" )}
{row.pdf && ( )}
)}
); }