1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
|
import { useState, useMemo } from "react";
interface DiffLine {
type: "add" | "remove" | "context" | "header" | "hunk";
content: string;
oldLineNumber?: number;
newLineNumber?: number;
}
interface DiffFile {
path: string;
status: "added" | "modified" | "deleted" | "renamed";
oldPath?: string; // For renames
additions: number;
deletions: number;
lines: DiffLine[];
}
interface OverlayDiffViewerProps {
diff: string;
changedFiles?: string[];
loading?: boolean;
error?: string;
onClose?: () => void;
title?: string;
}
function parseDiff(diffText: string): DiffFile[] {
if (!diffText.trim()) return [];
const files: DiffFile[] = [];
const lines = diffText.split("\n");
let currentFile: DiffFile | null = null;
let oldLineNum = 0;
let newLineNum = 0;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
// File header (diff --git a/path b/path)
if (line.startsWith("diff --git")) {
if (currentFile) {
files.push(currentFile);
}
// Extract paths
const match = line.match(/diff --git a\/(.+) b\/(.+)/);
const oldPath = match?.[1] || "";
const newPath = match?.[2] || oldPath;
currentFile = {
path: newPath,
oldPath: oldPath !== newPath ? oldPath : undefined,
status: "modified",
additions: 0,
deletions: 0,
lines: [],
};
continue;
}
if (!currentFile) continue;
// New file indicator
if (line.startsWith("new file mode")) {
currentFile.status = "added";
continue;
}
// Deleted file indicator
if (line.startsWith("deleted file mode")) {
currentFile.status = "deleted";
continue;
}
// Rename indicator
if (line.startsWith("rename from") || line.startsWith("rename to")) {
currentFile.status = "renamed";
continue;
}
// Hunk header (@@ -1,3 +1,4 @@)
if (line.startsWith("@@")) {
const hunkMatch = line.match(/@@ -(\d+),?\d* \+(\d+),?\d* @@/);
if (hunkMatch) {
oldLineNum = parseInt(hunkMatch[1], 10);
newLineNum = parseInt(hunkMatch[2], 10);
}
currentFile.lines.push({
type: "hunk",
content: line,
});
continue;
}
// Skip other headers (---, +++, index, etc.)
if (
line.startsWith("---") ||
line.startsWith("+++") ||
line.startsWith("index ") ||
line.startsWith("Binary files")
) {
currentFile.lines.push({
type: "header",
content: line,
});
continue;
}
// Diff content
if (line.startsWith("+")) {
currentFile.additions++;
currentFile.lines.push({
type: "add",
content: line.substring(1),
newLineNumber: newLineNum++,
});
} else if (line.startsWith("-")) {
currentFile.deletions++;
currentFile.lines.push({
type: "remove",
content: line.substring(1),
oldLineNumber: oldLineNum++,
});
} else if (line.startsWith(" ") || line === "") {
currentFile.lines.push({
type: "context",
content: line.substring(1) || "",
oldLineNumber: oldLineNum++,
newLineNumber: newLineNum++,
});
}
}
if (currentFile) {
files.push(currentFile);
}
return files;
}
function DiffFileView({ file, collapsed, onToggle }: { file: DiffFile; collapsed: boolean; onToggle: () => void }) {
const statusColors: Record<DiffFile["status"], string> = {
added: "text-green-400 bg-green-400/10",
modified: "text-yellow-400 bg-yellow-400/10",
deleted: "text-red-400 bg-red-400/10",
renamed: "text-purple-400 bg-purple-400/10",
};
const statusLabels: Record<DiffFile["status"], string> = {
added: "A",
modified: "M",
deleted: "D",
renamed: "R",
};
return (
<div className="border border-[rgba(117,170,252,0.2)] mb-2">
{/* File header */}
<button
onClick={onToggle}
className="w-full flex items-center gap-2 px-3 py-2 bg-[rgba(0,0,0,0.2)] hover:bg-[rgba(0,0,0,0.3)] transition-colors text-left"
>
<span className="font-mono text-[10px] text-[#555]">
{collapsed ? "▶" : "▼"}
</span>
<span
className={`px-1.5 py-0.5 font-mono text-[9px] font-bold ${statusColors[file.status]}`}
>
{statusLabels[file.status]}
</span>
<span className="font-mono text-sm text-[#dbe7ff] flex-1 truncate">
{file.oldPath ? (
<>
<span className="text-[#555]">{file.oldPath}</span>
<span className="text-[#75aafc] mx-1">→</span>
{file.path}
</>
) : (
file.path
)}
</span>
<span className="font-mono text-[10px]">
{file.additions > 0 && (
<span className="text-green-400 mr-2">+{file.additions}</span>
)}
{file.deletions > 0 && (
<span className="text-red-400">-{file.deletions}</span>
)}
</span>
</button>
{/* File content */}
{!collapsed && (
<div className="overflow-x-auto">
<table className="w-full font-mono text-xs">
<tbody>
{file.lines.map((line, i) => {
if (line.type === "header" || line.type === "hunk") {
return (
<tr
key={i}
className="bg-[rgba(117,170,252,0.05)]"
>
<td
colSpan={3}
className="px-2 py-0.5 text-[#75aafc] select-none"
>
{line.content}
</td>
</tr>
);
}
const bgColor =
line.type === "add"
? "bg-green-400/10"
: line.type === "remove"
? "bg-red-400/10"
: "";
const textColor =
line.type === "add"
? "text-green-400"
: line.type === "remove"
? "text-red-400"
: "text-[#9bc3ff]";
const prefix =
line.type === "add"
? "+"
: line.type === "remove"
? "-"
: " ";
return (
<tr key={i} className={bgColor}>
{/* Old line number */}
<td className="w-10 px-2 py-0 text-right text-[#555] select-none border-r border-[rgba(117,170,252,0.1)]">
{line.type !== "add" ? line.oldLineNumber : ""}
</td>
{/* New line number */}
<td className="w-10 px-2 py-0 text-right text-[#555] select-none border-r border-[rgba(117,170,252,0.1)]">
{line.type !== "remove" ? line.newLineNumber : ""}
</td>
{/* Content */}
<td className={`px-2 py-0 whitespace-pre ${textColor}`}>
<span className="select-none mr-1 text-[#555]">
{prefix}
</span>
{line.content}
</td>
</tr>
);
})}
</tbody>
</table>
</div>
)}
</div>
);
}
export function OverlayDiffViewer({
diff,
changedFiles,
loading,
error,
onClose,
title = "Overlay Changes",
}: OverlayDiffViewerProps) {
const [collapsedFiles, setCollapsedFiles] = useState<Set<string>>(new Set());
const [showFullDiff, setShowFullDiff] = useState(true);
const parsedFiles = useMemo(() => parseDiff(diff), [diff]);
const toggleFile = (path: string) => {
setCollapsedFiles((prev) => {
const next = new Set(prev);
if (next.has(path)) {
next.delete(path);
} else {
next.add(path);
}
return next;
});
};
const expandAll = () => setCollapsedFiles(new Set());
const collapseAll = () => setCollapsedFiles(new Set(parsedFiles.map((f) => f.path)));
// Calculate totals
const totals = useMemo(() => {
return parsedFiles.reduce(
(acc, file) => ({
additions: acc.additions + file.additions,
deletions: acc.deletions + file.deletions,
files: acc.files + 1,
}),
{ additions: 0, deletions: 0, files: 0 }
);
}, [parsedFiles]);
if (loading) {
return (
<div className="panel p-4">
<div className="flex items-center justify-center h-32">
<div className="font-mono text-sm text-[#75aafc]">
Loading diff...
</div>
</div>
</div>
);
}
if (error) {
return (
<div className="panel p-4">
<div className="flex items-center justify-between mb-3">
<div className="font-mono text-xs text-[#9bc3ff] tracking-wide uppercase">
{title}
</div>
{onClose && (
<button
onClick={onClose}
className="font-mono text-xs text-[#555] hover:text-[#9bc3ff]"
>
Close
</button>
)}
</div>
<div className="bg-red-400/10 border border-red-400/30 p-3 font-mono text-sm text-red-400">
{error}
</div>
</div>
);
}
if (!diff.trim() && (!changedFiles || changedFiles.length === 0)) {
return (
<div className="panel p-4">
<div className="flex items-center justify-between mb-3">
<div className="font-mono text-xs text-[#9bc3ff] tracking-wide uppercase">
{title}
</div>
{onClose && (
<button
onClick={onClose}
className="font-mono text-xs text-[#555] hover:text-[#9bc3ff]"
>
Close
</button>
)}
</div>
<div className="text-center py-8 font-mono text-sm text-[#555]">
No changes detected in overlay
</div>
</div>
);
}
return (
<div className="panel flex flex-col max-h-[600px]">
{/* Header */}
<div className="flex items-center justify-between p-3 border-b border-[rgba(117,170,252,0.2)] shrink-0">
<div className="flex items-center gap-4">
<div className="font-mono text-xs text-[#9bc3ff] tracking-wide uppercase">
{title}
</div>
<div className="font-mono text-[10px] text-[#75aafc]">
{totals.files} file{totals.files !== 1 ? "s" : ""} changed
{totals.additions > 0 && (
<span className="text-green-400 ml-2">+{totals.additions}</span>
)}
{totals.deletions > 0 && (
<span className="text-red-400 ml-2">-{totals.deletions}</span>
)}
</div>
</div>
<div className="flex items-center gap-2">
<button
onClick={expandAll}
className="font-mono text-[10px] text-[#555] hover:text-[#9bc3ff]"
>
Expand All
</button>
<button
onClick={collapseAll}
className="font-mono text-[10px] text-[#555] hover:text-[#9bc3ff]"
>
Collapse All
</button>
<button
onClick={() => setShowFullDiff(!showFullDiff)}
className="font-mono text-[10px] text-[#555] hover:text-[#9bc3ff]"
>
{showFullDiff ? "File List" : "Full Diff"}
</button>
{onClose && (
<button
onClick={onClose}
className="font-mono text-xs text-[#555] hover:text-[#9bc3ff] ml-2"
>
Close
</button>
)}
</div>
</div>
{/* Content */}
<div className="flex-1 overflow-y-auto p-3">
{showFullDiff ? (
// Full diff view
parsedFiles.length > 0 ? (
parsedFiles.map((file) => (
<DiffFileView
key={file.path}
file={file}
collapsed={collapsedFiles.has(file.path)}
onToggle={() => toggleFile(file.path)}
/>
))
) : (
// Fallback to raw diff
<pre className="font-mono text-xs text-[#9bc3ff] whitespace-pre-wrap">
{diff}
</pre>
)
) : (
// File list view
<div className="space-y-1">
{(changedFiles || parsedFiles.map((f) => f.path)).map((path) => {
const file = parsedFiles.find((f) => f.path === path);
return (
<div
key={path}
className="flex items-center gap-2 px-2 py-1 hover:bg-[rgba(117,170,252,0.05)]"
>
{file && (
<span
className={`px-1 py-0.5 font-mono text-[9px] ${
file.status === "added"
? "text-green-400 bg-green-400/10"
: file.status === "deleted"
? "text-red-400 bg-red-400/10"
: file.status === "renamed"
? "text-purple-400 bg-purple-400/10"
: "text-yellow-400 bg-yellow-400/10"
}`}
>
{file.status.charAt(0).toUpperCase()}
</span>
)}
<span className="font-mono text-sm text-[#dbe7ff] flex-1 truncate">
{path}
</span>
{file && (
<span className="font-mono text-[10px]">
{file.additions > 0 && (
<span className="text-green-400 mr-1">+{file.additions}</span>
)}
{file.deletions > 0 && (
<span className="text-red-400">-{file.deletions}</span>
)}
</span>
)}
</div>
);
})}
</div>
)}
</div>
</div>
);
}
|