From f14490c076cef1a292280e290dc4eda43554a228 Mon Sep 17 00:00:00 2001 From: nikita Date: Sun, 5 Apr 2026 03:59:08 +0300 Subject: [PATCH] feat: rename --- frontend/src/modules/ide/api/scripts.api.ts | 11 +++++ frontend/src/modules/ide/store/useIDEStore.ts | 45 ++++++++++++++++--- 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/frontend/src/modules/ide/api/scripts.api.ts b/frontend/src/modules/ide/api/scripts.api.ts index 61d235b..3f41a48 100644 --- a/frontend/src/modules/ide/api/scripts.api.ts +++ b/frontend/src/modules/ide/api/scripts.api.ts @@ -72,4 +72,15 @@ export const scriptsApi = { deleteFolder: async (path: string): Promise => { await apiClient.delete(`/scripts/folder`, { data: { path } }); }, + + rename: async (payload: { + old_path: string; + new_path: string; + }): Promise<{ path: string }> => { + const res = await apiClient.post<{ path: string }>( + "/scripts/rename", + payload, + ); + return res.data; + }, }; diff --git a/frontend/src/modules/ide/store/useIDEStore.ts b/frontend/src/modules/ide/store/useIDEStore.ts index 4a62a5f..f9f2687 100644 --- a/frontend/src/modules/ide/store/useIDEStore.ts +++ b/frontend/src/modules/ide/store/useIDEStore.ts @@ -476,14 +476,45 @@ export const useIDEStore = create((set, get) => ({ alert(`"${value}" already exists.`); return; } - const newFiles = renameNode( - files!, - dialog.node.path || dialog.node.name, - value, - ); - if (newFiles) { - set({ files: newFiles }); + + const oldPath = dialog.node.path || dialog.node.name; + const newPath = parentPath ? `${parentPath}/${value}` : value; + + // Сохраняем раскрытые папки + const savedExpandedFolders = new Set(get().expandedFolders); + + try { + await scriptsApi.rename({ old_path: oldPath, new_path: newPath }); + await get().fetchTree(); + + // Восстанавливаем раскрытые папки + set({ expandedFolders: savedExpandedFolders }); + + // Раскрываем родительскую цепочку + const allParentPaths: string[] = []; + let current = parentPath; + while (current) { + allParentPaths.push(current); + const parts = current.split("/"); + parts.pop(); + current = parts.join("/"); + } + autoExpandPaths(new Set(allParentPaths)); + + // Если переименованный файл был открыт — обновим его в openFiles + const { openFiles, activeFile } = get(); + const updatedOpenFiles = openFiles.map((f) => + f.path === oldPath ? { ...f, name: value, path: newPath } : f, + ); + set({ openFiles: updatedOpenFiles }); + + if (activeFile?.path === oldPath) { + set({ activeFile: { ...activeFile, name: value, path: newPath } }); + } + } catch (e) { + console.error("Failed to rename:", e); } + set({ dialog: null }); return; }