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; } type Action = | { kind: "decide"; stage: ScreeningStage; decision: ScreeningDecisionValue } | { kind: "resolve"; stage: ScreeningStage }; const STAGE_LABEL: Record = { 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(null); const [revising, setRevising] = useState(null); const [reasonChoice, setReasonChoice] = useState(""); const [reasonNote, setReasonNote] = useState(""); const [resolveDecision, setResolveDecision] = useState(""); const [resolveNote, setResolveNote] = useState(""); const [showExtraction, setShowExtraction] = useState(false); const [busy, setBusy] = useState(false); const [error, setError] = useState(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 (
{STAGE_LABEL[stage]}: {revising === stage && ( )}
); } 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 (
{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 && ( <> {" "} )}
); } if (status.state === "conflict") { return (
{label}: conflict
    {[...status.byReviewer.values()].map((r) => (
  • {r.reviewer}: {decisionText(r)}
  • ))}
{mine && ( )}
); } if (!me) { return (
{label}: enter your name above to screen this record. {others > 0 && ` (${others} reviewer${others === 1 ? " has" : "s have"} decided — hidden)`}
); } if (mine) { return (
{label} — your decision: {decisionText(mine)} · waiting for a second reviewer
); } return ( <> {decideButtons(stage)} {others > 0 && (
1 of 2 reviewers has decided — their decision stays hidden until you decide.
)} ); } const ftIncluded = ft?.final?.decision === "include"; return (

{paper.title}

{paper.authors} {paper.journal ? ` · ${paper.journal}` : ""} {paper.year ? ` (${paper.year})` : ""}

{stageBlock("title_abstract", ta)} {ft && stageBlock("full_text", ft)} {action?.kind === "decide" && (
)} {action?.kind === "resolve" && (
Resolve {STAGE_LABEL[action.stage].toLowerCase()} conflict
{resolveDecision === "exclude" && (
)}