forked from argoproj/argo-cd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ui): add virtual scroll in app diff avoid to crash
Signed-off-by: linghaoSu <[email protected]>
- Loading branch information
Showing
6 changed files
with
179 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
...applications/components/application-resources-diff/application-resources-diff-section.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import * as React from 'react'; | ||
import {addQueueItem, clearQueue} from './diff-queue'; | ||
import {parseDiff} from 'react-diff-view'; | ||
import {useVirtualizer} from '@tanstack/react-virtual'; | ||
import {IndividualDiffSection} from './individual-diff-section'; | ||
|
||
interface DiffFileModel { | ||
loading: boolean; | ||
file?: any; | ||
} | ||
|
||
export interface ApplicationResourcesDiffSectionProps { | ||
prepareDiff: { | ||
a: string; | ||
b: string; | ||
name: string; | ||
hook?: boolean; | ||
}[]; | ||
compactDiff: boolean; | ||
inlineDiff: boolean; | ||
} | ||
|
||
export const ApplicationResourcesDiffSection = (props: ApplicationResourcesDiffSectionProps) => { | ||
const {prepareDiff, compactDiff, inlineDiff} = props; | ||
const parentRef = React.useRef(); | ||
|
||
const whiteBox = prepareDiff.length > 1 ? 'white-box' : ''; | ||
const showPath = prepareDiff.length > 1; | ||
const viewType = inlineDiff ? 'unified' : 'split'; | ||
|
||
const [diffProgress, setDiffProgress] = React.useState({ | ||
finished: 0, | ||
total: prepareDiff.length | ||
}); | ||
|
||
const [diffFiles, setDiffFiles] = React.useState<DiffFileModel[]>( | ||
prepareDiff.map(() => ({ | ||
loading: true, | ||
file: {} | ||
})) | ||
); | ||
|
||
React.useEffect(() => { | ||
prepareDiff.forEach((filePrepare, i) => { | ||
addQueueItem({ | ||
a: filePrepare.a, | ||
b: filePrepare.b, | ||
name: filePrepare.name, | ||
compactDiff: compactDiff, | ||
inlineDiff: inlineDiff | ||
}).then((diffText: string) => { | ||
const files = parseDiff(diffText); | ||
|
||
setDiffFiles(diffLines => { | ||
const newDiffFiles = [...diffLines]; | ||
newDiffFiles[i] = { | ||
loading: false, | ||
file: files[0] | ||
}; | ||
|
||
setDiffProgress(val => { | ||
return { | ||
...val, | ||
finished: val.finished + 1 | ||
}; | ||
}); | ||
|
||
return newDiffFiles; | ||
}); | ||
}); | ||
}); | ||
|
||
return () => { | ||
setDiffProgress({ | ||
finished: 0, | ||
total: prepareDiff.length | ||
}); | ||
setDiffFiles( | ||
prepareDiff.map(() => ({ | ||
loading: true, | ||
file: {} | ||
})) | ||
); | ||
clearQueue(); | ||
}; | ||
}, [compactDiff, prepareDiff]); | ||
|
||
// The virtualizer | ||
const rowVirtualizer = useVirtualizer({ | ||
count: prepareDiff.length, | ||
getScrollElement: () => parentRef.current, | ||
estimateSize: () => 35 | ||
}); | ||
|
||
const virtualizeItems = rowVirtualizer.getVirtualItems(); | ||
|
||
return ( | ||
<> | ||
{diffProgress.finished < diffProgress.total && ( | ||
<div style={{marginBottom: '10px'}}> | ||
{diffProgress.finished} / {diffProgress.total} files diff | ||
</div> | ||
)} | ||
<div | ||
ref={parentRef} | ||
style={{ | ||
height: 'calc(100vh - 260px)', | ||
width: '100%', | ||
overflowY: 'auto', | ||
contain: 'strict' | ||
}}> | ||
<div | ||
style={{ | ||
height: rowVirtualizer.getTotalSize(), | ||
width: '100%', | ||
position: 'relative' | ||
}}> | ||
<div | ||
style={{ | ||
position: 'absolute', | ||
top: 0, | ||
left: 0, | ||
width: '100%', | ||
transform: `translateY(${virtualizeItems[0]?.start ?? 0}px)` | ||
}}> | ||
{virtualizeItems.map((virtualRow: any) => ( | ||
<IndividualDiffSection | ||
key={virtualRow.key} | ||
dataIndex={virtualRow.index} | ||
ref={rowVirtualizer.measureElement} | ||
file={diffFiles[virtualRow.index].file} | ||
showPath={showPath} | ||
loading={diffFiles[virtualRow.index].loading} | ||
whiteBox={whiteBox} | ||
viewType={viewType} | ||
/> | ||
))} | ||
</div> | ||
</div> | ||
</div> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1916,6 +1916,18 @@ | |
dependencies: | ||
"@sinonjs/commons" "^3.0.0" | ||
|
||
"@tanstack/react-virtual@^3.10.7": | ||
version "3.10.7" | ||
resolved "https://registry.yarnpkg.com/@tanstack/react-virtual/-/react-virtual-3.10.7.tgz#428d0c29c6d2046ab905f4278446a3fe89bfd4ef" | ||
integrity sha512-yeP+M0G8D+15ZFPivpuQ5hoM4Fa/PzERBx8P8EGcfEsXX3JOb9G9UUrqc47ZXAxvK+YqzM9T5qlJUYUFOwCZJw== | ||
dependencies: | ||
"@tanstack/virtual-core" "3.10.7" | ||
|
||
"@tanstack/[email protected]": | ||
version "3.10.7" | ||
resolved "https://registry.yarnpkg.com/@tanstack/virtual-core/-/virtual-core-3.10.7.tgz#fb1d8ae1257c9fe3a6e99c911cad0d1a1b07f598" | ||
integrity sha512-ND5dfsU0n9F4gROzwNNDJmg6y8n9pI8YWxtgbfJ5UcNn7Hx+MxEXtXcQ189tS7sh8pmCObgz2qSiyRKTZxT4dg== | ||
|
||
"@tippy.js/react@^3.1.1": | ||
version "3.1.1" | ||
resolved "https://registry.yarnpkg.com/@tippy.js/react/-/react-3.1.1.tgz#027e4595e55f31430741fe8e0d92aaddfbe47efd" | ||
|