Skip to content

Commit 219b937

Browse files
authored
feat: define loadedKey to be controlled (#803)
1 parent c305470 commit 219b937

File tree

7 files changed

+64
-5
lines changed

7 files changed

+64
-5
lines changed

src/components/tree/__tests__/tree.test.tsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -450,8 +450,8 @@ describe('Test the Tree component', () => {
450450
},
451451
];
452452
const mockFn = jest.fn().mockImplementation(() => sleep(1000));
453-
const { getByText, container } = render(
454-
<TreeView data={data} onLoadData={mockFn} />
453+
const { getByText, container, rerender } = render(
454+
<TreeView data={data} onLoadData={mockFn} loadedKeys={[]} />
455455
);
456456

457457
act(() => {
@@ -466,6 +466,10 @@ describe('Test the Tree component', () => {
466466
});
467467
expect(container.querySelector('.codicon-spin')).toBeNull();
468468

469+
rerender(
470+
<TreeView data={data} onLoadData={mockFn} loadedKeys={['1']} />
471+
);
472+
469473
act(() => {
470474
// unfold it and open it again
471475
fireEvent.click(getByText('test1'));

src/components/tree/index.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export interface ITreeProps {
6262
className?: string;
6363
draggable?: boolean;
6464
expandKeys?: UniqueId[];
65+
loadedKeys?: string[];
6566
activeKey?: UniqueId;
6667
onExpand?: (expandedKeys: React.Key[], node: ITreeNodeItemProps) => void;
6768
onSelect?: (node: ITreeNodeItemProps, isUpdate?) => void;
@@ -83,6 +84,7 @@ const TreeView = ({
8384
className,
8485
data = [],
8586
draggable = false,
87+
loadedKeys,
8688
expandKeys: controlExpandKeys,
8789
activeKey: controlActiveKey,
8890
onExpand,
@@ -95,7 +97,6 @@ const TreeView = ({
9597
}: ITreeProps) => {
9698
const [expandKeys, setExpandKeys] = useState<UniqueId[]>([]);
9799
const [activeKey, setActiveKey] = useState<string | null>(null);
98-
const loadDataCache = useRef<Record<string, boolean>>({});
99100
const [loadingKeys, setLoadingKeys] = useState<string[]>([]);
100101
const dragOverNode = useRef<ITreeNodeItemProps>();
101102
const dragInfo = useRef<{
@@ -108,7 +109,7 @@ const TreeView = ({
108109

109110
const canLoadData = (key: string) => {
110111
if (!onLoadData) return false;
111-
if (loadDataCache.current.hasOwnProperty(key)) return false;
112+
if (loadedKeys?.includes(key)) return false;
112113
return true;
113114
};
114115

@@ -120,7 +121,6 @@ const TreeView = ({
120121
nextKeys.push(uuid!);
121122
return nextKeys;
122123
});
123-
loadDataCache.current[uuid] = true;
124124
onLoadData!(node).finally(() => {
125125
setLoadingKeys((keys) => {
126126
const nextKeys = keys.concat();

src/controller/explorer/folderTree.tsx

+5
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,11 @@ export class FolderTreeController
207207
public onLoadData = (treeNode: IFolderTreeNodeProps) => {
208208
const count = this.count(FolderTreeEvent.onLoadData);
209209
if (count) {
210+
// define current treeNode to be loaded
211+
this.folderTreeService.setLoadedKeys([
212+
...this.folderTreeService.getLoadedKeys(),
213+
treeNode.id.toString(),
214+
]);
210215
return new Promise<void>((resolve, reject) => {
211216
const callback = (node: IFolderTreeNodeProps) => {
212217
this.folderTreeService.update(node);

src/model/workbench/explorer/folderTree.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export interface IFolderTreeSubItem {
3636
folderPanelContextMenu?: IMenuItemProps[];
3737
current?: IFolderTreeNodeProps | null;
3838
expandKeys?: UniqueId[];
39+
loadedKeys?: string[];
3940
}
4041
export interface IFolderTree {
4142
folderTree?: IFolderTreeSubItem;

src/services/workbench/__tests__/folderTreeService.test.tsx

+22
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,28 @@ describe('Test StatusBarService', () => {
424424
});
425425
});
426426

427+
test('Should remove loadedCached when removed successfully', () => {
428+
folderTreeService.add({
429+
id: '1',
430+
name: 'test0',
431+
fileType: 'RootFolder',
432+
location: 'test0',
433+
children: [
434+
{
435+
id: '2',
436+
name: 'test',
437+
fileType: 'Folder',
438+
children: [],
439+
},
440+
],
441+
});
442+
folderTreeService.setLoadedKeys(['1', '2']);
443+
expect(folderTreeService.getLoadedKeys()).toEqual(['1', '2']);
444+
445+
folderTreeService.remove('2');
446+
expect(folderTreeService.getLoadedKeys()).toEqual(['1']);
447+
});
448+
427449
test('Should support to logger ERROR message when remove failed', () => {
428450
expectLoggerErrorToBeCalled(() => {
429451
folderTreeService.remove('1');

src/services/workbench/explorer/folderTreeService.ts

+25
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ export interface IFolderTreeService extends Component<IFolderTree> {
6565
* Set the expandKeys for folderTree
6666
*/
6767
setExpandKeys: (expandKeys: UniqueId[]) => void;
68+
/**
69+
* Get the loadedKeys for folderTree
70+
*/
71+
getLoadedKeys: () => string[];
72+
/**
73+
* Set the loadedKeys for folderTree
74+
*/
75+
setLoadedKeys: (loadedKeys: string[]) => void;
6876
/**
6977
* Active specific node,
7078
* or unactive any node in folder tree
@@ -264,6 +272,17 @@ export class FolderTreeService
264272
});
265273
}
266274

275+
public getLoadedKeys() {
276+
return this.state.folderTree?.loadedKeys || [];
277+
}
278+
279+
public setLoadedKeys(loadedKeys: string[]) {
280+
const { folderTree } = this.state;
281+
this.setState({
282+
folderTree: { ...folderTree, loadedKeys },
283+
});
284+
}
285+
267286
private setCurrentFolderLocation(data: IFolderTreeNodeProps, id: UniqueId) {
268287
const children = data.children;
269288
const { tree } = this.getCurrentRootFolderInfo(id);
@@ -429,6 +448,12 @@ export class FolderTreeService
429448

430449
tree.removeNode(id);
431450
if (index > -1) nextData[index] = tree.obj;
451+
// Remove loadedKey while removing node
452+
if (folderTree.loadedKeys?.includes(id.toString())) {
453+
folderTree.loadedKeys = folderTree.loadedKeys.filter(
454+
(key) => key !== id.toString()
455+
);
456+
}
432457
this.setState({
433458
folderTree,
434459
});

src/workbench/sidebar/explore/folderTree.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ const FolderTree: React.FunctionComponent<IFolderTreeProps> = (props) => {
8686
data = [],
8787
folderPanelContextMenu = [],
8888
expandKeys,
89+
loadedKeys,
8990
current,
9091
} = folderTree;
9192

@@ -236,6 +237,7 @@ const FolderTree: React.FunctionComponent<IFolderTreeProps> = (props) => {
236237
// root folder do not render
237238
activeKey={current?.id}
238239
expandKeys={expandKeys}
240+
loadedKeys={loadedKeys}
239241
data={data[0]?.children || []}
240242
className={classNames(
241243
folderTreeClassName,

0 commit comments

Comments
 (0)