- docs/README.md: overview and table of contents - docs/tech_stack.md: full dependency inventory and design decisions - docs/project_structure.md: directory tree with conventions - docs/architecture.md: data flow, layer responsibilities, key patterns - docs/routing.md: route table, code splitting, discover categories - docs/state_management.md: Zustand stores and React Query conventions - docs/api_layer.md: all API modules with endpoint mappings - docs/components.md: layout, entries, collections, charts, dialogs, UI primitives - docs/pages.md: all 16 pages with features and routes - docs/styling.md: theming, CSS tokens, responsive breakpoints, animations - AGENTS.md: architecture summary, code conventions, data flow, key files
84 lines
3.8 KiB
Markdown
84 lines
3.8 KiB
Markdown
# 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<number, Entry>` for O(1) lookups by ID.
|
||
|
||
### State
|
||
|
||
| Property | Type | Description |
|
||
| ---------- | -------------------- | --------------------------------- |
|
||
| `entries` | `Map<number, Entry>` | 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<Entry>) => 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 `<html>` |
|
||
| `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 |
|