{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "opentui/json",
  "title": "JSON",
  "description": "Pretty-printed JSON with collapsible nodes",
  "dependencies": [
    "@opentui/react"
  ],
  "registryDependencies": [
    "https://termcn.dev/r/opentui/use-theme.json"
  ],
  "files": [
    {
      "path": "registry/opentui/ui/json.tsx",
      "content": "/* @jsxImportSource @opentui/react */\nimport { useKeyboard } from \"@opentui/react\";\nimport React, { useState } from \"react\";\nimport type { ReactNode } from \"react\";\n\nimport { useTheme } from \"@/hooks/use-theme\";\n\nexport interface JSONViewProps {\n  data: unknown;\n  indent?: number;\n  collapsed?: boolean;\n  label?: string;\n}\n\ninterface JSONLine {\n  path: string;\n  depth: number;\n  key?: string;\n  value?: unknown;\n  type: \"open\" | \"close\" | \"primitive\";\n  openChar?: string;\n  closeChar?: string;\n  isLast: boolean;\n  collapsible: boolean;\n}\n\nconst buildLines = (\n  data: unknown,\n  depth: number,\n  key: string | undefined,\n  isLast: boolean,\n  pathPrefix: string\n): JSONLine[] => {\n  const path = key === undefined ? pathPrefix : `${pathPrefix}.${key}`;\n\n  if (Array.isArray(data)) {\n    const lines: JSONLine[] = [\n      {\n        closeChar: \"]\",\n        collapsible: true,\n        depth,\n        isLast,\n        key,\n        openChar: \"[\",\n        path,\n        type: \"open\",\n      },\n    ];\n    for (const [idx, item] of data.entries()) {\n      const childLines = buildLines(\n        item,\n        depth + 1,\n        undefined,\n        idx === data.length - 1,\n        path\n      );\n      lines.push(...childLines);\n    }\n    lines.push({\n      closeChar: \"]\",\n      collapsible: false,\n      depth,\n      isLast,\n      path: `${path}_close`,\n      type: \"close\",\n    });\n    return lines;\n  }\n\n  if (data !== null && typeof data === \"object\") {\n    const entries = Object.entries(data as Record<string, unknown>);\n    const lines: JSONLine[] = [\n      {\n        closeChar: \"}\",\n        collapsible: true,\n        depth,\n        isLast,\n        key,\n        openChar: \"{\",\n        path,\n        type: \"open\",\n      },\n    ];\n    for (const [idx, [k, v]] of entries.entries()) {\n      const childLines = buildLines(\n        v,\n        depth + 1,\n        k,\n        idx === entries.length - 1,\n        path\n      );\n      lines.push(...childLines);\n    }\n    lines.push({\n      closeChar: \"}\",\n      collapsible: false,\n      depth,\n      isLast,\n      path: `${path}_close`,\n      type: \"close\",\n    });\n    return lines;\n  }\n\n  return [\n    {\n      collapsible: false,\n      depth,\n      isLast,\n      key,\n      path,\n      type: \"primitive\",\n      value: data,\n    },\n  ];\n};\n\nexport const JSONView = ({\n  data,\n  indent = 2,\n  collapsed = false,\n  label,\n}: JSONViewProps) => {\n  const theme = useTheme();\n  const [collapsedPaths, setCollapsedPaths] = useState<Set<string>>(new Set());\n  const [cursor, setCursor] = useState(0);\n\n  const lines = buildLines(data, 0, undefined, true, \"root\");\n\n  const visibleLines: JSONLine[] = [];\n  const collapsedOpenPaths = new Set<string>();\n\n  for (const line of lines) {\n    const isHidden = [...collapsedOpenPaths].some(\n      (cp) =>\n        line.path.startsWith(`${cp}.`) ||\n        (line.path.startsWith(`${cp}_close`) && line.path !== `${cp}_close`)\n    );\n    if (line.type === \"close\") {\n      const openPath = line.path.replace(\"_close\", \"\");\n      if (collapsedOpenPaths.has(openPath)) {\n        continue;\n      }\n    }\n    if (!isHidden) {\n      visibleLines.push(line);\n    }\n    if (\n      line.type === \"open\" &&\n      (collapsedPaths.has(line.path) ||\n        (collapsed && collapsedPaths.size === 0 && line.depth === 0))\n    ) {\n      collapsedOpenPaths.add(line.path);\n    }\n  }\n\n  useKeyboard((key) => {\n    if (key.name === \"up\") {\n      setCursor((c) => Math.max(0, c - 1));\n    } else if (key.name === \"down\") {\n      setCursor((c) => Math.min(visibleLines.length - 1, c + 1));\n    } else if (key.name === \"space\") {\n      const line = visibleLines[cursor];\n      if (line && line.collapsible) {\n        setCollapsedPaths((prev) => {\n          const next = new Set(prev);\n          if (next.has(line.path)) {\n            next.delete(line.path);\n          } else {\n            next.add(line.path);\n          }\n          return next;\n        });\n      }\n    }\n  });\n\n  const renderValue = (value: unknown): ReactNode => {\n    if (value === null) {\n      return <text fg={theme.colors.mutedForeground}>null</text>;\n    }\n    if (typeof value === \"string\") {\n      return <text fg={theme.colors.success}>{`\"${value}\"`}</text>;\n    }\n    if (typeof value === \"number\" || typeof value === \"boolean\") {\n      return <text fg={theme.colors.warning}>{String(value)}</text>;\n    }\n    return <text fg={theme.colors.foreground}>{String(value)}</text>;\n  };\n\n  return (\n    <box flexDirection=\"column\">\n      {label && (\n        <text fg={theme.colors.primary}>\n          <b>{label}</b>\n        </text>\n      )}\n      {visibleLines.map((line, idx) => {\n        const isCursor = idx === cursor;\n        const spaces = \" \".repeat(line.depth * indent);\n        if (line.type === \"open\") {\n          const isCollapsed =\n            collapsedPaths.has(line.path) ||\n            (collapsed && collapsedPaths.size === 0 && line.depth === 0);\n          return (\n            <box key={line.path + idx}>\n              <text bg={isCursor ? theme.colors.selection : undefined}>\n                {spaces}\n                {line.key !== undefined ? (\n                  <>\n                    <text fg={theme.colors.primary}>{`\"${line.key}\"`}</text>\n                    <text fg={theme.colors.foreground}>{\": \"}</text>\n                  </>\n                ) : null}\n                <text fg={theme.colors.foreground}>{line.openChar}</text>\n                {isCollapsed ? (\n                  <>\n                    <text fg={theme.colors.mutedForeground}>{\" ... \"}</text>\n                    <text fg={theme.colors.foreground}>{line.closeChar}</text>\n                    {!line.isLast ? (\n                      <text fg={theme.colors.foreground}>,</text>\n                    ) : null}\n                  </>\n                ) : null}\n              </text>\n            </box>\n          );\n        }\n        if (line.type === \"close\") {\n          return (\n            <box key={line.path + idx}>\n              <text bg={isCursor ? theme.colors.selection : undefined}>\n                {spaces}\n                <text fg={theme.colors.foreground}>{line.closeChar}</text>\n                {!line.isLast ? (\n                  <text fg={theme.colors.foreground}>,</text>\n                ) : null}\n              </text>\n            </box>\n          );\n        }\n        return (\n          <box key={line.path + idx}>\n            <text bg={isCursor ? theme.colors.selection : undefined}>\n              {spaces}\n              {line.key !== undefined ? (\n                <>\n                  <text fg={theme.colors.primary}>{`\"${line.key}\"`}</text>\n                  <text fg={theme.colors.foreground}>{\": \"}</text>\n                </>\n              ) : null}\n              {renderValue(line.value)}\n              {!line.isLast ? (\n                <text fg={theme.colors.foreground}>,</text>\n              ) : null}\n            </text>\n          </box>\n        );\n      })}\n    </box>\n  );\n};\n",
      "type": "registry:ui",
      "target": "components/ui/json.tsx"
    }
  ],
  "categories": [
    "data"
  ],
  "type": "registry:ui"
}