File size: 6,228 Bytes
7c5ed63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { useState, memo } from 'react';

import { CopyButton } from './CopyButton';

interface JsonViewerProps {
  value: unknown;
  /** Default-open nesting depth */
  defaultOpenDepth?: number;
}

/** Strings longer than this get an inline expand affordance instead of a
 *  truncated preview-only display. */
const LONG_STRING_THRESHOLD = 200;

export function JsonViewer({ value, defaultOpenDepth = 2 }: JsonViewerProps) {
  return (
    <div className="font-mono text-[12px] leading-[1.5]">
      <Node value={value} depth={0} defaultOpenDepth={defaultOpenDepth} keyPath="" />
    </div>
  );
}

interface NodeProps {
  value: unknown;
  depth: number;
  defaultOpenDepth: number;
  keyPath: string;
  keyLabel?: string | number;
  isLast?: boolean;
}

const Node = memo(function Node({
  value,
  depth,
  defaultOpenDepth,
  keyPath,
  keyLabel,
  isLast,
}: NodeProps) {
  const [open, setOpen] = useState(depth < defaultOpenDepth);

  if (value === null)
    return <Leaf keyLabel={keyLabel} repr="null" color="text-fg-3" isLast={isLast} />;
  if (value === undefined)
    return <Leaf keyLabel={keyLabel} repr="undefined" color="text-fg-3" isLast={isLast} />;
  if (typeof value === 'boolean')
    return <Leaf keyLabel={keyLabel} repr={String(value)} color="text-[var(--color-cat-config)]" isLast={isLast} />;
  if (typeof value === 'number')
    return <Leaf keyLabel={keyLabel} repr={String(value)} color="text-[var(--color-sev-info)]" isLast={isLast} />;
  if (typeof value === 'string') {
    if (value.length <= LONG_STRING_THRESHOLD) {
      return (
        <Leaf
          keyLabel={keyLabel}
          repr={JSON.stringify(value)}
          color="text-[var(--color-cat-ephemeral)]"
          isLast={isLast}
        />
      );
    }
    return (
      <div>
        <button
          onClick={() => {
            setOpen((v) => !v);
          }}
          className="flex items-baseline gap-1 text-left hover:text-fg-0"
        >
          <span className="text-fg-3 w-3 shrink-0 inline-block">{open ? '▾' : '▸'}</span>
          {keyLabel !== undefined ? (
            <>
              <span className="text-fg-1">{keyLabel}</span>
              <span className="text-fg-3">:</span>
            </>
          ) : null}
          <span className="truncate text-[var(--color-cat-ephemeral)]">
            {`"${value.slice(0, LONG_STRING_THRESHOLD)}…"`}
          </span>
          <span className="text-fg-3 shrink-0">({value.length.toLocaleString()} chars)</span>
        </button>
        {open ? (
          <div className="ml-[5px] my-1 border-l border-border pl-3">
            <div className="relative border border-border bg-surface-0">
              <div className="absolute top-1 right-1 z-10">
                <CopyButton value={value} className="border border-border bg-surface-1 px-1.5 py-0.5" />
              </div>
              <pre className="max-h-[60vh] overflow-auto whitespace-pre-wrap break-words p-2 pr-16 font-mono text-[12px] leading-[1.55] text-fg-0">
                {value}
              </pre>
            </div>
          </div>
        ) : null}
      </div>
    );
  }
  if (Array.isArray(value)) {
    if (value.length === 0)
      return <Leaf keyLabel={keyLabel} repr="[]" color="text-fg-3" isLast={isLast} />;
    return (
      <div>
        <button
          onClick={() =>{  setOpen((v) => !v); }}
          className="flex items-baseline gap-1 text-left hover:text-fg-0"
        >
          <span className="text-fg-3 w-3 shrink-0 inline-block">{open ? '▾' : '▸'}</span>
          {keyLabel !== undefined ? (
            <span className="text-fg-1">{keyLabel}</span>
          ) : null}
          {keyLabel !== undefined ? <span className="text-fg-3">:</span> : null}
          <span className="text-fg-3">
            [{value.length}]
          </span>
        </button>
        {open ? (
          <div className="border-l border-border ml-[5px] pl-3">
            {value.map((v, i) => (
              <Node
                key={i}
                value={v}
                depth={depth + 1}
                defaultOpenDepth={defaultOpenDepth}
                keyPath={`${keyPath}[${i}]`}
                keyLabel={i}
                isLast={i === value.length - 1}
              />
            ))}
          </div>
        ) : null}
      </div>
    );
  }
  if (typeof value === 'object') {
    const entries = Object.entries(value as Record<string, unknown>);
    if (entries.length === 0)
      return <Leaf keyLabel={keyLabel} repr="{}" color="text-fg-3" isLast={isLast} />;
    return (
      <div>
        <button
          onClick={() =>{  setOpen((v) => !v); }}
          className="flex items-baseline gap-1 text-left hover:text-fg-0"
        >
          <span className="text-fg-3 w-3 shrink-0 inline-block">{open ? '▾' : '▸'}</span>
          {keyLabel !== undefined ? (
            <span className="text-fg-1">{keyLabel}</span>
          ) : null}
          {keyLabel !== undefined ? <span className="text-fg-3">:</span> : null}
          <span className="text-fg-3">
            {'{'}
            {entries.length}
            {'}'}
          </span>
        </button>
        {open ? (
          <div className="border-l border-border ml-[5px] pl-3">
            {entries.map(([k, v], i) => (
              <Node
                key={k}
                value={v}
                depth={depth + 1}
                defaultOpenDepth={defaultOpenDepth}
                keyPath={`${keyPath}.${k}`}
                keyLabel={k}
                isLast={i === entries.length - 1}
              />
            ))}
          </div>
        ) : null}
      </div>
    );
  }
  return <Leaf keyLabel={keyLabel} repr={typeof value} color="text-fg-3" isLast={isLast} />;
});

function Leaf({
  keyLabel,
  repr,
  color,
}: {
  keyLabel?: string | number;
  repr: string;
  color: string;
  isLast?: boolean;
}) {
  return (
    <div className="flex items-baseline gap-1">
      <span className="w-3 shrink-0" />
      {keyLabel !== undefined ? (
        <>
          <span className="text-fg-1">{keyLabel}</span>
          <span className="text-fg-3">:</span>
        </>
      ) : null}
      <span className={color}>{repr}</span>
    </div>
  );
}