import { useState } from "react"; import { Prism as SyntaxHighlighter } from "react-syntax-highlighter"; import { oneDark } from "react-syntax-highlighter/dist/esm/styles/prism"; const LANG_MAP = { python: "python", javascript: "javascript", typescript: "typescript", go: "go", rust: "rust", java: "java", cpp: "cpp", c: "c", markdown: "markdown", yaml: "yaml", json: "json", bash: "bash", }; const CopyIcon = () => ( ); // showRepo=true when querying all repos — makes the source repo visible on every card export default function SourceCard({ source, index, showRepo = false }) { const [open, setOpen] = useState(false); const [copied, setCopied] = useState(false); const lang = LANG_MAP[source.language] || "text"; const name = source.name ? `${source.name}()` : null; // When parent-doc expansion ran, the GitHub link points to the original matched // function (matched_start_line), not the full class range (start_line). // blob/HEAD resolves to the default branch (main or master) without hardcoding. const lineAnchor = (source.matched_start_line && source.matched_start_line !== source.start_line) ? source.matched_start_line : source.start_line; const githubUrl = `https://github.com/${source.repo}/blob/HEAD/${source.filepath}#L${lineAnchor}`; const scorePercent = source.score != null ? `${Math.round(source.score * 100)}%` : null; // Color-code by confidence using design-system tokens (sage/warning/red) const scoreColor = !source.score ? null : source.score >= 0.90 ? "var(--green)" // sage green — high confidence : source.score >= 0.70 ? "var(--warning)" // warm amber — moderate : "var(--red)"; // muted red — low confidence function handleCopy(e) { e.stopPropagation(); navigator.clipboard.writeText(source.text).then(() => { setCopied(true); setTimeout(() => setCopied(false), 1500); }); } return (
setOpen((o) => !o)} role="button" tabIndex={0} onKeyDown={(e) => e.key === "Enter" || e.key === " " ? setOpen(o => !o) : null} aria-expanded={open} aria-label={`Source ${index}: ${source.filepath}`} > {index} {showRepo && source.repo && ( {source.repo.split("/")[1]} )} {lang} e.stopPropagation()} title={`Open ${source.filepath} on GitHub`} > {source.filepath} {name && {name}} {source.matched_start_line && source.matched_start_line !== source.start_line ? ( // Parent-doc expanded: the matched function was swapped out for its enclosing class. // Show the original function lines + a badge indicating the class expansion. <> L{source.matched_start_line}–{source.matched_end_line} ↕ class ) : ( L{source.start_line}–{source.end_line} )} e.stopPropagation()} title={`Open on GitHub (L${lineAnchor})`} aria-label="Open on GitHub" > {scorePercent && ( {scorePercent} )}
{open && (
{source.text ? ( {source.text} ) : (
Code preview not stored —{" "} e.stopPropagation()}> open on GitHub ↗
)}
)}
); }