Central state management package for Fluid Buddha Design System
npm install @buddhacognitivelab/state-managerCentral state management package for the Fluid Buddha Design System. Provides a type-safe, performant, and predictable state management solution using Zustand with Immer for immutable updates.
- šŖ Centralized Store: Single source of truth for application state
- š Immutable Updates: Built-in Immer integration for safe state mutations
- šÆ Type Safety: Full TypeScript support with strict typing
- šØ Theme Management: Dynamic theme and skin switching
- š Spatial Layout: 3D zone management and camera controls
- š¤ Voice Engine: Voice recognition and synthesis state
- š¤ AI Integration: AI processing and conversation management
- š§ DevTools: Redux DevTools integration for debugging
- ā” Performance: Selective subscriptions to prevent unnecessary re-renders
``bash`
npm install @fluid-buddha/state-manageror
yarn add @fluid-buddha/state-manager
`typescript
import { useFluidBuddhaStore } from '@fluid-buddha/state-manager';
function MyComponent() {
// Subscribe to specific state (prevents unnecessary re-renders)
const activeTheme = useFluidBuddhaStore(state => state.activeTheme);
const setTheme = useFluidBuddhaStore(state => state.theme.setTheme);
return (
Current theme: {activeTheme?.name}
$3
`typescript
function VoiceStatus() {
const { engineStatus, isListening, isMuted } = useFluidBuddhaStore(state => ({
engineStatus: state.engineStatus,
isListening: state.isListening,
isMuted: state.isMuted
}));
return (
Status: {engineStatus}
Listening: {isListening ? 'Yes' : 'No'}
Muted: {isMuted ? 'Yes' : 'No'}
);
}
`$3
`typescript
function ActionButtons() {
// Get actions without subscribing to state changes
const actions = useFluidBuddhaStoreActions();
return (
);
}
`State Structure
The store is organized into four main slices:
$3
`typescript
interface ThemeState {
activeTheme: Theme | null;
activeSkin: Skin | null;
}interface ThemeActions {
setTheme: (theme: Theme) => void;
setSkin: (skin: Skin) => void;
clearTheme: () => void;
}
`$3
`typescript
interface SpatialState {
layout: { zones: ZoneConfig[] } | null;
activeZoneId: string | null;
camera: {
position: [number, number, number];
target: [number, number, number];
};
}interface SpatialActions {
setLayout: (layout: { zones: ZoneConfig[] }) => void;
setActiveZone: (zoneId: string) => void;
updateCamera: (position: [number, number, number], target: [number, number, number]) => void;
createZone: (zone: Omit) => string;
updateZone: (zoneId: string, updates: Partial>) => void;
deleteZone: (zoneId: string) => void;
// ... more actions
}
`$3
`typescript
interface VoiceState {
engineStatus: 'IDLE' | 'INITIALIZING' | 'LISTENING' | 'PROCESSING' | 'SYNTHESIZING' | 'ERROR';
isInitialized: boolean;
isMuted: boolean;
isListening: boolean;
vad: VoiceActivityDetection;
currentTranscript: {
text: string;
confidence: number;
isFinal: boolean;
timestamp: number;
};
// ... more properties
}interface VoiceActions {
initializeEngine: (config?: Partial) => Promise;
startListening: () => void;
stopListening: () => void;
toggleMute: () => void;
updateTranscript: (text: string, confidence: number, isFinal: boolean) => void;
// ... more actions
}
`$3
`typescript
interface AIState {
isProcessing: boolean;
lastResponse: string | null;
conversationHistory: Array<{
id: string;
role: 'user' | 'assistant';
content: string;
timestamp: number;
}>;
error: string | null;
}interface AIActions {
setProcessing: (processing: boolean) => void;
setLastResponse: (response: string) => void;
addToHistory: (role: 'user' | 'assistant', content: string) => void;
clearHistory: () => void;
setError: (error: string | null) => void;
}
`Advanced Usage
$3
`typescript
import { createFluidBuddhaStore } from '@fluid-buddha/state-manager';// Create a custom store instance
const customStore = createFluidBuddhaStore();
// Use with custom hook
function useCustomStore(selector: (state: FluidBuddhaStore) => T): T {
return useStore(customStore, selector);
}
`$3
`typescript
import { store } from '@fluid-buddha/state-manager';// Get current state
const currentState = store.getState();
// Subscribe to changes
const unsubscribe = store.subscribe((state) => {
console.log('State changed:', state);
});
// Update state
store.getState().theme.setTheme(newTheme);
// Cleanup
unsubscribe();
`$3
`typescript
import { withImmer, createImmerUpdater } from '@fluid-buddha/state-manager';// Use Immer utilities for complex state updates
const updateComplexState = createImmerUpdater((draft, payload) => {
draft.someNestedProperty.value = payload.newValue;
draft.anotherProperty.push(payload.newItem);
});
`Performance Considerations
$3
`typescript
// ā
Good - Only re-renders when activeTheme changes
const activeTheme = useFluidBuddhaStore(state => state.activeTheme);// ā Bad - Re-renders on any state change
const state = useFluidBuddhaStoreState();
const activeTheme = state.activeTheme;
`$3
`typescript
import { useMemo } from 'react';function ExpensiveComponent() {
const expensiveValue = useFluidBuddhaStore(
useMemo(
() => (state) => {
// Expensive computation
return state.zones.filter(zone => zone.isActive).length;
},
[]
)
);
return
Active zones: {expensiveValue};
}
`Development
$3
`bash
npm run build
`$3
`bash
npm test
npm run test:watch
`$3
`bash
npm run type-check
`$3
`bash
npm run lint
`Architecture
The state manager follows a slice-based architecture where each domain (theme, spatial, voice, AI) is managed in its own slice. All slices are combined into a single store with middleware for development tools and immutable updates.
`
@fluid-buddha/state-manager/
āāā src/
ā āāā store/
ā ā āāā createStore.ts # Main store creation
ā ā āāā types.ts # Type definitions
ā āāā slices/
ā ā āāā themeSlice.ts # Theme state management
ā ā āāā spatialSlice.ts # Spatial layout management
ā ā āāā voiceSlice.ts # Voice engine management
ā ā āāā aiSlice.ts # AI processing management
ā āāā hooks/
ā ā āāā useFluidBuddhaStore.ts # React hooks
ā āāā utils/
ā ā āāā immerUtils.ts # Immer utilities
ā āāā constants.ts # Default values
``- zustand: Core state management library
- immer: Immutable state updates
- react: Peer dependency for hooks
MIT License - see LICENSE file for details.
See the main project README for contribution guidelines.