diff-viewer.tsx428 lines, 12 KB
| 1 | "use client"; |
| 2 | |
| 3 | import React, { useState } from "react"; |
| 4 | import { Card, CardContent } from "@/components/ui/card"; |
| 5 | import { Badge } from "@/components/ui/badge"; |
| 6 | import { Button } from "@/components/ui/button"; |
| 7 | import { |
| 8 | ChevronDown, |
| 9 | ChevronRight, |
| 10 | Plus, |
| 11 | Minus, |
| 12 | FilePlus, |
| 13 | FileMinus, |
| 14 | FileEdit, |
| 15 | FileText, |
| 16 | Copy, |
| 17 | Check, |
| 18 | FolderTree, |
| 19 | AlertCircle, |
| 20 | } from "lucide-react"; |
| 21 | import { cn } from "@/lib/utils"; |
| 22 | |
| 23 | interface DiffFile { |
| 24 | sha: string; |
| 25 | filename: string; |
| 26 | status: string; |
| 27 | additions: number; |
| 28 | deletions: number; |
| 29 | changes: number; |
| 30 | patch?: string; |
| 31 | previousFilename?: string; |
| 32 | } |
| 33 | |
| 34 | interface DiffViewerProps { |
| 35 | files: DiffFile[]; |
| 36 | } |
| 37 | |
| 38 | export function DiffViewer({ files }: DiffViewerProps) { |
| 39 | const totalAdditions = files.reduce((sum, f) => sum + f.additions, 0); |
| 40 | const totalDeletions = files.reduce((sum, f) => sum + f.deletions, 0); |
| 41 | const [expandedFiles, setExpandedFiles] = useState<Set<string>>( |
| 42 | new Set(files.slice(0, 3).map((f) => f.sha)), |
| 43 | ); |
| 44 | |
| 45 | const toggleFile = (sha: string) => { |
| 46 | const next = new Set(expandedFiles); |
| 47 | if (next.has(sha)) { |
| 48 | next.delete(sha); |
| 49 | } else { |
| 50 | next.add(sha); |
| 51 | } |
| 52 | setExpandedFiles(next); |
| 53 | }; |
| 54 | |
| 55 | const expandAll = () => { |
| 56 | setExpandedFiles(new Set(files.map((f) => f.sha))); |
| 57 | }; |
| 58 | |
| 59 | const collapseAll = () => { |
| 60 | setExpandedFiles(new Set()); |
| 61 | }; |
| 62 | |
| 63 | return ( |
| 64 | <div className="space-y-4"> |
| 65 | {/* Summary bar */} |
| 66 | <div className="flex items-center justify-between"> |
| 67 | <div className="flex items-center gap-4"> |
| 68 | <div className="flex items-center gap-2"> |
| 69 | <div className="p-2 rounded-lg bg-primary/10"> |
| 70 | <FolderTree className="size-4 text-primary" /> |
| 71 | </div> |
| 72 | <div> |
| 73 | <span className="text-base font-medium tabular-nums"> |
| 74 | {files.length} |
| 75 | </span> |
| 76 | <span className="text-sm text-muted-foreground ml-1.5"> |
| 77 | {files.length === 1 ? "file" : "files"} changed |
| 78 | </span> |
| 79 | </div> |
| 80 | </div> |
| 81 | |
| 82 | <div className="h-5 w-px bg-border" /> |
| 83 | <div className="flex items-center gap-3 text-sm text-muted-foreground"> |
| 84 | <span className="flex items-center gap-1 text-emerald-600 dark:text-emerald-400"> |
| 85 | <Plus className="size-3.5" /> |
| 86 | <span className="tabular-nums">{totalAdditions}</span> |
| 87 | </span> |
| 88 | <span className="flex items-center gap-1 text-red-600 dark:text-red-400"> |
| 89 | <Minus className="size-3.5" /> |
| 90 | <span className="tabular-nums">{totalDeletions}</span> |
| 91 | </span> |
| 92 | </div> |
| 93 | </div> |
| 94 | |
| 95 | <div className="flex items-center gap-2"> |
| 96 | <Button variant={"ghost"} size={"sm"} onClick={expandAll}> |
| 97 | Expand all |
| 98 | </Button> |
| 99 | <Button variant={"ghost"} size={"sm"} onClick={collapseAll}> |
| 100 | Collapse all |
| 101 | </Button> |
| 102 | </div> |
| 103 | </div> |
| 104 | |
| 105 | <div className="space-y-3"> |
| 106 | {files.map((file) => ( |
| 107 | <DiffFileCard |
| 108 | key={file.sha} |
| 109 | file={file} |
| 110 | expanded={expandedFiles.has(file.sha)} |
| 111 | onToggle={() => toggleFile(file.sha)} |
| 112 | /> |
| 113 | ))} |
| 114 | </div> |
| 115 | </div> |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | function DiffFileCard({ |
| 120 | file, |
| 121 | expanded, |
| 122 | onToggle, |
| 123 | }: { |
| 124 | file: DiffFile; |
| 125 | expanded: boolean; |
| 126 | onToggle: () => void; |
| 127 | }) { |
| 128 | const [copied, setCopied] = useState(false); |
| 129 | const StatusIcon = getStatusIcon(file.status); |
| 130 | const statusConfig = getStatusConfig(file.status); |
| 131 | |
| 132 | const copyFilename = () => { |
| 133 | navigator.clipboard.writeText(file.filename); |
| 134 | setCopied(true); |
| 135 | setTimeout(() => setCopied(false), 2000); |
| 136 | }; |
| 137 | |
| 138 | const pathParts = file.filename.split("/"); |
| 139 | const fileName = pathParts.pop(); |
| 140 | const directory = pathParts.join("/"); |
| 141 | |
| 142 | return ( |
| 143 | <Card className="overflow-hidden"> |
| 144 | <button |
| 145 | onClick={onToggle} |
| 146 | className="flex items-center gap-3 w-full px-4 py-3 text-left hover:bg-muted/50 transition-colors" |
| 147 | > |
| 148 | <div className="shrink-0"> |
| 149 | {expanded ? ( |
| 150 | <ChevronDown className="size-4 text-muted-foreground" /> |
| 151 | ) : ( |
| 152 | <ChevronRight className="size-4 text-muted-foreground" /> |
| 153 | )} |
| 154 | </div> |
| 155 | |
| 156 | <div className={cn("p-1.5 rounded-md shrink-0", statusConfig.bg)}> |
| 157 | {React.createElement(StatusIcon, { |
| 158 | className: cn("size-4 text-muted-foreground truncate"), |
| 159 | })} |
| 160 | </div> |
| 161 | |
| 162 | <div className="flex-1 min-w-0 flex items-center gap-2"> |
| 163 | {directory && ( |
| 164 | <span className="text-sm text-muted-foreground font-mono truncate"> |
| 165 | {directory}/ |
| 166 | </span> |
| 167 | )} |
| 168 | <span className="text-sm font-medium font-mono truncate"> |
| 169 | {fileName} |
| 170 | </span> |
| 171 | {file.previousFilename && ( |
| 172 | <Badge variant={"outline"} className="text-xs shrink-0"> |
| 173 | {file.previousFilename} |
| 174 | </Badge> |
| 175 | )} |
| 176 | {file.changes > 300 && ( |
| 177 | <Badge |
| 178 | variant={"outline"} |
| 179 | className="text-[10px] shrink-0 gap-1 bg-amber-500/10 text-amber-600 dark:text-amber-400 border-amber-500/20" |
| 180 | > |
| 181 | <AlertCircle className="size-3" /> |
| 182 | Large changes |
| 183 | </Badge> |
| 184 | )} |
| 185 | </div> |
| 186 | |
| 187 | <div className="flex items-center gap-3 shrink-0"> |
| 188 | {/* Change bar visualization */} |
| 189 | <div className="hidden sm:flex items-center gap-0.5"> |
| 190 | {Array.from({ length: Math.min(5, file.additions) }).map((_, i) => ( |
| 191 | <div |
| 192 | key={`add-${i}`} |
| 193 | className="w-1.5 h-3 rounded-sm bg-emerald-500" |
| 194 | /> |
| 195 | ))} |
| 196 | {Array.from({ length: Math.min(5, file.deletions) }).map((_, i) => ( |
| 197 | <div |
| 198 | key={`del-${i}`} |
| 199 | className="w-1.5 h-3 rounded-sm bg-red-500" |
| 200 | /> |
| 201 | ))} |
| 202 | {file.additions + file.deletions === 0 && ( |
| 203 | <div className="w-1.5 h-3 rounded-sm bg-muted-foreground" /> |
| 204 | )} |
| 205 | </div> |
| 206 | |
| 207 | <div className="flex items-center gap-2 text-xs tabular-nums"> |
| 208 | <span className="text-emerald-600 dark:text-emerald-400 font-medium"> |
| 209 | +{file.additions} |
| 210 | </span> |
| 211 | <span className="text-red-600 dark:text-red-400 font-medium"> |
| 212 | -{file.deletions} |
| 213 | </span> |
| 214 | </div> |
| 215 | </div> |
| 216 | </button> |
| 217 | |
| 218 | {/* Expanded content */} |
| 219 | {expanded && ( |
| 220 | <CardContent className="p-0 border-t border-border/60"> |
| 221 | {file.patch ? ( |
| 222 | <div className="relative"> |
| 223 | <Button |
| 224 | variant={"ghost"} |
| 225 | size={"icon-sm"} |
| 226 | className="absolute top-2 right-2 z-10 opacity-10 hover:opacity-100 focus:opacity-100 transition-opacity bg-background/80 backdrop-blur-sm" |
| 227 | onClick={(e) => { |
| 228 | e.stopPropagation(); |
| 229 | copyFilename(); |
| 230 | }} |
| 231 | > |
| 232 | {copied ? <Check /> : <Copy className="w-4 h-4" />} |
| 233 | </Button> |
| 234 | |
| 235 | <DiffContent patch={file.patch} /> |
| 236 | </div> |
| 237 | ) : ( |
| 238 | <div className="py-8 text-center text-sm text-muted-foreground"> |
| 239 | <FileText className="size-8 mx-auto mb-2 opacity-50" /> |
| 240 | <p>No diff available for this file.</p> |
| 241 | <p className="text-xs mt-1"> |
| 242 | Binary file or too large to display. |
| 243 | </p> |
| 244 | </div> |
| 245 | )} |
| 246 | </CardContent> |
| 247 | )} |
| 248 | </Card> |
| 249 | ); |
| 250 | } |
| 251 | |
| 252 | function DiffContent({ patch }: { patch: string }) { |
| 253 | const lines = patch.split("\n"); |
| 254 | let oldLineNum = 0; |
| 255 | let newLineNum = 0; |
| 256 | |
| 257 | return ( |
| 258 | <div className="overflow-x-auto"> |
| 259 | <table className="w-full text-xs font-mono"> |
| 260 | <tbody> |
| 261 | {lines.map((line, index) => { |
| 262 | const lineInfo = parseLine(line, oldLineNum, newLineNum); |
| 263 | |
| 264 | if (lineInfo.isHunk) { |
| 265 | const match = line.match(/@@ -(\d+),?\d* \+(\d+),?\d* @@/); |
| 266 | if (match?.[1] && match?.[2]) { |
| 267 | oldLineNum = parseInt(match[1], 10) - 1; |
| 268 | newLineNum = parseInt(match[2], 10) - 1; |
| 269 | } |
| 270 | } else if (lineInfo.type === "deletion") { |
| 271 | oldLineNum++; |
| 272 | } else if (lineInfo.type === "addition") { |
| 273 | newLineNum++; |
| 274 | } else if (lineInfo.type === "context") { |
| 275 | oldLineNum++; |
| 276 | newLineNum++; |
| 277 | } |
| 278 | |
| 279 | return ( |
| 280 | <DiffTableRow |
| 281 | key={index} |
| 282 | line={line} |
| 283 | oldNum={lineInfo.type === "addition" ? null : oldLineNum} |
| 284 | newNum={lineInfo.type === "deletion" ? null : newLineNum} |
| 285 | lineInfo={lineInfo} |
| 286 | /> |
| 287 | ); |
| 288 | })} |
| 289 | </tbody> |
| 290 | </table> |
| 291 | </div> |
| 292 | ); |
| 293 | } |
| 294 | |
| 295 | function getStatusIcon(status: string) { |
| 296 | switch (status) { |
| 297 | case "added": |
| 298 | return FilePlus; |
| 299 | case "removed": |
| 300 | return FileMinus; |
| 301 | case "modified": |
| 302 | case "changed": |
| 303 | return FileEdit; |
| 304 | case "renamed": |
| 305 | return FileEdit; |
| 306 | default: |
| 307 | return FileText; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | function getStatusConfig(status: string) { |
| 312 | switch (status) { |
| 313 | case "added": |
| 314 | return { |
| 315 | color: "text-emerald-600 dark:text-emerald-400", |
| 316 | bg: "bg-emerald-500/10", |
| 317 | }; |
| 318 | case "removed": |
| 319 | return { |
| 320 | color: "text-red-600 dark:text-red-400", |
| 321 | bg: "bg-red-500/10", |
| 322 | }; |
| 323 | case "modified": |
| 324 | case "changed": |
| 325 | return { |
| 326 | color: "text-amber-600 dark:text-amber-400", |
| 327 | bg: "bg-amber-500/10", |
| 328 | }; |
| 329 | case "renamed": |
| 330 | return { |
| 331 | color: "text-blue-600 dark:text-blue-400", |
| 332 | bg: "bg-blue-500/10", |
| 333 | }; |
| 334 | default: |
| 335 | return { |
| 336 | color: "text-muted-foreground", |
| 337 | bg: "bg-muted", |
| 338 | }; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | interface LineInfo { |
| 343 | type: "addition" | "deletion" | "context" | "hunk"; |
| 344 | isHunk: boolean; |
| 345 | } |
| 346 | |
| 347 | function parseLine(line: string, _oldNum: number, _newNum: number): LineInfo { |
| 348 | if (line.startsWith("@@")) { |
| 349 | return { type: "hunk", isHunk: true }; |
| 350 | } |
| 351 | if (line.startsWith("+")) { |
| 352 | return { type: "addition", isHunk: false }; |
| 353 | } |
| 354 | if (line.startsWith("-")) { |
| 355 | return { type: "deletion", isHunk: false }; |
| 356 | } |
| 357 | return { type: "context", isHunk: false }; |
| 358 | } |
| 359 | |
| 360 | function DiffTableRow({ |
| 361 | line, |
| 362 | lineInfo, |
| 363 | oldNum, |
| 364 | newNum, |
| 365 | }: { |
| 366 | line: string; |
| 367 | lineInfo: LineInfo; |
| 368 | oldNum: number | null; |
| 369 | newNum: number | null; |
| 370 | }) { |
| 371 | const bgClass = { |
| 372 | addition: "bg-emerald-500/10", |
| 373 | deletion: "bg-red-500/10", |
| 374 | hunk: "bg-blue-500/10", |
| 375 | context: "", |
| 376 | }[lineInfo.type]; |
| 377 | |
| 378 | const textClass = { |
| 379 | addition: "text-emerald-700 dark:text-emerald-300", |
| 380 | deletion: "text-red-700 dark:text-red-300", |
| 381 | hunk: "text-blue-600 dark:text-blue-400", |
| 382 | context: "text-foreground", |
| 383 | }[lineInfo.type]; |
| 384 | |
| 385 | const lineNumClass = { |
| 386 | addition: "bg-emerald-500/5 text-emerald-600/70 dark:text-emerald-400/70", |
| 387 | deletion: "bg-red-500/5 text-red-600/70 dark:text-red-400/70", |
| 388 | hunk: "bg-blue-500/5", |
| 389 | context: "text-muted-foreground/50", |
| 390 | }[lineInfo.type]; |
| 391 | |
| 392 | if (lineInfo.isHunk) { |
| 393 | return ( |
| 394 | <tr className={bgClass}> |
| 395 | <td |
| 396 | colSpan={3} |
| 397 | className={cn("px-4 py-1.5 text-xs", textClass, lineNumClass)} |
| 398 | > |
| 399 | {line} |
| 400 | </td> |
| 401 | </tr> |
| 402 | ); |
| 403 | } |
| 404 | |
| 405 | return ( |
| 406 | <tr className={cn("group", bgClass)}> |
| 407 | <td |
| 408 | className={cn( |
| 409 | "w-12 px-2 py-0.5 text-right select-none border-r border-border/30", |
| 410 | lineNumClass, |
| 411 | )} |
| 412 | > |
| 413 | {oldNum || ""} |
| 414 | </td> |
| 415 | <td |
| 416 | className={cn( |
| 417 | "w-12 px-2 py-0.5 text-right select-none border-r border-border/30", |
| 418 | lineNumClass, |
| 419 | )} |
| 420 | > |
| 421 | {newNum || ""} |
| 422 | </td> |
| 423 | <td className={cn("px-4 py-0.5 whitespace-pre", textClass)}> |
| 424 | {line.slice(1) || " "} |
| 425 | </td> |
| 426 | </tr> |
| 427 | ); |
| 428 | } |