page.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import Link from "next/link";
  2. import { Code } from "@/components/code";
  3. export const metadata = {
  4. title: "Quick Start | json-render",
  5. };
  6. export default function QuickStartPage() {
  7. return (
  8. <article>
  9. <h1 className="text-3xl font-bold mb-4">Quick Start</h1>
  10. <p className="text-muted-foreground mb-8">
  11. Get up and running with json-render in 5 minutes.
  12. </p>
  13. <h2 className="text-xl font-semibold mt-12 mb-4">
  14. 1. Define your catalog
  15. </h2>
  16. <p className="text-sm text-muted-foreground mb-4">
  17. Create a catalog that defines what components AI can use:
  18. </p>
  19. <Code lang="typescript">{`// lib/catalog.ts
  20. import { defineCatalog } from '@json-render/core';
  21. import { schema } from '@json-render/react';
  22. import { z } from 'zod';
  23. export const catalog = defineCatalog(schema, {
  24. components: {
  25. Card: {
  26. props: z.object({
  27. title: z.string(),
  28. description: z.string().nullable(),
  29. }),
  30. slots: ["default"],
  31. description: "Container card with optional title",
  32. },
  33. Button: {
  34. props: z.object({
  35. label: z.string(),
  36. action: z.string().nullable(),
  37. }),
  38. description: "Clickable button that triggers an action",
  39. },
  40. Text: {
  41. props: z.object({
  42. content: z.string(),
  43. }),
  44. description: "Text paragraph",
  45. },
  46. },
  47. actions: {
  48. submit: {
  49. params: z.object({ formId: z.string() }),
  50. description: "Submit a form",
  51. },
  52. navigate: {
  53. params: z.object({ url: z.string() }),
  54. description: "Navigate to a URL",
  55. },
  56. },
  57. });`}</Code>
  58. <h2 className="text-xl font-semibold mt-12 mb-4">
  59. 2. Create your components and actions
  60. </h2>
  61. <p className="text-sm text-muted-foreground mb-4">
  62. Define React components that render each catalog type. Each component
  63. receives <code className="text-foreground">props</code>,{" "}
  64. <code className="text-foreground">children</code>, and{" "}
  65. <code className="text-foreground">onAction</code>:
  66. </p>
  67. <Code lang="tsx">{`// lib/components.tsx
  68. import { defineComponents } from '@json-render/react';
  69. import { catalog } from './catalog';
  70. export const components = defineComponents(catalog, {
  71. Card: ({ props, children }) => (
  72. <div className="p-4 border rounded-lg">
  73. <h2 className="font-bold">{props.title}</h2>
  74. {props.description && (
  75. <p className="text-gray-600">{props.description}</p>
  76. )}
  77. {children}
  78. </div>
  79. ),
  80. Button: ({ props, onAction }) => (
  81. <button
  82. className="px-4 py-2 bg-blue-500 text-white rounded"
  83. onClick={() => onAction?.(props.action)}
  84. >
  85. {props.label}
  86. </button>
  87. ),
  88. Text: ({ props }) => (
  89. <p>{props.content}</p>
  90. ),
  91. });`}</Code>
  92. <h2 className="text-xl font-semibold mt-12 mb-4">
  93. 3. Create an API route
  94. </h2>
  95. <p className="text-sm text-muted-foreground mb-4">
  96. Set up a streaming API route for AI generation:
  97. </p>
  98. <Code lang="typescript">{`// app/api/generate/route.ts
  99. import { streamText } from 'ai';
  100. import { catalog } from '@/lib/catalog';
  101. export async function POST(req: Request) {
  102. const { prompt } = await req.json();
  103. // Generate system prompt from catalog
  104. const systemPrompt = catalog.prompt();
  105. const result = streamText({
  106. model: 'anthropic/claude-haiku-4.5',
  107. system: systemPrompt,
  108. prompt,
  109. });
  110. return result.toTextStreamResponse();
  111. }`}</Code>
  112. <h2 className="text-xl font-semibold mt-12 mb-4">4. Render the UI</h2>
  113. <p className="text-sm text-muted-foreground mb-4">
  114. Use the providers and renderer to display AI-generated UI:
  115. </p>
  116. <Code lang="tsx">{`// app/page.tsx
  117. 'use client';
  118. import { DataProvider, ActionProvider, VisibilityProvider, Renderer, useUIStream } from '@json-render/react';
  119. import { catalog } from '@/lib/catalog';
  120. import { components } from '@/lib/components';
  121. export default function Page() {
  122. const { spec, isStreaming, send } = useUIStream({
  123. api: '/api/generate',
  124. });
  125. const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
  126. e.preventDefault();
  127. const formData = new FormData(e.currentTarget);
  128. send(formData.get('prompt') as string);
  129. };
  130. return (
  131. <DataProvider initialData={{}}>
  132. <VisibilityProvider>
  133. <ActionProvider handlers={{
  134. submit: (params) => console.log('Submit:', params),
  135. navigate: (params) => console.log('Navigate:', params),
  136. }}>
  137. <form onSubmit={handleSubmit}>
  138. <input
  139. name="prompt"
  140. placeholder="Describe what you want..."
  141. className="border p-2 rounded"
  142. />
  143. <button type="submit" disabled={isStreaming}>
  144. Generate
  145. </button>
  146. </form>
  147. <div className="mt-8">
  148. <Renderer spec={spec} catalog={catalog} components={components} loading={isStreaming} />
  149. </div>
  150. </ActionProvider>
  151. </VisibilityProvider>
  152. </DataProvider>
  153. );
  154. }`}</Code>
  155. <h2 className="text-xl font-semibold mt-12 mb-4">Next steps</h2>
  156. <ul className="list-disc list-inside text-sm text-muted-foreground space-y-2">
  157. <li>
  158. Learn about{" "}
  159. <Link
  160. href="/docs/catalog"
  161. className="text-foreground hover:underline"
  162. >
  163. catalogs
  164. </Link>{" "}
  165. in depth
  166. </li>
  167. <li>
  168. Explore{" "}
  169. <Link
  170. href="/docs/data-binding"
  171. className="text-foreground hover:underline"
  172. >
  173. data binding
  174. </Link>{" "}
  175. for dynamic values
  176. </li>
  177. <li>
  178. Add{" "}
  179. <Link
  180. href="/docs/actions"
  181. className="text-foreground hover:underline"
  182. >
  183. actions
  184. </Link>{" "}
  185. for interactivity
  186. </li>
  187. <li>
  188. Implement{" "}
  189. <Link
  190. href="/docs/visibility"
  191. className="text-foreground hover:underline"
  192. >
  193. conditional visibility
  194. </Link>
  195. </li>
  196. </ul>
  197. </article>
  198. );
  199. }