mirror of
https://github.com/multica-ai/multica.git
synced 2026-06-17 03:38:32 +02:00
- Remove workspaces[] from workspace store — list is server state, belongs in React Query - Change switchWorkspace(id) → switchWorkspace(ws) — caller provides full object from Query - Remove createWorkspace/leaveWorkspace/deleteWorkspace store actions (duplicated mutations) - Remove refreshWorkspaces store action — replaced by qc.fetchQuery + hydrateWorkspace - Enhance useLeaveWorkspace/useDeleteWorkspace mutations to re-select workspace when current is removed - useCreateWorkspace mutation now switches to new workspace on success - AuthInitializer seeds React Query cache on boot to avoid double fetch - Realtime sync: replace refreshWorkspaces() calls with qc.fetchQuery + hydrateWorkspace - Sidebar reads workspace list from useQuery(workspaceListOptions()) instead of Zustand - create-workspace modal and workspace settings tab use mutations directly - AGENTS.md: rewrite to match current monorepo architecture, pointing to CLAUDE.md Fixes workspace rename not updating sidebar without page refresh. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
61 lines
1.7 KiB
TypeScript
61 lines
1.7 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, type ReactNode } from "react";
|
|
import { useQueryClient } from "@tanstack/react-query";
|
|
import { getApi } from "../api";
|
|
import { useAuthStore } from "../auth";
|
|
import { useWorkspaceStore } from "../workspace";
|
|
import { workspaceKeys } from "../workspace/queries";
|
|
import { createLogger } from "../logger";
|
|
import { defaultStorage } from "./storage";
|
|
import type { StorageAdapter } from "../types/storage";
|
|
|
|
const logger = createLogger("auth");
|
|
|
|
export function AuthInitializer({
|
|
children,
|
|
onLogin,
|
|
onLogout,
|
|
storage = defaultStorage,
|
|
}: {
|
|
children: ReactNode;
|
|
onLogin?: () => void;
|
|
onLogout?: () => void;
|
|
storage?: StorageAdapter;
|
|
}) {
|
|
const qc = useQueryClient();
|
|
|
|
useEffect(() => {
|
|
const token = storage.getItem("multica_token");
|
|
if (!token) {
|
|
onLogout?.();
|
|
useAuthStore.setState({ isLoading: false });
|
|
return;
|
|
}
|
|
|
|
const api = getApi();
|
|
api.setToken(token);
|
|
const wsId = storage.getItem("multica_workspace_id");
|
|
|
|
Promise.all([api.getMe(), api.listWorkspaces()])
|
|
.then(([user, wsList]) => {
|
|
onLogin?.();
|
|
useAuthStore.setState({ user, isLoading: false });
|
|
// Seed React Query cache so components don't need a second fetch
|
|
qc.setQueryData(workspaceKeys.list(), wsList);
|
|
useWorkspaceStore.getState().hydrateWorkspace(wsList, wsId);
|
|
})
|
|
.catch((err) => {
|
|
logger.error("auth init failed", err);
|
|
api.setToken(null);
|
|
api.setWorkspaceId(null);
|
|
storage.removeItem("multica_token");
|
|
storage.removeItem("multica_workspace_id");
|
|
onLogout?.();
|
|
useAuthStore.setState({ user: null, isLoading: false });
|
|
});
|
|
}, []);
|
|
|
|
return <>{children}</>;
|
|
}
|