File size: 6,112 Bytes
88bb107 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | /**
* 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>
);
}
|