page.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. "use client";
  2. import { useState, useCallback, useMemo } from "react";
  3. import {
  4. DataProvider,
  5. ActionProvider,
  6. VisibilityProvider,
  7. useUIStream,
  8. Renderer,
  9. } from "@json-render/react";
  10. import { componentRegistry } from "@/components/ui";
  11. import { generateNextJSProject } from "@/lib/codegen";
  12. import {
  13. CodeHighlight,
  14. getLanguageFromPath,
  15. } from "@/components/code-highlight";
  16. const INITIAL_DATA = {
  17. analytics: {
  18. revenue: 125000,
  19. growth: 0.15,
  20. customers: 1234,
  21. orders: 567,
  22. salesByRegion: [
  23. { label: "US", value: 45000 },
  24. { label: "EU", value: 35000 },
  25. { label: "Asia", value: 28000 },
  26. { label: "Other", value: 17000 },
  27. ],
  28. recentTransactions: [
  29. {
  30. id: "TXN001",
  31. customer: "Acme Corp",
  32. amount: 1500,
  33. status: "completed",
  34. date: "2024-01-15",
  35. },
  36. {
  37. id: "TXN002",
  38. customer: "Globex Inc",
  39. amount: 2300,
  40. status: "pending",
  41. date: "2024-01-14",
  42. },
  43. {
  44. id: "TXN003",
  45. customer: "Initech",
  46. amount: 890,
  47. status: "completed",
  48. date: "2024-01-13",
  49. },
  50. {
  51. id: "TXN004",
  52. customer: "Umbrella Co",
  53. amount: 4200,
  54. status: "completed",
  55. date: "2024-01-12",
  56. },
  57. ],
  58. },
  59. form: {
  60. dateRange: "",
  61. region: "",
  62. },
  63. };
  64. const ACTION_HANDLERS = {
  65. export_report: () => alert("Exporting report..."),
  66. refresh_data: () => alert("Refreshing data..."),
  67. view_details: (params: Record<string, unknown>) =>
  68. alert(`Details: ${JSON.stringify(params)}`),
  69. apply_filter: () => alert("Applying filters..."),
  70. };
  71. function DashboardContent() {
  72. const [prompt, setPrompt] = useState("");
  73. const [showCodeExport, setShowCodeExport] = useState(false);
  74. const [selectedFile, setSelectedFile] = useState<string | null>(null);
  75. const { tree, isStreaming, error, send, clear } = useUIStream({
  76. api: "/api/generate",
  77. onError: (err) => console.error("Generation error:", err),
  78. });
  79. const exportedFiles = useMemo(
  80. () =>
  81. tree
  82. ? generateNextJSProject(tree, {
  83. projectName: "my-dashboard",
  84. data: INITIAL_DATA,
  85. })
  86. : [],
  87. [tree],
  88. );
  89. const handleSubmit = useCallback(
  90. async (e: React.FormEvent) => {
  91. e.preventDefault();
  92. if (!prompt.trim()) return;
  93. await send(prompt, { data: INITIAL_DATA });
  94. },
  95. [prompt, send],
  96. );
  97. const downloadAllFiles = useCallback(() => {
  98. // Create a simple zip-like download by creating individual file downloads
  99. // For a real implementation, you'd use a library like JSZip
  100. const allContent = exportedFiles
  101. .map((f) => `// ========== ${f.path} ==========\n${f.content}`)
  102. .join("\n\n");
  103. const blob = new Blob([allContent], { type: "text/plain" });
  104. const url = URL.createObjectURL(blob);
  105. const a = document.createElement("a");
  106. a.href = url;
  107. a.download = "my-dashboard-project.txt";
  108. a.click();
  109. URL.revokeObjectURL(url);
  110. }, [exportedFiles]);
  111. const copyFileContent = useCallback((content: string) => {
  112. navigator.clipboard.writeText(content);
  113. }, []);
  114. const examples = [
  115. "Revenue dashboard with metrics and chart",
  116. "Recent transactions table",
  117. "Customer count with trend",
  118. ];
  119. const hasElements = tree && Object.keys(tree.elements).length > 0;
  120. // Auto-select first file when modal opens
  121. const activeFile =
  122. selectedFile || (exportedFiles.length > 0 ? exportedFiles[0]?.path : null);
  123. const activeFileContent = exportedFiles.find(
  124. (f) => f.path === activeFile,
  125. )?.content;
  126. return (
  127. <div style={{ maxWidth: 960, margin: "0 auto", padding: "48px 24px" }}>
  128. <header style={{ marginBottom: 48 }}>
  129. <h1
  130. style={{
  131. margin: 0,
  132. fontSize: 32,
  133. fontWeight: 600,
  134. letterSpacing: "-0.02em",
  135. }}
  136. >
  137. Dashboard
  138. </h1>
  139. <p style={{ margin: "8px 0 0", color: "var(--muted)", fontSize: 16 }}>
  140. Generate widgets from prompts. Constrained to your catalog.
  141. </p>
  142. </header>
  143. <form onSubmit={handleSubmit} style={{ marginBottom: 32 }}>
  144. <div style={{ display: "flex", gap: 8, marginBottom: 16 }}>
  145. <input
  146. type="text"
  147. value={prompt}
  148. onChange={(e) => setPrompt(e.target.value)}
  149. placeholder="Describe what you want..."
  150. disabled={isStreaming}
  151. style={{
  152. flex: 1,
  153. padding: "12px 16px",
  154. background: "var(--card)",
  155. border: "1px solid var(--border)",
  156. borderRadius: "var(--radius)",
  157. color: "var(--foreground)",
  158. fontSize: 16,
  159. outline: "none",
  160. }}
  161. />
  162. <button
  163. type="submit"
  164. disabled={isStreaming || !prompt.trim()}
  165. style={{
  166. padding: "12px 24px",
  167. background: isStreaming ? "var(--border)" : "var(--foreground)",
  168. color: "var(--background)",
  169. border: "none",
  170. borderRadius: "var(--radius)",
  171. fontSize: 16,
  172. fontWeight: 500,
  173. opacity: isStreaming || !prompt.trim() ? 0.5 : 1,
  174. }}
  175. >
  176. {isStreaming ? "Generating..." : "Generate"}
  177. </button>
  178. {hasElements && (
  179. <>
  180. <button
  181. type="button"
  182. onClick={() => {
  183. setSelectedFile(null);
  184. setShowCodeExport(true);
  185. }}
  186. style={{
  187. padding: "12px 16px",
  188. background: "transparent",
  189. color: "var(--foreground)",
  190. border: "1px solid var(--border)",
  191. borderRadius: "var(--radius)",
  192. fontSize: 16,
  193. }}
  194. >
  195. Export Project
  196. </button>
  197. <button
  198. type="button"
  199. onClick={clear}
  200. style={{
  201. padding: "12px 16px",
  202. background: "transparent",
  203. color: "var(--muted)",
  204. border: "1px solid var(--border)",
  205. borderRadius: "var(--radius)",
  206. fontSize: 16,
  207. }}
  208. >
  209. Clear
  210. </button>
  211. </>
  212. )}
  213. </div>
  214. <div style={{ display: "flex", gap: 8, flexWrap: "wrap" }}>
  215. {examples.map((ex) => (
  216. <button
  217. key={ex}
  218. type="button"
  219. onClick={() => setPrompt(ex)}
  220. style={{
  221. padding: "6px 12px",
  222. background: "var(--card)",
  223. color: "var(--muted)",
  224. border: "1px solid var(--border)",
  225. borderRadius: "var(--radius)",
  226. fontSize: 13,
  227. }}
  228. >
  229. {ex}
  230. </button>
  231. ))}
  232. </div>
  233. </form>
  234. {error && (
  235. <div
  236. style={{
  237. padding: 16,
  238. marginBottom: 24,
  239. background: "var(--card)",
  240. border: "1px solid var(--border)",
  241. borderRadius: "var(--radius)",
  242. color: "#ef4444",
  243. fontSize: 14,
  244. }}
  245. >
  246. {error.message}
  247. </div>
  248. )}
  249. <div
  250. style={{
  251. minHeight: 300,
  252. padding: 24,
  253. background: "var(--card)",
  254. border: "1px solid var(--border)",
  255. borderRadius: "var(--radius)",
  256. }}
  257. >
  258. {!hasElements && !isStreaming ? (
  259. <div
  260. style={{
  261. textAlign: "center",
  262. padding: "60px 20px",
  263. color: "var(--muted)",
  264. }}
  265. >
  266. <p style={{ margin: 0 }}>Enter a prompt to generate a widget</p>
  267. </div>
  268. ) : tree ? (
  269. <Renderer
  270. tree={tree}
  271. registry={componentRegistry}
  272. loading={isStreaming}
  273. />
  274. ) : null}
  275. </div>
  276. {hasElements && (
  277. <details style={{ marginTop: 24 }}>
  278. <summary
  279. style={{ cursor: "pointer", fontSize: 14, color: "var(--muted)" }}
  280. >
  281. View JSON
  282. </summary>
  283. <pre
  284. style={{
  285. marginTop: 8,
  286. padding: 16,
  287. background: "var(--card)",
  288. border: "1px solid var(--border)",
  289. borderRadius: "var(--radius)",
  290. overflow: "auto",
  291. fontSize: 12,
  292. color: "var(--muted)",
  293. }}
  294. >
  295. {JSON.stringify(tree, null, 2)}
  296. </pre>
  297. </details>
  298. )}
  299. {/* Code Export Modal */}
  300. {showCodeExport && (
  301. <div
  302. style={{
  303. position: "fixed",
  304. inset: 0,
  305. background: "rgba(0, 0, 0, 0.7)",
  306. display: "flex",
  307. alignItems: "center",
  308. justifyContent: "center",
  309. zIndex: 50,
  310. }}
  311. onClick={() => setShowCodeExport(false)}
  312. >
  313. <div
  314. style={{
  315. background: "var(--background)",
  316. border: "1px solid var(--border)",
  317. borderRadius: "var(--radius)",
  318. width: "90%",
  319. maxWidth: 1000,
  320. height: "80vh",
  321. overflow: "hidden",
  322. display: "flex",
  323. flexDirection: "column",
  324. }}
  325. onClick={(e) => e.stopPropagation()}
  326. >
  327. {/* Header */}
  328. <div
  329. style={{
  330. padding: "16px 20px",
  331. borderBottom: "1px solid var(--border)",
  332. display: "flex",
  333. justifyContent: "space-between",
  334. alignItems: "center",
  335. }}
  336. >
  337. <div>
  338. <h2 style={{ margin: 0, fontSize: 18, fontWeight: 600 }}>
  339. Export Next.js Project
  340. </h2>
  341. <p
  342. style={{
  343. margin: "4px 0 0",
  344. fontSize: 13,
  345. color: "var(--muted)",
  346. }}
  347. >
  348. {exportedFiles.length} files generated
  349. </p>
  350. </div>
  351. <div style={{ display: "flex", gap: 8 }}>
  352. <button
  353. onClick={downloadAllFiles}
  354. style={{
  355. padding: "8px 16px",
  356. background: "var(--foreground)",
  357. color: "var(--background)",
  358. border: "none",
  359. borderRadius: "var(--radius)",
  360. fontSize: 14,
  361. fontWeight: 500,
  362. cursor: "pointer",
  363. }}
  364. >
  365. Download All
  366. </button>
  367. <button
  368. onClick={() => setShowCodeExport(false)}
  369. style={{
  370. padding: "8px 16px",
  371. background: "transparent",
  372. color: "var(--muted)",
  373. border: "1px solid var(--border)",
  374. borderRadius: "var(--radius)",
  375. fontSize: 14,
  376. cursor: "pointer",
  377. }}
  378. >
  379. Close
  380. </button>
  381. </div>
  382. </div>
  383. {/* Content */}
  384. <div style={{ flex: 1, display: "flex", overflow: "hidden" }}>
  385. {/* File List */}
  386. <div
  387. style={{
  388. width: 240,
  389. borderRight: "1px solid var(--border)",
  390. overflow: "auto",
  391. padding: "8px 0",
  392. }}
  393. >
  394. {exportedFiles.map((file) => (
  395. <button
  396. key={file.path}
  397. onClick={() => setSelectedFile(file.path)}
  398. style={{
  399. display: "block",
  400. width: "100%",
  401. padding: "8px 16px",
  402. background:
  403. activeFile === file.path
  404. ? "var(--border)"
  405. : "transparent",
  406. border: "none",
  407. textAlign: "left",
  408. fontSize: 13,
  409. fontFamily: "monospace",
  410. color:
  411. activeFile === file.path
  412. ? "var(--foreground)"
  413. : "var(--muted)",
  414. cursor: "pointer",
  415. }}
  416. >
  417. {file.path}
  418. </button>
  419. ))}
  420. </div>
  421. {/* File Content */}
  422. <div
  423. style={{
  424. flex: 1,
  425. overflow: "auto",
  426. display: "flex",
  427. flexDirection: "column",
  428. }}
  429. >
  430. {activeFile && (
  431. <>
  432. <div
  433. style={{
  434. padding: "8px 16px",
  435. borderBottom: "1px solid var(--border)",
  436. display: "flex",
  437. justifyContent: "space-between",
  438. alignItems: "center",
  439. background: "var(--card)",
  440. }}
  441. >
  442. <span style={{ fontSize: 13, fontFamily: "monospace" }}>
  443. {activeFile}
  444. </span>
  445. <button
  446. onClick={() =>
  447. activeFileContent &&
  448. copyFileContent(activeFileContent)
  449. }
  450. style={{
  451. padding: "4px 12px",
  452. background: "var(--border)",
  453. color: "var(--foreground)",
  454. border: "none",
  455. borderRadius: "var(--radius)",
  456. fontSize: 12,
  457. cursor: "pointer",
  458. }}
  459. >
  460. Copy
  461. </button>
  462. </div>
  463. <div
  464. style={{
  465. flex: 1,
  466. padding: 16,
  467. overflow: "auto",
  468. background: "var(--card)",
  469. }}
  470. >
  471. <CodeHighlight
  472. code={activeFileContent || ""}
  473. language={getLanguageFromPath(activeFile)}
  474. />
  475. </div>
  476. </>
  477. )}
  478. </div>
  479. </div>
  480. </div>
  481. </div>
  482. )}
  483. </div>
  484. );
  485. }
  486. export default function DashboardPage() {
  487. return (
  488. <DataProvider initialData={INITIAL_DATA}>
  489. <VisibilityProvider>
  490. <ActionProvider handlers={ACTION_HANDLERS}>
  491. <DashboardContent />
  492. </ActionProvider>
  493. </VisibilityProvider>
  494. </DataProvider>
  495. );
  496. }