Skip to content

Commit 30976cb

Browse files
committed
feat: lazy load files manager folders
1 parent b22bdce commit 30976cb

5 files changed

Lines changed: 455 additions & 38 deletions

File tree

client/src/components/FileTreeSidebar.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,35 @@ const FileTreeSidebar = ({
1313
files,
1414
currentFolder,
1515
onSelect,
16+
onLoadFolder,
1617
onDrop,
1718
onContextMenu,
19+
loadedParents = [],
20+
loadingParents = [],
21+
expandedKeys,
22+
onExpand,
1823
width = 250,
1924
}) => {
2025
// Convert flat folders list to tree data
2126
const treeData = useMemo(() => {
27+
const loadedParentSet = new Set(loadedParents);
28+
const loadingParentSet = new Set(loadingParents);
29+
2230
const buildTree = (parentId) => {
2331
const children = folders
2432
.filter((f) => f.parent === parentId)
2533
.map((f) => ({
2634
title: f.title,
2735
key: `folder-${f.key}`,
2836
isLeaf: false,
37+
loading: loadingParentSet.has(f.key),
2938
icon: ({ expanded }) =>
3039
expanded ? <FolderOpenFilled /> : <FolderFilled />,
31-
children: buildTree(f.key),
40+
children: loadedParentSet.has(f.key) ? buildTree(f.key) : undefined,
3241
}));
3342

3443
// Add files to tree
35-
if (files && files[parentId]) {
44+
if (loadedParentSet.has(parentId) && files && files[parentId]) {
3645
children.push(
3746
...files[parentId].map((f) => ({
3847
title: f.name,
@@ -53,6 +62,7 @@ const FileTreeSidebar = ({
5362
title: f.title,
5463
key: `folder-${f.key}`,
5564
isLeaf: false,
65+
loading: loadingParentSet.has(f.key),
5666
icon: ({ expanded }) =>
5767
expanded ? <FolderOpenFilled /> : <FolderFilled />,
5868
children: buildTree(f.key),
@@ -64,7 +74,7 @@ const FileTreeSidebar = ({
6474
}
6575

6676
return rootNodes;
67-
}, [folders, files]);
77+
}, [files, folders, loadedParents, loadingParents]);
6878

6979
const onSelectHandler = (keys, info) => {
7080
if (keys.length > 0) {
@@ -76,6 +86,15 @@ const FileTreeSidebar = ({
7686
}
7787
};
7888

89+
const handleLoadData = async (node) => {
90+
const key = String(node?.key || "");
91+
if (!key.startsWith("folder-") || !onLoadFolder) {
92+
return;
93+
}
94+
95+
await onLoadFolder(key.replace("folder-", ""));
96+
};
97+
7998
const handleDrop = (info) => {
8099
if (onDrop) {
81100
onDrop(info);
@@ -110,11 +129,14 @@ const FileTreeSidebar = ({
110129
</div>
111130
<DirectoryTree
112131
multiple={false}
113-
defaultExpandAll
114132
selectedKeys={[`folder-${currentFolder}`]}
115133
onSelect={onSelectHandler}
116134
treeData={treeData}
117135
expandAction="click"
136+
expandedKeys={expandedKeys}
137+
onExpand={onExpand}
138+
loadData={handleLoadData}
139+
loadedKeys={loadedParents.map((key) => `folder-${key}`)}
118140
style={{ backgroundColor: "transparent", fontSize: 13 }}
119141
titleRender={(nodeData) => (
120142
<span
@@ -130,6 +152,7 @@ const FileTreeSidebar = ({
130152
title={String(nodeData.title)}
131153
>
132154
{nodeData.title}
155+
{nodeData.loading ? " ..." : ""}
133156
</span>
134157
)}
135158
draggable

0 commit comments

Comments
 (0)