# State Architect

> Zustand store specialist - handles store generation, sync patterns, selectors, and persistence for totalaud.io state management.

- Skill: `majiayu000/state-architect` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds add majiayu000/state-architect`
- Raw SKILL.md: https://api.skillmd.com/api/skills/majiayu000/state-architect/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: majiayu000 (https://skillmd.com/u/majiayu000)
- Updated: 2026-09-09
- Page: https://skillmd.com/skills/majiayu000/state-architect

---


# State Architect

Technical specialist for Zustand state management in totalaud.io.

## Core Responsibility

Design and maintain consistent, performant state management across all workspace modes.

## Key Files

- `apps/aud-web/src/stores/useIdeasStore.ts` - Ideas Mode store
- `apps/aud-web/src/stores/useScoutStore.ts` - Scout Mode store
- `apps/aud-web/src/stores/useTimelineStore.ts` - Timeline Mode store
- `apps/aud-web/src/stores/usePitchStore.ts` - Pitch Mode store

## Expertise Areas

### Store Pattern Template

```typescript
import { create } from 'zustand'
import { persist, createJSONStorage } from 'zustand/middleware'
import { immer } from 'zustand/middleware/immer'

interface EntityState {
  // Data
  entities: Entity[]
  selectedId: string | null

  // UI State
  view: 'canvas' | 'list'
  filters: FilterState
  isLoading: boolean
  error: string | null

  // Sync State
  isSyncing: boolean
  lastSyncedAt: Date | null
  pendingChanges: Change[]

  // Actions
  addEntity: (entity: Entity) => void
  updateEntity: (id: string, updates: Partial<Entity>) => void
  removeEntity: (id: string) => void
  setView: (view: 'canvas' | 'list') => void
  sync: () => Promise<void>
}

export const useEntityStore = create<EntityState>()(
  persist(
    immer((set, get) => ({
      // Initial state
      entities: [],
      selectedId: null,
      view: 'list',
      filters: defaultFilters,
      isLoading: false,
      error: null,
      isSyncing: false,
      lastSyncedAt: null,
      pendingChanges: [],

      // Actions with immer
      addEntity: (entity) => set((state) => {
        state.entities.push(entity)
        state.pendingChanges.push({ type: 'add', entity })
      }),

      updateEntity: (id, updates) => set((state) => {
        const index = state.entities.findIndex(e => e.id === id)
        if (index !== -1) {
          Object.assign(state.entities[index], updates)
          state.pendingChanges.push({ type: 'update', id, updates })
        }
      }),

      removeEntity: (id) => set((state) => {
        state.entities = state.entities.filter(e => e.id !== id)
        state.pendingChanges.push({ type: 'remove', id })
      }),

      // Sync with Supabase
      sync: async () => {
        const { pendingChanges } = get()
        if (pendingChanges.length === 0) return

        set({ isSyncing: true })
        try {
          await syncToSupabase(pendingChanges)
          set({
            pendingChanges: [],
            lastSyncedAt: new Date(),
            isSyncing: false
          })
        } catch (error) {
          set({ error: error.message, isSyncing: false })
        }
      }
    })),
    {
      name: 'entity-storage',
      storage: createJSONStorage(() => localStorage),
      partialize: (state) => ({
        entities: state.entities,
        view: state.view,
        filters: state.filters
      })
    }
  )
)
```

### Selector Patterns

```typescript
// Memoised selectors for performance
export const useFilteredEntities = () => useEntityStore(
  (state) => state.entities.filter(e => matchesFilters(e, state.filters)),
  shallow
)

export const useEntityById = (id: string) => useEntityStore(
  (state) => state.entities.find(e => e.id === id)
)

export const useEntityCount = () => useEntityStore(
  (state) => state.entities.length
)
```

### Sync Patterns

**Optimistic Updates**:
1. Update local state immediately
2. Queue change for sync
3. Sync in background (debounced)
4. Rollback on error

**Debounced Sync**:
```typescript
const debouncedSync = useDebouncedCallback(
  () => store.sync(),
  2000 // 2 second debounce
)
```

### Persistence Migration

```typescript
{
  name: 'entity-storage',
  version: 2, // Increment on schema change
  migrate: (persistedState, version) => {
    if (version === 1) {
      // Migrate from v1 to v2
      return {
        ...persistedState,
        newField: defaultValue
      }
    }
    return persistedState
  }
}
```

## Common Tasks

### Create New Store

1. Copy template pattern
2. Define entity type
3. Add actions for CRUD
4. Configure persistence
5. Add Supabase sync
6. Write selector hooks
7. Add tests

### Add Filter to Store

1. Extend FilterState type
2. Add filter action
3. Update selector logic
4. Persist filter state
5. Test filter behaviour

### Debug Sync Issues

1. Check pendingChanges queue
2. Verify Supabase connection
3. Log sync attempts
4. Check error state
5. Test rollback behaviour

## Integration Points

- **Supabase Engineer**: Database sync
- **Route Builder**: API state hydration
- **Quality Lead**: Store tests
- **Dan**: Store generation tasks

## Success Metrics

- State updates feel instant (<50ms)
- Sync doesn't block UI
- Persistence survives refresh
- No memory leaks
- 80%+ boilerplate reduction via patterns

## Voice

- Technical but clear
- Pattern-focused
- Performance-aware
- British spelling throughout

