import { useState } from "react"; import { generateSynthesis } from "../api/protocols"; import type { SynthesisResult } from "../types"; interface SynthesisPanelProps { protocolId: string; initialSynthesis: SynthesisResult | null; } // Only matches real evidence-ID citations (e.g. [pubmed-12345], // [europepmc-98765]) - deliberately NOT a bare [a-zA-Z0-9_-]+ pattern, since // the model sometimes brackets abbreviations (e.g. "[ALT]") when it isn't // citing anything; matching only known source prefixes avoids styling // those as if they were real citations. const BOLD_PATTERN = /(\*\*(.+?)\*\*)|(\[((?:pubmed|europepmc)-[a-zA-Z0-9._-]+)\])/g; function renderInline(text: string) { const nodes: React.ReactNode[] = []; let lastIndex = 0; let key = 0; for (const match of text.matchAll(BOLD_PATTERN)) { const index = match.index ?? 0; if (index > lastIndex) nodes.push(text.slice(lastIndex, index)); const [full, boldFull, boldText, , citationId] = match; if (boldFull) { nodes.push({boldText}); } else if (citationId) { nodes.push( {citationId} ); } lastIndex = index + full.length; } if (lastIndex < text.length) nodes.push(text.slice(lastIndex)); return nodes; } function renderSynthesisBody(text: string) { const lines = text.split("\n"); const blocks: React.ReactNode[] = []; let listBuffer: string[] = []; let blockKey = 0; const flushList = () => { if (listBuffer.length === 0) return; blocks.push( ); listBuffer = []; }; for (const rawLine of lines) { const line = rawLine.trim(); if (line.startsWith("- ") || line.startsWith("* ")) { listBuffer.push(line.slice(2)); continue; } flushList(); if (line.length > 0) { blocks.push(

{renderInline(line)}

); } } flushList(); return blocks; } export function SynthesisPanel({ protocolId, initialSynthesis }: SynthesisPanelProps) { const [synthesis, setSynthesis] = useState(initialSynthesis); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); async function handleGenerate() { setLoading(true); setError(null); try { const result = await generateSynthesis(protocolId); setSynthesis(result); } catch (err) { setError(err instanceof Error ? err.message : "Failed to generate synthesis."); } finally { setLoading(false); } } return (

Narrative synthesis

{error &&
{error}
} {synthesis ? ( <>
{renderSynthesisBody(synthesis.text)}

Generated {new Date(synthesis.generated_at).toLocaleString()}

) : ( !error && (

No synthesis generated yet. This step uses an LLM to draft prose from the evidence table above — requires GROQ_API_KEY set in the backend's .env.

) )}
); }