import React from 'react'; import { Meta } from '@storybook/react-vite'; import { subHours } from 'date-fns'; import { z } from 'zod'; import { Chat, Session, SessionsList, SessionGroups, NewSessionButton, SessionMessages, ChatInput, SessionMessagePanel, SessionMessagesHeader, componentCatalog } from 'reachat'; export default { title: 'Demos/ComponentCatalog', component: Chat } as Meta; // --------------------------------------------------------------------------- // Sample components // --------------------------------------------------------------------------- const WeatherCard = ({ city, temperature, condition }: { city: string; temperature: number; condition: string; }) => (
{condition}
{city}
{temperature}°F
); const AlertBox = ({ title, message, severity }: { title: string; message: string; severity: string; }) => { const colors: Record = { info: { bg: '#e3f2fd', border: '#90caf9', text: '#1565c0' }, warning: { bg: '#fff3e0', border: '#ffcc80', text: '#e65100' }, error: { bg: '#ffebee', border: '#ef9a9a', text: '#c62828' }, success: { bg: '#e8f5e9', border: '#a5d6a7', text: '#2e7d32' } }; const c = colors[severity] || colors.info; return (
{title}
{message}
); }; const StatusBadge = ({ label, status }: { label: string; status: string; }) => { const colors: Record = { online: '#22c55e', offline: '#ef4444', away: '#f59e0b', busy: '#ef4444' }; return ( {label} ); }; const DataCard = ({ title, value, unit, trend }: { title: string; value: number; unit?: string; trend?: string; }) => (
{title}
{value} {unit && ( {unit} )}
{trend && (
{trend}
)}
); const RowLayout = ({ children }: { children?: React.ReactNode; }) => (
{children}
); // --------------------------------------------------------------------------- // Catalog // --------------------------------------------------------------------------- const catalog = componentCatalog({ WeatherCard: { description: 'Displays current weather conditions for a city', props: z.object({ city: z.string().describe('City name'), temperature: z.number().describe('Temperature in Fahrenheit'), condition: z.enum(['sunny', 'cloudy', 'rainy']).describe( 'Current weather condition' ) }), component: WeatherCard as any }, AlertBox: { description: 'Displays an alert or notification box', props: z.object({ title: z.string().describe('Alert title'), message: z.string().describe('Alert message body'), severity: z.enum(['info', 'warning', 'error', 'success']).describe( 'Severity level' ) }), component: AlertBox as any }, StatusBadge: { description: 'Displays a status indicator badge', props: z.object({ label: z.string().describe('Display label'), status: z.enum(['online', 'offline', 'away', 'busy']).describe( 'Current status' ) }), component: StatusBadge as any }, DataCard: { description: 'Displays a single metric or KPI', props: z.object({ title: z.string().describe('Metric name'), value: z.number().describe('Metric value'), unit: z.string().optional(), trend: z.string().optional() }), component: DataCard as any }, Row: { description: 'A horizontal flex layout container — use as a parent to lay out child components side-by-side', props: z.object({}), component: RowLayout as any } }); // --------------------------------------------------------------------------- // Session data // --------------------------------------------------------------------------- const weatherSpec = { type: 'WeatherCard', props: { city: 'San Francisco', temperature: 68, condition: 'cloudy' } }; const alertSpec = { type: 'AlertBox', props: { title: 'Deployment Successful', message: 'Version 2.4.1 has been deployed to production. All health checks passing.', severity: 'success' } }; const multiWeatherSpec = [ { type: 'WeatherCard', props: { city: 'San Francisco', temperature: 68, condition: 'cloudy' } }, { type: 'WeatherCard', props: { city: 'New York', temperature: 45, condition: 'rainy' } }, { type: 'WeatherCard', props: { city: 'Miami', temperature: 82, condition: 'sunny' } } ]; const dashboardSpec = { type: 'Row', props: {}, children: [ { type: 'DataCard', props: { title: 'Revenue', value: 12450, unit: '$', trend: '+12.5% from last month' } }, { type: 'DataCard', props: { title: 'Active Users', value: 8423, trend: '+5.2% from last week' } }, { type: 'DataCard', props: { title: 'Error Rate', value: 0.3, unit: '%', trend: '-0.1%' } } ] }; const sessions: Session[] = [ { id: 'session-1', title: 'Dynamic Components Demo', createdAt: subHours(new Date(), 2), updatedAt: new Date(), messages: [ { id: 'msg-1', role: 'user', content: "What's the weather in San Francisco?", createdAt: subHours(new Date(), 2) }, { id: 'msg-2', role: 'assistant', content: `Here's the current weather: \`\`\`component ${JSON.stringify(weatherSpec, null, 2)} \`\`\` Looks like a typical foggy day in the city!`, createdAt: subHours(new Date(), 2) }, { id: 'msg-3', role: 'user', content: 'Show me weather for multiple cities', createdAt: subHours(new Date(), 1.5) }, { id: 'msg-4', role: 'assistant', content: `Here's a comparison of weather across three cities: \`\`\`component ${JSON.stringify(multiWeatherSpec, null, 2)} \`\`\` San Francisco is mild, New York is rainy, and Miami is warm and sunny.`, createdAt: subHours(new Date(), 1.5) }, { id: 'msg-5', role: 'user', content: 'Any deployment updates?', createdAt: subHours(new Date(), 1) }, { id: 'msg-6', role: 'assistant', content: `Yes! Here's the latest update: \`\`\`component ${JSON.stringify(alertSpec, null, 2)} \`\`\` Everything looks good. All services are healthy.`, createdAt: subHours(new Date(), 1) }, { id: 'msg-7', role: 'user', content: 'Show me the dashboard metrics', createdAt: subHours(new Date(), 0.5) }, { id: 'msg-8', role: 'assistant', content: `Here's your dashboard overview with nested components: \`\`\`component ${JSON.stringify(dashboardSpec, null, 2)} \`\`\` Revenue is trending up and error rates are down. Looking great!`, createdAt: subHours(new Date(), 0.5) } ] } ]; const errorSessions: Session[] = [ { id: 'session-errors', title: 'Error Handling Demo', createdAt: subHours(new Date(), 1), updatedAt: new Date(), messages: [ { id: 'msg-err-1', role: 'user', content: 'Show me an unknown component', createdAt: subHours(new Date(), 1) }, { id: 'msg-err-2', role: 'assistant', content: `Here's a component that doesn't exist in the catalog: \`\`\`component { "type": "NonExistentWidget", "props": { "foo": "bar" } } \`\`\` The error should be displayed gracefully above.`, createdAt: subHours(new Date(), 1) }, { id: 'msg-err-3', role: 'user', content: 'Show me invalid JSON', createdAt: subHours(new Date(), 0.5) }, { id: 'msg-err-4', role: 'assistant', content: `Here's some broken JSON: \`\`\`component { type: WeatherCard, props: invalid } \`\`\` The parser should handle this gracefully.`, createdAt: subHours(new Date(), 0.5) }, { id: 'msg-err-5', role: 'user', content: 'Show me invalid props', createdAt: new Date() }, { id: 'msg-err-6', role: 'assistant', content: `Here's a valid component type with wrong props: \`\`\`component { "type": "WeatherCard", "props": { "city": 123, "temperature": "not-a-number", "condition": "unknown" } } \`\`\` Zod validation should catch the prop type mismatches.`, createdAt: new Date() } ] } ]; // --------------------------------------------------------------------------- // Stories // --------------------------------------------------------------------------- const storyStyle = { position: 'absolute' as const, top: 0, left: 0, right: 0, bottom: 0, padding: 20, margin: 20, borderRadius: 5 }; export const BasicExample = () => (
); export const ErrorHandling = () => (
); export const ChatViewOnly = () => (
); export const SystemPromptPreview = () => { const prompt = catalog.systemPrompt(); return (

Generated LLM System Prompt

This is what catalog.systemPrompt() generates. Include this in your LLM system message so it knows how to use your registered components.

        {prompt}
      
); };