| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189 |
- "use client";
- import { useEffect, useState, useCallback, useRef, useMemo } from "react";
- import { flushSync } from "react-dom";
- import { useUIStream, type TokenUsage } from "@json-render/react";
- import type { Spec } from "@json-render/core";
- import { collectUsedComponents, serializeProps } from "@json-render/codegen";
- import { toast } from "sonner";
- import {
- ResizablePanelGroup,
- ResizablePanel,
- ResizableHandle,
- } from "@/components/ui/resizable";
- import { CodeBlock } from "./code-block";
- import { CopyButton } from "./copy-button";
- import { Toaster } from "./ui/sonner";
- import { Header } from "./header";
- import { Sheet, SheetContent, SheetTitle } from "./ui/sheet";
- import { JsonEditor } from "@visual-json/react";
- import type { JsonValue } from "@visual-json/react";
- import { PlaygroundRenderer } from "@/lib/render/renderer";
- import { playgroundCatalog } from "@/lib/render/catalog";
- import { buildCatalogDisplayData } from "@/lib/render/catalog-display";
- type Tab = "json" | "nested" | "stream" | "catalog" | "visual";
- type RenderView = "preview" | "code";
- type MobileView =
- | "json"
- | "nested"
- | "stream"
- | "catalog"
- | "visual"
- | "preview"
- | "generated-code";
- interface Version {
- id: string;
- prompt: string;
- tree: Spec | null;
- status: "generating" | "complete" | "error";
- usage: TokenUsage | null;
- rawLines: string[];
- }
- /**
- * Convert a flat Spec into a nested tree structure that is easier for humans
- * to read. Children keys are resolved recursively into inline objects.
- */
- function specToNested(spec: Spec): Record<string, unknown> {
- function resolve(key: string): Record<string, unknown> {
- const el = spec.elements[key];
- if (!el) return { _key: key, _missing: true };
- const node: Record<string, unknown> = { type: el.type };
- if (el.props && Object.keys(el.props).length > 0) {
- node.props = el.props;
- }
- if (el.visible !== undefined) {
- node.visible = el.visible;
- }
- if (el.on && Object.keys(el.on).length > 0) {
- node.on = el.on;
- }
- if (el.repeat) {
- node.repeat = el.repeat;
- }
- if (el.children && el.children.length > 0) {
- node.children = el.children.map(resolve);
- }
- return node;
- }
- const result: Record<string, unknown> = {};
- if (spec.state && Object.keys(spec.state).length > 0) {
- result.state = spec.state;
- }
- result.elements = resolve(spec.root);
- return result;
- }
- const EXAMPLE_PROMPTS = [
- "Create a login form",
- "Build a pricing page",
- "Design a user profile card",
- "Make a contact form",
- ];
- export function Playground() {
- const [versions, setVersions] = useState<Version[]>([]);
- const [selectedVersionId, setSelectedVersionId] = useState<string | null>(
- null,
- );
- const [inputValue, setInputValue] = useState("");
- const [activeTab, setActiveTab] = useState<Tab>("json");
- const [catalogSection, setCatalogSection] = useState<
- "components" | "actions"
- >("components");
- const [renderView, setRenderView] = useState<RenderView>("preview");
- const [mobileView, setMobileView] = useState<MobileView>("preview");
- const [versionsSheetOpen, setVersionsSheetOpen] = useState(false);
- const inputRef = useRef<HTMLTextAreaElement>(null);
- const mobileInputRef = useRef<HTMLTextAreaElement>(null);
- const versionsEndRef = useRef<HTMLDivElement>(null);
- // Track the currently generating version ID
- const generatingVersionIdRef = useRef<string | null>(null);
- // Track the current tree for use as previousSpec in next generation
- const currentTreeRef = useRef<Spec | null>(null);
- const {
- spec: apiSpec,
- isStreaming,
- usage: streamUsage,
- rawLines: streamRawLines,
- send,
- clear,
- } = useUIStream({
- api: "/api/generate",
- onError: (err: Error) => {
- console.error("Generation error:", err);
- toast.error(err.message || "Generation failed. Please try again.");
- // Mark the version as errored
- if (generatingVersionIdRef.current) {
- const erroredVersionId = generatingVersionIdRef.current;
- setVersions((prev) =>
- prev.map((v) =>
- v.id === erroredVersionId ? { ...v, status: "error" as const } : v,
- ),
- );
- generatingVersionIdRef.current = null;
- }
- },
- } as Parameters<typeof useUIStream>[0]);
- // Get the selected version
- const selectedVersion = versions.find((v) => v.id === selectedVersionId);
- // Determine which tree to display:
- // - If streaming and selected version is the generating one, show apiSpec
- // - Otherwise show the selected version's tree
- const isSelectedVersionGenerating =
- selectedVersionId === generatingVersionIdRef.current && isStreaming;
- const hasValidApiTree =
- apiSpec && apiSpec.root && Object.keys(apiSpec.elements).length > 0;
- const currentTree =
- isSelectedVersionGenerating && hasValidApiTree
- ? apiSpec
- : (selectedVersion?.tree ??
- (isSelectedVersionGenerating ? apiSpec : null));
- // Raw JSONL lines: live from stream during generation, or stored per version
- const currentRawLines = isSelectedVersionGenerating
- ? streamRawLines
- : (selectedVersion?.rawLines ?? []);
- // Keep the ref updated with the current tree for use in handleSubmit
- if (
- currentTree &&
- currentTree.root &&
- Object.keys(currentTree.elements).length > 0
- ) {
- currentTreeRef.current = currentTree;
- }
- // Scroll to bottom when versions change
- useEffect(() => {
- versionsEndRef.current?.scrollIntoView({ behavior: "smooth" });
- }, [versions]);
- // Update version when streaming completes
- useEffect(() => {
- if (
- !isStreaming &&
- apiSpec &&
- apiSpec.root &&
- generatingVersionIdRef.current
- ) {
- const completedVersionId = generatingVersionIdRef.current;
- setVersions((prev) =>
- prev.map((v) =>
- v.id === completedVersionId
- ? {
- ...v,
- tree: apiSpec,
- status: "complete" as const,
- usage: streamUsage,
- rawLines: streamRawLines,
- }
- : v,
- ),
- );
- generatingVersionIdRef.current = null;
- }
- }, [isStreaming, apiSpec, streamUsage, streamRawLines]);
- const handleSubmit = useCallback(async () => {
- if (!inputValue.trim() || isStreaming) return;
- const newVersionId = Date.now().toString();
- const newVersion: Version = {
- id: newVersionId,
- prompt: inputValue.trim(),
- tree: null,
- status: "generating",
- usage: null,
- rawLines: [],
- };
- generatingVersionIdRef.current = newVersionId;
- setVersions((prev) => [...prev, newVersion]);
- setSelectedVersionId(newVersionId);
- setInputValue("");
- // Pass the current tree as context so the API can iterate on it
- await send(inputValue.trim(), { previousSpec: currentTreeRef.current });
- }, [inputValue, isStreaming, send]);
- const handleKeyDown = useCallback(
- (e: React.KeyboardEvent) => {
- if (e.key === "Enter" && !e.shiftKey) {
- e.preventDefault();
- handleSubmit();
- }
- },
- [handleSubmit],
- );
- const handleVisualChange = useCallback(
- (value: JsonValue) => {
- if (!selectedVersionId || isStreaming) return;
- setVersions((prev) =>
- prev.map((v) =>
- v.id === selectedVersionId
- ? { ...v, tree: value as unknown as Spec }
- : v,
- ),
- );
- },
- [selectedVersionId, isStreaming],
- );
- const jsonCode = currentTree
- ? JSON.stringify(currentTree, null, 2)
- : "// waiting...";
- const nestedCode = useMemo(() => {
- if (!currentTree || !currentTree.root) return "// waiting...";
- return JSON.stringify(specToNested(currentTree), null, 2);
- }, [currentTree]);
- const generatedCode = useMemo(() => {
- if (!currentTree || !currentTree.root) {
- return "// Generate a UI to see the code";
- }
- const tree = currentTree;
- const components = collectUsedComponents(tree);
- function generateJSX(key: string, indent: number): string {
- const element = tree.elements[key];
- if (!element) return "";
- const spaces = " ".repeat(indent);
- const componentName = element.type;
- const propsObj: Record<string, unknown> = {};
- for (const [k, v] of Object.entries(element.props)) {
- if (v !== null && v !== undefined) {
- propsObj[k] = v;
- }
- }
- const propsStr = serializeProps(propsObj);
- const hasChildren = element.children && element.children.length > 0;
- if (!hasChildren) {
- return propsStr
- ? `${spaces}<${componentName} ${propsStr} />`
- : `${spaces}<${componentName} />`;
- }
- const lines: string[] = [];
- lines.push(
- propsStr
- ? `${spaces}<${componentName} ${propsStr}>`
- : `${spaces}<${componentName}>`,
- );
- for (const childKey of element.children!) {
- lines.push(generateJSX(childKey, indent + 1));
- }
- lines.push(`${spaces}</${componentName}>`);
- return lines.join("\n");
- }
- const jsx = generateJSX(tree.root, 2);
- const imports = Array.from(components).sort().join(", ");
- return `"use client";
- import { ${imports} } from "@/components/ui";
- export default function Page() {
- return (
- <div className="min-h-screen p-8 flex items-center justify-center">
- ${jsx}
- </div>
- );
- }`;
- }, [currentTree]);
- // Chat pane content
- const chatPane = (
- <div className="h-full flex flex-col border-t border-border">
- <div className="border-b border-border px-3 h-9 flex items-center">
- <span className="text-xs font-mono text-muted-foreground">
- versions
- </span>
- </div>
- <div
- className={`flex-1 p-2 min-h-0 ${versions.length > 0 ? "overflow-y-auto space-y-1" : "flex"}`}
- >
- {versions.length === 0 ? (
- <div className="flex-1 flex flex-col items-center justify-center text-center px-4">
- <p className="text-sm text-muted-foreground mb-4">
- Describe what you want to build, then iterate on it.
- </p>
- <div className="flex flex-wrap gap-2 justify-center">
- {EXAMPLE_PROMPTS.map((prompt) => (
- <button
- key={prompt}
- onMouseDown={(e) => {
- e.preventDefault();
- flushSync(() => setInputValue(prompt));
- // chatPane is rendered in both desktop and mobile layouts,
- // so inputRef may point to the hidden instance. Find the
- // textarea in the same layout container as the clicked button.
- const container = (e.currentTarget as HTMLElement).closest(
- ".h-full.flex.flex-col",
- );
- const el =
- container?.querySelector<HTMLTextAreaElement>(
- "textarea",
- ) ?? inputRef.current;
- if (el) {
- el.focus();
- el.setSelectionRange(prompt.length, prompt.length);
- }
- }}
- className="text-xs px-2 py-1 rounded border border-border text-muted-foreground hover:text-foreground hover:border-foreground/30 transition-colors"
- >
- {prompt}
- </button>
- ))}
- </div>
- </div>
- ) : (
- versions.map((version, index) => (
- <button
- key={version.id}
- onClick={() => setSelectedVersionId(version.id)}
- className={`w-full text-left px-3 py-2 rounded text-sm transition-colors ${
- selectedVersionId === version.id
- ? "bg-muted text-foreground"
- : "text-muted-foreground hover:bg-muted/50 hover:text-foreground"
- }`}
- >
- <div className="flex items-center gap-2">
- <span className="text-xs font-mono text-muted-foreground/70 shrink-0">
- v{index + 1}
- </span>
- <span className="truncate flex-1">{version.prompt}</span>
- {version.status === "generating" && (
- <span className="text-xs text-muted-foreground shrink-0 animate-pulse">
- ...
- </span>
- )}
- {version.status === "error" && (
- <span className="text-xs text-red-500 shrink-0">failed</span>
- )}
- </div>
- {version.usage && (
- <div className="flex items-center gap-2 mt-1 ml-6">
- <span className="text-[10px] font-mono text-muted-foreground/60">
- {version.usage.totalTokens.toLocaleString()} tokens
- </span>
- </div>
- )}
- </button>
- ))
- )}
- <div ref={versionsEndRef} />
- </div>
- <div
- className="border-t border-border p-3 cursor-text"
- onMouseDown={(e) => {
- // Focus textarea unless clicking a button or the textarea itself
- const target = e.target as HTMLElement;
- if (!target.closest("button") && target.tagName !== "TEXTAREA") {
- e.preventDefault();
- inputRef.current?.focus();
- }
- }}
- >
- <textarea
- ref={inputRef}
- value={inputValue}
- onChange={(e) => setInputValue(e.target.value)}
- onKeyDown={handleKeyDown}
- placeholder="Describe changes..."
- className="w-full bg-background text-base sm:text-sm resize-none outline-none placeholder:text-muted-foreground/50"
- rows={2}
- autoFocus
- />
- <div className="flex justify-between items-center mt-2">
- {versions.length > 0 ? (
- <button
- onClick={() => {
- setVersions([]);
- setSelectedVersionId(null);
- clear();
- }}
- className="text-xs text-muted-foreground hover:text-foreground transition-colors"
- >
- Clear
- </button>
- ) : (
- <div />
- )}
- {isStreaming ? (
- <button
- onClick={() => clear()}
- className="w-7 h-7 rounded-full bg-primary text-primary-foreground flex items-center justify-center hover:bg-primary/90 transition-colors"
- aria-label="Stop"
- >
- <svg
- width="14"
- height="14"
- viewBox="0 0 24 24"
- fill="currentColor"
- stroke="none"
- >
- <rect x="6" y="6" width="12" height="12" />
- </svg>
- </button>
- ) : (
- <button
- onClick={handleSubmit}
- disabled={!inputValue.trim()}
- className="w-7 h-7 rounded-full bg-primary text-primary-foreground flex items-center justify-center hover:bg-primary/90 transition-colors disabled:opacity-30"
- aria-label="Send"
- >
- <svg
- width="14"
- height="14"
- viewBox="0 0 24 24"
- fill="none"
- stroke="currentColor"
- strokeWidth="2"
- strokeLinecap="round"
- strokeLinejoin="round"
- >
- <path d="M5 12h14" />
- <path d="m12 5 7 7-7 7" />
- </svg>
- </button>
- )}
- </div>
- </div>
- </div>
- );
- // Catalog data for the catalog tab
- const catalogData = useMemo(
- () => buildCatalogDisplayData(playgroundCatalog.data),
- [],
- );
- // Code pane content
- const copyText =
- activeTab === "stream"
- ? currentRawLines.join("\n")
- : activeTab === "json"
- ? jsonCode
- : activeTab === "nested"
- ? nestedCode
- : activeTab === "visual"
- ? jsonCode
- : "";
- const codePane = (
- <div className="h-full flex flex-col border-t border-border">
- <div className="border-b border-border px-3 h-9 flex items-center gap-3">
- {(["json", "visual", "nested", "stream", "catalog"] as const).map(
- (tab) => (
- <button
- key={tab}
- onClick={() => setActiveTab(tab)}
- className={`text-xs font-mono transition-colors ${
- activeTab === tab
- ? "text-foreground"
- : "text-muted-foreground hover:text-foreground"
- }`}
- >
- {tab}
- </button>
- ),
- )}
- <div className="flex-1" />
- {activeTab !== "catalog" && activeTab !== "visual" && (
- <CopyButton text={copyText} className="text-muted-foreground" />
- )}
- </div>
- <div className="flex-1 overflow-auto">
- {activeTab === "visual" ? (
- currentTree ? (
- <JsonEditor
- value={currentTree as unknown as JsonValue}
- onChange={handleVisualChange}
- readOnly={isStreaming}
- sidebarOpen={false}
- height="100%"
- className="h-full"
- style={
- {
- "--vj-bg": "var(--background)",
- "--vj-bg-panel": "var(--background)",
- "--vj-bg-hover": "var(--muted)",
- "--vj-bg-selected": "var(--primary)",
- "--vj-bg-selected-muted": "var(--muted)",
- "--vj-text": "var(--foreground)",
- "--vj-text-selected": "var(--primary-foreground)",
- "--vj-text-muted": "var(--muted-foreground)",
- "--vj-text-dim": "var(--muted-foreground)",
- "--vj-border": "var(--border)",
- "--vj-border-subtle": "var(--border)",
- "--vj-accent": "var(--primary)",
- "--vj-accent-muted": "var(--muted)",
- "--vj-input-bg": "var(--secondary)",
- "--vj-input-border": "var(--border)",
- } as React.CSSProperties
- }
- />
- ) : (
- <div className="text-muted-foreground/50 p-3 text-sm font-mono">
- {"// generate a spec to edit visually"}
- </div>
- )
- ) : activeTab === "catalog" ? (
- <div className="h-full flex flex-col text-sm">
- <div className="flex items-center gap-3 px-3 h-9 border-b border-border">
- {(
- [
- {
- key: "components",
- label: `components (${catalogData.components.length})`,
- },
- {
- key: "actions",
- label: `actions (${catalogData.actions.length})`,
- },
- ] as const
- ).map(({ key, label }) => (
- <button
- key={key}
- onClick={() => setCatalogSection(key)}
- className={`text-xs font-mono transition-colors ${
- catalogSection === key
- ? "text-foreground"
- : "text-muted-foreground hover:text-foreground"
- }`}
- >
- {label}
- </button>
- ))}
- </div>
- <div className="flex-1 overflow-auto p-3">
- {catalogSection === "components" ? (
- <div className="space-y-3">
- {catalogData.components.map((comp) => (
- <div
- key={comp.name}
- className="pb-3 border-b border-border last:border-b-0"
- >
- <div className="flex items-baseline gap-2 mb-1">
- <span className="font-mono font-medium text-foreground">
- {comp.name}
- </span>
- {comp.slots.length > 0 && (
- <span className="text-[10px] font-mono px-1.5 py-0.5 rounded bg-muted text-muted-foreground">
- slots: {comp.slots.join(", ")}
- </span>
- )}
- </div>
- {comp.description && (
- <p className="text-xs text-muted-foreground mb-2">
- {comp.description}
- </p>
- )}
- {comp.props.length > 0 && (
- <div className="flex flex-wrap gap-1 mb-1">
- {comp.props.map((p) => (
- <span
- key={p.name}
- className="text-[11px] font-mono px-1.5 py-0.5 rounded bg-green-500/10 text-green-700 dark:text-green-400"
- >
- {p.name}
- <span className="text-green-700/50 dark:text-green-400/50">
- : {p.type}
- </span>
- </span>
- ))}
- </div>
- )}
- {comp.events.length > 0 && (
- <div className="flex flex-wrap gap-1 mt-1.5">
- {comp.events.map((e) => (
- <span
- key={e}
- className="text-[11px] font-mono px-1.5 py-0.5 rounded bg-blue-500/10 text-blue-600 dark:text-blue-400"
- >
- on.{e}
- </span>
- ))}
- </div>
- )}
- </div>
- ))}
- </div>
- ) : (
- <div className="space-y-3">
- {catalogData.actions.map((action) => (
- <div
- key={action.name}
- className="pb-3 border-b border-border last:border-b-0"
- >
- <span className="font-mono font-medium text-foreground">
- {action.name}
- </span>
- {action.description && (
- <p className="text-xs text-muted-foreground mt-1 mb-2">
- {action.description}
- </p>
- )}
- {action.params.length > 0 && (
- <div className="flex flex-wrap gap-1">
- {action.params.map((p) => (
- <span
- key={p.name}
- className="text-[11px] font-mono px-1.5 py-0.5 rounded bg-green-500/10 text-green-700 dark:text-green-400"
- >
- {p.name}
- <span className="text-green-700/50 dark:text-green-400/50">
- : {p.type}
- </span>
- </span>
- ))}
- </div>
- )}
- </div>
- ))}
- </div>
- )}
- </div>
- </div>
- ) : activeTab === "stream" ? (
- currentRawLines.length > 0 ? (
- <CodeBlock
- code={currentRawLines.join("\n")}
- lang="json"
- fillHeight
- hideCopyButton
- />
- ) : (
- <div className="text-muted-foreground/50 p-3 text-sm font-mono">
- {isStreaming ? "streaming..." : "// waiting for generation"}
- </div>
- )
- ) : activeTab === "nested" ? (
- <CodeBlock code={nestedCode} lang="json" fillHeight hideCopyButton />
- ) : (
- <CodeBlock code={jsonCode} lang="json" fillHeight hideCopyButton />
- )}
- </div>
- </div>
- );
- // Preview pane content
- const previewPane = (
- <div className="h-full flex flex-col border-t border-border">
- <div className="border-b border-border px-3 h-9 flex items-center gap-3">
- {(
- [
- { key: "preview", label: "preview" },
- { key: "code", label: "code" },
- ] as const
- ).map(({ key, label }) => (
- <button
- key={key}
- onClick={() => setRenderView(key)}
- className={`text-xs font-mono transition-colors ${
- renderView === key
- ? "text-foreground"
- : "text-muted-foreground hover:text-foreground"
- }`}
- >
- {label}
- </button>
- ))}
- <div className="flex-1" />
- {renderView === "code" && (
- <CopyButton text={generatedCode} className="text-muted-foreground" />
- )}
- </div>
- <div className="flex-1 overflow-auto">
- {renderView === "preview" ? (
- currentTree && currentTree.root ? (
- <div className="w-full min-h-full flex items-center justify-center p-6">
- <PlaygroundRenderer
- spec={currentTree}
- data={currentTree.state}
- loading={isStreaming}
- />
- </div>
- ) : (
- <div className="h-full flex items-center justify-center text-muted-foreground/50 text-sm">
- {isStreaming
- ? "generating..."
- : "// enter a prompt to generate UI"}
- </div>
- )
- ) : (
- <CodeBlock
- code={generatedCode}
- lang="tsx"
- fillHeight
- hideCopyButton
- />
- )}
- </div>
- </div>
- );
- return (
- <div className="h-full flex flex-col">
- <Header />
- {/* Desktop: 3-pane resizable layout */}
- <div className="hidden lg:flex flex-1 min-h-0">
- <ResizablePanelGroup className="flex-1">
- <ResizablePanel defaultSize={25} minSize={15}>
- {chatPane}
- </ResizablePanel>
- <ResizableHandle />
- <ResizablePanel defaultSize={35} minSize={20}>
- {codePane}
- </ResizablePanel>
- <ResizableHandle />
- <ResizablePanel defaultSize={40} minSize={20}>
- {previewPane}
- </ResizablePanel>
- </ResizablePanelGroup>
- </div>
- {/* Mobile: toolbar + content + prompt input */}
- <div className="flex lg:hidden flex-col flex-1 min-h-0">
- {/* Top toolbar */}
- <div className="border-b border-border px-3 h-9 flex items-center gap-3 shrink-0 overflow-x-auto">
- {/* Version badge */}
- <button
- onClick={() => setVersionsSheetOpen(true)}
- className="text-xs font-mono font-medium px-1.5 py-0.5 rounded bg-muted text-foreground shrink-0"
- >
- v
- {versions.length > 0
- ? versions.findIndex((v) => v.id === selectedVersionId) + 1 ||
- versions.length
- : 0}
- </button>
- {/* Code tabs */}
- {(["json", "visual", "nested", "stream", "catalog"] as const).map(
- (tab) => (
- <button
- key={tab}
- onClick={() => setMobileView(tab)}
- className={`text-xs font-mono transition-colors shrink-0 ${
- mobileView === tab
- ? "text-foreground"
- : "text-muted-foreground hover:text-foreground"
- }`}
- >
- {tab}
- </button>
- ),
- )}
- <div className="flex-1" />
- {/* Preview / code toggle */}
- {[
- { key: "preview" as const, label: "preview" },
- { key: "generated-code" as const, label: "code" },
- ].map(({ key, label }) => (
- <button
- key={key}
- onClick={() => setMobileView(key)}
- className={`text-xs font-mono transition-colors shrink-0 ${
- mobileView === key
- ? "text-foreground"
- : "text-muted-foreground hover:text-foreground"
- }`}
- >
- {label}
- </button>
- ))}
- </div>
- {/* Main content area */}
- <div className="flex-1 min-h-0 overflow-auto">
- {mobileView === "visual" ? (
- currentTree ? (
- <JsonEditor
- value={currentTree as unknown as JsonValue}
- onChange={handleVisualChange}
- readOnly={isStreaming}
- sidebarOpen={false}
- height="100%"
- className="h-full"
- style={
- {
- "--vj-bg": "var(--background)",
- "--vj-bg-panel": "var(--background)",
- "--vj-bg-hover": "var(--muted)",
- "--vj-bg-selected": "var(--primary)",
- "--vj-bg-selected-muted": "var(--muted)",
- "--vj-text": "var(--foreground)",
- "--vj-text-selected": "var(--primary-foreground)",
- "--vj-text-muted": "var(--muted-foreground)",
- "--vj-text-dim": "var(--muted-foreground)",
- "--vj-border": "var(--border)",
- "--vj-border-subtle": "var(--border)",
- "--vj-accent": "var(--primary)",
- "--vj-accent-muted": "var(--muted)",
- "--vj-input-bg": "var(--secondary)",
- "--vj-input-border": "var(--border)",
- } as React.CSSProperties
- }
- />
- ) : (
- <div className="text-muted-foreground/50 p-3 text-sm font-mono">
- {"// generate a spec to edit visually"}
- </div>
- )
- ) : mobileView === "catalog" ? (
- <div className="h-full flex flex-col text-sm">
- <div className="flex items-center gap-3 px-3 h-9 border-b border-border">
- {(
- [
- {
- key: "components",
- label: `components (${catalogData.components.length})`,
- },
- {
- key: "actions",
- label: `actions (${catalogData.actions.length})`,
- },
- ] as const
- ).map(({ key, label }) => (
- <button
- key={key}
- onClick={() => setCatalogSection(key)}
- className={`text-xs font-mono transition-colors ${
- catalogSection === key
- ? "text-foreground"
- : "text-muted-foreground hover:text-foreground"
- }`}
- >
- {label}
- </button>
- ))}
- </div>
- <div className="flex-1 overflow-auto p-3">
- {catalogSection === "components" ? (
- <div className="space-y-3">
- {catalogData.components.map((comp) => (
- <div
- key={comp.name}
- className="pb-3 border-b border-border last:border-b-0"
- >
- <div className="flex items-baseline gap-2 mb-1">
- <span className="font-mono font-medium text-foreground">
- {comp.name}
- </span>
- {comp.slots.length > 0 && (
- <span className="text-[10px] font-mono px-1.5 py-0.5 rounded bg-muted text-muted-foreground">
- slots: {comp.slots.join(", ")}
- </span>
- )}
- </div>
- {comp.description && (
- <p className="text-xs text-muted-foreground mb-2">
- {comp.description}
- </p>
- )}
- {comp.props.length > 0 && (
- <div className="flex flex-wrap gap-1 mb-1">
- {comp.props.map((p) => (
- <span
- key={p.name}
- className="text-[11px] font-mono px-1.5 py-0.5 rounded bg-green-500/10 text-green-700 dark:text-green-400"
- >
- {p.name}
- <span className="text-green-700/50 dark:text-green-400/50">
- : {p.type}
- </span>
- </span>
- ))}
- </div>
- )}
- {comp.events.length > 0 && (
- <div className="flex flex-wrap gap-1 mt-1.5">
- {comp.events.map((e) => (
- <span
- key={e}
- className="text-[11px] font-mono px-1.5 py-0.5 rounded bg-blue-500/10 text-blue-600 dark:text-blue-400"
- >
- on.{e}
- </span>
- ))}
- </div>
- )}
- </div>
- ))}
- </div>
- ) : (
- <div className="space-y-3">
- {catalogData.actions.map((action) => (
- <div
- key={action.name}
- className="pb-3 border-b border-border last:border-b-0"
- >
- <span className="font-mono font-medium text-foreground">
- {action.name}
- </span>
- {action.description && (
- <p className="text-xs text-muted-foreground mt-1 mb-2">
- {action.description}
- </p>
- )}
- {action.params.length > 0 && (
- <div className="flex flex-wrap gap-1">
- {action.params.map((p) => (
- <span
- key={p.name}
- className="text-[11px] font-mono px-1.5 py-0.5 rounded bg-green-500/10 text-green-700 dark:text-green-400"
- >
- {p.name}
- <span className="text-green-700/50 dark:text-green-400/50">
- : {p.type}
- </span>
- </span>
- ))}
- </div>
- )}
- </div>
- ))}
- </div>
- )}
- </div>
- </div>
- ) : mobileView === "stream" ? (
- currentRawLines.length > 0 ? (
- <CodeBlock
- code={currentRawLines.join("\n")}
- lang="json"
- fillHeight
- hideCopyButton
- />
- ) : (
- <div className="text-muted-foreground/50 p-3 text-sm font-mono">
- {isStreaming ? "streaming..." : "// waiting for generation"}
- </div>
- )
- ) : mobileView === "nested" ? (
- <CodeBlock
- code={nestedCode}
- lang="json"
- fillHeight
- hideCopyButton
- />
- ) : mobileView === "json" ? (
- <CodeBlock code={jsonCode} lang="json" fillHeight hideCopyButton />
- ) : mobileView === "preview" ? (
- currentTree && currentTree.root ? (
- <div className="w-full min-h-full flex items-center justify-center p-6">
- <PlaygroundRenderer
- spec={currentTree}
- data={currentTree.state}
- loading={isStreaming}
- />
- </div>
- ) : (
- <div className="h-full flex flex-col items-center justify-center text-center px-4">
- {isStreaming ? (
- <p className="text-sm text-muted-foreground/50">
- generating...
- </p>
- ) : (
- <>
- <p className="text-sm text-muted-foreground mb-4">
- Describe what you want to build, then iterate on it.
- </p>
- <div className="flex flex-wrap gap-2 justify-center">
- {EXAMPLE_PROMPTS.map((prompt) => (
- <button
- key={prompt}
- onMouseDown={(e) => {
- e.preventDefault();
- flushSync(() => setInputValue(prompt));
- mobileInputRef.current?.focus();
- mobileInputRef.current?.setSelectionRange(
- prompt.length,
- prompt.length,
- );
- }}
- className="text-xs px-2 py-1 rounded border border-border text-muted-foreground hover:text-foreground hover:border-foreground/30 transition-colors"
- >
- {prompt}
- </button>
- ))}
- </div>
- </>
- )}
- </div>
- )
- ) : (
- /* generated-code */
- <CodeBlock
- code={generatedCode}
- lang="tsx"
- fillHeight
- hideCopyButton
- />
- )}
- </div>
- {/* Prompt input pinned to bottom */}
- <div
- className="border-t border-border p-3 shrink-0 cursor-text"
- onMouseDown={(e) => {
- const target = e.target as HTMLElement;
- if (!target.closest("button") && target.tagName !== "TEXTAREA") {
- e.preventDefault();
- mobileInputRef.current?.focus();
- }
- }}
- >
- <textarea
- ref={mobileInputRef}
- value={inputValue}
- onChange={(e) => setInputValue(e.target.value)}
- onKeyDown={handleKeyDown}
- placeholder="Describe changes..."
- className="w-full bg-background text-base resize-none outline-none placeholder:text-muted-foreground/50"
- rows={2}
- />
- <div className="flex justify-between items-center mt-2">
- {versions.length > 0 ? (
- <button
- onClick={() => {
- setVersions([]);
- setSelectedVersionId(null);
- clear();
- }}
- className="text-xs text-muted-foreground hover:text-foreground transition-colors"
- >
- Clear
- </button>
- ) : (
- <div />
- )}
- {isStreaming ? (
- <button
- onClick={() => clear()}
- className="w-7 h-7 rounded-full bg-primary text-primary-foreground flex items-center justify-center hover:bg-primary/90 transition-colors"
- aria-label="Stop"
- >
- <svg
- width="14"
- height="14"
- viewBox="0 0 24 24"
- fill="currentColor"
- stroke="none"
- >
- <rect x="6" y="6" width="12" height="12" />
- </svg>
- </button>
- ) : (
- <button
- onClick={handleSubmit}
- disabled={!inputValue.trim()}
- className="w-7 h-7 rounded-full bg-primary text-primary-foreground flex items-center justify-center hover:bg-primary/90 transition-colors disabled:opacity-30"
- aria-label="Send"
- >
- <svg
- width="14"
- height="14"
- viewBox="0 0 24 24"
- fill="none"
- stroke="currentColor"
- strokeWidth="2"
- strokeLinecap="round"
- strokeLinejoin="round"
- >
- <path d="M5 12h14" />
- <path d="m12 5 7 7-7 7" />
- </svg>
- </button>
- )}
- </div>
- </div>
- {/* Versions sheet */}
- <Sheet open={versionsSheetOpen} onOpenChange={setVersionsSheetOpen}>
- <SheetContent>
- <SheetTitle className="text-sm font-mono mb-4">Versions</SheetTitle>
- <div className="flex-1 overflow-y-auto space-y-1">
- {versions.map((version, index) => (
- <button
- key={version.id}
- onClick={() => {
- setSelectedVersionId(version.id);
- setVersionsSheetOpen(false);
- }}
- className={`w-full text-left px-3 py-2 rounded text-sm transition-colors ${
- selectedVersionId === version.id
- ? "bg-muted text-foreground"
- : "text-muted-foreground hover:bg-muted/50 hover:text-foreground"
- }`}
- >
- <div className="flex items-center gap-2">
- <span className="text-xs font-mono text-muted-foreground/70 shrink-0">
- v{index + 1}
- </span>
- <span className="truncate flex-1">{version.prompt}</span>
- {version.status === "generating" && (
- <span className="text-xs text-muted-foreground shrink-0 animate-pulse">
- ...
- </span>
- )}
- {version.status === "error" && (
- <span className="text-xs text-red-500 shrink-0">
- failed
- </span>
- )}
- </div>
- {version.usage && (
- <div className="flex items-center gap-2 mt-1 ml-6">
- <span className="text-[10px] font-mono text-muted-foreground/60">
- {version.usage.totalTokens.toLocaleString()} tokens
- </span>
- </div>
- )}
- </button>
- ))}
- {versions.length === 0 && (
- <p className="text-sm text-muted-foreground px-3">
- No versions yet. Enter a prompt to get started.
- </p>
- )}
- </div>
- </SheetContent>
- </Sheet>
- </div>
- <Toaster position="bottom-right" />
- </div>
- );
- }
|