import { memo } from "react"; interface DiffBlockProps { content: string } interface DiffLine { type: "add" | "remove" | "context" | "header"; content: string; } function parseDiff(raw: string): DiffLine[] { return raw.split("\n").map(line => { if (line.startsWith("+++") || line.startsWith("---") || line.startsWith("@@")) return { type: "header" as const, content: line }; if (line.startsWith("+")) return { type: "add" as const, content: line.slice(1) }; if (line.startsWith("-")) return { type: "remove" as const, content: line.slice(1) }; return { type: "context" as const, content: line.startsWith(" ") ? line.slice(1) : line }; }); } const DiffBlock = memo(function DiffBlock({ content }: DiffBlockProps) { const lines = parseDiff(content); const adds = lines.filter(l => l.type === "add").length; const removes = lines.filter(l => l.type === "remove").length; return (
Diff +{adds} -{removes}
{lines.map((line, i) => { const bg = line.type === "add" ? "rgba(34,197,94,0.08)" : line.type === "remove" ? "rgba(239,68,68,0.08)" : line.type === "header" ? "rgba(79,142,247,0.06)" : "transparent"; const color = line.type === "add" ? "#86efac" : line.type === "remove" ? "#fca5a5" : line.type === "header" ? "#7aadff" : "#c0c0d0"; const prefix = line.type === "add" ? "+" : line.type === "remove" ? "−" : line.type === "header" ? "" : " "; return ( ); })}
{prefix} {line.content}
); } ); export default DiffBlock;