State

From vultisig/vultisig-windows · View interactive version

State is an AI agent skill from the GitHub repository vultisig/vultisig-windows. Guide for state management patterns in this codebase. Use when creating state providers, value providers, persistent state hooks, validating stored values, deciding how components should access state, or choosing storage (desktop vs extension).

How to use this skill

Skill file

State Management Patterns

Utilities live in @lib/ui/state/. Persistent state hooks and storage are in clients/desktop/src/ (desktop) or @lib/extension/storage (extension).

State Provider (Mutable)

setupStateProvider creates a React Context with internal useState. Returns [Provider, useHook] where the hook gives [value, setValue].

import { setupStateProvider } from '@lib/ui/state/setupStateProvider'

// With default initial value -- Provider's initialValue prop is optional
export const [AmountProvider, useAmount] = setupStateProvider<number | null>('amount', null)

// Without default -- Provider must receive initialValue prop
export const [ReceiverProvider, useReceiver] = setupStateProvider<string>('SendReceiver')

Value Provider (Read-only)

setupValueProvider creates a Context where the Provider accepts a value prop (no internal state). The hook returns the value directly.

import { setupValueProvider } from '@lib/ui/state/setupValueProvider'

export const [CurrencyProvider, useCurrency] = setupValueProvider<Currency>('currency')

// Usage: const currency = useCurrency()

Use when the value is computed or fetched elsewhere and just needs to be distributed down the tree.

Persistent State (Desktop Only)

createPersistentStateHook creates a hook backed by LocalStorage (browser) or TemporaryStorage (SSR). Uses useSyncExternalStore for synchronization and supports cross-tab sync via storage events.

The desktop app defines its key enum and hook in clients/desktop/src/state/persistentState.ts:

import { createPersistentStateHook } from '../lib/ui/state/createPersistentStateHook'
import { LocalStorage } from '../lib/ui/state/LocalStorage'
import { TemporaryStorage } from '../lib/ui/state/TemporaryStorage'

export enum PersistentStateKey {
  VaultCreationMpcLib = 'vaultCreationMpcLib-v2',
}

const Storage = typeof window !== 'undefined' ? LocalStorage : TemporaryStorage
export const persistentStorage = new Storage()
export const usePersistentState =
  createPersistentStateHook<PersistentStateKey>(persistentStorage)

Usage follows the same [value, setValue] pattern. Define domain-specific hooks that call usePersistentState with a key and default:

export const useVaultCreationMpcLib = () => {
  return usePersistentState<MpcLib>(
    PersistentStateKey.VaultCreationMpcLib,
    defaultMpcLib,
    validateOneOf(mpcLibOptions)
  )
}

Storage by Client: Desktop vs Extension

Desktop app uses two persistence mechanisms:

  1. UI/session persistence — createPersistentStateHook with LocalStorage (or TemporaryStorage when typeof window === 'undefined'). Same process, cross-tab sync via storage events. Use for preferences and small UI state that should survive reload.
  2. Domain data — Wails/Go backend (wailsjs/go/storage/Store) with SQL-backed store for vaults, coins, address book, vault folders. Use for user data and anything that must be durable and queryable.

Extension does not use createPersistentStateHook. It uses Chrome storage:

When adding new persisted state:

Persistent State Validation

An optional third parameter to usePersistentState validates the stored value on the first read. This protects against stale data after code changes (e.g., a union type gains/loses members).

The validate function has three outcomes:

For union types, use validateOneOf from @lib/utils/array/isOneOf:

import { validateOneOf } from '@lib/utils/array/isOneOf'

export const useVaultCreationMpcLib = () => {
  return usePersistentState<MpcLib>(
    PersistentStateKey.VaultCreationMpcLib,
    defaultMpcLib,
    validateOneOf(mpcLibOptions)
  )
}

validateOneOf(options) returns a function that throws if the value is not a member of options. The hook catches the error via attempt and falls back to initialValue.

For compound objects with constrained fields, write a custom validate function that returns a corrected object or undefined, or throws to force fallback.

Only needed for constrained types (unions, enums). Types like boolean, number | null, string[], or free-form strings often don't need validation.

When the default value comes from a const array, use constArray[0] (or the appropriate element) instead of repeating the literal so the default stays in sync. See the derive-union-from-const skill.

Component Autonomy

Domain-specific components should access state directly via hooks. Never pass props that a component can obtain itself.

// Good — component gets its own state
export const VaultHeader = () => {
  const [vaultId] = useVaultId()
  return <Header title={vaultId} />
}

export const VaultPage = () => <VaultHeader />
// Bad — unnecessary prop drilling
export const VaultHeader = ({ vaultId }: { vaultId: string }) => {
  return <Header title={vaultId} />
}

Only pass props to reusable/generic components that are used in multiple contexts and shouldn't know about domain logic (e.g. Button, Input, Modal).

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.