7.8 KiB
7.8 KiB
State Utilities Audit
Date: 2026-01-11
Action: 4.6.1.1 - List all state utility files
Status: ✅ Complete
Summary
This document lists all state utility files found in apps/web/src/utils/ and documents their purposes, usage, and status.
State Utility Files Found
1. stateMiddleware.ts
- Purpose: Zustand middleware for analytics tracking and error handling
- Exports:
setAnalyticsHandler()- Set analytics handlersetErrorHandler()- Set error handlerstateMiddleware()- Middleware function
- Key Features:
- Tracks state changes for analytics
- Handles errors in state updates
- Provides context for debugging
- Used By: Library Store (
stores/library.ts) - Status: ⚠️ Used by Library Store (may be unnecessary complexity)
- Test File:
stateMiddleware.test.ts✅
2. stateNormalization.ts
- Purpose: Utilities for normalized state management (entities by ID)
- Exports:
normalize()- Convert array to normalized statedenormalize()- Convert normalized state to arrayaddToNormalized()- Add item to normalized stateupdateInNormalized()- Update item in normalized stateremoveFromNormalized()- Remove item from normalized statereplaceNormalized()- Replace normalized statemergeNormalized()- Merge normalized statesgetById()- Get item by IDgetByIds()- Get items by IDs
- Key Features:
- Normalized state structure:
{ byId: Record<string, T>, allIds: string[] } - Efficient lookups and updates
- Normalized state structure:
- Used By: Library Store (
stores/library.ts) - Status: ⚠️ Used by Library Store (will become obsolete when Library Store migrates to React Query)
- Test File: None ❌
3. stateInvalidation.ts
- Purpose: Unified state invalidation for Zustand stores and React Query
- Exports:
invalidateState()- Invalidate state by resource typeinvalidateStateAfterMutation()- Invalidate after mutation
- Key Features:
- Invalidates both Zustand stores and React Query cache
- Supports resource-based invalidation
- Integrates with response cache
- Used By: Various mutation handlers
- Status: ✅ Active (useful for migration period)
- Test File: None ❌
4. stateHydration.ts
- Purpose: Hydrate Zustand stores from API on app load
- Exports:
hydrateState()- Hydrate stores from APIuseStateHydration()- React hook for hydration
- Key Features:
- Loads initial state from API
- Hydrates Auth, Library, and Chat stores
- Handles errors and loading states
- Used By: App initialization, store hydration
- Status: ⚠️ Used for Zustand stores (will become obsolete when stores migrate to React Query)
- Test File: None ❌
5. stateCleanup.ts
- Purpose: Cleanup utilities for Zustand stores (size/age-based)
- Exports:
applyCleanup()- Apply cleanup strategy to store
- Key Features:
- Size-based cleanup (keep N items)
- Age-based cleanup (remove old items)
- Normalized state cleanup support
- Used By: Potentially Library Store
- Status: ⚠️ May be unused (needs verification)
- Test File:
stateCleanup.test.ts✅
6. stateVersioning.ts
- Purpose: State versioning and migration utilities for Zustand stores
- Exports:
applyMigrations()- Apply migrations to stateversionState()- Add version to stateunversionState()- Remove version from statecreateVersionedStorage()- Create versioned storage adapter
- Key Features:
- Supports state schema migrations
- Version tracking for persisted state
- Migration functions for breaking changes
- Used By: Potentially for store migrations
- Status: ⚠️ May be unused (needs verification)
- Test Files:
stateVersioning.test.ts✅,stateVersioning.example.ts✅
7. statePersistence.ts
- Purpose: Persistence utilities for Zustand stores
- Exports: (needs inspection)
- Key Features: (needs inspection)
- Used By: (needs inspection)
- Status: ⚠️ Needs inspection
- Test File: None ❌
8. broadcastSync.ts
- Purpose: Synchronize Zustand stores across browser tabs/windows
- Exports:
broadcastSync()- Middleware for tab synchronizationcreateSynchronizedStore()- Create synchronized store
- Key Features:
- Uses BroadcastChannel API
- Syncs state changes across tabs
- Handles conflicts and race conditions
- Used By: UI Store (
stores/ui.ts), Auth Store (features/auth/store/authStore.ts) - Status: ✅ Active (useful for multi-tab sync)
- Test File: None ❌
9. undoRedo.ts
- Purpose: Undo/redo functionality for Zustand stores
- Exports:
undoRedo()- Middleware for undo/redouseUndoRedo()- React hook for undo/redoUndoRedoManager- Class for managing historyundoRedoManager- Singleton instance
- Key Features:
- History tracking for state changes
- Undo/redo operations
- Configurable history size
- Used By: Library Store (
stores/library.ts) - Status: ⚠️ Used by Library Store (will become obsolete when Library Store migrates to React Query)
- Test File: None ❌
10. storeSelectors.ts
- Purpose: Optimized selectors for Zustand stores to prevent unnecessary re-renders
- Exports:
useAuthUser()- Get user from auth storeuseAuthStatus()- Get auth statususeStoreSelector()- Generic store selector
- Key Features:
- Uses
useShallowfor shallow comparison - Prevents unnecessary re-renders
- Type-safe selectors
- Uses
- Used By: Various components
- Status: ✅ Active (useful optimization)
- Test File: None ❌
File Statistics
- Total State Utility Files: 10
- With Tests: 3 (30%)
- Without Tests: 7 (70%)
- Actively Used: 6
- Potentially Unused: 2 (stateCleanup, stateVersioning - needs verification)
- Will Become Obsolete: 4 (stateMiddleware, stateNormalization, stateHydration, undoRedo - when stores migrate)
Usage Analysis
Utilities Used by Stores
- Library Store:
stateMiddleware,stateNormalization,undoRedo - UI Store:
broadcastSync - Auth Store:
broadcastSync - Chat Store: None (uses Immer middleware)
Utilities Used by Components
- storeSelectors: Used by various components for optimized selectors
- stateInvalidation: Used by mutation handlers
- stateHydration: Used for app initialization
Migration Impact
Will Become Obsolete (After Store Migration)
- stateMiddleware - Only used by Library Store
- stateNormalization - Only used by Library Store
- stateHydration - Only hydrates Zustand stores
- undoRedo - Only used by Library Store
Will Remain Useful
- broadcastSync - Useful for UI state sync across tabs
- storeSelectors - Useful for optimized Zustand selectors
- stateInvalidation - Useful during migration period
Needs Investigation
- stateCleanup - Verify if actually used
- stateVersioning - Verify if actually used
- statePersistence - Inspect purpose and usage
Recommendations
High Priority
- ✅ Action 4.6.1.1: Audit complete - 10 utilities documented
- Action 4.6.1.2: Verify usage of
stateCleanup,stateVersioning,statePersistence - Action 4.6.1.3: Add tests for utilities without tests (7 files)
Medium Priority
- Plan removal of obsolete utilities after store migration
- Document migration path for utilities that will become obsolete
Low Priority
- Consider consolidating similar utilities
- Review if
stateMiddlewarecomplexity is justified
Next Steps
- ✅ Action 4.6.1.1: List complete - 10 state utilities documented
- Action 4.6.1.2: Verify usage of potentially unused utilities
- Action 4.6.1.3: Add missing tests