Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/silent-beds-create.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/devtools-ui': patch
---

Improvements to the json tree component, now supports expansion length config
6 changes: 6 additions & 0 deletions .changeset/tricky-cloths-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@tanstack/devtools': patch
'@tanstack/devtools-vite': patch
---

improve open-source by using location origin
275 changes: 178 additions & 97 deletions packages/devtools-ui/src/components/tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,97 +3,19 @@ import clsx from 'clsx'
import { css, useStyles } from '../styles/use-styles'
import { CopiedCopier, Copier, ErrorCopier } from './icons'

export function JsonTree(props: { value: any; copyable?: boolean }) {
return <JsonValue isRoot value={props.value} copyable={props.copyable} />
}
type CopyState = 'NoCopy' | 'SuccessCopy' | 'ErrorCopy'

const CopyButton = (props: { value: unknown }) => {
const styles = useStyles()
const [copyState, setCopyState] = createSignal<CopyState>('NoCopy')

return (
<button
class={styles().tree.actionButton}
title="Copy object to clipboard"
aria-label={`${
copyState() === 'NoCopy'
? 'Copy object to clipboard'
: copyState() === 'SuccessCopy'
? 'Object copied to clipboard'
: 'Error copying object to clipboard'
}`}
onClick={
copyState() === 'NoCopy'
? () => {
navigator.clipboard
.writeText(JSON.stringify(props.value, null, 2))
.then(
() => {
setCopyState('SuccessCopy')
setTimeout(() => {
setCopyState('NoCopy')
}, 1500)
},
(err) => {
console.error('Failed to copy: ', err)
setCopyState('ErrorCopy')
setTimeout(() => {
setCopyState('NoCopy')
}, 1500)
},
)
}
: undefined
}
>
<Switch>
<Match when={copyState() === 'NoCopy'}>
<Copier />
</Match>
<Match when={copyState() === 'SuccessCopy'}>
<CopiedCopier theme={'dark'} />
</Match>
<Match when={copyState() === 'ErrorCopy'}>
<ErrorCopier />
</Match>
</Switch>
</button>
)
}

