import { useFileDiff } from '@/api/git' import { Loader2, FileText, FilePlus, FileX, FileEdit, File, Plus, Minus, ArrowLeft, ExternalLink } from 'lucide-react' import { Button } from '@/components/ui/button' import { cn } from '@/lib/utils' import type { GitFileStatusType } from '@/types/git' interface FileDiffViewProps { repoId: number filePath: string onBack?: () => void onOpenFile?: (path: string, lineNumber?: number) => void isMobile?: boolean } interface DiffLine { type: 'add' | 'remove' | 'context' | 'header' | 'hunk' content: string oldLineNumber?: number newLineNumber?: number } function parseDiff(diff: string): DiffLine[] { const lines = diff.split('\n') const result: DiffLine[] = [] let oldLine = 0 let newLine = 0 for (const line of lines) { if (line.startsWith('diff --git') || line.startsWith('index ') || line.startsWith('---') || line.startsWith('+++')) { result.push({ type: 'header', content: line }) } else if (line.startsWith('@@')) { const match = line.match(/@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@/) if (match) { oldLine = parseInt(match[1], 10) newLine = parseInt(match[2], 10) } result.push({ type: 'hunk', content: line }) } else if (line.startsWith('+')) { result.push({ type: 'add', content: line.substring(1), newLineNumber: newLine }) newLine++ } else if (line.startsWith('-')) { result.push({ type: 'remove', content: line.substring(1), oldLineNumber: oldLine }) oldLine++ } else if (line.startsWith(' ') || line === '') { result.push({ type: 'context', content: line.substring(1) || '', oldLineNumber: oldLine, newLineNumber: newLine }) oldLine++ newLine++ } } return result } const statusConfig: Record = { modified: { icon: FileEdit, color: 'text-yellow-500', bgColor: 'bg-yellow-500/10', label: 'Modified' }, added: { icon: FilePlus, color: 'text-green-500', bgColor: 'bg-green-500/10', label: 'Added' }, deleted: { icon: FileX, color: 'text-red-500', bgColor: 'bg-red-500/10', label: 'Deleted' }, renamed: { icon: FileText, color: 'text-blue-500', bgColor: 'bg-blue-500/10', label: 'Renamed' }, untracked: { icon: File, color: 'text-muted-foreground', bgColor: 'bg-muted/50', label: 'Untracked' }, copied: { icon: FileText, color: 'text-purple-500', bgColor: 'bg-purple-500/10', label: 'Copied' }, } function DiffLineComponent({ line, showLineNumbers, onLineClick }: { line: DiffLine; showLineNumbers: boolean; onLineClick?: (lineNumber: number) => void }) { if (line.type === 'header') { return (
{line.content}
) } if (line.type === 'hunk') { return (
{line.content}
) } const bgClass = line.type === 'add' ? 'bg-green-500/10' : line.type === 'remove' ? 'bg-red-500/10' : '' const textClass = line.type === 'add' ? 'text-green-600 dark:text-green-400' : line.type === 'remove' ? 'text-red-600 dark:text-red-400' : 'text-foreground' const lineNumber = line.newLineNumber ?? line.oldLineNumber const isClickable = onLineClick && lineNumber !== undefined return (
isClickable && lineNumber !== undefined && onLineClick(lineNumber)} > {showLineNumbers && (
{line.oldLineNumber || ''} {line.newLineNumber || ''}
)}
{line.type === 'add' && } {line.type === 'remove' && }
        {line.content || ' '}
      
) } export function FileDiffView({ repoId, filePath, onBack, onOpenFile, isMobile = false }: FileDiffViewProps) { const { data: diffData, isLoading, error } = useFileDiff(repoId, filePath) const fileName = filePath.split('/').pop() || filePath const dirPath = filePath.includes('/') ? filePath.substring(0, filePath.lastIndexOf('/')) : '' if (isLoading) { return (
) } if (error) { return (

Failed to load diff

{error.message}

{onBack && ( )}
) } if (!diffData) { return null } const config = statusConfig[diffData.status] const Icon = config.icon const diffLines = diffData.diff ? parseDiff(diffData.diff) : [] return (
{onBack && ( )}
{onOpenFile ? ( ) : ( <>
{fileName}
{dirPath &&
{dirPath}
} )}
{config.label} {!diffData.isBinary && ( <> +{diffData.additions} -{diffData.deletions} )}
{diffData.isBinary ? (

Binary file - cannot display diff

) : !diffData.diff ? (

No changes to display

) : (
{diffLines.map((line, index) => ( onOpenFile(filePath, lineNum) : undefined} /> ))}
)}
) }