'use client'; import { useState, useCallback } from 'react'; import { DataProvider, ActionProvider, VisibilityProvider, useUIStream, Renderer, } from '@json-render/react'; import { componentRegistry } from '@/components/ui-components'; // Sample data for the dashboard const INITIAL_DATA = { analytics: { revenue: 125000, growth: 0.15, customers: 1234, orders: 567, salesByRegion: [ { label: 'US', value: 45000 }, { label: 'EU', value: 35000 }, { label: 'Asia', value: 28000 }, { label: 'Other', value: 17000 }, ], recentTransactions: [ { id: 'TXN001', customer: 'Acme Corp', amount: 1500, status: 'completed', date: '2024-01-15' }, { id: 'TXN002', customer: 'Globex Inc', amount: 2300, status: 'pending', date: '2024-01-14' }, { id: 'TXN003', customer: 'Initech', amount: 890, status: 'completed', date: '2024-01-13' }, { id: 'TXN004', customer: 'Umbrella Co', amount: 4200, status: 'completed', date: '2024-01-12' }, ], }, form: { dateRange: '', region: '', }, }; // Action handlers const ACTION_HANDLERS = { export_report: () => { alert('Exporting report... (demo)'); }, refresh_data: () => { alert('Refreshing data... (demo)'); }, view_details: (params: Record) => { alert(`Viewing details for: ${JSON.stringify(params)}`); }, apply_filter: () => { alert('Applying filters... (demo)'); }, }; function DashboardContent() { const [prompt, setPrompt] = useState(''); const { tree, isStreaming, error, send, clear, } = useUIStream({ api: '/api/generate', onError: (err) => console.error('Generation error:', err), }); const handleSubmit = useCallback( async (e: React.FormEvent) => { e.preventDefault(); if (!prompt.trim()) return; await send(prompt, { data: INITIAL_DATA }); }, [prompt, send] ); const examplePrompts = [ 'Show me a revenue dashboard with key metrics and a chart of sales by region', 'Create a transactions table with recent orders', 'Build a widget showing customer count with a trend indicator', 'Make a simple card with total revenue and an export button', ]; const hasElements = tree && Object.keys(tree.elements).length > 0; return (
{/* Header */}

json-render Dashboard Demo

AI-powered widget generation with guardrails. Only catalog components can be generated.

{/* Input Form */}
setPrompt(e.target.value)} placeholder="Describe the widget you want to create..." style={{ flex: 1, padding: '12px 16px', borderRadius: '8px', border: '1px solid #d1d5db', fontSize: '15px', outline: 'none', }} disabled={isStreaming} />
{/* Example Prompts */}
Try: {examplePrompts.map((example, i) => ( ))}
{/* Error Display */} {error && (
Error: {error.message}
)} {/* Generated UI */}
{!hasElements && !isStreaming ? (
🎨

Enter a prompt above to generate a dashboard widget

) : tree ? ( ) : null}
{/* Debug: Show generated JSON */} {hasElements && (
View Generated JSON
            {JSON.stringify(tree, null, 2)}
          
)} {/* Info Box */}

How json-render Works

  • Guardrails: AI can only generate components from the catalog (Card, Metric, Chart, etc.)
  • Data Binding: Components reference data paths like{' '} /analytics/revenue
  • Named Actions: Buttons declare actions like{' '} export_report - you control what they do
  • Your Design System: The catalog maps to your actual React components
); } export default function DashboardPage() { return ( ); }