import { useState } from "react"; import { recordRobAssessment } from "../api/protocols"; import type { Protocol, RobAssessmentEntry, RobReference, RobSummaryItem, ScreeningRecordEntry, SearchResultItem, StudyDesign, } from "../types"; import { isFinallyIncluded, reviewerKey } from "../utils/screeningStatus"; import { RobForm, type RobFormValues } from "./RobForm"; import { useRobReference } from "./useRobReference"; import "./review-fixes.css"; interface RobSectionProps { protocol: Protocol; papers: SearchResultItem[]; screeningHistory: ScreeningRecordEntry[]; robEntries: RobAssessmentEntry[]; robSummary: RobSummaryItem[]; onRefresh: () => Promise; } const outcomeKey = (o: string) => o.split(/\s+/).filter(Boolean).join(" ").toLowerCase(); export function keyQuestionsFor(protocol: Protocol, reference: RobReference, design: StudyDesign): number[] { const experimental = reference.designs.find((d) => d.value === design)?.experimental; const keys = experimental ? protocol.rob_key_questions_experimental ?? [8, 9] : protocol.rob_key_questions_observational ?? [4, 8, 9]; const applicable = new Set(reference.questions.filter((q) => q.applies_to.includes(design)).map((q) => q.number)); return keys.filter((q) => applicable.has(q)).sort((a, b) => a - b); } type Open = | { kind: "assess"; paperId: string; outcome?: string; initial?: RobAssessmentEntry } | { kind: "resolve"; paperId: string; outcome: string; initial: RobAssessmentEntry; highlight: number[] }; /** Risk of bias (OHAT) for every study included at full text. */ export function RobSection({ protocol, papers, screeningHistory, robEntries, robSummary, onRefresh }: RobSectionProps) { const { reference, error: refError } = useRobReference(); const [reviewer, setReviewer] = useState(""); const [open, setOpen] = useState(null); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const mode = protocol.reviewers_required ?? 1; const included = papers.filter((p) => isFinallyIncluded(screeningHistory, p.id, mode)); const me = reviewerKey(reviewer); if (refError) return
{refError}
; if (!reference) return null; const ref = reference; async function save(values: RobFormValues, paperId: string, adjudication: boolean) { setError(null); if (!me) { setError("Enter your name above first."); return; } setBusy(true); try { await recordRobAssessment(protocol.id, { paper_id: paperId, outcome: values.outcome, study_design: values.study_design, answers: values.answers, reviewer: reviewer.trim(), role: adjudication ? "adjudication" : "review", note: adjudication ? values.note : null, }); setOpen(null); await onRefresh(); } catch (err) { setError(err instanceof Error ? err.message : "Failed to save the assessment."); } finally { setBusy(false); } } /** Latest review entry per reviewer for one study/outcome. */ function reviewsFor(paperId: string, outcome: string): Map { const map = new Map(); [...robEntries] .filter((e) => e.paper_id === paperId && outcomeKey(e.outcome) === outcomeKey(outcome) && (e.role ?? "review") === "review") .sort((a, b) => a.assessed_at.localeCompare(b.assessed_at)) .forEach((e) => map.set(reviewerKey(e.reviewer), e)); return map; } const designLabel = (d: StudyDesign) => ref.designs.find((x) => x.value === d)?.label ?? d; const symbol = (rating: string) => ref.ratings.find((r) => r.value === rating)?.symbol ?? rating; function itemBlock(paper: SearchResultItem, item: RobSummaryItem) { const reviews = reviewsFor(paper.id, item.outcome); const mine = me ? reviews.get(me) : undefined; const others = reviews.size - (mine ? 1 : 0); const label = `Outcome: ${item.outcome}`; if (item.final) { return (
{label} — {designLabel(item.final.study_design)} ·{" "} {item.tier ? `Tier ${item.tier}` : "Tier not determined (no key question applies)"} {item.state === "agreed" && ` · agreed by ${item.reviewers.join(" and ")}`} {item.state === "adjudicated" && ` · resolved by ${item.final.reviewer}: “${item.final.note ?? ""}”`} {(mine || mode === 1) && ( )}
); } if (item.state === "conflict") { const [a, b] = [...reviews.values()]; const questions = item.conflicting_questions.includes(0) ? [] : item.conflicting_questions; return (
{label}: reviewers disagree {item.conflicting_questions.includes(0) && a && b && (
Study design: {a.reviewer} chose {designLabel(a.study_design)}, {b.reviewer} chose {designLabel(b.study_design)}.
)} {questions.length > 0 && a && b && (
    {questions.map((q) => { const ra = a.answers.find((x) => x.question === q); const rb = b.answers.find((x) => x.question === q); return (
  • Q{q}: {a.reviewer} {symbol(ra?.rating ?? "")} vs {b.reviewer} {symbol(rb?.rating ?? "")}
  • ); })}
)}
{a && ( )} {mine && ( )}
); } // pending / awaiting second reviewer (blinded) if (mine) { return (
{label} — your assessment is saved · waiting for a second reviewer
); } return (
{label} — {others} of 2 reviewers assessed (hidden until you assess)
); } return (

Risk of bias (OHAT)

Each study included at full text is assessed with the OHAT risk-of-bias tool, per outcome. Only the questions that apply to the study's design are shown, and every rating needs a justification. {mode === 2 && " Two reviewers assess independently; ratings stay hidden until both are done, and any difference must be resolved."}

{protocol.rob_criteria && (
Review-specific criteria for this protocol

{protocol.rob_criteria}

)} setReviewer(e.target.value)} placeholder="Your name or initials, e.g. AB" /> {included.length === 0 && (

No studies are included at full text yet — finish screening first.

)} {error &&
{error}
}
{included.map((paper) => { const items = robSummary.filter((i) => i.paper_id === paper.id); const formOpen = open && open.paperId === paper.id; return (

{paper.title}

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

{items.map((item) => itemBlock(paper, item))} {!formOpen && ( )} {formOpen && open && ( keyQuestionsFor(protocol, ref, d)} highlight={open.kind === "resolve" ? open.highlight : []} requireNote={open.kind === "resolve"} lockOutcome={!!open.outcome} busy={busy} onCancel={() => setOpen(null)} onSubmit={(values) => save(values, paper.id, open.kind === "resolve")} /> )}
); })}
); }