-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathContentDiffViewer.tsx
More file actions
173 lines (153 loc) · 4.87 KB
/
Copy pathContentDiffViewer.tsx
File metadata and controls
173 lines (153 loc) · 4.87 KB
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
import { useState } from 'react'
import { useMobile } from '@/hooks/useMobile'
import { Plus, Minus, ChevronDown, ChevronUp } from 'lucide-react'
import { diffLines } from 'diff'
import type { Change } from 'diff'
import { cn } from '@/lib/utils'
interface ContentDiffViewerProps {
before: string
after: string
}
const CONTEXT_LINES = 1
const MAX_LINE_DISPLAY_LENGTH = 200
interface DiffLineEntry {
type: 'add' | 'remove' | 'context'
content: string
oldLineNumber?: number
newLineNumber?: number
}
function truncateLine(line: string): string {
if (line.length <= MAX_LINE_DISPLAY_LENGTH) {
return line
}
return line.substring(0, MAX_LINE_DISPLAY_LENGTH) + '...'
}
export function ContentDiffViewer({ before, after }: ContentDiffViewerProps) {
const isMobile = useMobile()
const [showMore, setShowMore] = useState(false)
const computeDiffLines = (): DiffLineEntry[] => {
const difference = diffLines(before, after, {
ignoreWhitespace: false,
})
const result: DiffLineEntry[] = []
let oldLine = 0
let newLine = 0
difference.forEach((change: Change) => {
const lines = change.value.split('\n')
if (lines[lines.length - 1] === '') {
lines.pop()
}
lines.forEach((line) => {
if (change.added) {
newLine++
result.push({
type: 'add',
content: line,
newLineNumber: newLine,
})
} else if (change.removed) {
oldLine++
result.push({
type: 'remove',
content: line,
oldLineNumber: oldLine,
})
} else {
oldLine++
newLine++
result.push({
type: 'context',
content: line,
oldLineNumber: oldLine,
newLineNumber: newLine,
})
}
})
})
return result
}
const getCompressedDiff = (allLines: DiffLineEntry[]): DiffLineEntry[] => {
const changedIndices = new Set<number>()
allLines.forEach((line, index) => {
if (line.type === 'add' || line.type === 'remove') {
for (let i = Math.max(0, index - CONTEXT_LINES); i <= Math.min(allLines.length - 1, index + CONTEXT_LINES); i++) {
changedIndices.add(i)
}
}
})
return allLines.filter((_, index) => changedIndices.has(index))
}
const allLines = computeDiffLines()
const compressedLines = getCompressedDiff(allLines)
const displayedLines = showMore ? allLines : compressedLines
const hiddenCount = allLines.length - compressedLines.length
if (compressedLines.length === 0) {
return (
<div className="px-3 py-2 text-xs text-muted-foreground">
No changes
</div>
)
}
return (
<div className="flex flex-col bg-background">
<div className="overflow-y-auto max-h-48">
{displayedLines.map((line, index) => {
const isAdd = line.type === 'add'
const isRemove = line.type === 'remove'
return (
<div
key={index}
className={cn(
'flex font-mono text-xs',
isAdd && 'bg-success/10',
isRemove && 'bg-destructive/10',
)}
>
{!isMobile && (
<div className="flex-shrink-0 w-14 flex text-muted-foreground/50 text-[10px]">
<span className="w-7 px-1 text-right">
{line.oldLineNumber || ''}
</span>
<span className="w-7 px-1 text-right">
{line.newLineNumber || ''}
</span>
</div>
)}
<div className="w-4 flex-shrink-0 flex items-center justify-center">
{isAdd && <Plus className="w-2.5 h-2.5 text-success" />}
{isRemove && <Minus className="w-2.5 h-2.5 text-destructive" />}
</div>
<pre
className={cn(
'flex-1 px-1 py-0.5 whitespace-pre-wrap break-all',
isAdd && 'text-success',
isRemove && 'text-destructive',
)}
>
{truncateLine(line.content)}
</pre>
</div>
)
})}
</div>
{!showMore && hiddenCount > 0 && (
<button
onClick={() => setShowMore(true)}
className="px-3 py-1 text-xs text-muted-foreground hover:text-foreground flex items-center gap-1"
>
<ChevronDown className="w-3 h-3" />
+{hiddenCount} more lines
</button>
)}
{showMore && hiddenCount > 0 && (
<button
onClick={() => setShowMore(false)}
className="px-3 py-1 text-xs text-muted-foreground hover:text-foreground flex items-center gap-1"
>
<ChevronUp className="w-3 h-3" />
Show less
</button>
)}
</div>
)
}