|
| 1 | +import { useState, useEffect } from 'react'; |
| 2 | +import { SchemaRenderer } from '@object-ui/react'; |
| 3 | +import '@object-ui/components'; |
| 4 | +import { examples, exampleCategories, ExampleKey } from './data/examples'; |
| 5 | +import { Monitor, Tablet, Smartphone, Copy, Check, Code2 } from 'lucide-react'; |
| 6 | + |
| 7 | +type ViewportSize = 'desktop' | 'tablet' | 'mobile'; |
| 8 | + |
| 9 | +export default function Playground() { |
| 10 | + const [selectedExample, setSelectedExample] = useState<ExampleKey>('dashboard'); |
| 11 | + const [code, setCode] = useState(examples['dashboard']); |
| 12 | + const [schema, setSchema] = useState<any>(null); |
| 13 | + const [jsonError, setJsonError] = useState<string | null>(null); |
| 14 | + const [viewportSize, setViewportSize] = useState<ViewportSize>('desktop'); |
| 15 | + const [copied, setCopied] = useState(false); |
| 16 | + |
| 17 | + // Real-time JSON parsing |
| 18 | + useEffect(() => { |
| 19 | + try { |
| 20 | + const parsed = JSON.parse(code); |
| 21 | + setSchema(parsed); |
| 22 | + setJsonError(null); |
| 23 | + } catch (e) { |
| 24 | + setJsonError((e as Error).message); |
| 25 | + // Keep previous schema on error |
| 26 | + } |
| 27 | + }, [code]); |
| 28 | + |
| 29 | + const handleExampleChange = (key: ExampleKey) => { |
| 30 | + setSelectedExample(key); |
| 31 | + setCode(examples[key]); |
| 32 | + }; |
| 33 | + |
| 34 | + const handleCopySchema = async () => { |
| 35 | + try { |
| 36 | + await navigator.clipboard.writeText(code); |
| 37 | + setCopied(true); |
| 38 | + setTimeout(() => setCopied(false), 2000); |
| 39 | + } catch (err) { |
| 40 | + console.error('Failed to copy:', err); |
| 41 | + } |
| 42 | + }; |
| 43 | + |
| 44 | + const viewportStyles: Record<ViewportSize, string> = { |
| 45 | + desktop: 'w-full', |
| 46 | + tablet: 'max-w-3xl mx-auto', |
| 47 | + mobile: 'max-w-md mx-auto' |
| 48 | + }; |
| 49 | + |
| 50 | + return ( |
| 51 | + <div className="flex h-screen w-screen bg-background"> |
| 52 | + {/* 1. Sidebar: Example Selector */} |
| 53 | + <aside className="w-64 bg-muted/30 border-r overflow-auto"> |
| 54 | + <div className="p-6 border-b"> |
| 55 | + <h1 className="text-xl font-bold">Object UI</h1> |
| 56 | + <p className="text-sm text-muted-foreground mt-1">Live Playground</p> |
| 57 | + </div> |
| 58 | + |
| 59 | + <div className="p-4 space-y-6"> |
| 60 | + {Object.entries(exampleCategories).map(([category, keys]) => ( |
| 61 | + <div key={category}> |
| 62 | + <h2 className="text-xs font-semibold text-muted-foreground uppercase tracking-wider mb-2"> |
| 63 | + {category} |
| 64 | + </h2> |
| 65 | + <div className="space-y-1"> |
| 66 | + {keys.map((key) => ( |
| 67 | + <button |
| 68 | + key={key} |
| 69 | + onClick={() => handleExampleChange(key as ExampleKey)} |
| 70 | + className={` |
| 71 | + block w-full text-left px-3 py-2 rounded-md text-sm transition-colors |
| 72 | + ${key === selectedExample |
| 73 | + ? 'bg-primary text-primary-foreground font-medium' |
| 74 | + : 'hover:bg-muted text-foreground' |
| 75 | + } |
| 76 | + `} |
| 77 | + > |
| 78 | + {key.split('-').map(word => word.charAt(0).toUpperCase() + word.slice(1)).join(' ')} |
| 79 | + </button> |
| 80 | + ))} |
| 81 | + </div> |
| 82 | + </div> |
| 83 | + ))} |
| 84 | + </div> |
| 85 | + </aside> |
| 86 | + |
| 87 | + {/* 2. Middle: Code Editor */} |
| 88 | + <div className="w-1/2 h-full border-r flex flex-col"> |
| 89 | + <div className="border-b px-4 py-3 bg-muted/20 flex items-center justify-between"> |
| 90 | + <div className="flex items-center gap-2"> |
| 91 | + <Code2 className="h-4 w-4 text-muted-foreground" /> |
| 92 | + <h2 className="text-sm font-semibold">JSON Schema</h2> |
| 93 | + </div> |
| 94 | + <button |
| 95 | + onClick={handleCopySchema} |
| 96 | + className="flex items-center gap-2 px-3 py-1.5 text-xs rounded-md bg-background hover:bg-muted transition-colors border" |
| 97 | + > |
| 98 | + {copied ? ( |
| 99 | + <> |
| 100 | + <Check className="h-3 w-3" /> |
| 101 | + Copied! |
| 102 | + </> |
| 103 | + ) : ( |
| 104 | + <> |
| 105 | + <Copy className="h-3 w-3" /> |
| 106 | + Copy Schema |
| 107 | + </> |
| 108 | + )} |
| 109 | + </button> |
| 110 | + </div> |
| 111 | + |
| 112 | + {jsonError && ( |
| 113 | + <div className="px-4 py-2 bg-red-50 border-b border-red-200 text-red-700 text-sm"> |
| 114 | + <strong>JSON Error:</strong> {jsonError} |
| 115 | + </div> |
| 116 | + )} |
| 117 | + |
| 118 | + <div className="flex-1 overflow-hidden"> |
| 119 | + <textarea |
| 120 | + value={code} |
| 121 | + onChange={(e) => setCode(e.target.value)} |
| 122 | + className="w-full h-full p-4 font-mono text-sm resize-none focus:outline-none border-0 bg-background" |
| 123 | + spellCheck={false} |
| 124 | + style={{ |
| 125 | + tabSize: 2, |
| 126 | + lineHeight: '1.6' |
| 127 | + }} |
| 128 | + /> |
| 129 | + </div> |
| 130 | + </div> |
| 131 | + |
| 132 | + {/* 3. Right: Live Preview */} |
| 133 | + <div className="w-1/2 h-full flex flex-col bg-muted/10"> |
| 134 | + <div className="border-b px-4 py-3 bg-muted/20 flex items-center justify-between"> |
| 135 | + <h2 className="text-sm font-semibold">Preview</h2> |
| 136 | + |
| 137 | + {/* Viewport Size Toggles */} |
| 138 | + <div className="flex items-center gap-1 bg-background rounded-md p-1 border"> |
| 139 | + <button |
| 140 | + onClick={() => setViewportSize('desktop')} |
| 141 | + className={`p-1.5 rounded transition-colors ${ |
| 142 | + viewportSize === 'desktop' ? 'bg-primary text-primary-foreground' : 'hover:bg-muted' |
| 143 | + }`} |
| 144 | + title="Desktop View" |
| 145 | + > |
| 146 | + <Monitor className="h-4 w-4" /> |
| 147 | + </button> |
| 148 | + <button |
| 149 | + onClick={() => setViewportSize('tablet')} |
| 150 | + className={`p-1.5 rounded transition-colors ${ |
| 151 | + viewportSize === 'tablet' ? 'bg-primary text-primary-foreground' : 'hover:bg-muted' |
| 152 | + }`} |
| 153 | + title="Tablet View" |
| 154 | + > |
| 155 | + <Tablet className="h-4 w-4" /> |
| 156 | + </button> |
| 157 | + <button |
| 158 | + onClick={() => setViewportSize('mobile')} |
| 159 | + className={`p-1.5 rounded transition-colors ${ |
| 160 | + viewportSize === 'mobile' ? 'bg-primary text-primary-foreground' : 'hover:bg-muted' |
| 161 | + }`} |
| 162 | + title="Mobile View" |
| 163 | + > |
| 164 | + <Smartphone className="h-4 w-4" /> |
| 165 | + </button> |
| 166 | + </div> |
| 167 | + </div> |
| 168 | + |
| 169 | + <div className="flex-1 overflow-auto p-8"> |
| 170 | + <div className={`${viewportStyles[viewportSize]} transition-all duration-300`}> |
| 171 | + <div className="rounded-lg border shadow-sm bg-background p-6"> |
| 172 | + {schema && !jsonError ? ( |
| 173 | + <SchemaRenderer schema={schema} /> |
| 174 | + ) : ( |
| 175 | + <div className="text-center py-12 text-muted-foreground"> |
| 176 | + {jsonError ? ( |
| 177 | + <div className="space-y-2"> |
| 178 | + <p className="text-red-500 font-semibold">Invalid JSON</p> |
| 179 | + <p className="text-sm">Fix the syntax error to see the preview</p> |
| 180 | + </div> |
| 181 | + ) : ( |
| 182 | + <p>Select an example to get started</p> |
| 183 | + )} |
| 184 | + </div> |
| 185 | + )} |
| 186 | + </div> |
| 187 | + </div> |
| 188 | + </div> |
| 189 | + </div> |
| 190 | + </div> |
| 191 | + ); |
| 192 | +} |
0 commit comments