import { useMemo, useState } from 'react'; import { ChevronDown, ChevronRight, FileCode } from 'lucide-react'; interface Props { diff: string; } const INITIAL_LINES = 10; export function DiffSnippet({ diff }: Props) { const [expanded, setExpanded] = useState(false); const lines = useMemo(() => diff.split('\n'), [diff]); const totalLines = lines.length; // Find the first and last content lines (skip leading ---/+++ headers) const firstContentIdx = lines.findIndex( (l) => l.startsWith('+') || l.startsWith('-') || l.startsWith(' '), ); // Count content lines that are either +, -, or context lines const contentLineCount = lines.filter( (l) => l.startsWith('+') || l.startsWith('-') || l.startsWith(' '), ).length; // Show first N content lines, plus header lines const displayLines = useMemo(() => { const sliceEnd = expanded ? lines.length : Math.min(firstContentIdx + INITIAL_LINES + contentLineCount, lines.length); return lines.slice(0, sliceEnd); }, [lines, expanded, firstContentIdx, contentLineCount]); const hasMore = totalLines > displayLines.length; if (totalLines === 0) return null; return (
{displayLines.map((line, i) => { // Determine color class based on line prefix let colorClass = 'text-muted-foreground/60'; if (line.startsWith('+')) colorClass = 'text-emerald-600 dark:text-emerald-400'; else if (line.startsWith('-')) colorClass = 'text-red-500 dark:text-red-400'; else if (line.startsWith('@@')) colorClass = 'text-muted-foreground'; else if (line.startsWith('---') || line.startsWith('+++')) colorClass = 'text-muted-foreground/50'; return (
{line}
); })} {hasMore && !expanded && ( )}
); }