2.3 KiB
2.3 KiB
title, tags, aliases
| title | tags | aliases | |||||
|---|---|---|---|---|---|---|---|
| Stores |
|
|
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
export function useLibraryEntries(): Entry[] {
return Array.from(useLibraryStore((s) => s.entries).values());
}
Returns all entries as an array. Used by Statistics, Pages, etc.
Usage Pattern
// 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
App.tsxrenders →useLibrary()hook firesGET /api/v1/entries→ backend returns all entries with relations- Each entry passed through
normalizeEntry()→ Normalizers setEntries(normalized)→ map populated,isLoaded = true- Mutations call
addEntry/updateEntry/removeEntryfor 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/lightclass ondocument.documentElement