Spaces:
Sleeping

omnidiag / frontend /src /components /EvidenceActionsCard.jsx
yahyoha's picture
Space snapshot of af9d80f (binaries stripped)
ef838e0
Raw History Blame Contribute Delete
10.5 kB
import { BookOpenCheck, ExternalLink, Loader2, AlertTriangle, ChevronDown, ClipboardList } from 'lucide-react';
import {
summariseEvidence,
EVIDENCE_TITLE,
EVIDENCE_LOADING,
ACTION_PLAN_ANCHOR_ID,
} from '../utils/evidenceSummary';
/**
* Card B β€” Evidence-backed actions (v3 Β§1).
*
* What trials (or, labelled as such, cohorts) measured for an action, on a
* future outcome, in their own population. It is deliberately a separate card
* from Estimate drivers β€” its own border, accent and icon, never nested in the
* SHAP card β€” because the two answer different questions and must not be read
* together as "treat X and this patient's number falls".
*
* Every number shown comes from the /evidence-actions response (copied from
* backend/evidence/registry.yaml); wording and classification come from
* evidenceSummary.js, which the PDF uses too.
*
* Rendering states: loading β†’ spinner; error β†’ "Evidence could not be loaded"
* with no numbers; empty β†’ says nothing maps; ready β†’ items.
*/
function Header() {
return (
<div className="card-header border-b border-teal-200 bg-teal-50/60">
<h2 className="text-lg font-semibold text-teal-900 flex items-center gap-2">
<BookOpenCheck className="w-5 h-5 text-teal-700" aria-hidden="true" />
{EVIDENCE_TITLE}
</h2>
</div>
);
}
function ScopeNote({ text }) {
return (
<p className="mt-4 pt-3 border-t border-teal-100 text-xs text-gray-500 leading-relaxed" role="note">
{text}
</p>
);
}
function scrollToActionPlan(event) {
event.preventDefault();
document.getElementById(ACTION_PLAN_ANCHOR_ID)?.scrollIntoView({ behavior: 'smooth', block: 'center' });
}
function EvidenceBadge({ effect }) {
if (!effect.label) return null;
const cls = effect.observational
? 'bg-amber-100 text-amber-800 border-amber-300'
: 'bg-teal-100 text-teal-800 border-teal-200';
return (
<span className={`inline-block text-[11px] font-medium px-1.5 py-0.5 rounded border ${cls}`}>
{effect.label}
</span>
);
}
function SourceChip({ effect }) {
if (!effect.url) return null;
return (
<a
href={effect.url}
target="_blank"
rel="noopener noreferrer"
className="inline-flex items-center gap-1 text-[11px] text-teal-800 bg-white border border-teal-200 rounded-full px-2 py-0.5 hover:bg-teal-50"
title={effect.citationText}
>
{effect.citation}
<ExternalLink className="w-3 h-3" aria-hidden="true" />
</a>
);
}
function RuleSource({ source }) {
if (!source) return null;
return (
<div className="mt-1 flex flex-wrap items-center gap-1.5">
<EvidenceBadge effect={source} />
<SourceChip effect={source} />
{source.locator && <span className="text-[11px] text-gray-500">{source.locator}</span>}
</div>
);
}
/** One-line source for the compact sections: short name, linked to the DOI. */
function CompactSource({ source }) {
if (!source) return null;
return (
<>
{' Β· '}
<a
href={source.url}
target="_blank"
rel="noopener noreferrer"
className="text-teal-800 underline decoration-teal-300 hover:decoration-teal-700"
title={source.citationText}
>
{source.compact}
</a>
</>
);
}
function Item({ item }) {
return (
<li
className={`rounded-lg border p-3 ${
item.muted ? 'border-gray-200 bg-gray-50 text-gray-600' : 'border-teal-200 bg-white text-gray-800'
}`}
data-status={item.status}
>
<p className="text-sm font-semibold">{item.action}</p>
{item.headline && <p className="text-xs font-semibold text-gray-700 mt-0.5">{item.headline}</p>}
{item.reasons.length > 1 && (
<ul className="mt-1 list-disc pl-4 text-xs text-gray-700">
{item.reasons.map((r) => (
<li key={r.text}>
{r.text}
{r.basis && <span className="text-gray-500"> β€” {r.basis}</span>}
</li>
))}
</ul>
)}
{item.ruleLine && <p className="font-mono text-[11px] text-gray-600 mt-1">{item.ruleLine}</p>}
<RuleSource source={item.ruleSource} />
{item.reasons.filter((r) => r.source).map((r) => <RuleSource key={r.text} source={r.source} />)}
{item.conditions.map((line) => (
<p key={line} className="text-xs font-semibold text-gray-700 mt-1">{line}</p>
))}
{item.outcomeLine && <p className="text-xs mt-1">{item.outcomeLine}</p>}
{item.mappedLine && (
<p className="text-[11px] text-gray-500 mt-0.5">
{item.mappedLine}{item.matchLine && ` Β· ${item.matchLine}`}
</p>
)}
<ul className="mt-2 space-y-2">
{item.effects.map((effect) => (
<li key={effect.text + effect.citation} className="text-xs">
<p className="font-mono text-[12px]">{effect.text}</p>
{effect.showSource && (
<div className="mt-1 flex flex-wrap items-center gap-1.5">
<EvidenceBadge effect={effect} />
<SourceChip effect={effect} />
</div>
)}
</li>
))}
</ul>
{(item.caveatLines.length > 0 || item.measurementLine || item.notes.length > 0) && (
<div className="mt-2 space-y-0.5 text-[11px] text-gray-600">
{item.caveatLines.map((c) => (
<p key={c} className="flex gap-1">
<AlertTriangle className="w-3 h-3 shrink-0 mt-0.5 text-amber-600" aria-hidden="true" />
<span>{c}</span>
</p>
))}
{item.measurementLine && <p>{item.measurementLine}</p>}
{item.notes.map((n) => <p key={n}>{n}</p>)}
</div>
)}
</li>
);
}
export default function EvidenceActionsCard({ response, loading, error, supported = true }) {
const ev = summariseEvidence({ response, loading, error, supported });
if (ev.state === 'unavailable') return null;
return (
<div className="card border-2 border-teal-300" data-testid="evidence-actions-card">
<Header />
<div className="card-body">
{ev.state === 'loading' && (
<div className="flex items-center gap-2 text-sm text-gray-500 py-4">
<Loader2 className="w-4 h-4 animate-spin text-teal-600" />
{EVIDENCE_LOADING}
</div>
)}
{ev.state === 'error' && (
<div className="flex items-center gap-2 text-sm text-gray-600 py-2">
<AlertTriangle className="w-4 h-4 text-amber-500" aria-hidden="true" />
{ev.message}
</div>
)}
{(ev.state === 'ready' || ev.state === 'empty') && (
<>
{ev.firstAction && (
<a
href={`#${ACTION_PLAN_ANCHOR_ID}`}
onClick={scrollToActionPlan}
className="mb-3 flex items-start gap-2 rounded-lg border border-slate-300 bg-slate-50 p-3 text-sm text-slate-900 hover:bg-slate-100"
>
<ClipboardList className="w-4 h-4 shrink-0 mt-0.5" aria-hidden="true" />
<span>{ev.firstAction}</span>
</a>
)}
{ev.message && <p className="text-sm text-gray-600">{ev.message}</p>}
{ev.sections.map((section) => (
section.collapsed ? (
<details key={section.group} className="mt-4" data-section={section.group}>
<summary className="text-xs text-gray-600 cursor-pointer hover:text-gray-800 flex items-center gap-1">
<ChevronDown className="w-3 h-3" />
{section.title} ({section.items.length})
</summary>
<ul className="mt-2 space-y-1 text-xs text-gray-600">
{section.items.map((item) => (
<li key={item.id}>
<span className="font-medium text-gray-700">{item.action}</span> β€” {item.headline}
{item.ruleLine && <span className="font-mono text-[11px]"> Β· {item.ruleLine}</span>}
<CompactSource source={item.ruleSource} />
</li>
))}
</ul>
</details>
) : (
<section key={section.group} className="mt-4" data-section={section.group}>
<h3 className="text-xs font-semibold uppercase tracking-wide text-teal-800 mb-2">
{section.title}
</h3>
{section.compact ? (
<ul className="space-y-1 text-xs text-gray-600">
{section.items.map((item) => (
<li key={item.id}>
<span className="font-medium text-gray-700">{item.action}</span> β€” {item.headline}
<CompactSource source={item.ruleSource} />
</li>
))}
</ul>
) : (
<ul className="space-y-3">
{section.items.map((item) => <Item key={item.id} item={item} />)}
</ul>
)}
</section>
)
))}
{ev.associationOnly.length > 0 && (
<details className="mt-4">
<summary className="text-xs text-gray-600 cursor-pointer hover:text-gray-800 flex items-center gap-1">
<ChevronDown className="w-3 h-3" />
{ev.associationOnlyTitle} ({ev.associationOnly.length})
</summary>
<ul className="mt-2 space-y-1 text-xs text-gray-600">
{ev.associationOnly.map((a) => (
<li key={a.feature}>
<span className="font-medium text-gray-700">{a.feature}</span> β€” {a.statement}
</li>
))}
</ul>
</details>
)}
{ev.notInModel.length > 0 && (
<div className="mt-3 text-[11px] text-gray-500">
<span className="font-medium">{ev.notInModelTitle}:</span>{' '}
{ev.notInModel.map((n, i) => (
<span key={n.id}>
{i > 0 && '; '}
{n.action} β€” {n.statement}
</span>
))}
</div>
)}
</>
)}
{ev.scopeNote && <ScopeNote text={ev.scopeNote} />}
</div>
</div>
);
}