Skip to content
Source
Sign inSign up
TanStack/devtools
Open History to see the last commit

tree.tsx

444 lines11.6 KB

1import { For, Match, Show, Switch, createSignal } from 'solid-js'
2import clsx from 'clsx'
3import dayjs from 'dayjs'
4​
5// css
6import { createStyles, css } from '../styles/use-styles'
7​
8// icons
9import { CopiedCopier, Copier, ErrorCopier } from './icons'
10​
11// utils
12import type { CollapsiblePaths } from '../utils/deep-keys'
13​
14export function JsonTree<TData, TName extends CollapsiblePaths<TData>>(props: {
15 value: TData
16 copyable?: boolean
17​
18 defaultExpansionDepth?: number
19 collapsePaths?: Array<TName>
20​
21 config?: { dateFormat?: string }
22}) {
23 return (
24 <JsonValue
25 isRoot
26 value={props.value}
27 copyable={props.copyable}
28 depth={0}
29 defaultExpansionDepth={props.defaultExpansionDepth ?? 1}
30 path=""
31 collapsePaths={props.collapsePaths}
32 config={props.config}
33 />
34 )
36​
37function JsonValue(props: {
38 value: any
39 keyName?: string
40 isRoot?: boolean
41 isLastKey?: boolean
42 copyable?: boolean
43​
44 defaultExpansionDepth: number
45 depth: number
46​
47 collapsePaths?: Array<string>
48 path: string
49​
50 config?: { dateFormat?: string }
51}) {
52 const styles = createStyles()
53​
54 return (
55 <span class={styles().tree.valueContainer(props.isRoot ?? false)}>
56 {props.keyName &&
57 typeof props.value !== 'object' &&
58 !Array.isArray(props.value) && (
59 <span class={styles().tree.valueKey}>
60 &quot;{props.keyName}&quot;:{' '}
61 </span>
62 )}
63​
64 {(() => {
65 if (typeof props.value === 'string') {
66 return (
67 <span class={styles().tree.valueString}>
68 &quot;{props.value}&quot;
69 </span>
70 )
71 }
72​
73 if (typeof props.value === 'number') {
74 return (
75 <span class={styles().tree.valueNumber}>{String(props.value)}</span>
76 )
77 }
78​
79 if (typeof props.value === 'boolean') {
80 return (
81 <span class={styles().tree.valueBoolean}>
82 {String(props.value)}
83 </span>
84 )
85 }
86​
87 if (props.value === null) {
88 return <span class={styles().tree.valueNull}>null</span>
89 }
90​
91 if (props.value === undefined) {
92 return <span class={styles().tree.valueNull}>undefined</span>
93 }
94​
95 if (typeof props.value === 'function') {
96 return (
97 <span class={styles().tree.valueFunction}>
98 {String(props.value)}
99 </span>
100 )
101 }
102​
103 if (Array.isArray(props.value)) {
104 return (
105 <ArrayValue
106 defaultExpansionDepth={props.defaultExpansionDepth}
107 depth={props.depth}
108 copyable={props.copyable}
109 keyName={props.keyName}
110 value={props.value}
111 collapsePaths={props.collapsePaths}
112 path={props.path}
113 config={props.config}
114 />
115 )
116 }
117​
118 if (typeof props.value === 'object') {
119 return (
120 <ObjectValue
121 defaultExpansionDepth={props.defaultExpansionDepth}
122 depth={props.depth}
123 copyable={props.copyable}
124 keyName={props.keyName}
125 value={props.value}
126 collapsePaths={props.collapsePaths}
127 path={props.path}
128 config={props.config}
129 />
130 )
131 }
132​
133 return <span />
134 })()}
135 {props.copyable && (
136 <div class={clsx(styles().tree.actions, 'actions')}>
137 <CopyButton value={props.value} />
138 </div>
139 )}
140 {props.isLastKey || props.isRoot ? '' : <span>,</span>}
141 </span>
142 )
144​
145const ArrayValue = (props: {
146 value: Array<any>
147 copyable?: boolean
148 keyName?: string
149 defaultExpansionDepth: number
150 depth: number
151 collapsePaths?: Array<string>
152 path: string
153 config?: { dateFormat?: string }
154}) => {
155 const styles = createStyles()
156​
157 const [expanded, setExpanded] = createSignal(
158 props.depth <= props.defaultExpansionDepth &&
159 !props.collapsePaths?.includes(props.path),
160 )
161​
162 if (props.value.length === 0) {
163 return (
164 <span class={styles().tree.expanderContainer}>
165 {props.keyName && (
166 <span class={clsx(styles().tree.valueKey, styles().tree.collapsible)}>
167 &quot;{props.keyName}&quot;:{' '}
168 </span>
169 )}
170​
171 <span class={styles().tree.valueBraces}>[]</span>
172 </span>
173 )
174 }
175 return (
176 <span class={styles().tree.expanderContainer}>
177 <Expander
178 onClick={() => setExpanded(!expanded())}
179 expanded={expanded()}
180 />
181​
182 {props.keyName && (
183 <span
184 onclick={(e) => {
185 e.stopPropagation()
186 e.stopImmediatePropagation()
187 setExpanded(!expanded())
188 }}
189 class={clsx(styles().tree.valueKey, styles().tree.collapsible)}
190 >
191 &quot;{props.keyName}&quot;:{' '}
192 <span class={styles().tree.info}>{props.value.length} items</span>
193 </span>
194 )}
195​
196 <span class={styles().tree.valueBraces}>[</span>
197​
198 <Show when={expanded()}>
199 <span class={styles().tree.expandedLine(Boolean(props.keyName))}>
200 <For each={props.value}>
201 {(item, i) => {
202 const isLastKey = i() === props.value.length - 1
203 return (
204 <JsonValue
205 copyable={props.copyable}
206 value={item}
207 isLastKey={isLastKey}
208 defaultExpansionDepth={props.defaultExpansionDepth}
209 depth={props.depth + 1}
210 collapsePaths={props.collapsePaths}
211 path={props.path ? `${props.path}[${i()}]` : `[${i()}]`}
212 config={props.config}
213 />
214 )
215 }}
216 </For>
217 </span>
218 </Show>
219​
220 <Show when={!expanded()}>
221 <span
222 onClick={(e) => {
223 e.stopPropagation()
224 e.stopImmediatePropagation()
225 setExpanded(!expanded())
226 }}
227 class={clsx(styles().tree.valueKey, styles().tree.collapsible)}
228 >
229 {`...`}
230 </span>
231 </Show>
232 <span class={styles().tree.valueBraces}>]</span>
233 </span>
234 )
236​
237const ObjectValue = (props: {
238 value: Record<string, any>
239 keyName?: string
240 copyable?: boolean
241 defaultExpansionDepth: number
242 depth: number
243 collapsePaths?: Array<string>
244 path: string
245 config?: { dateFormat?: string }
246}) => {
247 const styles = createStyles()
248​
249 const [expanded, setExpanded] = createSignal(
250 props.depth <= props.defaultExpansionDepth &&
251 !props.collapsePaths?.includes(props.path),
252 )
253​
254 const keys = Object.keys(props.value)
255 const lastKeyName = keys[keys.length - 1]
256​
257 if (props.value instanceof Date) {
258 return (
259 <span class={styles().tree.expanderContainer}>
260 {props.keyName && (
261 <span class={clsx(styles().tree.valueKey, styles().tree.collapsible)}>
262 &quot;{props.keyName}&quot;:{' '}
263 </span>
264 )}
265​
266 <span class={styles().tree.valueBraces}>
267 {dayjs(props.value).format(
268 props.config?.dateFormat ? props.config.dateFormat : 'DDMMMYY',
269 )}
270 </span>
271 </span>
272 )
273 }
274​
275 if (keys.length === 0) {
276 return (
277 <span class={styles().tree.expanderContainer}>
278 {props.keyName && (
279 <span class={clsx(styles().tree.valueKey, styles().tree.collapsible)}>
280 &quot;{props.keyName}&quot;:{' '}
281 </span>
282 )}
283​
284 <span class={styles().tree.valueBraces}>{'{}'}</span>
285 </span>
286 )
287 }
288​
289 return (
290 <span class={styles().tree.expanderContainer}>
291 {props.keyName && (
292 <Expander
293 onClick={() => setExpanded(!expanded())}
294 expanded={expanded()}
295 />
296 )}
297​
298 {props.keyName && (
299 <span
300 onClick={(e) => {
301 e.stopPropagation()
302 e.stopImmediatePropagation()
303 setExpanded(!expanded())
304 }}
305 class={clsx(styles().tree.valueKey, styles().tree.collapsible)}
306 >
307 &quot;{props.keyName}&quot;:{' '}
308 <span class={styles().tree.info}>{keys.length} items</span>
309 </span>
310 )}
311​
312 <span class={styles().tree.valueBraces}>{'{'}</span>
313​
314 <Show when={expanded()}>
315 <span class={styles().tree.expandedLine(Boolean(props.keyName))}>
316 <For each={keys}>
317 {(k) => (
318 <>
319 <JsonValue
320 value={props.value[k]}
321 keyName={k}
322 isLastKey={lastKeyName === k}
323 copyable={props.copyable}
324 defaultExpansionDepth={props.defaultExpansionDepth}
325 depth={props.depth + 1}
326 collapsePaths={props.collapsePaths}
327 path={`${props.path}${props.path ? '.' : ''}${k}`}
328 config={props.config}
329 />
330 </>
331 )}
332 </For>
333 </span>
334 </Show>
335​
336 <Show when={!expanded()}>
337 <span
338 onClick={(e) => {
339 e.stopPropagation()
340 e.stopImmediatePropagation()
341 setExpanded(!expanded())
342 }}
343 class={clsx(styles().tree.valueKey, styles().tree.collapsible)}
344 >
345 {`...`}
346 </span>
347 </Show>
348​
349 <span class={styles().tree.valueBraces}>{'}'}</span>
350 </span>
351 )
353​
354type CopyState = 'NoCopy' | 'SuccessCopy' | 'ErrorCopy'
355​
356const CopyButton = (props: { value: unknown }) => {
357 const styles = createStyles()
358 const [copyState, setCopyState] = createSignal<CopyState>('NoCopy')
359​
360 return (
361 <button
362 class={styles().tree.actionButton}
363 title="Copy object to clipboard"
364 aria-label={`${
365 copyState() === 'NoCopy'
366 ? 'Copy object to clipboard'
367 : copyState() === 'SuccessCopy'
368 ? 'Object copied to clipboard'
369 : 'Error copying object to clipboard'
370 }`}
371 onClick={
372 copyState() === 'NoCopy'
373 ? () => {
374 navigator.clipboard
375 .writeText(JSON.stringify(props.value, null, 2))
376 .then(
377 () => {
378 setCopyState('SuccessCopy')
379 setTimeout(() => {
380 setCopyState('NoCopy')
381 }, 1500)
382 },
383 (err) => {
384 console.error('Failed to copy: ', err)
385 setCopyState('ErrorCopy')
386 setTimeout(() => {
387 setCopyState('NoCopy')
388 }, 1500)
389 },
390 )
391 }
392 : undefined
393 }
394 >
395 <Switch>
396 <Match when={copyState() === 'NoCopy'}>
397 <Copier />
398 </Match>
399 <Match when={copyState() === 'SuccessCopy'}>
400 <CopiedCopier theme={'dark'} />
401 </Match>
402 <Match when={copyState() === 'ErrorCopy'}>
403 <ErrorCopier />
404 </Match>
405 </Switch>
406 </button>
407 )
409​
410const Expander = (props: { expanded: boolean; onClick: () => void }) => {
411 const styles = createStyles()
412 return (
413 <span
414 onClick={props.onClick}
415 class={clsx(
416 styles().tree.expander,
417 css`
418 transform: rotate(${props.expanded ? 90 : 0}deg);
419 `,
420 props.expanded &&
421 css`
422 & svg {
423 top: -1px;
424 }
425 `,
426 )}
427 >
428 <svg
429 width="16"
430 height="16"
431 viewBox="0 0 16 16"
432 fill="none"
433 xmlns="http://www.w3.org/2000/svg"
434 >
435 <path
436 d="M6 12L10 8L6 4"
437 stroke-width="2"
438 stroke-linecap="round"
439 stroke-linejoin="round"
440 />
441 </svg>
442 </span>
443 )