Systematic-tool-review / frontend /src /components /DualScreeningRow.tsx
Sadum1's picture
Upload folder using huggingface_hub
dcc1b84 verified
Raw History Blame Contribute Delete
12 kB
import { useState } from "react";
import { recordExtraction, recordScreeningDecision } from "../api/protocols";
import type {
ExtractionRecordEntry,
ScreeningDecisionValue,
ScreeningRecordEntry,
ScreeningStage,
SearchResultItem,
} from "../types";
import { OTHER_REASON, exclusionReasonPayload } from "../utils/exclusionReasons";
import { reviewerKey, stageStatus, type StageStatus } from "../utils/screeningStatus";
import { ExtractionForm } from "./ExtractionForm";
import { ReasonPicker } from "./ReasonPicker";
import { ExtractionList } from "./ExtractionList";
import type { ReasonOptions } from "./ScreeningBoard";
import "./review-fixes.css";
interface DualScreeningRowProps {
protocolId: string;
paper: SearchResultItem;
reviewer: string;
screeningHistory: ScreeningRecordEntry[];
extractionRecords: ExtractionRecordEntry[];
reasonOptions: ReasonOptions;
defaultOutcome?: string;
onRefresh: () => Promise<void>;
}
type Action =
| { kind: "decide"; stage: ScreeningStage; decision: ScreeningDecisionValue }
| { kind: "resolve"; stage: ScreeningStage };
const STAGE_LABEL: Record<ScreeningStage, string> = {
title_abstract: "Title/abstract",
full_text: "Full text",
};
function decisionText(r: ScreeningRecordEntry): string {
return r.decision === "include" ? "Included" : `Excluded β€” ${r.reason ?? "no reason recorded"}`;
}
export function DualScreeningRow({
protocolId,
paper,
reviewer,
screeningHistory,
extractionRecords,
reasonOptions,
defaultOutcome = "",
onRefresh,
}: DualScreeningRowProps) {
const [action, setAction] = useState<Action | null>(null);
const [revising, setRevising] = useState<ScreeningStage | null>(null);
const [reasonChoice, setReasonChoice] = useState("");
const [reasonNote, setReasonNote] = useState("");
const [resolveDecision, setResolveDecision] = useState<ScreeningDecisionValue | "">("");
const [resolveNote, setResolveNote] = useState("");
const [showExtraction, setShowExtraction] = useState(false);
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
const me = reviewerKey(reviewer);
const ta = stageStatus(screeningHistory, paper.id, "title_abstract", 2);
const ft =
ta.final?.decision === "include"
? stageStatus(screeningHistory, paper.id, "full_text", 2)
: undefined;
const extraction = extractionRecords
.filter((r) => r.paper_id === paper.id)
.sort((a, b) => b.extracted_at.localeCompare(a.extracted_at))[0];
function resetForms() {
setAction(null);
setRevising(null);
setReasonChoice("");
setReasonNote("");
setResolveDecision("");
setResolveNote("");
setError(null);
}
async function submit(stage: ScreeningStage, decision: ScreeningDecisionValue, adjudication: boolean) {
setError(null);
if (!me) {
setError("Enter your name above first.");
return;
}
const reason = decision === "exclude" ? exclusionReasonPayload(reasonChoice, reasonNote) : null;
if (decision === "exclude" && !reason) {
setError(
reasonChoice === OTHER_REASON ? "Describe the reason when choosing Other." : "Choose a reason for exclusion."
);
return;
}
if (adjudication && !resolveNote.trim()) {
setError("Explain how the conflict was resolved.");
return;
}
setBusy(true);
try {
await recordScreeningDecision(protocolId, {
paper_id: paper.id,
stage,
decision,
reviewer: reviewer.trim(),
reason: reason?.reason ?? null,
reason_category: reason?.reason_category ?? null,
role: adjudication ? "adjudication" : "review",
note: adjudication ? resolveNote.trim() : null,
});
resetForms();
await onRefresh();
} catch (err) {
setError(err instanceof Error ? err.message : "Failed to save the decision.");
} finally {
setBusy(false);
}
}
function choose(stage: ScreeningStage, decision: ScreeningDecisionValue) {
if (decision === "include") {
void submit(stage, "include", false);
} else {
setError(null);
setAction({ kind: "decide", stage, decision: "exclude" });
}
}
function decideButtons(stage: ScreeningStage) {
return (
<div className="screening-actions">
<span className="stage-label">{STAGE_LABEL[stage]}:</span>
<button className="secondary-btn" disabled={busy} onClick={() => choose(stage, "include")}>
Include
</button>
<button className="secondary-btn" disabled={busy} onClick={() => choose(stage, "exclude")}>
Exclude
</button>
{revising === stage && (
<button className="change-btn" onClick={resetForms}>
Cancel
</button>
)}
</div>
);
}
function stageBlock(stage: ScreeningStage, status: StageStatus) {
const mine = me ? status.byReviewer.get(me) : undefined;
const others = status.byReviewer.size - (mine ? 1 : 0);
const label = STAGE_LABEL[stage];
if (revising === stage) return decideButtons(stage);
if (status.final) {
const final = status.final;
return (
<div className={`decision-tag ${final.decision === "include" ? "included" : "excluded"}`}>
{label}: {decisionText(final)} β€”{" "}
{status.state === "adjudicated"
? `conflict resolved by ${final.reviewer}: β€œ${final.note ?? ""}”`
: `agreed by ${[...status.byReviewer.values()].map((r) => r.reviewer).join(" and ")}`}
{mine && (
<>
{" "}
<button className="change-btn" onClick={() => setRevising(stage)}>
Change my decision
</button>
</>
)}
</div>
);
}
if (status.state === "conflict") {
return (
<div className="conflict-box">
<strong>{label}: conflict</strong>
<ul className="history-change">
{[...status.byReviewer.values()].map((r) => (
<li key={r.id}>
{r.reviewer}: {decisionText(r)}
</li>
))}
</ul>
<div className="screening-actions">
<button
className="primary-btn"
disabled={busy}
onClick={() => {
setError(null);
setAction({ kind: "resolve", stage });
}}
>
Resolve conflict
</button>
{mine && (
<button className="change-btn" onClick={() => setRevising(stage)}>
Change my decision
</button>
)}
</div>
</div>
);
}
if (!me) {
return (
<div className="decision-line">
{label}: enter your name above to screen this record.
{others > 0 && ` (${others} reviewer${others === 1 ? " has" : "s have"} decided β€” hidden)`}
</div>
);
}
if (mine) {
return (
<div className="decision-line">
{label} β€” your decision: {decisionText(mine)} Β· waiting for a second reviewer
<button className="change-btn" onClick={() => setRevising(stage)}>
Change
</button>
</div>
);
}
return (
<>
{decideButtons(stage)}
{others > 0 && (
<div className="decision-line">
1 of 2 reviewers has decided β€” their decision stays hidden until you decide.
</div>
)}
</>
);
}
const ftIncluded = ft?.final?.decision === "include";
return (
<div className="screening-row">
<p className="screening-title">{paper.title}</p>
<p className="paper-meta">
{paper.authors}
{paper.journal ? ` Β· ${paper.journal}` : ""}
{paper.year ? ` (${paper.year})` : ""}
</p>
{stageBlock("title_abstract", ta)}
{ft && stageBlock("full_text", ft)}
{action?.kind === "decide" && (
<div className="reason-prompt">
<ReasonPicker
options={reasonOptions[action.stage]}
choice={reasonChoice}
note={reasonNote}
onChoice={setReasonChoice}
onNote={setReasonNote}
/>
<button className="primary-btn" disabled={busy} onClick={() => submit(action.stage, "exclude", false)}>
Confirm exclude
</button>
<button className="change-btn" onClick={resetForms}>
Cancel
</button>
</div>
)}
{action?.kind === "resolve" && (
<div className="resolve-form">
<strong>Resolve {STAGE_LABEL[action.stage].toLowerCase()} conflict</strong>
<div className="screening-actions">
<label className="source-checkbox">
<input
type="radio"
name={`resolve-${paper.id}`}
checked={resolveDecision === "include"}
onChange={() => setResolveDecision("include")}
/>
Include
</label>
<label className="source-checkbox">
<input
type="radio"
name={`resolve-${paper.id}`}
checked={resolveDecision === "exclude"}
onChange={() => setResolveDecision("exclude")}
/>
Exclude
</label>
</div>
{resolveDecision === "exclude" && (
<div className="reason-prompt">
<ReasonPicker
options={reasonOptions[action.stage]}
choice={reasonChoice}
note={reasonNote}
onChoice={setReasonChoice}
onNote={setReasonNote}
/>
</div>
)}
<textarea
placeholder="Justification (required), e.g. Discussed with both reviewers: the abstract confirms a narrative review"
value={resolveNote}
onChange={(e) => setResolveNote(e.target.value)}
/>
<div className="screening-actions">
<button
className="primary-btn"
disabled={busy || !resolveDecision}
onClick={() => resolveDecision && submit(action.stage, resolveDecision, true)}
>
Save resolution
</button>
<button className="change-btn" onClick={resetForms}>
Cancel
</button>
</div>
<p className="form-hint">Recorded under your name ({reviewer.trim() || "enter it above"}).</p>
</div>
)}
{ftIncluded && !extraction && !showExtraction && (
<div className="decision-tag included">
Included β€” ready for extraction
<button className="secondary-btn" onClick={() => setShowExtraction(true)}>
Extract data
</button>
</div>
)}
{ftIncluded && showExtraction && (
<ExtractionForm
reviewer={reviewer}
busy={busy}
defaultOutcome={defaultOutcome}
onCancel={() => setShowExtraction(false)}
onSubmit={async (data) => {
setBusy(true);
setError(null);
try {
await recordExtraction(protocolId, { ...data, paper_id: paper.id, extracted_by: reviewer.trim() });
setShowExtraction(false);
await onRefresh();
} catch (err) {
setError(err instanceof Error ? err.message : "Failed to save extraction.");
} finally {
setBusy(false);
}
}}
/>
)}
{ftIncluded && extraction && !showExtraction && (
<ExtractionList
records={extractionRecords.filter((r) => r.paper_id === paper.id)}
onAddAnother={() => setShowExtraction(true)}
/>
)}
{error && <div className="form-error">{error}</div>}
</div>
);
}