8.6 KiB
8.6 KiB
Zustand Stores Audit
Date: 2026-01-11
Action: 4.5.1.1 - List all Zustand stores
Status: ✅ Complete
Summary
This document lists all Zustand stores found in the codebase, their locations, purposes, and key characteristics.
Stores Found
1. UI Store (apps/web/src/stores/ui.ts)
- Export:
useUIStore - Purpose: UI state management (theme, language, sidebar, notifications)
- Middlewares:
persist,devtools,broadcastSync - State Type:
UIStore(UIState & UIActions) - Key State:
theme: 'light' | 'dark' | 'system'language: 'en' | 'fr'sidebarOpen: booleannotifications: Notification[]
- Category: ✅ UI State (appropriate for Zustand)
- Migration Priority: Low (UI state should stay in Zustand)
2. Library Store (apps/web/src/stores/library.ts)
- Export:
useLibraryStore - Purpose: Library items, favorites, pagination state
- Middlewares:
persist,devtools,stateMiddleware,undoRedo - State Type:
LibraryStore(LibraryState & LibraryActions & WithUndoRedo) - Key State:
items: NormalizedState<LibraryItem>⚠️ Domain Datafavorites: NormalizedState<LibraryItem>⚠️ Domain Datapagination: { page, limit, total, hasNext, hasPrev }⚠️ Domain DataisLoading: booleanerror: ApiError | null
- Category: ⚠️ Domain Data (should migrate to React Query)
- Migration Priority: High (contains domain data that should be server state)
3. Chat Store (apps/web/src/stores/chat.ts)
- Export:
useChatStore - Purpose: Chat conversations, messages, typing indicators
- Middlewares:
persist,devtools - State Type:
ChatStore(ChatState & ChatActions) - Key State:
conversations: Conversation[]⚠️ Domain DatacurrentConversation: Conversation | null✅ UI Statemessages: Record<string, ChatMessage[]>⚠️ Domain DatatypingUsers: Record<string, string[]>✅ UI State (ephemeral)isConnected: boolean✅ UI StateisLoading: booleanerror: string | null
- Category: ⚠️ Mixed (domain data + UI state)
- Migration Priority: Medium (domain data should migrate to React Query, UI state can stay)
4. Cart Store (apps/web/src/stores/cartStore.ts)
- Export:
useCartStore - Purpose: Shopping cart items and operations
- Middlewares:
persist - State Type:
CartStore(CartState) - Key State:
items: CartItem[]✅ UI State (client-side cart before checkout)- Actions:
addItem,removeItem,updateQuantity,clearCart,getTotal,getItemCount
- Category: ✅ UI State (appropriate for Zustand - client-side cart)
- Migration Priority: Low (cart is client-side state before checkout)
- Analysis:
- Cart items are temporary client-side state before checkout
- Not domain data - cart is created client-side, not fetched from server
- Persisted to localStorage for user convenience
- No server synchronization needed until checkout
- ✅ Correctly categorized as UI state
5. Auth Store (apps/web/src/features/auth/store/authStore.ts)
- Export:
useAuthStore - Purpose: Authentication state (user, tokens, auth status)
- Middlewares:
persist,broadcastSync - State Type:
AuthStore(AuthState & AuthActions) - Key State:
user: User | null⚠️ Domain Data (should migrate to React Query)isAuthenticated: boolean✅ UI State (can stay)isLoading: booleanerror: ApiError | null_refreshUserPromise: Promise<void> | null(internal deduplication)
- Category: ⚠️ Mixed (domain data + UI state)
- Migration Priority: High (user data should migrate to React Query - Action 4.1.1.x)
6. Chat Store (Feature) (apps/web/src/features/chat/store/chatStore.ts)
- Export:
useChatStore(⚠️ DUPLICATE NAME withstores/chat.ts) - Purpose: Chat state management (feature-specific, uses Immer)
- Middlewares:
immer,devtools - State Type: Chat store with Immer middleware
- Key State:
conversations: Conversation[]⚠️ Domain Datamessages: Record<string, ChatMessage[]>⚠️ Domain DatacurrentConversationId: string | null✅ UI StatetypingUsers: Record<string, string[]>✅ UI StatewsStatus: 'disconnected' | 'connecting' | 'connected' | 'error'✅ UI State
- Category: ⚠️ Domain Data (duplicate store - needs investigation)
- Migration Priority: High (investigate which store is used, consolidate)
- Note:
stores/index.tsexportsstores/chat.ts, but feature store exists separately
7. Player Store (apps/web/src/features/player/store/playerStore.ts)
- Export:
usePlayerStore(likely) - Purpose: Audio player state (current track, playback state, queue)
- Middlewares:
persist - State Type: Player store
- Key State: (needs inspection)
- Category: ✅ UI State (player state is client-side)
- Migration Priority: Low (player state should stay in Zustand)
Store Locations
Centralized Stores (apps/web/src/stores/)
ui.ts- UI Storelibrary.ts- Library Storechat.ts- Chat StorecartStore.ts- Cart Storetypes.ts- Type definitionsindex.ts- Exports
Feature Stores (apps/web/src/features/*/store/)
auth/store/authStore.ts- Auth Storechat/store/chatStore.ts- Chat Store (duplicate?)player/store/playerStore.ts- Player Store
Store Characteristics
Middleware Usage
- persist: Used in 6/7 stores (UI, Library, Chat, Cart, Auth, Player)
- devtools: Used in 3/7 stores (UI, Library, Chat)
- broadcastSync: Used in 2/7 stores (UI, Auth)
- stateMiddleware: Used in 1/7 stores (Library)
- undoRedo: Used in 1/7 stores (Library)
- immer: Used in 1/7 stores (Chat feature store)
Domain Data Stores (Should Migrate to React Query)
- Library Store -
items,favorites,pagination - Auth Store -
user(Action 4.1.1.x in progress) - Chat Store -
conversations,messages
UI State Stores (Appropriate for Zustand)
- UI Store - Theme, language, sidebar, notifications
- Cart Store - Shopping cart items
- Player Store - Audio player state
- Auth Store -
isAuthenticatedflag
Mixed Stores (Need Partial Migration)
- Auth Store -
user(domain) +isAuthenticated(UI) - Chat Store -
conversations/messages(domain) +currentConversation/typingUsers(UI)
Issues Identified
1. Duplicate Chat Stores ⚠️ CRITICAL
- Store 1:
apps/web/src/stores/chat.ts(exported instores/index.ts)- Used by:
ChatMessages.tsx,stateHydration.ts,storeSelectors.ts,stores.test.ts - Middlewares:
persist,devtools
- Used by:
- Store 2:
apps/web/src/features/chat/store/chatStore.ts(feature-specific)- Used by: Most chat feature components (
ChatSidebar,ChatPage,ChatInput,ChatRoom,CreateRoomDialog,useChat.ts,TypingIndicator) - Middlewares:
immer,devtools
- Used by: Most chat feature components (
- Problem: Both export
useChatStorewith same name, causing potential conflicts - Action Required:
- Determine which store is the source of truth
- Consolidate into single store
- Update all imports to use consolidated store
- Remove duplicate store
2. Domain Data in Zustand ⚠️
- Library Store contains domain data (
items,favorites) - Auth Store contains domain data (
user) - Chat Store contains domain data (
conversations,messages) - Action Required: Migrate domain data to React Query (Epic 4.1)
3. Complex Middleware Stack
- Library Store uses
stateMiddlewareandundoRedo- may be unnecessary complexity - Action Required: Audit middleware usage (Action 4.1.2.x)
Migration Roadmap
Phase 1: High Priority (Domain Data)
- ✅ Auth Store -
user→ React Query (Action 4.1.1.x in progress) - Library Store -
items,favorites→ React Query - Chat Store -
conversations,messages→ React Query
Phase 2: Medium Priority (Cleanup)
- Investigate duplicate Chat stores
- Simplify middleware stacks
- Remove
stateMiddlewareif unnecessary
Phase 3: Low Priority (Keep in Zustand)
- UI Store - Keep (UI state)
- Cart Store - Keep (client-side cart)
- Player Store - Keep (player state)
Next Steps
- ✅ Action 4.5.1.1: List complete - 7 stores identified
- Action 4.5.1.2: Categorize stores (UI state vs domain data) - See categorization above
- Action 4.1.1.x: Migrate Auth Store
userto React Query (in progress) - Action 4.1.2.x: Migrate Library Store domain data to React Query
- Investigate duplicate Chat stores