|
| 1 | +import { useState, useMemo, useCallback } from "react"; |
| 2 | +import yaml from "js-yaml"; |
| 3 | +import { Editor } from "./components/Editor"; |
| 4 | +import { FormatToggle, type Format } from "./components/FormatToggle"; |
| 5 | +import { ExamplePicker } from "./components/ExamplePicker"; |
| 6 | +import { TabBar, type Tab } from "./components/TabBar"; |
| 7 | +import { DemoView } from "./components/DemoView"; |
| 8 | +import { AtprotoView } from "./components/AtprotoView"; |
| 9 | +import { ValidationBadge } from "./components/ValidationBadge"; |
| 10 | +import { validate } from "./validate"; |
| 11 | +import { examples } from "./examples"; |
| 12 | + |
| 13 | +interface AppProps { |
| 14 | + initialExample?: string; |
| 15 | + fullscreen?: boolean; |
| 16 | +} |
| 17 | + |
| 18 | +function serializeDocument( |
| 19 | + doc: Record<string, unknown>, |
| 20 | + format: Format, |
| 21 | +): string { |
| 22 | + if (format === "json") { |
| 23 | + return JSON.stringify(doc, null, 2); |
| 24 | + } |
| 25 | + return yaml.dump(doc, { indent: 2, lineWidth: 80, noRefs: true }); |
| 26 | +} |
| 27 | + |
| 28 | +function convertFormat( |
| 29 | + source: string, |
| 30 | + from: Format, |
| 31 | + to: Format, |
| 32 | +): string | null { |
| 33 | + try { |
| 34 | + const parsed = |
| 35 | + from === "json" |
| 36 | + ? JSON.parse(source) |
| 37 | + : (yaml.load(source) as Record<string, unknown>); |
| 38 | + return serializeDocument(parsed, to); |
| 39 | + } catch { |
| 40 | + return null; |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +export function App({ |
| 45 | + initialExample = "rfc0003", |
| 46 | + fullscreen = false, |
| 47 | +}: AppProps) { |
| 48 | + const initialDoc = |
| 49 | + examples.find((e) => e.id === initialExample) ?? examples[0]; |
| 50 | + |
| 51 | + const [source, setSource] = useState(() => |
| 52 | + serializeDocument(initialDoc.document, "json"), |
| 53 | + ); |
| 54 | + const [format, setFormat] = useState<Format>("json"); |
| 55 | + const [activeTab, setActiveTab] = useState<Tab>("demo"); |
| 56 | + const [selectedExample, setSelectedExample] = useState(initialDoc.id); |
| 57 | + |
| 58 | + const validation = useMemo(() => validate(source, format), [source, format]); |
| 59 | + |
| 60 | + const handleFormatChange = useCallback( |
| 61 | + (newFormat: Format) => { |
| 62 | + if (newFormat === format) return; |
| 63 | + const converted = convertFormat(source, format, newFormat); |
| 64 | + if (converted !== null) { |
| 65 | + setSource(converted); |
| 66 | + setFormat(newFormat); |
| 67 | + } |
| 68 | + }, |
| 69 | + [source, format], |
| 70 | + ); |
| 71 | + |
| 72 | + const handleExampleChange = useCallback( |
| 73 | + (id: string) => { |
| 74 | + const example = examples.find((e) => e.id === id); |
| 75 | + if (!example) return; |
| 76 | + setSelectedExample(id); |
| 77 | + setSource(serializeDocument(example.document, format)); |
| 78 | + }, |
| 79 | + [format], |
| 80 | + ); |
| 81 | + |
| 82 | + const outerClass = fullscreen |
| 83 | + ? "flex flex-col w-full h-full border border-slate-200 rounded-xl overflow-hidden bg-white shadow-sm" |
| 84 | + : "flex flex-col h-[500px] border border-slate-200 rounded-xl overflow-hidden bg-white shadow-sm"; |
| 85 | + |
| 86 | + return ( |
| 87 | + <div className={outerClass}> |
| 88 | + {/* Toolbar */} |
| 89 | + <div className="flex items-center gap-3 px-4 py-2 bg-slate-50 border-b border-slate-200"> |
| 90 | + <ExamplePicker |
| 91 | + selected={selectedExample} |
| 92 | + onChange={handleExampleChange} |
| 93 | + /> |
| 94 | + <FormatToggle format={format} onChange={handleFormatChange} /> |
| 95 | + <ValidationBadge valid={validation.valid} errors={validation.errors} /> |
| 96 | + <div className="ml-auto"> |
| 97 | + <TabBar active={activeTab} onChange={setActiveTab} /> |
| 98 | + </div> |
| 99 | + </div> |
| 100 | + |
| 101 | + {/* Validation errors */} |
| 102 | + {!validation.valid && |
| 103 | + validation.errors && |
| 104 | + validation.errors.length > 0 && ( |
| 105 | + <div className="px-4 py-2 bg-red-50 border-b border-red-200 text-sm text-red-700 font-mono overflow-auto max-h-32"> |
| 106 | + {validation.errors.map((error, i) => ( |
| 107 | + <div key={i} className="py-0.5"> |
| 108 | + {error} |
| 109 | + </div> |
| 110 | + ))} |
| 111 | + </div> |
| 112 | + )} |
| 113 | + |
| 114 | + {/* Split panels */} |
| 115 | + <div className="flex flex-1 min-h-0 overflow-hidden"> |
| 116 | + {/* Left: Editor */} |
| 117 | + <div className="relative flex-1 min-w-0 border-r border-slate-200"> |
| 118 | + <Editor value={source} onChange={setSource} format={format} /> |
| 119 | + </div> |
| 120 | + |
| 121 | + {/* Right: Output */} |
| 122 | + <div className="relative flex-1 min-w-0"> |
| 123 | + {activeTab === "demo" ? ( |
| 124 | + <DemoView source={source} format={format} /> |
| 125 | + ) : ( |
| 126 | + <AtprotoView source={source} format={format} /> |
| 127 | + )} |
| 128 | + </div> |
| 129 | + </div> |
| 130 | + </div> |
| 131 | + ); |
| 132 | +} |
0 commit comments