|
| 1 | +/* eslint-disable @next/next/no-html-link-for-pages */ |
| 2 | +"use client"; |
| 3 | + |
| 4 | +import { useMemo, useState } from "react"; |
| 5 | +import { useJsonParser } from "@hashbrownai/react"; |
| 6 | +import { useChatKit } from "@/components/chat/chat-kit"; |
| 7 | + |
| 8 | +const jsonDocument = `{ |
| 9 | + "ui": [ |
| 10 | + { |
| 11 | + "weather": { |
| 12 | + "props":{ |
| 13 | + "themeColor": "blue", |
| 14 | + "temperature": 29.7, |
| 15 | + "humidity": 70, |
| 16 | + "windSpeed": 12.7, |
| 17 | + "feelsLike": 19.2, |
| 18 | + "location": "Huntsville, Alabama, United States" |
| 19 | + } |
| 20 | + } |
| 21 | + }, |
| 22 | + { |
| 23 | + "p": { |
| 24 | + "props": {}, |
| 25 | + "children": "Huntsville, AL is cold right now: 29.7°F, feeling closer to 19.2°F with a brisk 12.7 mph wind. Humidity is 70%, so the air may feel a bit damp on top of the chill."}},{"p":{"props":{},"children":"If you're heading out, plan on a warm coat, gloves, and something to block the wind." |
| 26 | + } |
| 27 | + } |
| 28 | + ] |
| 29 | +}`; |
| 30 | + |
| 31 | +export default function UiRendererPage() { |
| 32 | + const [cursor, setCursor] = useState(jsonDocument.length); |
| 33 | + const jsonSlice = jsonDocument.slice(0, cursor); |
| 34 | + const kit = useChatKit(); |
| 35 | + const { parserState, value } = useJsonParser(jsonSlice, kit.schema); |
| 36 | + |
| 37 | + const ticks = useMemo(() => Array.from({ length: jsonDocument.length }), []); |
| 38 | + |
| 39 | + return ( |
| 40 | + <main className="flex h-screen w-screen flex-col bg-slate-950 text-slate-100"> |
| 41 | + <header className="flex items-center justify-between border-b border-slate-800 px-6 py-4"> |
| 42 | + <div> |
| 43 | + <div className="text-xs uppercase tracking-[0.35em] text-slate-400"> |
| 44 | + Streaming JSON Renderer |
| 45 | + </div> |
| 46 | + <h1 className="text-2xl font-semibold text-slate-100"> |
| 47 | + UI Renderer Playground |
| 48 | + </h1> |
| 49 | + </div> |
| 50 | + <div className="flex items-center gap-4 text-xs text-slate-400"> |
| 51 | + <a |
| 52 | + href="/" |
| 53 | + className="rounded-full border border-slate-800 px-3 py-1 text-[10px] font-semibold uppercase tracking-[0.2em] text-slate-300 transition hover:border-slate-600 hover:text-white" |
| 54 | + > |
| 55 | + Chat Demo |
| 56 | + </a> |
| 57 | + <a |
| 58 | + href="/parser" |
| 59 | + className="rounded-full border border-slate-800 px-3 py-1 text-[10px] font-semibold uppercase tracking-[0.2em] text-slate-300 transition hover:border-slate-600 hover:text-white" |
| 60 | + > |
| 61 | + Parser Demo |
| 62 | + </a> |
| 63 | + <span>Characters: {cursor}</span> |
| 64 | + </div> |
| 65 | + </header> |
| 66 | + |
| 67 | + <div className="grid min-h-0 flex-1 grid-cols-1 md:grid-cols-[minmax(0,1fr)_720px]"> |
| 68 | + <section className="flex min-h-0 flex-col border-b border-slate-800 bg-slate-900/40 md:border-b-0 md:border-r"> |
| 69 | + <div className="flex min-h-0 flex-1 flex-col"> |
| 70 | + <div className="border-b border-slate-800 px-6 py-3 text-xs uppercase tracking-[0.3em] text-slate-400"> |
| 71 | + JSON Document |
| 72 | + </div> |
| 73 | + <div className="min-h-0 flex-1 overflow-auto px-6 py-4"> |
| 74 | + <pre className="whitespace-pre-wrap text-sm leading-6"> |
| 75 | + <span className="text-emerald-200"> |
| 76 | + {jsonDocument.slice(0, cursor)} |
| 77 | + </span> |
| 78 | + <span className="text-slate-600"> |
| 79 | + {jsonDocument.slice(cursor)} |
| 80 | + </span> |
| 81 | + </pre> |
| 82 | + </div> |
| 83 | + </div> |
| 84 | + <div className="flex min-h-0 flex-[0.6] flex-col border-t border-slate-800"> |
| 85 | + <div className="border-b border-slate-800 px-6 py-3 text-xs uppercase tracking-[0.3em] text-slate-400"> |
| 86 | + Resolved Value |
| 87 | + </div> |
| 88 | + <div className="min-h-0 flex-1 overflow-auto px-6 py-4"> |
| 89 | + <pre className="whitespace-pre-wrap text-sm leading-6 text-slate-200"> |
| 90 | + {value ? JSON.stringify(value, null, 2) : "—"} |
| 91 | + </pre> |
| 92 | + </div> |
| 93 | + </div> |
| 94 | + </section> |
| 95 | + |
| 96 | + <section className="flex min-h-0 flex-col bg-slate-950"> |
| 97 | + <div className="border-b border-slate-800 px-6 py-3 text-xs uppercase tracking-[0.3em] text-slate-400"> |
| 98 | + UI Renderer |
| 99 | + </div> |
| 100 | + <div className="min-h-0 flex-1 overflow-auto px-6 py-6"> |
| 101 | + {value ? ( |
| 102 | + <div className="rounded-2xl border border-emerald-500/20 bg-emerald-500/10 px-5 py-4 text-emerald-50 shadow-lg shadow-emerald-500/10"> |
| 103 | + <div className="text-[10px] font-semibold uppercase tracking-[0.25em] text-emerald-200/80"> |
| 104 | + Rendered UI |
| 105 | + </div> |
| 106 | + <div className="mt-3 space-y-3 text-[15px] leading-6 text-white [&_*]:text-white"> |
| 107 | + {kit.render(value)} |
| 108 | + </div> |
| 109 | + </div> |
| 110 | + ) : ( |
| 111 | + <div className="flex h-full items-center justify-center text-sm text-slate-400"> |
| 112 | + Start scrubbing to build the UI. |
| 113 | + </div> |
| 114 | + )} |
| 115 | + {parserState.error ? ( |
| 116 | + <div className="mt-4 rounded-xl border border-rose-500/40 bg-rose-500/10 px-4 py-3 text-xs text-rose-200"> |
| 117 | + {parserState.error.message} (line {parserState.error.line}, col |
| 118 | + {parserState.error.column}) |
| 119 | + </div> |
| 120 | + ) : null} |
| 121 | + </div> |
| 122 | + </section> |
| 123 | + </div> |
| 124 | + |
| 125 | + <div className="border-t border-slate-800 bg-slate-950 px-6 py-4"> |
| 126 | + <div className="mb-3 text-xs uppercase tracking-[0.3em] text-slate-400"> |
| 127 | + Slider |
| 128 | + </div> |
| 129 | + <input |
| 130 | + type="range" |
| 131 | + min={0} |
| 132 | + max={jsonDocument.length} |
| 133 | + value={cursor} |
| 134 | + onChange={(event) => setCursor(Number(event.target.value))} |
| 135 | + className="h-2 w-full cursor-pointer accent-emerald-400" |
| 136 | + /> |
| 137 | + <div |
| 138 | + className="mt-3 grid h-3 w-full items-end gap-[1px]" |
| 139 | + style={{ |
| 140 | + gridTemplateColumns: `repeat(${ticks.length}, minmax(0, 1fr))`, |
| 141 | + }} |
| 142 | + > |
| 143 | + {ticks.map((_, index) => ( |
| 144 | + <span |
| 145 | + key={index} |
| 146 | + className={`h-full rounded-sm ${ |
| 147 | + index < cursor ? "bg-emerald-500/80" : "bg-slate-700/60" |
| 148 | + }`} |
| 149 | + /> |
| 150 | + ))} |
| 151 | + </div> |
| 152 | + </div> |
| 153 | + </main> |
| 154 | + ); |
| 155 | +} |
0 commit comments