--- title: Stores tags: - frontend - zustand - state-management aliases: - State Management - Zustand --- # Stores Zustand stores in `react_frontend/src/stores/`. Three stores managing client-side state. --- ## useLibraryStore > [!info] File: `stores/useLibraryStore.ts` The core application store. Holds **all library entries** in a `Map` for O(1) lookups. ### State | Field | Type | Description | |---|---|---| | `entries` | `Map` | All entries keyed by ID | | `isLoaded` | `boolean` | True after initial fetch completes | ### Actions | Action | Signature | Description | |---|---|---| | `setEntries` | `(entries: Entry[]) => void` | Bulk load entries (clears + rebuilds map) | | `addEntry` | `(entry: Entry) => void` | Add single entry | | `updateEntry` | `(id: number, data: Partial) => void` | Partial update | | `removeEntry` | `(id: number) => void` | Delete entry from store | ### Helper Hook ```typescript export function useLibraryEntries(): Entry[] { return Array.from(useLibraryStore((s) => s.entries).values()); } ``` Returns all entries as an array. Used by [[Statistics|LibraryGrowthChart]], [[Pages|LibraryPage]], etc. ### Usage Pattern ```typescript // Read single entry const entry = useLibraryStore((s) => s.entries.get(entryId)); // Read title (used in breadcrumbs) const title = useLibraryStore((s) => s.entries.get(id)?.title); // Check if library is loaded const isLoaded = useLibraryStore((s) => s.isLoaded); ``` ### Population Flow 1. `App.tsx` renders → `useLibrary()` hook fires 2. `GET /api/v1/entries` → backend returns all entries with relations 3. Each entry passed through `normalizeEntry()` → [[Normalizers]] 4. `setEntries(normalized)` → map populated, `isLoaded = true` 5. Mutations call `addEntry`/`updateEntry`/`removeEntry` for optimistic updates --- ## useThemeStore > [!info] File: `stores/useThemeStore.ts` Theme toggle with localStorage persistence. ### State | Field | Type | Description | |---|---|---| | `theme` | `'dark' \| 'light'` | Current theme | ### Actions | Action | Description | |---|---| | `toggleTheme()` | Switches between dark/light | | `setTheme(theme)` | Set specific theme | ### Persistence Uses Zustand `persist` middleware: - Storage key: `showy-theme` - Stored in `localStorage` - On change: toggles `dark`/`light` class on `document.documentElement` --- ## useCommandPaletteStore > [!info] File: `stores/useCommandPaletteStore.ts` Manages [[Components#Command Palette|CommandPalette]] open/close state and recently viewed history. ### State | Field | Type | Description | |---|---|---| | `isOpen` | `boolean` | Whether the palette is currently visible | | `recentlyViewed` | `RecentItem[]` | Up to 10 recently visited entries (library + TMDB-only) | ### RecentItem Shape ```typescript interface RecentItem { id: string; // "lib-{id}" or "tmdb-{tmdbId}" title: string; type: 'movie' | 'tv'; poster_path: string | null; libraryId?: number; // set for library entries tmdbId?: number; // set for TMDB or library entries } ``` ### Actions | Action | Description | |---|---| | `open()` | Open the palette | | `close()` | Close the palette | | `toggle()` | Toggle open/close | | `addRecentlyViewed(item)` | Prepend to recents list (deduped by `id`, capped at 10) | ### Persistence `recentlyViewed` is read from and written to `localStorage` key `showy-recent-viewed` on every `addRecentlyViewed` call (no Zustand persist middleware — manual). --- ## useLibraryFiltersStore > [!info] File: `stores/useLibraryFiltersStore.ts` Persists [[Library|LibraryPage]] filter/search/sort state across navigation so filters survive when navigating to entry details and back. ### State | Field | Type | Description | |---|---|---| | `activeTab` | `FilterTab` | `'all' \| 'movies' \| 'series' \| 'watchlist'` | | `search` | `string` | Search query | | `sortField` | `SortField` | `'title' \| 'date_added' \| 'release_date' \| 'rating'` | | `sortDir` | `SortDir` | `'asc' \| 'desc'` | | `filters` | `Filters` | `{ genre: string; status: string; year: string }` | ### Actions | Action | Description | |---|---| | `setActiveTab(tab)` | Set active tab filter | | `setSearch(query)` | Set search query | | `setSortField(field)` | Set sort field | | `setSortDir(dir)` | Set sort direction | | `setFilters(filters)` | Set genre/status/year filters | | `clearFilters()` | Reset all filters to defaults |