# Library Store Domain Data Usage Audit

**Date**: 2025-01-27  
**Action**: Prerequisite audit for Action 4.1.2.4 - Remove domain data from library store  
**Status**: ✅ Complete

## Summary

This audit identifies all components and files that use the library store's domain data fields (`items`, `favorites`, `pagination`) to determine migration requirements before removing them from the store.

## Components Using Library Store Domain Data

### 1. **DashboardPage.tsx** (`apps/web/src/pages/DashboardPage.tsx`)

- **Usage**:
  - `useLibraryItems()` - Gets library items array
  - `useLibraryActions()` - Gets `fetchItems` action
  - `useLibraryStatus()` - Gets `isLoading` status
- **Line**: 4, 19-20, 25-27
- **Purpose**: Displays recent library items on dashboard
- **Migration**: Replace with `useLibraryItems()` React Query hook
- **Risk**: MEDIUM - Active component, needs careful migration

### 2. **DashboardPage.tsx** (`apps/web/src/features/dashboard/pages/DashboardPage.tsx`)

- **Usage**:
  - `useLibraryItems()` - Gets library items array
  - `useLibraryActions()` - Gets `fetchItems` action
  - `useLibraryStatus()` - Gets `isLoading` status
- **Line**: 3, 17-19, 21-23
- **Purpose**: Displays recent library items on dashboard
- **Migration**: Replace with `useLibraryItems()` React Query hook
- **Risk**: MEDIUM - Active component, needs careful migration

### 3. **storeSelectors.ts** (`apps/web/src/utils/storeSelectors.ts`)

- **Usage**:
  - `useLibraryItems()` - Selector that denormalizes `items`
  - `useLibraryFavorites()` - Selector that denormalizes `favorites`
  - `useLibraryPagination()` - Selector that returns `pagination`
  - `useLibraryStatus()` - Selector that returns `isLoading` and `error`
  - `useLibraryActions()` - Selector that returns all actions
- **Line**: 87-133
- **Purpose**: Provides optimized selectors for library store
- **Migration**:
  - `useLibraryItems()` → Use React Query `useLibraryItems()` hook directly
  - `useLibraryFavorites()` → Use React Query `useLibraryFavorites()` hook directly
  - `useLibraryPagination()` → Extract from React Query response
  - `useLibraryStatus()` → Use React Query `isLoading` and `error` directly
  - `useLibraryActions()` → Create React Query mutations for actions
- **Risk**: HIGH - Used by multiple components, central utility

## Files That Reference Library Store (But May Not Use Domain Data)

### 4. **LibraryPage.tsx** (`apps/web/src/features/library/pages/LibraryPage.tsx`)

- **Status**: ✅ Already migrated to React Query
- **Usage**: Uses `useQuery` directly with `getTracks()` API call
- **Line**: 106-114
- **Migration**: Not needed - already using React Query

### 5. **stateHydration.ts** (`apps/web/src/utils/stateHydration.ts`)

- **Usage**: May hydrate library store on app initialization
- **Migration**: Remove library store hydration, React Query handles this
- **Risk**: LOW - Utility function

### 6. **stateInvalidation.ts** (`apps/web/src/utils/stateInvalidation.ts`)

- **Usage**: May invalidate library store after mutations
- **Migration**: Update to invalidate React Query cache instead
- **Risk**: MEDIUM - Used by mutation handlers

### 7. **stores.test.ts** (`apps/web/src/test/stores.test.ts`)

- **Usage**: Tests library store functionality
- **Migration**: Update tests to test React Query hooks instead
- **Risk**: LOW - Test file only

## Migration Strategy

### Phase 1: Update storeSelectors.ts

1. Update `useLibraryItems()` to use React Query hook directly
2. Update `useLibraryFavorites()` to use React Query hook directly
3. Update `useLibraryPagination()` to extract from React Query response
4. Update `useLibraryStatus()` to use React Query status
5. Create React Query mutations for `useLibraryActions()`

### Phase 2: Update Components

1. Update `pages/DashboardPage.tsx` to use updated selectors
2. Update `features/dashboard/pages/DashboardPage.tsx` to use updated selectors
3. Verify no regressions

### Phase 3: Remove Domain Data from Store

1. Remove `items`, `favorites`, `pagination` from `LibraryState`
2. Remove `fetchItems`, `fetchFavorites` actions
3. Keep only `filters` (UI state) and `setFilters` action
4. Remove `isLoading`, `error` (React Query handles these)

### Phase 4: Cleanup

1. Remove `undoRedo` middleware (no domain data to undo)
2. Remove `stateNormalization` utilities (no normalized state)
3. Remove `stateMiddleware` (no domain data to track)
4. Update `stateHydration.ts` to skip library store
5. Update `stateInvalidation.ts` to use React Query invalidation

## Summary Statistics

- **Components using domain data**: 2 (both DashboardPage variants)
- **Utilities using domain data**: 1 (storeSelectors.ts)
- **Components already migrated**: 1 (LibraryPage.tsx)
- **Files needing updates**: 3 (2 components + 1 utility)
- **Risk level**: MEDIUM-HIGH (central utility + active components)

## Next Steps

1. ✅ Create React Query hooks (Action 4.1.2.3.1) - Complete
2. Update `storeSelectors.ts` to use React Query hooks
3. Update DashboardPage components
4. Remove domain data from library store (Action 4.1.2.4)
5. Remove middleware and utilities (Actions 4.1.2.5-4.1.2.7)
