refactor(web): restructure to feature-based architecture with zustand stores
- Remove tab system entirely (tab-store, tab-bar, tab-link) - Split monolithic AuthContext into zustand auth + workspace stores - Move issue components/config to features/issues/ - Move WebSocket provider to features/realtime/ - Move api.ts to shared/ - Migrate all consumers from useAuth() to direct store imports - Simplify sidebar: replace hand-built dropdown with shadcn DropdownMenu, replace custom layout wrapper with SidebarInset - Remove unused @multica/store and @multica/hooks dependencies - Add @/ path alias and zustand dependency - Update CLAUDE.md with feature-based architecture conventions Net change: +293 / -2435 lines Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
768a555a80
commit
a2d7501d57
48 changed files with 604 additions and 1704 deletions
2
apps/web/features/auth/index.ts
Normal file
2
apps/web/features/auth/index.ts
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
export { useAuthStore } from "./store";
|
||||
export { AuthInitializer } from "./initializer";
|
||||
32
apps/web/features/auth/initializer.tsx
Normal file
32
apps/web/features/auth/initializer.tsx
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
"use client";
|
||||
|
||||
import { useEffect, type ReactNode } from "react";
|
||||
import { useAuthStore } from "./store";
|
||||
import { useWorkspaceStore } from "@/features/workspace";
|
||||
import { api } from "@/shared/api";
|
||||
|
||||
/**
|
||||
* Initializes auth + workspace state from localStorage on mount.
|
||||
* Must wrap the app to ensure stores are hydrated before children render.
|
||||
*/
|
||||
export function AuthInitializer({ children }: { children: ReactNode }) {
|
||||
const initialize = useAuthStore((s) => s.initialize);
|
||||
const user = useAuthStore((s) => s.user);
|
||||
const isLoading = useAuthStore((s) => s.isLoading);
|
||||
const hydrateWorkspace = useWorkspaceStore((s) => s.hydrateWorkspace);
|
||||
|
||||
useEffect(() => {
|
||||
initialize();
|
||||
}, [initialize]);
|
||||
|
||||
useEffect(() => {
|
||||
if (isLoading || !user) return;
|
||||
const wsId = localStorage.getItem("multica_workspace_id");
|
||||
|
||||
api.listWorkspaces().then((wsList) => {
|
||||
hydrateWorkspace(wsList, wsId);
|
||||
}).catch(console.error);
|
||||
}, [user, isLoading, hydrateWorkspace]);
|
||||
|
||||
return <>{children}</>;
|
||||
}
|
||||
61
apps/web/features/auth/store.ts
Normal file
61
apps/web/features/auth/store.ts
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
"use client";
|
||||
|
||||
import { create } from "zustand";
|
||||
import type { User } from "@multica/types";
|
||||
import { api } from "@/shared/api";
|
||||
|
||||
interface AuthState {
|
||||
user: User | null;
|
||||
isLoading: boolean;
|
||||
|
||||
initialize: () => Promise<void>;
|
||||
login: (email: string, name?: string) => Promise<User>;
|
||||
logout: () => void;
|
||||
setUser: (user: User) => void;
|
||||
}
|
||||
|
||||
export const useAuthStore = create<AuthState>((set) => ({
|
||||
user: null,
|
||||
isLoading: true,
|
||||
|
||||
initialize: async () => {
|
||||
const token = localStorage.getItem("multica_token");
|
||||
if (!token) {
|
||||
set({ isLoading: false });
|
||||
return;
|
||||
}
|
||||
|
||||
api.setToken(token);
|
||||
|
||||
try {
|
||||
const user = await api.getMe();
|
||||
set({ user, isLoading: false });
|
||||
} catch {
|
||||
api.setToken(null);
|
||||
api.setWorkspaceId(null);
|
||||
localStorage.removeItem("multica_token");
|
||||
localStorage.removeItem("multica_workspace_id");
|
||||
set({ user: null, isLoading: false });
|
||||
}
|
||||
},
|
||||
|
||||
login: async (email: string, name?: string) => {
|
||||
const { token, user } = await api.login(email, name);
|
||||
localStorage.setItem("multica_token", token);
|
||||
api.setToken(token);
|
||||
set({ user });
|
||||
return user;
|
||||
},
|
||||
|
||||
logout: () => {
|
||||
localStorage.removeItem("multica_token");
|
||||
localStorage.removeItem("multica_workspace_id");
|
||||
api.setToken(null);
|
||||
api.setWorkspaceId(null);
|
||||
set({ user: null });
|
||||
},
|
||||
|
||||
setUser: (user: User) => {
|
||||
set({ user });
|
||||
},
|
||||
}));
|
||||
Loading…
Add table
Add a link
Reference in a new issue