# State Management ## Stores Overview Showy uses **Zustand** for synchronous client-side state and **React Query** for all server-side (async) state. The two layers work together: | Store | Scope | Persistence | Purpose | | ----------------- | -------- | -------------- | -------------------------------------------- | | `useLibraryStore` | App-wide | In-memory | Cache of all library entries as a Map | | `useThemeStore` | App-wide | `localStorage` | Light / dark theme preference | | React Query cache | Per-hook | In-memory | Server state caching, refetching, pagination | --- ## `useLibraryStore` **File:** `src/stores/useLibraryStore.ts` Holds the full set of user library entries in a `Map` for O(1) lookups by ID. ### State | Property | Type | Description | | ---------- | -------------------- | --------------------------------- | | `entries` | `Map` | All library entries keyed by ID | | `isLoaded` | `boolean` | Whether the initial fetch has run | ### Actions | Action | Signature | Description | | ------------- | -------------------------------------------- | ------------------------------------------------- | | `setEntries` | `(entries: Entry[]) => void` | Batch-replace all entries, mark `isLoaded = true` | | `addEntry` | `(entry: Entry) => void` | Add a single entry (marks it as `isInLibrary`) | | `updateEntry` | `(id: number, data: Partial) => void` | Merge-update an existing entry | | `removeEntry` | `(id: number) => void` | Delete an entry from the store | ### Derived Selector ```ts useLibraryEntries() // returns Entry[] (array from the Map values) ``` ### Usage Pattern 1. `useLibrary()` hook (in `useEntries.ts`) fetches all entries on mount and calls `setEntries()`. 2. Mutation hooks (`useCreateEntry`, `useDeleteEntry`, etc.) update the store immediately on success. 3. `EntryCard` and other components read from the store to check `isInLibrary` for TMDB entries. --- ## `useThemeStore` **File:** `src/stores/useThemeStore.ts` Manages the light/dark theme and persists the choice to `localStorage` under the key `showy-theme`. ### State | Property | Type | Description | | -------- | ------------------- | -------------------- | | `theme` | `'dark' \| 'light'` | Current active theme | ### Actions | Action | Description | | ------------- | -------------------------------------------------------- | | `setTheme` | Set theme explicitly and apply the CSS class to `` | | `toggleTheme` | Toggle between dark ↔ light | Theme is applied by toggling the `dark` / `light` class on `document.documentElement`, which Tailwind CSS uses for its dark-mode variant. --- ## React Query Conventions | Convention | Pattern | | -------------------- | ----------------------------------------------------------------------- | | **Query keys** | Domain-scoped arrays: `['entries']`, `['entry', id]`, `['collections']` | | **Stale time** | 2–10 minutes depending on volatility | | **Error handling** | 404 responses return empty arrays/objects as fallback | | **Enabled flag** | Conditional fetching via `enabled: !!id` for dependent queries | | **Infinite queries** | Used for TMDB pagination (trending, popular, search) | | **Invalidation** | Mutations invalidate related query keys on success |