@@ -299,37 +299,32 @@ export const useIDEStore = create<IDEState>((set, get) => ({
|
||||
fetchTree: async () => {
|
||||
try {
|
||||
const data = await scriptsApi.getTree();
|
||||
const nodeMap = new Map<string, FileNode>();
|
||||
|
||||
data.forEach((item) => {
|
||||
nodeMap.set(item.name, {
|
||||
const convertItem = (item: any): FileNode => {
|
||||
const node: FileNode = {
|
||||
id: item.id,
|
||||
name: item.name,
|
||||
type: item.type === "folder" ? "folder" : "file",
|
||||
content: item.content || "",
|
||||
path: item.name,
|
||||
interpreter_id: item.interpreter_id,
|
||||
children: item.type === "folder" ? [] : undefined,
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const hasParent = new Set<string>();
|
||||
data.forEach((item) => {
|
||||
if (item.children) {
|
||||
item.children.forEach((childName: string) => {
|
||||
hasParent.add(childName);
|
||||
const parent = nodeMap.get(item.name);
|
||||
const child = nodeMap.get(childName);
|
||||
if (parent?.children && child) {
|
||||
parent.children.push(child);
|
||||
}
|
||||
});
|
||||
if (item.type === "folder") {
|
||||
node.children = [];
|
||||
if (item.children && Array.isArray(item.children)) {
|
||||
node.children = item.children.map((child: any) => {
|
||||
const childNode = convertItem(child);
|
||||
childNode.path = `${item.name}/${child.name}`;
|
||||
return childNode;
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const roots = [...nodeMap.entries()]
|
||||
.filter(([name]) => !hasParent.has(name))
|
||||
.map(([, node]) => node);
|
||||
return node;
|
||||
};
|
||||
|
||||
const roots = data.map((item) => convertItem(item));
|
||||
|
||||
set({
|
||||
files: {
|
||||
|
||||
Reference in New Issue
Block a user