import { For, Match, Show, Switch, createSignal } from 'solid-js' import clsx from 'clsx' import dayjs from 'dayjs' // css import { createStyles, css } from '../styles/use-styles' // icons import { CopiedCopier, Copier, ErrorCopier } from './icons' // utils import type { CollapsiblePaths } from '../utils/deep-keys' export function JsonTree>(props: { value: TData copyable?: boolean defaultExpansionDepth?: number collapsePaths?: Array config?: { dateFormat?: string } }) { return ( ) } function JsonValue(props: { value: any keyName?: string isRoot?: boolean isLastKey?: boolean copyable?: boolean defaultExpansionDepth: number depth: number collapsePaths?: Array path: string config?: { dateFormat?: string } }) { const styles = createStyles() return ( {props.keyName && typeof props.value !== 'object' && !Array.isArray(props.value) && ( "{props.keyName}":{' '} )} {(() => { if (typeof props.value === 'string') { return ( "{props.value}" ) } if (typeof props.value === 'number') { return ( {String(props.value)} ) } if (typeof props.value === 'boolean') { return ( {String(props.value)} ) } if (props.value === null) { return null } if (props.value === undefined) { return undefined } if (typeof props.value === 'function') { return ( {String(props.value)} ) } if (Array.isArray(props.value)) { return ( ) } if (typeof props.value === 'object') { return ( ) } return })()} {props.copyable && (
)} {props.isLastKey || props.isRoot ? '' : ,}
) } const ArrayValue = (props: { value: Array copyable?: boolean keyName?: string defaultExpansionDepth: number depth: number collapsePaths?: Array path: string config?: { dateFormat?: string } }) => { const styles = createStyles() const [expanded, setExpanded] = createSignal( props.depth <= props.defaultExpansionDepth && !props.collapsePaths?.includes(props.path), ) if (props.value.length === 0) { return ( {props.keyName && ( "{props.keyName}":{' '} )} [] ) } return ( setExpanded(!expanded())} expanded={expanded()} /> {props.keyName && ( { e.stopPropagation() e.stopImmediatePropagation() setExpanded(!expanded()) }} class={clsx(styles().tree.valueKey, styles().tree.collapsible)} > "{props.keyName}":{' '} {props.value.length} items )} [ {(item, i) => { const isLastKey = i() === props.value.length - 1 return ( ) }} { e.stopPropagation() e.stopImmediatePropagation() setExpanded(!expanded()) }} class={clsx(styles().tree.valueKey, styles().tree.collapsible)} > {`...`} ] ) } const ObjectValue = (props: { value: Record keyName?: string copyable?: boolean defaultExpansionDepth: number depth: number collapsePaths?: Array path: string config?: { dateFormat?: string } }) => { const styles = createStyles() const [expanded, setExpanded] = createSignal( props.depth <= props.defaultExpansionDepth && !props.collapsePaths?.includes(props.path), ) const keys = Object.keys(props.value) const lastKeyName = keys[keys.length - 1] if (props.value instanceof Date) { return ( {props.keyName && ( "{props.keyName}":{' '} )} {dayjs(props.value).format( props.config?.dateFormat ? props.config.dateFormat : 'DDMMMYY', )} ) } if (keys.length === 0) { return ( {props.keyName && ( "{props.keyName}":{' '} )} {'{}'} ) } return ( {props.keyName && ( setExpanded(!expanded())} expanded={expanded()} /> )} {props.keyName && ( { e.stopPropagation() e.stopImmediatePropagation() setExpanded(!expanded()) }} class={clsx(styles().tree.valueKey, styles().tree.collapsible)} > "{props.keyName}":{' '} {keys.length} items )} {'{'} {(k) => ( <> )} { e.stopPropagation() e.stopImmediatePropagation() setExpanded(!expanded()) }} class={clsx(styles().tree.valueKey, styles().tree.collapsible)} > {`...`} {'}'} ) } type CopyState = 'NoCopy' | 'SuccessCopy' | 'ErrorCopy' const CopyButton = (props: { value: unknown }) => { const styles = createStyles() const [copyState, setCopyState] = createSignal('NoCopy') return ( ) } const Expander = (props: { expanded: boolean; onClick: () => void }) => { const styles = createStyles() return ( ) }