Files
Showy/docs-vault/Stores.md
T

2.3 KiB

title, tags, aliases
title tags aliases
Stores
frontend
zustand
state-management
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

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

  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