Skip to content

Commit 675b2c1

Browse files
authored
fix: Fix the tree component echo issue on the file details page (#1167)
* fix: Fix the tree component echo issue on the file details page * fix: Optimize the loading effect
1 parent b2e576e commit 675b2c1

3 files changed

Lines changed: 32 additions & 43 deletions

File tree

moon/apps/web/components/CodeView/TreeView/RepoTree.tsx

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useCallback, useEffect, useState } from 'react';
33
import ArticleIcon from '@mui/icons-material/Article';
44
import FolderRounded from '@mui/icons-material/FolderRounded';
55
import FolderOpenIcon from '@mui/icons-material/FolderOpen';
6-
import { Box, CircularProgress } from '@mui/material';
6+
import { Box, CircularProgress, Skeleton } from '@mui/material';
77
import {
88
TreeItemDragAndDropOverlay,
99
TreeItemIcon,
@@ -170,9 +170,11 @@ const RepoTree = ( {flag, directory }: {flag:string, directory: any[] }) => {
170170
}
171171

172172
return sortProjectsByType(directory).map((item) => {
173-
173+
174174
const nodeName = item?.name ?? '';
175-
const currentPath = `${parentBasePath}/${nodeName}`.replace('//', '/') || '/';
175+
const currentPath = flag === 'contents' ?
176+
`${parentBasePath}/${nodeName}`.replace('//', '/') || '/'
177+
: `${parentBasePath}`.replace('//', '/') || '/'
176178

177179
// eslint-disable-next-line no-console
178180
// console.log('生成节点路径:', nodeName, '=>', currentPath);
@@ -192,7 +194,7 @@ const RepoTree = ( {flag, directory }: {flag:string, directory: any[] }) => {
192194
] : undefined,
193195
};
194196
});
195-
}, []);
197+
}, [flag]);
196198

197199
useEffect(() => {
198200
if (!Array.isArray(directory) || directory.length === 0) {
@@ -205,7 +207,7 @@ const RepoTree = ( {flag, directory }: {flag:string, directory: any[] }) => {
205207

206208
setTreeData(convertToTreeData(rootPath, directory));
207209
setIsInitialLoading(false);
208-
}, [directory, convertToTreeData,basePath]);
210+
}, [directory, convertToTreeData, basePath]);
209211

