-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJson.tsx
More file actions
178 lines (167 loc) · 6.13 KB
/
Json.tsx
File metadata and controls
178 lines (167 loc) · 6.13 KB
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
import { ReactNode, useState } from 'react'
import styles from './Json.module.css'
import { isPrimitive, shouldObjectCollapse } from './helpers.js'
import { cn } from '../../lib'
import { useWidth } from './useWidth.js'
interface JsonProps {
json: unknown
label?: string
className?: string
}
/**
* JSON viewer component with collapsible objects and arrays.
*/
export default function Json({ json, label, className }: JsonProps): ReactNode {
return <div className={cn(styles.json, className)}>
<JsonContent json={json} label={label} />
</div>
}
function JsonContent({ json, label }: JsonProps): ReactNode {
let div
if (Array.isArray(json)) {
div = <JsonArray array={json} label={label} />
} else if (typeof json === 'object' && json !== null) {
div = <JsonObject label={label} obj={json} />
} else {
// primitive
const key = label ? <span className={styles.key}>{label}: </span> : ''
if (typeof json === 'string') {
div = <>{key}<span className={styles.string}>{JSON.stringify(json)}</span></>
} else if (typeof json === 'number') {
div = <>{key}<span className={styles.number}>{JSON.stringify(json)}</span></>
} else if (typeof json === 'bigint') {
// it's not really json, but show it anyway
div = <>{key}<span className={styles.number}>{json.toString()}</span></>
} else if (json === undefined) {
// it's not json
div = <>{key}<span className={styles.other}>undefined</span></>
} else {
div = <>{key}<span className={styles.other}>{JSON.stringify(json)}</span></>
}
}
return div
}
function CollapsedArray({ array }: {array: unknown[]}): ReactNode {
const { elementRef, width } = useWidth<HTMLSpanElement>()
const maxCharacterCount = Math.max(20, Math.floor(width / 8))
const separator = ', '
const children: ReactNode[] = []
let suffix: string | undefined = undefined
let characterCount = 0
for (const [index, value] of array.entries()) {
if (index > 0) {
characterCount += separator.length
children.push(<span key={`separator-${index - 1}`}>{separator}</span>)
}
// should we continue?
if (isPrimitive(value)) {
const asString = typeof value === 'bigint' ? value.toString() :
value === undefined ? 'undefined' /* see JsonContent - even if JSON.stringify([undefined]) === '[null]' */:
JSON.stringify(value)
characterCount += asString.length
if (characterCount < maxCharacterCount) {
children.push(<JsonContent json={value} key={`value-${index}`} />)
continue
}
}
// no: it was the last entry
children.push(<span key="rest">...</span>)
suffix = ` length: ${array.length}`
break
}
return (
<>
<span className={styles.array}>{'['}</span>
<span ref={elementRef} className={styles.array}>{children}</span>
<span className={styles.array}>{']'}</span>
{suffix && <span className={styles.comment}>{suffix}</span>}
</>
)
}
function JsonArray({ array, label }: { array: unknown[], label?: string }): ReactNode {
const [collapsed, setCollapsed] = useState(shouldObjectCollapse(array))
const key = label ? <span className={styles.key}>{label}: </span> : ''
if (collapsed) {
return <div className={styles.clickable} onClick={() => { setCollapsed(false) }}>
<span className={styles.drill}>{'\u25B6'}</span>
{key}
<CollapsedArray array={array}></CollapsedArray>
</div>
}
return <>
<div className={styles.clickable} onClick={() => { setCollapsed(true) }}>
<span className={styles.drill}>{'\u25BC'}</span>
{key}
<span className={styles.array}>{'['}</span>
</div>
<ul>
{array.map((item, index) => <li key={index}>{<Json json={item} />}</li>)}
</ul>
<div className={styles.array}>{']'}</div>
</>
}
function CollapsedObject({ obj }: {obj: object}): ReactNode {
const { elementRef, width } = useWidth<HTMLSpanElement>()
const maxCharacterCount = Math.max(20, Math.floor(width / 8))
const separator = ', '
const kvSeparator = ': '
const children: ReactNode[] = []
let suffix: string | undefined = undefined
const entries = Object.entries(obj)
let characterCount = 0
for (const [index, [key, value]] of entries.entries()) {
if (index > 0) {
characterCount += separator.length
children.push(<span key={`separator-${index - 1}`}>{separator}</span>)
}
// should we continue?
if (isPrimitive(value)) {
const asString = typeof value === 'bigint' ? value.toString() :
value === undefined ? 'undefined' /* see JsonContent - even if JSON.stringify([undefined]) === '[null]' */:
JSON.stringify(value)
characterCount += key.length + kvSeparator.length + asString.length
if (characterCount < maxCharacterCount) {
children.push(<JsonContent json={value as unknown} label={key} key={`value-${index}`} />)
continue
}
}
// no: it was the last entry
children.push(<span key="rest">...</span>)
suffix = ` entries: ${entries.length}`
break
}
return (
<>
<span className={styles.object}>{'{'}</span>
<span ref={elementRef} className={styles.object}>{children}</span>
<span className={styles.object}>{'}'}</span>
{suffix && <span className={styles.comment}>{suffix}</span>}
</>
)
}
function JsonObject({ obj, label }: { obj: object, label?: string }): ReactNode {
const [collapsed, setCollapsed] = useState(shouldObjectCollapse(obj))
const key = label ? <span className={styles.key}>{label}: </span> : ''
if (collapsed) {
return <div className={styles.clickable} onClick={() => { setCollapsed(false) }}>
<span className={styles.drill}>{'\u25B6'}</span>
{key}
<CollapsedObject obj={obj}></CollapsedObject>
</div>
}
return <>
<div className={styles.clickable} onClick={() => { setCollapsed(true) }}>
<span className={styles.drill}>{'\u25BC'}</span>
{key}
<span className={styles.object}>{'{'}</span>
</div>
<ul>
{Object.entries(obj).map(([key, value]) =>
<li key={key}>
<Json json={value as unknown} label={key} />
</li>
)}
</ul>
<div className={styles.object}>{'}'}</div>
</>
}