diff --git a/frontend/src/modules/ide/store/useIDEStore.ts b/frontend/src/modules/ide/store/useIDEStore.ts index 93dd972..47ee7e3 100644 --- a/frontend/src/modules/ide/store/useIDEStore.ts +++ b/frontend/src/modules/ide/store/useIDEStore.ts @@ -299,37 +299,32 @@ export const useIDEStore = create((set, get) => ({ fetchTree: async () => { try { const data = await scriptsApi.getTree(); - const nodeMap = new Map(); - 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(); - 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: { diff --git a/frontend/src/pages/templates.page.tsx b/frontend/src/pages/templates.page.tsx index e43a334..ee95823 100644 --- a/frontend/src/pages/templates.page.tsx +++ b/frontend/src/pages/templates.page.tsx @@ -7,54 +7,34 @@ import type { FileNode } from "../modules/ide"; import { scriptsApi } from "../modules/ide/api/scripts.api"; const convertTreeToFileNode = (data: any[]): FileNode => { - const nodeMap = new Map(); - const childrenMap = new Map(); - - // Создаём все узлы - 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, - }); + }; - if (item.children && item.children.length > 0) { - childrenMap.set(item.name, item.children); - } - }); - - // Строим дерево - const roots: FileNode[] = []; - const hasParent = new Set(); - - childrenMap.forEach((children, parentName) => { - const parentNode = nodeMap.get(parentName); - if (!parentNode) return; - - children.forEach((childName: string) => { - hasParent.add(childName); - const childNode = nodeMap.get(childName); - if (childNode && parentNode.children) { - parentNode.children.push(childNode); + 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; + }); } - }); - }); - - // Корневые элементы — те у кого нет родителя - nodeMap.forEach((node, name) => { - if (!hasParent.has(name)) { - roots.push(node); } - }); + + return node; + }; return { name: "templates", type: "folder", - children: roots, + children: data.map((item) => convertItem(item)), }; };