feat: workspace 目录树服务端懒加载
- api.ts: listWorkspaceDirectories 支持 parent_path 查询; WorkspaceDirectory 增加 has_children; 同步 WorkspaceBoundApi 签名 - AuthContext: 绑定透传 parentPath - scriptWorkspaceStore: 新增 expandedPaths/loadingChildrenPaths/loadedChildPaths, loadChildren/toggleExpanded, 局部刷新 createFolder/deleteDirectory - WorkspaceTree: 移除 useState, 改为受控展开/加载状态 - ScriptExplorer: 从 store 读取并透传展开状态与 toggle
This commit is contained in:
@@ -79,6 +79,9 @@ type State = {
|
||||
previewError: string | null;
|
||||
|
||||
pythonEditorBuffers: Record<string, PythonEditorBuffer>;
|
||||
expandedPaths: Set<string>;
|
||||
loadingChildrenPaths: Set<string>;
|
||||
loadedChildPaths: Set<string>;
|
||||
|
||||
// actions
|
||||
setApiOnline: (online: boolean) => void;
|
||||
@@ -103,6 +106,8 @@ type State = {
|
||||
createFolder: (name: string, parentPath: string) => Promise<void>;
|
||||
deleteScript: (script: ScriptItem) => Promise<void>;
|
||||
deleteDirectory: (path: string) => Promise<void>;
|
||||
toggleExpanded: (path: string) => Promise<void>;
|
||||
loadChildren: (parentPath: string) => Promise<void>;
|
||||
toggleScriptLock: (script: ScriptItem) => Promise<void>;
|
||||
openPublishDialog: (script: ScriptItem) => void;
|
||||
submitPublish: (releaseNote: string, visibility: Visibility) => Promise<void>;
|
||||
@@ -166,6 +171,9 @@ export const useScriptWorkspaceStore = create<State>((set, get) => {
|
||||
previewError: null,
|
||||
|
||||
pythonEditorBuffers: {},
|
||||
expandedPaths: new Set<string>(),
|
||||
loadingChildrenPaths: new Set<string>(),
|
||||
loadedChildPaths: new Set<string>(),
|
||||
|
||||
setApiOnline: (online) => set({ apiOnline: online }),
|
||||
setKeyword: (keyword) => set({ keyword }),
|
||||
@@ -197,6 +205,9 @@ export const useScriptWorkspaceStore = create<State>((set, get) => {
|
||||
previewLoading: false,
|
||||
previewError: null,
|
||||
pythonEditorBuffers: {},
|
||||
expandedPaths: new Set<string>(),
|
||||
loadingChildrenPaths: new Set<string>(),
|
||||
loadedChildPaths: new Set<string>(),
|
||||
});
|
||||
},
|
||||
|
||||
@@ -207,12 +218,15 @@ export const useScriptWorkspaceStore = create<State>((set, get) => {
|
||||
try {
|
||||
const [items, folderItems] = await Promise.all([
|
||||
api.listScripts(),
|
||||
api.listWorkspaceDirectories(),
|
||||
api.listWorkspaceDirectories(""),
|
||||
]);
|
||||
const nextLoaded = new Set(get().loadedChildPaths);
|
||||
nextLoaded.add("");
|
||||
set({
|
||||
scripts: items,
|
||||
directories: folderItems,
|
||||
apiOnline: true,
|
||||
loadedChildPaths: nextLoaded,
|
||||
});
|
||||
const validIds = new Set(items.map((item) => item.script_id));
|
||||
const currentSelected = get().selectedId;
|
||||
@@ -236,6 +250,56 @@ export const useScriptWorkspaceStore = create<State>((set, get) => {
|
||||
}
|
||||
},
|
||||
|
||||
loadChildren: async (parentPath) => {
|
||||
const api = requireApi();
|
||||
if (get().loadedChildPaths.has(parentPath)) return;
|
||||
const next = new Set(get().loadingChildrenPaths);
|
||||
next.add(parentPath);
|
||||
set({ loadingChildrenPaths: next });
|
||||
try {
|
||||
const children = await api.listWorkspaceDirectories(parentPath);
|
||||
set((state) => {
|
||||
const nextLoaded = new Set(state.loadedChildPaths);
|
||||
nextLoaded.add(parentPath);
|
||||
const trimmed = state.directories.filter(
|
||||
(d) => d.parent_path !== parentPath,
|
||||
);
|
||||
return {
|
||||
directories: [...trimmed, ...children],
|
||||
loadedChildPaths: nextLoaded,
|
||||
loadingChildrenPaths: new Set(
|
||||
[...state.loadingChildrenPaths].filter((p) => p !== parentPath),
|
||||
),
|
||||
};
|
||||
});
|
||||
} catch (error) {
|
||||
set((state) => ({
|
||||
loadingChildrenPaths: new Set(
|
||||
[...state.loadingChildrenPaths].filter((p) => p !== parentPath),
|
||||
),
|
||||
}));
|
||||
pushToast(
|
||||
"error",
|
||||
error instanceof Error ? error.message : "目录加载失败",
|
||||
);
|
||||
}
|
||||
},
|
||||
|
||||
toggleExpanded: async (path) => {
|
||||
const state = get();
|
||||
const isOpen = state.expandedPaths.has(path);
|
||||
const next = new Set(state.expandedPaths);
|
||||
if (isOpen) {
|
||||
next.delete(path);
|
||||
} else {
|
||||
next.add(path);
|
||||
if (!state.loadedChildPaths.has(path)) {
|
||||
void get().loadChildren(path);
|
||||
}
|
||||
}
|
||||
set({ expandedPaths: next });
|
||||
},
|
||||
|
||||
selectScript: (id) => {
|
||||
_selectedId = id;
|
||||
set({ selectedId: id });
|
||||
@@ -712,7 +776,25 @@ export const useScriptWorkspaceStore = create<State>((set, get) => {
|
||||
ui.setFolderBusy(true);
|
||||
try {
|
||||
await api.createWorkspaceDirectory(trimmed, parentPath);
|
||||
await get().load(true);
|
||||
if (parentPath === "") {
|
||||
await get().load(true);
|
||||
} else {
|
||||
set((state) => {
|
||||
const nextLoaded = new Set(state.loadedChildPaths);
|
||||
for (const p of state.loadedChildPaths) {
|
||||
if (p.startsWith(`${parentPath}/`)) nextLoaded.delete(p);
|
||||
}
|
||||
nextLoaded.delete(parentPath);
|
||||
return {
|
||||
loadedChildPaths: nextLoaded,
|
||||
directories: state.directories.filter(
|
||||
(d) => !d.parent_path.startsWith(`${parentPath}/`),
|
||||
),
|
||||
expandedPaths: new Set(state.expandedPaths),
|
||||
};
|
||||
});
|
||||
await get().loadChildren(parentPath);
|
||||
}
|
||||
ui.closeFolderDialog();
|
||||
pushToast("success", `${trimmed} 文件夹已创建`);
|
||||
} catch (error) {
|
||||
@@ -779,6 +861,9 @@ export const useScriptWorkspaceStore = create<State>((set, get) => {
|
||||
await get().endEditing(false, false);
|
||||
if (_editSession?.script_id === activeScript.script_id) return;
|
||||
}
|
||||
const parentPath = path.includes("/")
|
||||
? path.split("/").slice(0, -1).join("/")
|
||||
: "";
|
||||
try {
|
||||
const result = await api.deleteWorkspaceDirectory(path);
|
||||
const selectedScript = get().scripts.find(
|
||||
@@ -790,7 +875,28 @@ export const useScriptWorkspaceStore = create<State>((set, get) => {
|
||||
) {
|
||||
get().selectScript(null);
|
||||
}
|
||||
await get().load(true);
|
||||
set((state) => {
|
||||
const nextLoaded = new Set(state.loadedChildPaths);
|
||||
const nextExpanded = new Set(state.expandedPaths);
|
||||
for (const p of state.loadedChildPaths) {
|
||||
if (p === path || p.startsWith(`${path}/`)) nextLoaded.delete(p);
|
||||
}
|
||||
for (const p of state.expandedPaths) {
|
||||
if (p === path || p.startsWith(`${path}/`)) nextExpanded.delete(p);
|
||||
}
|
||||
return {
|
||||
loadedChildPaths: nextLoaded,
|
||||
expandedPaths: nextExpanded,
|
||||
directories: state.directories.filter(
|
||||
(d) => d.parent_path !== path && !d.parent_path.startsWith(`${path}/`),
|
||||
),
|
||||
};
|
||||
});
|
||||
if (parentPath === "") {
|
||||
await get().load(true);
|
||||
} else {
|
||||
await get().loadChildren(parentPath);
|
||||
}
|
||||
pushToast(
|
||||
"success",
|
||||
`${path} 已删除(含 ${result.deleted_scripts} 个脚本)`,
|
||||
|
||||
Reference in New Issue
Block a user