{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "schema-browser",
  "title": "Schema browser",
  "description": "Searchable schema and table tree beside a table page with row counts, related-table chips, column list with primary and foreign keys that navigate, sample rows and generated DDL, each copyable.",
  "dependencies": [
    "lucide-react"
  ],
  "registryDependencies": [
    "https://goodui.dev/r/badge.json",
    "https://goodui.dev/r/button.json",
    "https://goodui.dev/r/input.json",
    "https://goodui.dev/r/tree-expand.json"
  ],
  "files": [
    {
      "path": "registry/blocks/tables/schema-browser.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { Check, Copy, Database, Hash, Key, Link2, Search, Table2, Type } from \"lucide-react\"\nimport { Badge } from \"@/components/goodui/badge\"\nimport { Button } from \"@/components/goodui/button\"\nimport { Input } from \"@/components/goodui/input\"\nimport { TreeExpand, type TreeExpandNode } from \"@/components/goodui/tree-expand\"\nimport { cn } from \"@/lib/utils\"\n\n/* ---------- Demo data ---------- */\n\ntype Column = { name: string; type: string; nullable?: boolean; pk?: boolean; fk?: string; description?: string }\ntype Table = { schema: string; name: string; rows: number; size: string; description: string; columns: Column[]; sample: (string | number | boolean | null)[][] }\nconst TABLES: Table[] = [\n  {\n    schema: \"public\",\n    name: \"bookings\",\n    rows: 184_220,\n    size: \"96 MB\",\n    description: \"One row per appointment. Status moves pending → confirmed → completed / cancelled / no_show.\",\n    columns: [\n      { name: \"id\", type: \"uuid\", pk: true, description: \"Primary key\" },\n      { name: \"client_id\", type: \"uuid\", fk: \"clients.id\" },\n      { name: \"staff_id\", type: \"uuid\", fk: \"staff.id\" },\n      { name: \"service_id\", type: \"uuid\", fk: \"services.id\" },\n      { name: \"starts_at\", type: \"timestamptz\", description: \"Local time of the location, stored in UTC\" },\n      { name: \"status\", type: \"booking_status\", description: \"enum: pending, confirmed, completed, cancelled, no_show\" },\n      { name: \"total_cents\", type: \"integer\", description: \"Final charged amount including tip\" },\n      { name: \"rating\", type: \"smallint\", nullable: true, description: \"1–5, set after the visit\" },\n      { name: \"created_at\", type: \"timestamptz\" },\n    ],\n    sample: [\n      [\"8f2a…\", \"c_41…\", \"s_02…\", \"sv_9…\", \"2026-03-14 09:30\", \"confirmed\", 24000, null],\n      [\"7b1c…\", \"c_18…\", \"s_03…\", \"sv_4…\", \"2026-03-13 14:00\", \"completed\", 8500, 5],\n      [\"6d0e…\", \"c_77…\", \"s_01…\", \"sv_1…\", \"2026-03-13 11:15\", \"no_show\", 0, null],\n    ],\n  },\n  {\n    schema: \"public\",\n    name: \"clients\",\n    rows: 6_210,\n    size: \"4 MB\",\n    description: \"Everyone who has booked or been added at the desk.\",\n    columns: [\n      { name: \"id\", type: \"uuid\", pk: true },\n      { name: \"name\", type: \"text\" },\n      { name: \"email\", type: \"citext\", nullable: true },\n      { name: \"phone\", type: \"text\", nullable: true },\n      { name: \"tags\", type: \"text[]\", description: \"VIP, sensitive scalp, …\" },\n      { name: \"created_at\", type: \"timestamptz\" },\n    ],\n    sample: [\n      [\"c_41…\", \"Maya Chen\", \"maya@example.com\", \"+1415…\", \"{VIP}\"],\n      [\"c_18…\", \"Tom Becker\", \"tom.b@example.com\", null, \"{}\"],\n    ],\n  },\n  {\n    schema: \"public\",\n    name: \"staff\",\n    rows: 14,\n    size: \"48 kB\",\n    description: \"Stylists, techs and front desk.\",\n    columns: [\n      { name: \"id\", type: \"uuid\", pk: true },\n      { name: \"name\", type: \"text\" },\n      { name: \"role\", type: \"text\" },\n      { name: \"hourly_rate\", type: \"numeric(8,2)\" },\n      { name: \"active\", type: \"boolean\" },\n    ],\n    sample: [\n      [\"s_01…\", \"Ana Petrova\", \"Senior stylist\", 34, true],\n      [\"s_02…\", \"Leo Martins\", \"Barber\", 28, true],\n    ],\n  },\n  {\n    schema: \"public\",\n    name: \"services\",\n    rows: 42,\n    size: \"64 kB\",\n    description: \"Menu items with duration and price.\",\n    columns: [\n      { name: \"id\", type: \"uuid\", pk: true },\n      { name: \"name\", type: \"text\" },\n      { name: \"category\", type: \"text\" },\n      { name: \"duration_min\", type: \"integer\" },\n      { name: \"price_cents\", type: \"integer\" },\n    ],\n    sample: [\n      [\"sv_9…\", \"Balayage\", \"Color\", 150, 22000],\n      [\"sv_4…\", \"Women's cut & blow-dry\", \"Hair\", 60, 8500],\n    ],\n  },\n  {\n    schema: \"analytics\",\n    name: \"fact_bookings\",\n    rows: 182_906,\n    size: \"71 MB\",\n    description: \"Warehouse table built nightly by bookings_to_warehouse.\",\n    columns: [\n      { name: \"booking_id\", type: \"uuid\", pk: true },\n      { name: \"date_key\", type: \"integer\", fk: \"dim_date.date_key\" },\n      { name: \"stylist\", type: \"text\" },\n      { name: \"service_category\", type: \"text\" },\n      { name: \"revenue\", type: \"numeric(10,2)\" },\n      { name: \"is_no_show\", type: \"boolean\" },\n    ],\n    sample: [[ \"8f2a…\", 20260314, \"Ana Petrova\", \"Color\", 240.0, false ]],\n  },\n]\nconst TYPE_ICON = (t: string) => (/int|numeric|small|big/.test(t) ? <Hash /> : /time|date/.test(t) ? <Type /> : <Type />)\n\n/* ---------- Block ---------- */\n\nexport default function SchemaBrowser() {\n  const [query, setQuery] = React.useState(\"\")\n  const [selected, setSelected] = React.useState<string>(\"public.bookings\")\n  const [tab, setTab] = React.useState<\"columns\" | \"sample\" | \"ddl\">(\"columns\")\n  const [copied, setCopied] = React.useState(false)\n\n  const q = query.toLowerCase()\n  const nodes: TreeExpandNode[] = [\"public\", \"analytics\"].map((schema) => ({\n    id: schema,\n    label: schema,\n    icon: <Database className=\"size-4\" />,\n    meta: <span className=\"text-[11px] text-foreground/70\">{TABLES.filter((t) => t.schema === schema).length}</span>,\n    children: TABLES.filter((t) => t.schema === schema && (!q || t.name.includes(q) || t.columns.some((c) => c.name.includes(q)))).map((t) => ({\n      id: `${t.schema}.${t.name}`,\n      label: t.name,\n      icon: <Table2 className=\"size-4\" />,\n      meta: <span className=\"text-[11px] tabular-nums text-foreground/70\">{t.rows.toLocaleString(\"en-US\")}</span>,\n    })),\n  }))\n  const table = TABLES.find((t) => `${t.schema}.${t.name}` === selected) ?? TABLES[0]!\n  const ddl = `CREATE TABLE ${table.schema}.${table.name} (\\n${table.columns.map((c) => `  ${c.name.padEnd(14)} ${c.type}${c.pk ? \" PRIMARY KEY\" : c.nullable ? \"\" : \" NOT NULL\"}${c.fk ? ` REFERENCES ${c.fk.split(\".\")[0]}(${c.fk.split(\".\")[1]})` : \"\"}`).join(\",\\n\")}\\n);`\n  const copy = (text: string) => {\n    void navigator.clipboard?.writeText(text)\n    setCopied(true)\n    window.setTimeout(() => setCopied(false), 1500)\n  }\n  const related = TABLES.filter((t) => t.columns.some((c) => c.fk?.startsWith(table.name + \".\")) || table.columns.some((c) => c.fk?.startsWith(t.name + \".\"))).filter((t) => t.name !== table.name)\n\n  return (\n    <div className=\"p-4 md:p-6\">\n      <div className=\"mx-auto grid max-w-6xl gap-4 md:grid-cols-[260px_1fr]\">\n        {/* Tree */}\n        <aside className=\"rounded-2xl border bg-card p-3 shadow-xs\">\n          <Input size=\"sm\" label=\"Search tables and columns\" labelMode=\"none\" leading={<Search />} placeholder=\"Search tables, columns…\" value={query} onValueChange={setQuery} clearable fullWidth />\n          <div className=\"mt-2 max-h-[460px] overflow-y-auto\">\n            <TreeExpand nodes={nodes} defaultExpanded={[\"public\", \"analytics\"]} selected={selected} onSelectedChange={(id, node) => id && node?.children === undefined && setSelected(id)} label=\"Schemas\" />\n          </div>\n        </aside>\n\n        {/* Detail */}\n        <section className=\"min-w-0 rounded-2xl border bg-card shadow-xs\">\n          <div className=\"border-b p-5\">\n            <div className=\"flex flex-wrap items-start justify-between gap-3\">\n              <div>\n                <div className=\"flex items-center gap-2 font-mono text-xs text-muted-foreground\">\n                  <Database className=\"size-3.5\" /> {table.schema}\n                </div>\n                <h2 className=\"mt-0.5 font-heading text-xl font-semibold tracking-tight\">{table.name}</h2>\n                <p className=\"mt-1 max-w-xl text-sm text-muted-foreground\">{table.description}</p>\n              </div>\n              <div className=\"flex gap-2\">\n                <Badge variant=\"outline\" tone=\"neutral\" size=\"sm\">\n                  {table.rows.toLocaleString(\"en-US\")} rows\n                </Badge>\n                <Badge variant=\"outline\" tone=\"neutral\" size=\"sm\">\n                  {table.size}\n                </Badge>\n              </div>\n            </div>\n            {related.length > 0 && (\n              <div className=\"mt-3 flex flex-wrap items-center gap-1.5 text-xs text-muted-foreground\">\n                <Link2 className=\"size-3.5\" /> Related:\n                {related.map((r) => (\n                  <button key={r.name} type=\"button\" onClick={() => setSelected(`${r.schema}.${r.name}`)} className=\"rounded-full border px-2 py-0.5 font-mono text-[11px] hover:bg-muted\">\n                    {r.name}\n                  </button>\n                ))}\n              </div>\n            )}\n          </div>\n\n          <div className=\"flex items-center justify-between gap-2 border-b px-3 py-2\">\n            <div role=\"tablist\" aria-label=\"Table views\" className=\"flex rounded-lg bg-muted p-0.5 text-xs\">\n              {(\n                [\n                  [\"columns\", `Columns (${table.columns.length})`],\n                  [\"sample\", \"Sample rows\"],\n                  [\"ddl\", \"DDL\"],\n                ] as const\n              ).map(([k, l]) => (\n                <button key={k} role=\"tab\" type=\"button\" aria-selected={tab === k} onClick={() => setTab(k)} className={cn(\"rounded-md px-3 py-1.5 font-medium transition-colors\", tab === k ? \"bg-background shadow-xs\" : \"text-foreground/70\")}>\n                  {l}\n                </button>\n              ))}\n            </div>\n            <Button size=\"xs\" variant=\"ghost\" tone=\"neutral\" iconLeft={copied ? <Check /> : <Copy />} onClick={() => copy(tab === \"ddl\" ? ddl : `SELECT ${table.columns.map((c) => c.name).join(\", \")}\\nFROM ${table.schema}.${table.name}\\nLIMIT 100;`)}>\n              {copied ? \"Copied\" : tab === \"ddl\" ? \"Copy DDL\" : \"Copy SELECT\"}\n            </Button>\n          </div>\n\n          {tab === \"columns\" && (\n            <ul className=\"divide-y\">\n              {table.columns.map((c) => (\n                <li key={c.name} className=\"flex flex-wrap items-center gap-3 px-5 py-2.5\">\n                  <span className=\"flex size-7 items-center justify-center rounded-md bg-muted text-muted-foreground [&_svg]:size-3.5\">{c.pk ? <Key /> : c.fk ? <Link2 /> : TYPE_ICON(c.type)}</span>\n                  <code className=\"min-w-32 font-mono text-sm font-medium\">{c.name}</code>\n                  <code className=\"rounded bg-muted px-1.5 py-0.5 font-mono text-[11px] text-foreground/70\">{c.type}</code>\n                  {c.pk && (\n                    <Badge variant=\"soft\" tone=\"warning\" size=\"xs\">\n                      PK\n                    </Badge>\n                  )}\n                  {c.fk && (\n                    <button type=\"button\" onClick={() => setSelected(`${c.fk!.split(\".\")[0] === \"dim_date\" ? \"analytics\" : \"public\"}.${c.fk!.split(\".\")[0]}`)} className=\"inline-flex items-center gap-1 rounded-full bg-sky-500/10 px-2 py-0.5 text-[11px] font-medium text-sky-800 hover:bg-sky-500/20 dark:text-sky-300\">\n                      <Link2 className=\"size-3\" /> {c.fk}\n                    </button>\n                  )}\n                  {c.nullable && <span className=\"text-[11px] text-muted-foreground\">nullable</span>}\n                  {c.description && <span className=\"basis-full text-xs text-muted-foreground sm:basis-auto sm:ml-auto sm:text-right\">{c.description}</span>}\n                </li>\n              ))}\n            </ul>\n          )}\n          {tab === \"sample\" && (\n            <div className=\"overflow-x-auto\">\n              <table className=\"w-full text-xs\">\n                <thead>\n                  <tr className=\"bg-muted/50 text-left text-muted-foreground\">\n                    {table.columns.slice(0, table.sample[0]?.length ?? 0).map((c) => (\n                      <th key={c.name} className=\"px-3 py-2 font-mono font-medium\">\n                        {c.name}\n                      </th>\n                    ))}\n                  </tr>\n                </thead>\n                <tbody>\n                  {table.sample.map((r, i) => (\n                    <tr key={i} className=\"border-t\">\n                      {r.map((v, j) => (\n                        <td key={j} className={cn(\"px-3 py-1.5 font-mono\", v === null && \"text-muted-foreground/60 italic\", typeof v === \"number\" && \"tabular-nums\")}>\n                          {v === null ? \"null\" : String(v)}\n                        </td>\n                      ))}\n                    </tr>\n                  ))}\n                </tbody>\n              </table>\n              <p className=\"border-t px-3 py-2 text-[11px] text-muted-foreground\">Sampled from the read replica · PII columns masked</p>\n            </div>\n          )}\n          {tab === \"ddl\" && <pre className=\"overflow-x-auto p-5 font-mono text-xs leading-relaxed\">{ddl}</pre>}\n        </section>\n      </div>\n    </div>\n  )\n}\n",
      "type": "registry:component",
      "target": "components/goodui/schema-browser.tsx"
    }
  ],
  "categories": [
    "tables"
  ],
  "type": "registry:block"
}
