"use client"; import { useMemo, useState } from "react"; import { cn, toPrettyJson } from "@/lib/utils"; type DataViewerProps = { title: string; value: unknown; defaultExpandedDepth?: number; maxHeight?: number; }; const highlightJson = (json: string | undefined): React.ReactNode[] => { if (!json) return []; const parts: React.ReactNode[] = []; let i = 0; const regex = /("(?:\\.|[^"\\])*")\s*:|("(?:\\.|[^"\\])*")|(-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)|(\btrue\b|\bfalse\b)|(\bnull\b)/g; let lastIndex = 0; let match: RegExpExecArray | null = regex.exec(json); while (match !== null) { if (match.index > lastIndex) { parts.push( {json.slice(lastIndex, match.index)} , ); } if (match[1]) { // Key parts.push( {match[1]} , ); parts.push( : , ); } else if (match[2]) { // String value parts.push( {match[2]} , ); } else if (match[3]) { // Number parts.push( {match[3]} , ); } else if (match[4]) { // Boolean parts.push( {match[4]} , ); } else if (match[5]) { // Null parts.push( {match[5]} , ); } lastIndex = regex.lastIndex; match = regex.exec(json); } if (lastIndex < json.length) { parts.push( {json.slice(lastIndex)} , ); } return parts; }; const renderPrimitive = ({ value }: { value: unknown }) => { if (value === null) return null; if (value === undefined) return undefined; if (typeof value === "string") return "{value}"; if (typeof value === "number" || typeof value === "boolean") { return ( {String(value)} ); } return ( {String(value)} ); }; const JsonNode = ({ name, value, depth, defaultExpandedDepth, }: { name: string; value: unknown; depth: number; defaultExpandedDepth: number; }) => { if (value === null || typeof value !== "object") { return (
{title}
{highlightJson(prettyJson)}
)}