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

fix(tree): Fixed node expand error #726

Merged
merged 2 commits into from
Dec 9, 2024
Merged
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
111 changes: 76 additions & 35 deletions moon/src/components/RepoTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,51 +60,48 @@ const RepoTree = ({ directory }) => {
};

// append the clicked dir to the treeData
const appendTreeData = (treeData, subItems, clickedNodeKey) => {
const appendTreeData = (treeData, subItems, clickedNodeTitle: string) => {
return treeData.map(item => {
if (item.key === clickedNodeKey) {
if (item.title === clickedNodeTitle) {
return {
...item,
children: subItems
};
} else if (Array.isArray(item.children)) {
return {
...item,
children: appendTreeData(item.children, subItems, clickedNodeKey)
children: appendTreeData(item.children, subItems, clickedNodeTitle)
};
}
});
};

const onExpand = async (keys, { expanded, node }) => {
// push new url and query to router
console.log("OnExpanded!");
console.log("keys", keys);
console.log("node", node.path);
// router.push({ query: { repo_path: "/projects/freighter", object_id: node.key } });
var responseData;
try {
const response = await fetch(`/api/tree?path=${node.path}`);

if (!response.ok) {
throw new Error('Failed to fetch tree data');
}

console.log('Response status:', response.status);

responseData = await response.json();
console.log('Response data:', responseData);

} catch (error) {
console.error('Error fetching tree data:', error);
}
// onRenderTree(node.key);
const onExpand = async (expandedKeys, {expanded, node}) => {
if (expanded) {
const subTreeData = convertToTreeData(responseData.items);
const newTreeData = appendTreeData(treeData, subTreeData, node.key);
// setExpandedKeys([...expandedKeys, node.key]);
let responseData;
try {
// query tree by path
const reqPath = pathname.replace('/tree', '') + '/' + node.title;
if (node.path && node.path !== '' && node.path !== undefined) {
responseData = await fetch(`/api/tree?path=${node.path}`)
.then(response => response.json())
.catch(e => {
throw new Error('Failed to fetch tree data');
})
} else {
responseData = await fetch(`/api/tree?path=${reqPath}`)
.then(response => response.json())
.catch(e => {
throw new Error('Failed to fetch tree data');
})
}
} catch (error) {
console.error('Error fetching tree data:', error);
}
const subTreeData = convertToTreeData(responseData.data.data);
const newTreeData = appendTreeData(treeData, subTreeData, node.title);
setExpandedKeys([...expandedKeys, node.key]);
setTreeData(newTreeData);
// setCurrentPath([...currentPath, node.title]); // for breadcrumb
} else {
setExpandedKeys(expandedKeys.filter(key => key !== node.key));
}
Expand All @@ -116,12 +113,56 @@ const RepoTree = ({ directory }) => {
// according to the current route, splicing the next route and determine the type to jump
let real_path = pathname.replace('/tree', '');
if (Array.isArray(treeData) && treeData?.length > 0) {
const clickNode = treeData[pathArray[1]] as TreeNode
// determine file type and router push
if (clickNode.isLeaf) {
router.push(`/blob/${real_path}/${clickNode.title}`);
if (Array.isArray(pathArray) && pathArray.length === 2) {
// root folder
const clickNode = treeData[pathArray[1]] as TreeNode
// determine file type and router push
if (clickNode.isLeaf) {
router.push(`/blob/${real_path}/${clickNode.title}`);
} else {
router.push(`${pathname}/${clickNode.title}`);
}
} else {
router.push(`${pathname}/${clickNode.title}`);
// child list, recursively find the target node
const findNode = (data: TreeNode[], indices: number[]): TreeNode | null => {
if (indices.length === 0) return null;
if (indices.length === 1) return data[indices[0]];

const node = data[indices[1]] as TreeNode;
let current = node;

for (let i = 2; i < indices.length; i++) {
if (!current.children) return null;
current = current.children[indices[i]] as TreeNode;
}

return current;
};

// build the path
const buildPath = (indices: number[]): string => {
let path = '';
let current = treeData[indices[1]] as TreeNode;
path += current.title;

for (let i = 2; i < indices.length; i++) {
if (!current.children) break;
current = current.children[indices[i]] as TreeNode;
path += '/' + current.title;
}

return path;
};

const targetNode = findNode(treeData, pathArray);
if (targetNode) {
const fullPath = buildPath(pathArray);
if (targetNode.isLeaf) {
router.push(`/blob/${real_path}/${fullPath}`);
} else {
router.push(`${pathname}/${fullPath}`);
}
}
}
} else {
router.push(`${pathname}`)
Expand Down
Loading