210212
const sortProjectsByType = (projects: any[]) => {
211213
if (!Array.isArray(projects) || projects.length === 0) {
@@ -230,8 +232,9 @@ const RepoTree = ( {flag, directory }: {flag:string, directory: any[] }) => {
230232
// console.warn('updateTreeData: 接收到非数组或空的当前树数据', currentTree);
231233
return [];
232234
}
233-
235+
234236
return currentTree.map((node) => {
237+
235238
if (node.id === nodeId) {
236239
return {
237240
...node,
@@ -363,23 +366,25 @@ const RepoTree = ( {flag, directory }: {flag:string, directory: any[] }) => {
363366

364367
return (
365368
<>
366-
{isInitialLoading ? (
369+
{isInitialLoading? (
367370
<Box sx={{ display: 'flex', justifyContent: 'center', padding: '16px' }}>
368-
<CircularProgress />
371+
<CircularProgress size="1.2rem" color="inherit"/>
369372
</Box>
370-
) : treeData.length === 0 ? (
371-
<Box sx={{ color: 'text.secondary', padding: '16px' }}>
372-
no data
373+
)
374+
: treeData.length === 0 ? (
375+
<Box sx={{ display: 'flex', paddingLeft:'16px' }}>
376+
<Skeleton width="200px" height="30px"/>
373377
</Box>
374-
) : (
378+
)
379+
: (
375380
<RichTreeView
376381
items={treeData}
377382
defaultExpandedItems={['grid', 'pickers']}
378383
expandedItems={expandedNodes}
379384
selectedItems={selectedNode}
380385
onExpandedItemsChange={handleNodeToggle}
381386
onSelectedItemsChange={handleNodeSelect}
382-
sx={{ height: 'fit-content', flexGrow: 1, maxWidth: 400, overflowY: 'auto' }}
387+
sx={{ height: 'fit-content', flexGrow: 1, Width: 400, overflowY: 'auto' }}
383388
slots={{
384389
item: (itemProps) => (
385390
<CustomTreeItem

moon/apps/web/pages/[org]/code/blob/[...path].tsx

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useMemo, useState } from 'react'
1+
import React, { useMemo } from 'react'
22
import { Flex, Layout } from 'antd'
33
import BreadCrumb from '@/components/CodeView/TreeView/BreadCrumb'
44
import CodeContent from '@/components/CodeView/BlobView/CodeContent'
@@ -14,17 +14,16 @@ import { useGetTreeCommitInfo } from '@/hooks/useGetTreeCommitInfo'
1414

1515
const codeStyle = {
1616
borderRadius: 8,
17-
width: 'calc(85% - 8px)',
1817
background: '#fff',
1918
border: '1px solid #d1d9e0',
20-
margin: '0 8px'
19+
margin: '0 8px',
20+
width: 'calc(80% - 8px)',
2121
}
2222

2323
const treeStyle = {
2424
borderRadius: 8,
2525
overflow: 'hidden',
2626
width: 'calc(20% - 8px)',
27-
maxWidth: 'calc(20% - 8px)',
2827
background: '#fff'
2928
}
3029

@@ -89,35 +88,22 @@ function BlobPage() {
8988
date: '3 months ago'
9089
}
9190

92-
const newPath = new_path?.split("/").slice(0, -1).join("/")
91+
const newPath = useMemo(() => {
92+
return new_path?.split("/").slice(0, -1).join("/");
93+
}, [new_path]);
9394
const { data: TreeCommitInfo } = useGetTreeCommitInfo(newPath)
95+
9496

9597
type DirectoryType = NonNullable<CommonResultVecTreeCommitItem['data']>
9698
const directory: DirectoryType = useMemo(() => TreeCommitInfo?.data ?? [], [TreeCommitInfo])
97-
const [newDirectory, setNewDirectory] = useState<any[]>([])
98-
99-
// eslint-disable-next-line no-console
100-
console.log(directory, 'directory==directory')
10199

102-
useEffect(()=>{
103-
const handleDirectory = () => {
104-
const filteredItems = directory.filter((item) => {
105-
return item.content_type !== 'directory';
106-
});
100+
const newDirectory = useMemo(() => {
101+
if (!directory || directory.length === 0) return [];
107102

108-
// eslint-disable-next-line no-console
109-
console.log(filteredItems, 'filteredItems==');
110-
setNewDirectory(filteredItems); // 直接设置过滤后的数组,而非嵌套数组
111-
};
112-
113-
handleDirectory()
103+
const currentFileName = new_path.split('/').pop() || '';
114104

115-
},[directory, setNewDirectory])
116-
117-
118-
119-
120-
105+
return directory.filter(item => item.name === currentFileName);
106+
}, [directory, new_path]);
121107

122108

123109
const handleAddComment = (__content: string, __lineNumber?: number) => {
@@ -141,7 +127,7 @@ function BlobPage() {
141127
<RepoTree flag={'detail'} directory={newDirectory} />
142128
</Layout>
143129

144-
<Layout style={{background: '#fff'}}>
130+
<Layout style={codeStyle}>
145131
<Layout className='m-2'>
146132
<CommitHistory flag={'details'} info={commitInfo}/>
147133
</Layout>

moon/apps/web/pages/[org]/code/tree/[...path]/index.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,11 @@ function TreeDetailPage() {
4545
date: '3 months ago'
4646
}
4747

48-
49-
5048
const treeStyle = {
5149
borderRadius: 8,
5250
overflow: 'hidden',
5351
width: 'calc(20% - 8px)',
54-
maxWidth: 'calc(20% - 8px)',
52+
minWidth: 'calc(20% - 8px)',
5553
background: '#fff'
5654
}
5755

0 commit comments

Comments
 (0)