Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: define loadedKey to be controlled #803

Merged
merged 3 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/components/tree/__tests__/tree.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,8 @@ describe('Test the Tree component', () => {
},
];
const mockFn = jest.fn().mockImplementation(() => sleep(1000));
const { getByText, container } = render(
<TreeView data={data} onLoadData={mockFn} />
const { getByText, container, rerender } = render(
<TreeView data={data} onLoadData={mockFn} loadedKeys={[]} />
);

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

rerender(
<TreeView data={data} onLoadData={mockFn} loadedKeys={['1']} />
);

act(() => {
// unfold it and open it again
fireEvent.click(getByText('test1'));
Expand Down
6 changes: 3 additions & 3 deletions src/components/tree/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export interface ITreeProps {
className?: string;
draggable?: boolean;
expandKeys?: UniqueId[];
loadedKeys?: string[];
activeKey?: UniqueId;
onExpand?: (expandedKeys: React.Key[], node: ITreeNodeItemProps) => void;
onSelect?: (node: ITreeNodeItemProps, isUpdate?) => void;
Expand All @@ -83,6 +84,7 @@ const TreeView = ({
className,
data = [],
draggable = false,
loadedKeys,
expandKeys: controlExpandKeys,
activeKey: controlActiveKey,
onExpand,
Expand All @@ -95,7 +97,6 @@ const TreeView = ({
}: ITreeProps) => {
const [expandKeys, setExpandKeys] = useState<UniqueId[]>([]);
const [activeKey, setActiveKey] = useState<string | null>(null);
const loadDataCache = useRef<Record<string, boolean>>({});
const [loadingKeys, setLoadingKeys] = useState<string[]>([]);
const dragOverNode = useRef<ITreeNodeItemProps>();
const dragInfo = useRef<{
Expand All @@ -108,7 +109,7 @@ const TreeView = ({

const canLoadData = (key: string) => {
if (!onLoadData) return false;
if (loadDataCache.current.hasOwnProperty(key)) return false;
if (loadedKeys?.includes(key)) return false;
return true;
};

Expand All @@ -120,7 +121,6 @@ const TreeView = ({
nextKeys.push(uuid!);
return nextKeys;
});
loadDataCache.current[uuid] = true;
onLoadData!(node).finally(() => {
setLoadingKeys((keys) => {
const nextKeys = keys.concat();
Expand Down
5 changes: 5 additions & 0 deletions src/controller/explorer/folderTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ export class FolderTreeController
public onLoadData = (treeNode: IFolderTreeNodeProps) => {
const count = this.count(FolderTreeEvent.onLoadData);
if (count) {
// define current treeNode to be loaded
this.folderTreeService.setLoadedKeys([
...this.folderTreeService.getLoadedKeys(),
treeNode.id.toString(),
]);
return new Promise<void>((resolve, reject) => {
const callback = (node: IFolderTreeNodeProps) => {
this.folderTreeService.update(node);
Expand Down
1 change: 1 addition & 0 deletions src/model/workbench/explorer/folderTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface IFolderTreeSubItem {
folderPanelContextMenu?: IMenuItemProps[];
current?: IFolderTreeNodeProps | null;
expandKeys?: UniqueId[];
loadedKeys?: string[];
}
export interface IFolderTree {
folderTree?: IFolderTreeSubItem;
Expand Down
22 changes: 22 additions & 0 deletions src/services/workbench/__tests__/folderTreeService.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,28 @@ describe('Test StatusBarService', () => {
});
});

test('Should remove loadedCached when removed successfully', () => {
folderTreeService.add({
id: '1',
name: 'test0',
fileType: 'RootFolder',
location: 'test0',
children: [
{
id: '2',
name: 'test',
fileType: 'Folder',
children: [],
},
],
});
folderTreeService.setLoadedKeys(['1', '2']);
expect(folderTreeService.getLoadedKeys()).toEqual(['1', '2']);

folderTreeService.remove('2');
expect(folderTreeService.getLoadedKeys()).toEqual(['1']);
});

test('Should support to logger ERROR message when remove failed', () => {
expectLoggerErrorToBeCalled(() => {
folderTreeService.remove('1');
Expand Down
25 changes: 25 additions & 0 deletions src/services/workbench/explorer/folderTreeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ export interface IFolderTreeService extends Component<IFolderTree> {
* Set the expandKeys for folderTree
*/
setExpandKeys: (expandKeys: UniqueId[]) => void;
/**
* Get the loadedKeys for folderTree
*/
getLoadedKeys: () => string[];
/**
* Set the loadedKeys for folderTree
*/
setLoadedKeys: (loadedKeys: string[]) => void;
/**
* Active specific node,
* or unactive any node in folder tree
Expand Down Expand Up @@ -264,6 +272,17 @@ export class FolderTreeService
});
}

public getLoadedKeys() {
return this.state.folderTree?.loadedKeys || [];
}

public setLoadedKeys(loadedKeys: string[]) {
const { folderTree } = this.state;
this.setState({
folderTree: { ...folderTree, loadedKeys },
});
}

private setCurrentFolderLocation(data: IFolderTreeNodeProps, id: UniqueId) {
const children = data.children;
const { tree } = this.getCurrentRootFolderInfo(id);
Expand Down Expand Up @@ -429,6 +448,12 @@ export class FolderTreeService

tree.removeNode(id);
if (index > -1) nextData[index] = tree.obj;
// Remove loadedKey while removing node
if (folderTree.loadedKeys?.includes(id.toString())) {
folderTree.loadedKeys = folderTree.loadedKeys.filter(
(key) => key !== id.toString()
);
}
this.setState({
folderTree,
});
Expand Down
2 changes: 2 additions & 0 deletions src/workbench/sidebar/explore/folderTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ const FolderTree: React.FunctionComponent<IFolderTreeProps> = (props) => {
data = [],
folderPanelContextMenu = [],
expandKeys,
loadedKeys,
current,
} = folderTree;

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