diff --git a/src/pages/code-graph/store/index.ts b/src/pages/code-graph/store/index.ts index b71da73..e5be0c3 100644 --- a/src/pages/code-graph/store/index.ts +++ b/src/pages/code-graph/store/index.ts @@ -85,20 +85,30 @@ export const useCodeGraphStore = create()((set, get) => ({ loadProjects: async () => { set({ projectsLoading: true }); const url = get().url || API_URL; + const loadingToast = toast.loading('获取项目列表中...'); try { const res = await projectApi.project.list(undefined, { url }); + toast.dismiss(loadingToast); if (res.code === 200) { - set({ projects: (res.data?.list as ProjectItem[]) ?? [] }); + const projects = (res.data?.list as ProjectItem[]) ?? []; + projects.sort((a, b) => { + if (a.status === 'active' && b.status !== 'active') return -1; + if (a.status !== 'active' && b.status === 'active') return 1; + return a.path.localeCompare(b.path); + }); + set({ projects }); } else { toast.error('获取项目列表失败'); } } catch { + toast.dismiss(loadingToast); toast.error('获取项目列表失败'); } finally { set({ projectsLoading: false }); } }, addProject: async (filepath, name, type = 'filepath') => { + const loadingToast = toast.loading('添加项目中...'); try { const url = get().url || API_URL; let res: Result | null = null; @@ -112,6 +122,7 @@ export const useCodeGraphStore = create()((set, get) => ({ repo: filepath, }, { url }); } + toast.dismiss(loadingToast); if (res.code === 200) { toast.success('项目添加成功'); await get().loadProjects(); @@ -121,14 +132,17 @@ export const useCodeGraphStore = create()((set, get) => ({ return false; } } catch { + toast.dismiss(loadingToast); toast.error('项目添加失败'); return false; } }, initProject: async () => { + const loadingToast = toast.loading('初始化项目中...'); try { const url = get().url || API_URL; const res = await projectApi.project.init(undefined, { url }); + toast.dismiss(loadingToast); if (res.code === 200) { toast.success('项目初始化成功'); await get().loadProjects(); @@ -136,13 +150,16 @@ export const useCodeGraphStore = create()((set, get) => ({ toast.error(res.message ?? '项目初始化失败'); } } catch { + toast.dismiss(loadingToast); toast.error('项目初始化失败'); } }, removeProject: async (path) => { + const loadingToast = toast.loading('移除项目中...'); try { const url = get().url || API_URL; const res = await projectApi.project.remove({ filepath: path }, { url }); + toast.dismiss(loadingToast); if (res.code === 200) { toast.success('项目已移除'); set((s) => ({ projects: s.projects.filter((p) => p.path !== path) })); @@ -150,18 +167,21 @@ export const useCodeGraphStore = create()((set, get) => ({ toast.error(res.message ?? '移除失败'); } } catch { + toast.dismiss(loadingToast); toast.error('移除失败'); } }, toggleProjectStatus: async (path) => { + const project = get().projects.find((p) => p.path === path); + if (!project) return; + + const loadingToast = toast.loading(project.status === 'active' ? '停止监听中...' : '启动监听中...'); try { const url = get().url || API_URL; - const project = get().projects.find((p) => p.path === path); - if (!project) return; if (project.status === 'active') { - // 暂停项目监听 const res = await projectApi.project.stop({ filepath: path }, { url }); + toast.dismiss(loadingToast); if (res.code === 200) { toast.success('项目已停止监听'); set((s) => ({ @@ -171,8 +191,8 @@ export const useCodeGraphStore = create()((set, get) => ({ toast.error(res.message ?? '操作失败'); } } else { - // 重新启动项目监听 const res = await projectApi.project.add({ filepath: path }, { url }); + toast.dismiss(loadingToast); if (res.code === 200) { toast.success('项目已开始监听'); set((s) => ({ @@ -183,6 +203,7 @@ export const useCodeGraphStore = create()((set, get) => ({ } } } catch { + toast.dismiss(loadingToast); toast.error('操作失败'); } }, @@ -234,16 +255,19 @@ export const useCodeGraphStore = create()((set, get) => ({ return res; }, saveFile: async (filepath, content) => { - const url = get().url || API_URL; + const loadingToast = toast.loading('保存文件中...'); try { + const url = get().url || API_URL; const b64 = btoa(new TextEncoder().encode(content).reduce((s, b) => s + String.fromCharCode(b), '')); const res = await projectApi['project-file']['update-content']({ filepath, content: b64 }, { url }); + toast.dismiss(loadingToast); if (res.code === 200) { toast.success('保存成功'); } else { toast.error(res.message ?? '保存失败'); } } catch { + toast.dismiss(loadingToast); toast.error('保存失败'); } },