'use client' import { Chevron } from '@payloadcms/ui' import * as React from 'react' import './index.scss' const chars = { leftCurlyBracket: '\u007B', leftSquareBracket: '\u005B', rightCurlyBracket: '\u007D', rightSquareBracket: '\u005D', } const baseClass = 'query-inspector' const Bracket = ({ type, comma = false, position, }: { comma?: boolean position: 'end' | 'start' type: 'array' | 'object' }) => { const rightBracket = type === 'object' ? chars.rightCurlyBracket : chars.rightSquareBracket const leftBracket = type === 'object' ? chars.leftCurlyBracket : chars.leftSquareBracket const bracketToRender = position === 'end' ? rightBracket : leftBracket return ( {bracketToRender} {position === 'end' && comma ? ',' : null} ) } type Args = { isEmpty?: boolean object: Record | any[] objectKey?: string parentType?: 'array' | 'object' trailingComma?: boolean } export const RenderJSON = ({ isEmpty = false, object, objectKey, parentType = 'object', trailingComma = false, }: Args) => { const objectKeys = object ? Object.keys(object) : [] const objectLength = objectKeys.length const [isOpen, setIsOpen] = React.useState(true) return (
    • {isOpen && objectKeys.map((key, keyIndex) => { let value = object[key] let type = 'string' const isLastKey = keyIndex === objectLength - 1 if (value === null) { type = 'null' } else if (value instanceof Date) { type = 'date' value = value.toISOString() } else if (Array.isArray(value)) { type = 'array' } else if (typeof value === 'object') { type = 'object' } else if (typeof value === 'number') { type = 'number' } else if (typeof value === 'boolean') { type = 'boolean' } else { type = 'string' } if (type === 'object' || type === 'array') { return ( ) } if ( type === 'date' || type === 'string' || type === 'null' || type === 'number' || type === 'boolean' ) { const parentHasKey = Boolean(parentType === 'object' && key) const rowClasses = [ `${baseClass}__row-line`, `${baseClass}__value-type--${type}`, `${baseClass}__row-line--${objectKey ? 'nested' : 'top'}`, ] .filter(Boolean) .join(' ') return (
    • {parentHasKey ? {`"${key}": `} : null} {JSON.stringify(value)} {isLastKey ? '' : ','}
    • ) } })}
    {!isEmpty && ( )}
  • ) }