This commit is contained in:
xiongxiao
2026-03-16 01:13:47 +08:00
committed by cnb
parent 31a3c48c48
commit 66f70b144a

View File

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