Updated the Obsidian docs-vault
This commit is contained in:
1 parent
72fd77ee01
commit
260282a0e2
36 files changed
+3497
-144
No files matched your search
@@ -0,0 +1,98 @@
|
||||
---
|
||||
title: Stores
|
||||
tags:
|
||||
- frontend
|
||||
- zustand
|
||||
- state-management
|
||||
aliases:
|
||||
- State Management
|
||||
- Zustand
|
||||
---
|
||||
|
||||
# Stores
|
||||
|
||||
Zustand stores in `react_frontend/src/stores/`. Two stores managing client-side state.
|
||||
|
||||
---
|
||||
|
||||
## useLibraryStore
|
||||
|
||||
> [!info] File: `stores/useLibraryStore.ts`
|
||||
|
||||
The core application store. Holds **all library entries** in a `Map<number, Entry>` for O(1) lookups.
|
||||
|
||||
### State
|
||||
|
||||
| Field | Type | Description |
|
||||
|---|---|---|
|
||||
| `entries` | `Map<number, Entry>` | 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<Entry>) => 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`
|
||||
Reference in new issue
Block a user