import { Card, CardContent, CardHeader } from '@/components/ui/card'; import { Badge } from '@/components/ui/badge'; import { FileText, CheckCircle, XCircle } from 'lucide-react'; import { cn } from '@/lib/utils'; interface DiffRendererProps { path: string; oldStr: string; newStr: string; result?: string; isError?: boolean; className?: string; } export function DiffRenderer({ path, oldStr, newStr, result, isError = false, className }: DiffRendererProps) { // Split strings into lines for diff rendering const oldLines = oldStr.split('\n'); const newLines = newStr.split('\n'); // Simple diff algorithm - find common prefix and suffix const findCommonPrefix = (a: string[], b: string[]) => { let i = 0; while (i < Math.min(a.length, b.length) && a[i] === b[i]) { i++; } return i; }; const findCommonSuffix = (a: string[], b: string[], prefixLength: number) => { let i = 0; const maxLength = Math.min(a.length - prefixLength, b.length - prefixLength); while (i < maxLength && a[a.length - 1 - i] === b[b.length - 1 - i]) { i++; } return i; }; const prefixLength = findCommonPrefix(oldLines, newLines); const suffixLength = findCommonSuffix(oldLines, newLines, prefixLength); const commonPrefix = oldLines.slice(0, prefixLength); const commonSuffix = oldLines.slice(oldLines.length - suffixLength); const removedLines = oldLines.slice(prefixLength, oldLines.length - suffixLength); const addedLines = newLines.slice(prefixLength, newLines.length - suffixLength); return (
{path}
{isError ? ( Failed ) : ( Success )}
{/* Context before changes */} {commonPrefix.length > 0 && (
{commonPrefix.slice(-3).map((line, index) => (
{prefixLength - 3 + index + 1} {line || ' '}
))}
)} {/* Removed lines */} {removedLines.map((line, index) => (
-{prefixLength + index + 1} - {line || ' '}
))} {/* Added lines */} {addedLines.map((line, index) => (
+{prefixLength + index + 1} + {line || ' '}
))} {/* Context after changes */} {commonSuffix.length > 0 && (
{commonSuffix.slice(0, 3).map((line, index) => (
{prefixLength + Math.max(removedLines.length, addedLines.length) + index + 1} {line || ' '}
))}
)} {/* Handle case where there are no changes */} {removedLines.length === 0 && addedLines.length === 0 && (
No changes detected
)}
{/* Summary */}
{removedLines.length > 0 && ( -{removedLines.length} line{removedLines.length !== 1 ? 's' : ''} )} {addedLines.length > 0 && ( +{addedLines.length} line{addedLines.length !== 1 ? 's' : ''} )}
{/* Tool Result */} {result && (
{result}
)}
); }