"use client"; import { useState } from "react"; import { useAui, AuiProvider, useAuiState, useAuiEvent, } from "@assistant-ui/store"; import { FooList, FooListResource } from "./store/foo-store"; /** * Single Foo component - displays and allows editing a single foo */ const Foo = () => { const aui = useAui(); const fooId = useAuiState((s) => s.foo.id); const fooBar = useAuiState((s) => s.foo.bar); // Each foo logs its own events - only receives events from THIS foo instance useAuiEvent("foo.updated", (payload) => { console.log(`[${fooId}] Updated to: ${payload.newValue}`); }); const handleUpdate = () => { aui.foo().updateBar(`Updated at ${new Date().toLocaleTimeString()}`); }; return (
ID: {fooId}
Value: {fooBar}
); }; const FooListLength = () => { const fooListLength = useAuiState((s) => s.fooList.foos.length); return ( ({fooListLength} items) ); }; const AddFooButton = () => { const aui = useAui(); return ( ); }; type EventLogEntry = { id: number; event: string; payload: unknown; timestamp: Date; }; let idCounter = 0; /** * EventLog component - demonstrates event subscription */ const EventLog = () => { const [logs, setLogs] = useState([]); // Subscribe to all events using the wildcard selector useAuiEvent("*", (data) => { setLogs((prev) => [ { id: ++idCounter, event: data.event, payload: data.payload, timestamp: new Date(), }, ...prev.slice(0, 9), // Keep last 10 entries ]); }); return (

Event Log

{logs.length === 0 ? (

No events yet. Try updating or deleting a foo.

) : ( logs.map((log) => (
{log.event} {JSON.stringify(log.payload)} {log.timestamp.toLocaleTimeString()}
)) )}
); }; /** * Example App - demonstrates the store with styled components * * Note: The fooList scope is also registered in foo-scope.ts as a default, * but we're explicitly passing it here for clarity in the example. */ export const ExampleApp = () => { const aui = useAui({ fooList: FooListResource({ initialValues: true }), }); return (

Foo List

Each item is rendered in its own FooProvider with scoped access

{() => }
); };