forked from web3infra-foundation/mega
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileDiff.tsx
More file actions
318 lines (282 loc) · 9.6 KB
/
FileDiff.tsx
File metadata and controls
318 lines (282 loc) · 9.6 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
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
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { type ChangeTypes, type FileDiffMetadata } from '@pierre/diffs'
import { FileDiff as PierreFileDiff } from '@pierre/diffs/react'
import { useTheme } from 'next-themes'
import { useRouter } from 'next/router'
import { Virtuoso } from 'react-virtuoso'
import { CommonPageDiffItemSchema, CommonResultCodeReviewResponse, CommonResultVecMuiTreeNode } from '@gitmono/types'
import { LoadingSpinner } from '@gitmono/ui'
import { ExpandIcon, SparklesIcon } from '@gitmono/ui/Icons'
import { cn } from '@gitmono/ui/src/utils'
import FileTree from '@/components/DiffView/TreeView/FileTree'
import { CommentForm, CommentThread, HoverButton, useComments } from './comment'
import { useGetComments } from './hooks/useGetComments'
import { DiffItem, generateParsedFiles, parsedDiffs } from './parsedDiffs'
interface FileDiffProps {
fileChangeData: CommonPageDiffItemSchema
fileChangeIsLoading: boolean
treeData: CommonResultVecMuiTreeNode['data']
treeIsLoading: boolean
page: number
hasMoreData: boolean
isBlockingLoading: boolean
onLoadMore: () => void
}
function SingleFileDiffView({
filePath,
fileDiffMetadata,
changeType,
isBinary,
hasContent,
clLink,
commentsData
}: {
filePath: string
fileDiffMetadata: FileDiffMetadata | null
changeType: ChangeTypes | null
isBinary: boolean
hasContent: boolean
clLink: string
commentsData?: CommonResultCodeReviewResponse
}) {
const { resolvedTheme } = useTheme()
const {
annotations,
selectedRange,
handleLineSelectionEnd,
addCommentAtLine,
handleSubmitComment,
handleCancelComment
} = useComments(filePath, commentsData?.data)
const getChangeTypeMessage = (changeType: ChangeTypes | null): string | null => {
switch (changeType) {
case 'new':
return 'This file was added.'
case 'deleted':
return 'This file was deleted.'
case 'rename-pure':
return 'This file was renamed.'
case 'rename-changed':
return 'This file was renamed and modified.'
default:
return null
}
}
const message = getChangeTypeMessage(changeType)
if (fileDiffMetadata && hasContent) {
return (
<PierreFileDiff
fileDiff={fileDiffMetadata}
lineAnnotations={annotations}
selectedLines={selectedRange}
renderAnnotation={(annotation) =>
annotation.metadata ? (
<CommentThread thread={annotation.metadata} clLink={clLink} />
) : (
<CommentForm
side={annotation.side}
lineNumber={annotation.lineNumber}
filePath={filePath}
fileDiff={fileDiffMetadata}
selectedRange={selectedRange}
clLink={clLink}
onSubmit={handleSubmitComment}
onCancel={handleCancelComment}
/>
)
}
renderHoverUtility={(getHoveredLine) => (
<HoverButton getHoveredLine={getHoveredLine} onAddComment={addCommentAtLine} />
)}
options={{
theme: resolvedTheme === 'dark' ? 'min-dark' : 'min-light',
diffStyle: 'unified',
diffIndicators: 'classic',
overflow: 'wrap',
disableFileHeader: true,
enableLineSelection: true,
enableHoverUtility: true,
onLineSelectionEnd: handleLineSelectionEnd,
unsafeCSS: `
::-webkit-scrollbar { display: none !important; }
code { padding: 0 !important; }
`
}}
style={{ '--diffs-font-size': '14px' } as React.CSSProperties}
/>
)
}
if (isBinary) {
return (
<div className='p-4 text-center'>
<div className='text-primary'>Binary file</div>
{message && <div className='text-secondary mt-1 text-sm'>{message}</div>}
</div>
)
}
if (message) {
return (
<div className='p-4 text-center'>
<div className='text-primary'>Load Diff</div>
<div className='text-secondary mt-1 text-sm'>{message}</div>
</div>
)
}
if (!hasContent) {
return <div className='text-tertiary p-4 text-center'>No content change</div>
}
return null
}
export default function FileDiff({
fileChangeData,
fileChangeIsLoading,
treeData,
treeIsLoading,
page,
hasMoreData,
isBlockingLoading,
onLoadMore
}: FileDiffProps) {
const virtuosoRef = useRef<any>(null)
const router = useRouter()
const { link } = router.query
const clLink = typeof link === 'string' ? link : ''
const { data: commentsData } = useGetComments(clLink)
const [pageDataMap, setPageDataMap] = useState<Map<number, DiffItem[]>>(new Map())
useEffect(() => {
if (fileChangeData?.items && !fileChangeIsLoading) {
setPageDataMap((prev) => {
const newMap = new Map(prev)
if (!newMap.has(page)) {
newMap.set(page, fileChangeData.items)
}
return newMap
})
}
}, [fileChangeData, fileChangeIsLoading, page])
const fileDiff = useMemo(() => {
const allItems: DiffItem[] = []
for (let i = 1; i <= page; i++) {
const pageData = pageDataMap.get(i)
if (pageData) {
allItems.push(...pageData)
}
}
return allItems
}, [pageDataMap, page])
const diffFiles = useMemo(() => parsedDiffs(fileDiff), [fileDiff])
const parsedFiles = useMemo(() => generateParsedFiles(diffFiles), [diffFiles])
const [expandedMap, setExpandedMap] = useState<Record<string, boolean>>(() =>
Object.fromEntries(diffFiles.map((f) => [f.path, false]))
)
const fileRefs = useRef<Record<string, HTMLDivElement | null>>({})
const toggleExpanded = (path: string) => {
setExpandedMap((prev) => ({ ...prev, [path]: !prev[path] }))
}
const scrollToFile = useCallback(
(filePath: string) => {
const index = parsedFiles.findIndex((pf) => pf.file.path === filePath)
if (index !== -1) {
setExpandedMap((prev) => ({ ...prev, [filePath]: true }))
if (virtuosoRef.current) {
virtuosoRef.current.scrollToIndex(index)
}
}
},
[parsedFiles]
)
const loadMoreDiffs = useCallback(() => {
if (fileChangeIsLoading || !hasMoreData) return
onLoadMore()
}, [fileChangeIsLoading, hasMoreData, onLoadMore])
useEffect(() => {
setExpandedMap(Object.fromEntries(diffFiles.map((f) => [f.path, false])))
}, [diffFiles])
const DiffItemComponent = (index: number) => {
const { file, fileDiffMetadata, stats, changeType, isBinary, hasContent } = parsedFiles[index]
const isExpanded = expandedMap[file.path]
const isRenamed = changeType === 'rename-pure' || changeType === 'rename-changed'
return (
<div
id={file.path}
key={file.path}
ref={(el) => void (fileRefs.current[file.path] = el)}
className='border-primary mb-4 w-full rounded-lg border'
>
<div
onClick={() => toggleExpanded(file.path)}
className={cn(
'text-primary flex cursor-pointer items-center justify-between px-4 py-2 text-sm',
isExpanded && 'border-primary border-b'
)}
>
<span className='flex items-center'>
{isExpanded ? (
<SparklesIcon className='align-middle text-xl' />
) : (
<ExpandIcon className='align-middle text-xl' />
)}
<span className='ml-1'>
{isRenamed && file.oldPath ? (
<span>
<span className='text-secondary line-through'>{file.oldPath}</span>
<span className='text-secondary mx-1'>→</span>
<span>{file.path}</span>
</span>
) : (
file.path
)}
</span>
</span>
<span className='text-xs font-bold'>
<span className='text-green-500'>+{stats.additions}</span>{' '}
<span className='text-red-500'>−{stats.deletions}</span>
</span>
</div>
<div className='copyable-text'>
{isExpanded && (
<SingleFileDiffView
filePath={file.path}
fileDiffMetadata={fileDiffMetadata}
changeType={changeType}
isBinary={isBinary}
hasContent={hasContent}
clLink={clLink}
commentsData={commentsData}
/>
)}
</div>
</div>
)
}
return (
<div className='relative mt-3 flex font-sans'>
<div className='sticky top-5 h-[80vh] w-[300px] overflow-y-auto rounded-lg p-2'>
<FileTree treeData={treeData} treeDataLoading={treeIsLoading} onFileClick={scrollToFile} />
</div>
<div className='relative h-full w-full flex-1 px-4'>
{isBlockingLoading && (
<div className='bg-primary/80 absolute inset-0 z-10 flex items-center justify-center rounded-lg'>
<div className='bg-primary flex items-center rounded-md px-3 py-2 shadow-lg'>
<LoadingSpinner />
<span className='text-secondary ml-2 text-sm'>Loading diffs...</span>
</div>
</div>
)}
{fileDiff.length === 0 && !fileChangeIsLoading && page === 1 ? (
<div className='text-primary flex h-[85vh] items-center justify-center'>No File Changed</div>
) : (
<Virtuoso
ref={virtuosoRef}
style={{ height: '76vh', paddingBottom: '20px' }}
totalCount={parsedFiles.length}
itemContent={DiffItemComponent}
endReached={loadMoreDiffs}
components={{ Footer: () => null }}
increaseViewportBy={350}
/>
)}
</div>
</div>
)
}