{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "schema-viewer",
  "title": "Schema Viewer",
  "description": "Database table schema inspector displaying columns, types, nullability, defaults, key badges, and clickable foreign key links.",
  "dependencies": [
    "lucide-react"
  ],
  "registryDependencies": [
    "badge",
    "button",
    "input",
    "utils",
    "@drift-ui/use-controllable-state",
    "@drift-ui/shape"
  ],
  "files": [
    {
      "path": "registry/default/ui/schema-viewer.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport {\n  KeyRoundIcon,\n  Link2Icon,\n  SearchIcon,\n  SearchXIcon,\n  Table2Icon,\n  XIcon,\n} from \"lucide-react\"\n\nimport { useControllableState } from \"@/registry/default/hooks/use-controllable-state\"\nimport {\n  filterColumns,\n  formatTableName,\n  getColumnBadges,\n  isColumnNullable,\n  SCHEMA_BADGE_META,\n  type SchemaBadgeKind,\n  type SchemaColumn,\n  type SchemaTable,\n} from \"@/registry/default/lib/schema-viewer-utils\"\nimport { cn } from \"@/registry/default/lib/utils\"\nimport { Badge } from \"@/registry/default/ui/badge\"\nimport { Button } from \"@/registry/default/ui/button\"\nimport { Input } from \"@/registry/default/ui/input\"\n\nexport type { SchemaBadgeKind, SchemaColumn, SchemaTable }\n\nconst BADGE_VARIANT: Record<\n  SchemaBadgeKind,\n  \"default\" | \"secondary\" | \"outline\"\n> = {\n  pk: \"default\",\n  fk: \"secondary\",\n  unique: \"outline\",\n  index: \"outline\",\n}\n\nexport interface SchemaViewerProps extends Omit<\n  React.ComponentProps<\"div\">,\n  \"children\"\n> {\n  table: SchemaTable\n  onReferenceClick?: (ref: string, column: SchemaColumn) => void\n  filter?: string\n  defaultFilter?: string\n  onFilterChange?: (filter: string) => void\n  searchable?: boolean\n}\n\nfunction SchemaViewer({\n  table,\n  onReferenceClick,\n  filter: controlledFilter,\n  defaultFilter = \"\",\n  onFilterChange,\n  searchable = true,\n  className,\n  \"aria-label\": ariaLabel,\n  ...props\n}: SchemaViewerProps) {\n  const filterInputRef = React.useRef<HTMLInputElement>(null)\n  const { value: filterQuery, setValue: setFilterQuery } = useControllableState(\n    {\n      value: controlledFilter,\n      defaultValue: defaultFilter,\n      onChange: onFilterChange,\n    }\n  )\n\n  const tableName = formatTableName(table)\n  const needle = searchable ? filterQuery.trim() : \"\"\n  const visibleColumns = React.useMemo(\n    () => (needle ? filterColumns(table.columns, needle) : table.columns),\n    [table.columns, needle]\n  )\n\n  const totalCount = table.columns.length\n  const visibleCount = visibleColumns.length\n\n  function clearFilter() {\n    setFilterQuery(\"\")\n    filterInputRef.current?.focus()\n  }\n\n  return (\n    <div\n      data-slot=\"schema-viewer\"\n      className={cn(\n        \"flex w-full flex-col overflow-hidden rounded-box border bg-card text-card-foreground\",\n        className\n      )}\n      {...props}\n    >\n      <div\n        data-slot=\"schema-viewer-header\"\n        className=\"flex flex-col gap-2.5 border-b bg-muted/30 px-3.5 py-3 sm:flex-row sm:items-center sm:justify-between\"\n      >\n        <div className=\"flex min-w-0 flex-col gap-0.5\">\n          <div className=\"flex items-center gap-2\">\n            <Table2Icon\n              aria-hidden\n              className=\"size-4 shrink-0 text-muted-foreground\"\n            />\n            <span className=\"truncate font-mono text-sm font-semibold text-foreground\">\n              {tableName}\n            </span>\n            <span className=\"shrink-0 text-xs text-muted-foreground tabular-nums\">\n              {needle\n                ? `${visibleCount} of ${totalCount} ${totalCount === 1 ? \"column\" : \"columns\"}`\n                : `${totalCount} ${totalCount === 1 ? \"column\" : \"columns\"}`}\n            </span>\n          </div>\n          {table.description && (\n            <p className=\"text-xs text-muted-foreground\">{table.description}</p>\n          )}\n        </div>\n\n        {searchable && (\n          <div className=\"relative w-full sm:w-56\">\n            <SearchIcon\n              aria-hidden\n              className=\"pointer-events-none absolute top-1/2 left-2.5 size-3.5 -translate-y-1/2 text-muted-foreground\"\n            />\n            <Input\n              ref={filterInputRef}\n              type=\"search\"\n              aria-label=\"Filter columns\"\n              placeholder=\"Filter columns\"\n              value={filterQuery}\n              onChange={(event) => setFilterQuery(event.target.value)}\n              className={cn(\"h-control-sm py-0 pl-8 text-xs\", needle && \"pr-8\")}\n            />\n            {needle && (\n              <button\n                type=\"button\"\n                aria-label=\"Clear filter\"\n                onClick={clearFilter}\n                className=\"absolute top-1/2 right-1.5 inline-flex size-5 -translate-y-1/2 items-center justify-center rounded-sm text-muted-foreground outline-none hover:bg-accent hover:text-foreground focus-visible:ring-[3px] focus-visible:ring-ring/50\"\n              >\n                <XIcon aria-hidden className=\"size-3.5\" />\n              </button>\n            )}\n          </div>\n        )}\n      </div>\n\n      <div className=\"overflow-x-auto\">\n        <table\n          aria-label={ariaLabel ?? `${tableName} schema`}\n          className=\"w-full border-collapse text-left text-xs\"\n        >\n          <thead>\n            <tr className=\"border-b bg-muted/40 text-muted-foreground\">\n              <th scope=\"col\" className=\"px-3.5 py-2 font-medium\">\n                Column\n              </th>\n              <th scope=\"col\" className=\"px-3.5 py-2 font-medium\">\n                Type\n              </th>\n              <th scope=\"col\" className=\"px-3.5 py-2 font-medium\">\n                Nullable\n              </th>\n              <th scope=\"col\" className=\"px-3.5 py-2 font-medium\">\n                Default\n              </th>\n              <th scope=\"col\" className=\"px-3.5 py-2 font-medium\">\n                Constraints\n              </th>\n            </tr>\n          </thead>\n          <tbody className=\"divide-y\">\n            {totalCount === 0 ? (\n              <tr>\n                <td\n                  colSpan={5}\n                  className=\"px-3.5 py-6 text-center text-muted-foreground\"\n                >\n                  No columns defined\n                </td>\n              </tr>\n            ) : visibleCount === 0 ? (\n              <tr>\n                <td colSpan={5} className=\"px-3.5 py-6 text-center\">\n                  <div className=\"flex flex-col items-center justify-center gap-1.5\">\n                    <SearchXIcon\n                      aria-hidden\n                      className=\"size-5 text-muted-foreground\"\n                    />\n                    <p className=\"text-sm font-medium text-foreground\">\n                      No columns found\n                    </p>\n                    <p className=\"text-xs text-muted-foreground\">\n                      Nothing matches &ldquo;{needle}&rdquo;\n                    </p>\n                    <Button\n                      type=\"button\"\n                      variant=\"ghost\"\n                      size=\"sm\"\n                      className=\"mt-1\"\n                      onClick={clearFilter}\n                    >\n                      Clear filter\n                    </Button>\n                  </div>\n                </td>\n              </tr>\n            ) : (\n              visibleColumns.map((column) => {\n                const badges = getColumnBadges(column)\n                const nullable = isColumnNullable(column)\n                const reference = column.references?.trim()\n\n                return (\n                  <tr\n                    key={column.name}\n                    data-slot=\"schema-viewer-row\"\n                    className=\"transition-colors hover:bg-muted/30\"\n                  >\n                    <th\n                      scope=\"row\"\n                      className=\"px-3.5 py-2.5 align-top font-mono font-medium text-foreground\"\n                    >\n                      <div className=\"flex flex-col gap-0.5\">\n                        <span className=\"inline-flex items-center gap-1.5\">\n                          {column.primaryKey && (\n                            <KeyRoundIcon\n                              aria-hidden\n                              className=\"size-3 shrink-0 text-primary\"\n                            />\n                          )}\n                          <span>{column.name}</span>\n                        </span>\n                        {column.description && (\n                          <span className=\"font-sans text-[11px] font-normal text-muted-foreground\">\n                            {column.description}\n                          </span>\n                        )}\n                      </div>\n                    </th>\n                    <td className=\"px-3.5 py-2.5 align-top font-mono whitespace-nowrap text-muted-foreground\">\n                      {column.type}\n                    </td>\n                    <td className=\"px-3.5 py-2.5 align-top whitespace-nowrap\">\n                      <span\n                        className={cn(\n                          \"font-mono text-[11px]\",\n                          nullable\n                            ? \"text-muted-foreground\"\n                            : \"font-medium text-foreground\"\n                        )}\n                      >\n                        {nullable ? \"NULL\" : \"NOT NULL\"}\n                      </span>\n                    </td>\n                    <td className=\"px-3.5 py-2.5 align-top font-mono whitespace-nowrap text-muted-foreground\">\n                      {column.default !== undefined ? (\n                        <span className=\"text-foreground\">\n                          {column.default}\n                        </span>\n                      ) : (\n                        <span role=\"img\" aria-label=\"No default\">\n                          —\n                        </span>\n                      )}\n                    </td>\n                    <td className=\"px-3.5 py-2.5 align-top\">\n                      <div className=\"flex flex-wrap items-center gap-1.5\">\n                        {badges.map((kind) => {\n                          const meta = SCHEMA_BADGE_META[kind]\n                          return (\n                            <Badge\n                              key={kind}\n                              role=\"img\"\n                              variant={BADGE_VARIANT[kind]}\n                              title={meta.label}\n                              aria-label={meta.label}\n                              className=\"h-5 px-1.5 font-mono text-[10px]\"\n                            >\n                              {meta.shortLabel}\n                            </Badge>\n                          )\n                        })}\n                        {reference && (\n                          <a\n                            href={`#${encodeURIComponent(reference)}`}\n                            aria-label={`References ${reference}`}\n                            onClick={(event) => {\n                              if (onReferenceClick) {\n                                event.preventDefault()\n                                onReferenceClick(reference, column)\n                              }\n                            }}\n                            className=\"inline-flex items-center gap-1 rounded-sm font-mono text-xs text-primary underline decoration-primary/40 underline-offset-2 outline-none hover:decoration-primary focus-visible:ring-[3px] focus-visible:ring-ring/50\"\n                          >\n                            <Link2Icon\n                              aria-hidden\n                              className=\"size-3 shrink-0\"\n                            />\n                            <span>{reference}</span>\n                          </a>\n                        )}\n                        {badges.length === 0 && !reference && (\n                          <span\n                            role=\"img\"\n                            aria-label=\"No constraints\"\n                            className=\"text-muted-foreground\"\n                          >\n                            —\n                          </span>\n                        )}\n                      </div>\n                    </td>\n                  </tr>\n                )\n              })\n            )}\n          </tbody>\n        </table>\n      </div>\n\n      <span role=\"status\" className=\"sr-only\">\n        {needle\n          ? `${visibleCount} of ${totalCount} ${totalCount === 1 ? \"column\" : \"columns\"} matching ${needle}`\n          : \"\"}\n      </span>\n    </div>\n  )\n}\n\nexport { SchemaViewer }\n",
      "type": "registry:ui"
    },
    {
      "path": "registry/default/lib/schema-viewer-utils.ts",
      "content": "export type SchemaBadgeKind = \"pk\" | \"fk\" | \"unique\" | \"index\"\n\nexport type SchemaForeignKeyRef = {\n  table: string\n  column: string\n  raw: string\n}\n\nexport type SchemaColumn = {\n  name: string\n  type: string\n  primaryKey?: boolean\n  references?: string\n  unique?: boolean\n  indexed?: boolean\n  nullable?: boolean\n  default?: string\n  description?: string\n}\n\nexport type SchemaTable = {\n  name: string\n  schema?: string\n  description?: string\n  columns: SchemaColumn[]\n}\n\nexport const SCHEMA_BADGE_META: Record<\n  SchemaBadgeKind,\n  { shortLabel: string; label: string }\n> = {\n  pk: { shortLabel: \"PK\", label: \"Primary key\" },\n  fk: { shortLabel: \"FK\", label: \"Foreign key\" },\n  unique: { shortLabel: \"UQ\", label: \"Unique\" },\n  index: { shortLabel: \"IDX\", label: \"Indexed\" },\n}\n\n/**\n * Parse a foreign-key reference string into table and column parts.\n * Dotted strings are assumed to end with a column name (`table.column` or\n * `schema.table.column`); a bare name without dots returns `column: \"\"`.\n */\nexport function parseReference(ref: string): SchemaForeignKeyRef | null {\n  const trimmed = ref.trim()\n  if (!trimmed) return null\n  const lastDot = trimmed.lastIndexOf(\".\")\n  if (lastDot <= 0 || lastDot === trimmed.length - 1) {\n    return { table: trimmed, column: \"\", raw: trimmed }\n  }\n  return {\n    table: trimmed.slice(0, lastDot),\n    column: trimmed.slice(lastDot + 1),\n    raw: trimmed,\n  }\n}\n\nexport function getColumnBadges(column: SchemaColumn): SchemaBadgeKind[] {\n  const badges: SchemaBadgeKind[] = []\n  if (column.primaryKey) badges.push(\"pk\")\n  if (column.references?.trim()) badges.push(\"fk\")\n  if (column.unique) badges.push(\"unique\")\n  if (column.indexed) badges.push(\"index\")\n  return badges\n}\n\nexport function isColumnNullable(column: SchemaColumn): boolean {\n  if (column.nullable !== undefined) return column.nullable\n  return !column.primaryKey\n}\n\nexport function filterColumns(\n  columns: SchemaColumn[],\n  query: string\n): SchemaColumn[] {\n  const needle = query.trim().toLowerCase()\n  if (!needle) return columns\n  return columns.filter((column) => column.name.toLowerCase().includes(needle))\n}\n\nexport function formatTableName(\n  table: Pick<SchemaTable, \"name\" | \"schema\">\n): string {\n  return table.schema ? `${table.schema}.${table.name}` : table.name\n}\n\nexport function summarizeColumns(columns: SchemaColumn[]) {\n  let primaryKeys = 0\n  let foreignKeys = 0\n  let unique = 0\n  let indexed = 0\n\n  for (const column of columns) {\n    if (column.primaryKey) primaryKeys++\n    if (column.references?.trim()) foreignKeys++\n    if (column.unique) unique++\n    if (column.indexed) indexed++\n  }\n\n  return {\n    total: columns.length,\n    primaryKeys,\n    foreignKeys,\n    unique,\n    indexed,\n  }\n}\n",
      "type": "registry:lib"
    }
  ],
  "type": "registry:ui"
}