const Expander = (props: { expanded: boolean }) => {
const styles = useStyles()
export function JsonTree(props: {
value: any
copyable?: boolean
defaultExpansionDepth?: number
}) {
return (
<span
class={clsx(
styles().tree.expander,
css`
transform: rotate(${props.expanded ? 90 : 0}deg);
`,
props.expanded &&
css`
& svg {
top: -1px;
}
`,
)}
>
<svg
width="16"
height="16"
viewBox="0 0 16 16"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M6 12L10 8L6 4"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
/>
</svg>
</span>
<JsonValue
isRoot
value={props.value}
copyable={props.copyable}
depth={0}
defaultExpansionDepth={props.defaultExpansionDepth ?? 1}
/>
)
}

Expand All @@ -103,8 +25,18 @@ function JsonValue(props: {
isRoot?: boolean
isLastKey?: boolean
copyable?: boolean
defaultExpansionDepth: number
depth: number
}) {
const { value, keyName, isRoot = false, isLastKey, copyable } = props
const {
value,
keyName,
isRoot = false,
isLastKey,
copyable,
defaultExpansionDepth,
depth,
} = props
const styles = useStyles()

return (
Expand Down Expand Up @@ -137,12 +69,24 @@ function JsonValue(props: {
}
if (Array.isArray(value)) {
return (
<ArrayValue copyable={copyable} keyName={keyName} value={value} />
<ArrayValue
defaultExpansionDepth={defaultExpansionDepth}
depth={depth}
copyable={copyable}
keyName={keyName}
value={value}
/>
)
}
if (typeof value === 'object') {
return (
<ObjectValue copyable={copyable} keyName={keyName} value={value} />
<ObjectValue
defaultExpansionDepth={defaultExpansionDepth}
depth={depth}
copyable={copyable}
keyName={keyName}
value={value}
/>
)
}
return <span />
Expand All @@ -161,16 +105,36 @@ const ArrayValue = ({
value,
keyName,
copyable,
defaultExpansionDepth,
depth,
}: {
value: Array<any>
copyable?: boolean
keyName?: string
defaultExpansionDepth: number
depth: number
}) => {
const styles = useStyles()
const [expanded, setExpanded] = createSignal(true)
const [expanded, setExpanded] = createSignal(depth <= defaultExpansionDepth)

if (value.length === 0) {
return (
<span class={styles().tree.expanderContainer}>
{keyName && (
<span class={clsx(styles().tree.valueKey, styles().tree.collapsible)}>
&quot;{keyName}&quot;:{' '}
</span>
)}
<span class={styles().tree.valueBraces}>[]</span>
</span>
)
}
return (
<span class={styles().tree.expanderContainer}>
<Expander expanded={expanded()} />
<Expander
onClick={() => setExpanded(!expanded())}
expanded={expanded()}
/>
{keyName && (
<span
onclick={(e) => {
Expand All @@ -195,6 +159,8 @@ const ArrayValue = ({
copyable={copyable}
value={item}
isLastKey={isLastKey}
defaultExpansionDepth={defaultExpansionDepth}
depth={depth + 1}
/>
)
}}
Expand Down Expand Up @@ -222,19 +188,40 @@ const ObjectValue = ({
value,
keyName,
copyable,
defaultExpansionDepth,
depth,
}: {
value: Record<string, any>
keyName?: string
copyable?: boolean
defaultExpansionDepth: number
depth: number
}) => {
const styles = useStyles()
const [expanded, setExpanded] = createSignal(true)
const [expanded, setExpanded] = createSignal(depth <= defaultExpansionDepth)
const keys = Object.keys(value)
const lastKeyName = keys[keys.length - 1]

if (keys.length === 0) {
return (
<span class={styles().tree.expanderContainer}>
{keyName && (
<span class={clsx(styles().tree.valueKey, styles().tree.collapsible)}>
&quot;{keyName}&quot;:{' '}
</span>
)}
<span class={styles().tree.valueBraces}>{'{}'}</span>
</span>
)
}
return (
<span class={styles().tree.expanderContainer}>
{keyName && <Expander expanded={expanded()} />}
{keyName && (
<Expander
onClick={() => setExpanded(!expanded())}
expanded={expanded()}
/>
)}
{keyName && (
<span
onClick={(e) => {
Expand All @@ -259,6 +246,8 @@ const ObjectValue = ({
keyName={k}
isLastKey={lastKeyName === k}
copyable={copyable}
defaultExpansionDepth={defaultExpansionDepth}
depth={depth + 1}
/>
</>
)}
Expand All @@ -281,3 +270,95 @@ const ObjectValue = ({
</span>
)
}

type CopyState = 'NoCopy' | 'SuccessCopy' | 'ErrorCopy'

const CopyButton = (props: { value: unknown }) => {
const styles = useStyles()
const [copyState, setCopyState] = createSignal<CopyState>('NoCopy')

return (
<button
class={styles().tree.actionButton}
title="Copy object to clipboard"
aria-label={`${
copyState() === 'NoCopy'
? 'Copy object to clipboard'
: copyState() === 'SuccessCopy'
? 'Object copied to clipboard'
: 'Error copying object to clipboard'
}`}
onClick={
copyState() === 'NoCopy'
? () => {
navigator.clipboard
.writeText(JSON.stringify(props.value, null, 2))
.then(
() => {
setCopyState('SuccessCopy')
setTimeout(() => {
setCopyState('NoCopy')
}, 1500)
},
(err) => {
console.error('Failed to copy: ', err)
setCopyState('ErrorCopy')
setTimeout(() => {
setCopyState('NoCopy')
}, 1500)
},
)
}
: undefined
}
>
<Switch>
<Match when={copyState() === 'NoCopy'}>
<Copier />
</Match>
<Match when={copyState() === 'SuccessCopy'}>
<CopiedCopier theme={'dark'} />
</Match>
<Match when={copyState() === 'ErrorCopy'}>
<ErrorCopier />
</Match>
</Switch>
</button>
)
}

const Expander = (props: { expanded: boolean; onClick: () => void }) => {
const styles = useStyles()
return (
<span
onClick={props.onClick}
class={clsx(
styles().tree.expander,
css`
transform: rotate(${props.expanded ? 90 : 0}deg);
`,
props.expanded &&
css`
& svg {
top: -1px;
}
`,
)}
>
<svg
width="16"
height="16"
viewBox="0 0 16 16"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M6 12L10 8L6 4"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
/>
</svg>
</span>
)
}
1 change: 1 addition & 0 deletions packages/devtools-ui/src/styles/use-styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ const stylesFactory = (theme: Theme = 'dark') => {
`,
expander: css`
position: absolute;
cursor: pointer;
left: -16px;
top: 3px;
& path {
Expand Down
Loading