diff --git a/frontend/app/components/admin/ProjectManagementPage.tsx b/frontend/app/components/admin/ProjectManagementPage.tsx index ccbb8e5..3cfb20f 100644 --- a/frontend/app/components/admin/ProjectManagementPage.tsx +++ b/frontend/app/components/admin/ProjectManagementPage.tsx @@ -20,7 +20,7 @@ export function ProjectManagementPage({ onConnectionChange: (online: boolean) => void; }) { const api = useApi(); - const { user } = useAuth(); + const { user, refreshWorkspaces } = useAuth(); const [projects, setProjects] = useState([]); const [projectLoading, setProjectLoading] = useState(true); const [projectDialogOpen, setProjectDialogOpen] = useState(false); @@ -107,6 +107,7 @@ export function ProjectManagementPage({ description: projectForm.description.trim() || undefined, }); setProjects((current) => [...current, created]); + void refreshWorkspaces(); onNotify({ tone: "success", message: "项目已创建" }); } setProjectDialogOpen(false); diff --git a/frontend/app/context/AuthContext.tsx b/frontend/app/context/AuthContext.tsx index abbb329..eb78023 100644 --- a/frontend/app/context/AuthContext.tsx +++ b/frontend/app/context/AuthContext.tsx @@ -48,6 +48,7 @@ type AuthContextValue = { login: (username: string, password: string) => Promise; logout: () => Promise; setCurrentWorkspace: (workspaceId: string) => void; + refreshWorkspaces: () => Promise; }; const AuthContext = createContext(null); @@ -167,6 +168,23 @@ export function AuthProvider({ children }: { children: ReactNode }) { window.localStorage.setItem(workspaceStorageKey, workspace.workspace_id); }, [workspaces]); + const refreshWorkspaces = useCallback(async () => { + try { + const session = await authRequest("/api/v1/auth/me"); + const storedId = typeof window === "undefined" + ? null + : window.localStorage.getItem(workspaceStorageKey); + const newWorkspace = selectWorkspace(session, storedId); + setWorkspaces(session.workspaces); + setWorkspace(newWorkspace); + if (newWorkspace && typeof window !== "undefined") { + window.localStorage.setItem(workspaceStorageKey, newWorkspace.workspace_id); + } + } catch (error) { + console.error("刷新 workspace 列表失败:", error); + } + }, []); + const value = useMemo(() => ({ user, workspaces, @@ -175,7 +193,8 @@ export function AuthProvider({ children }: { children: ReactNode }) { login, logout, setCurrentWorkspace, - }), [currentWorkspace, loading, login, logout, setCurrentWorkspace, user, workspaces]); + refreshWorkspaces, + }), [currentWorkspace, loading, login, logout, setCurrentWorkspace, user, workspaces, refreshWorkspaces]); if (loading) { return (