| 1 | import { useState } from "react"; |
| 2 | import { ChevronRight } from "lucide-react"; |
| 3 | import { useT } from "../lib/i18n"; |
| 4 | import { visibleTranscriptMemoryCitations } from "../lib/memoryCitationVisibility"; |
| 5 | import type { MemoryCitation } from "../lib/types"; |
| 6 | |
| 7 | export function MemoryCitations({ citations }: { citations?: MemoryCitation[] }) { |
| 8 | const t = useT(); |
| 9 | const [open, setOpen] = useState(false); |
| 10 | const clean = visibleTranscriptMemoryCitations(citations) |
| 11 | .filter((citation) => (citation.source ?? citation.id ?? citation.note ?? "").trim() !== "") |
| 12 | .slice(0, 5); |
| 13 | if (clean.length === 0) return null; |
| 14 | return ( |
| 15 | <div className="msg-memory-citations"> |
| 16 | <button |
| 17 | type="button" |
| 18 | className="msg-memory-citations__toggle" |
| 19 | aria-expanded={open} |
| 20 | onClick={() => setOpen((value) => !value)} |
| 21 | > |
| 22 | <ChevronRight className={`msg-memory-citations__chevron${open ? " msg-memory-citations__chevron--open" : ""}`} size={15} /> |
| 23 | <span>{t("msg.memoryCompilerCitationsCount", { n: clean.length })}</span> |
| 24 | </button> |
| 25 | {open && ( |
| 26 | <div className="msg-memory-citations__body"> |
| 27 | {clean.map((citation, index) => { |
| 28 | const lines = memoryCitationLines(citation, t); |
| 29 | return ( |
| 30 | <div key={`${citation.id ?? citation.source}-${index}`} className="msg-memory-citations__item"> |
| 31 | <div className="msg-memory-citations__source"> |
| 32 | <span>{memoryCitationSource(citation)}</span> |
| 33 | {lines && <span className="msg-memory-citations__lines">{lines}</span>} |
| 34 | </div> |
| 35 | {citation.note && <div className="msg-memory-citations__note">{citation.note}</div>} |
| 36 | </div> |
| 37 | ); |
| 38 | })} |
| 39 | </div> |
| 40 | )} |
| 41 | </div> |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | function memoryCitationSource(citation: MemoryCitation): string { |
| 46 | const source = (citation.source || citation.id || "Memory v5").trim(); |
| 47 | if (citation.kind === "compiler_reference" && source === "Memory v5") return "Memory v5 compiler"; |
| 48 | return source; |
| 49 | } |
| 50 | |
| 51 | function memoryCitationLines(citation: MemoryCitation, t: ReturnType<typeof useT>): string { |
| 52 | const start = citation.lineStart ?? 0; |
| 53 | const end = citation.lineEnd ?? 0; |
| 54 | if (start <= 0) return ""; |
| 55 | if (end > 0 && end !== start) return t("msg.memoryCitationLineRange", { start, end }); |
| 56 | return t("msg.memoryCitationLine", { line: start }); |
| 57 | } |
| 58 |