import * as React from 'react'; import styled from '@emotion/styled'; import isArray from 'lodash/isArray'; import isNumber from 'lodash/isNumber'; import isString from 'lodash/isString'; import AnnotatedText from 'app/components/events/meta/annotatedText'; import ExternalLink from 'app/components/links/externalLink'; import {IconOpen} from 'app/icons'; import {Meta} from 'app/types'; import {isUrl} from 'app/utils'; import Toggle from './toggle'; import {analyzeStringForRepr, naturalCaseInsensitiveSort} from './utils'; type Value = null | string | boolean | number | {[key: string]: Value} | Value[]; type Props = React.HTMLAttributes & { data: Value; preserveQuotes?: boolean; withAnnotatedText?: boolean; maxDefaultDepth?: number; meta?: Meta; jsonConsts?: boolean; }; type State = { data: Value; withAnnotatedText: boolean; }; function getValueWithAnnotatedText(v: Value, meta?: Meta) { return ; } class ContextData extends React.Component { static defaultProps = { data: null, withAnnotatedText: false, }; renderValue(value: Value) { const {preserveQuotes, meta, withAnnotatedText, jsonConsts, maxDefaultDepth} = this.props; const maxDepth = maxDefaultDepth ?? 2; // eslint-disable-next-line @typescript-eslint/no-shadow function walk(value: Value, depth: number) { let i = 0; const children: React.ReactNode[] = []; if (value === null) { return {jsonConsts ? 'null' : 'None'}; } if (value === true || value === false) { return ( {jsonConsts ? (value ? 'true' : 'false') : value ? 'True' : 'False'} ); } if (isString(value)) { const valueInfo = analyzeStringForRepr(value); const valueToBeReturned = withAnnotatedText ? getValueWithAnnotatedText(valueInfo.repr, meta) : valueInfo.repr; const out = [ {preserveQuotes ? `"${valueToBeReturned}"` : valueToBeReturned} , ]; if (valueInfo.isString && isUrl(value)) { out.push( ); } return out; } if (isNumber(value)) { const valueToBeReturned = withAnnotatedText && meta ? getValueWithAnnotatedText(value, meta) : value; return {valueToBeReturned}; } if (isArray(value)) { for (i = 0; i < value.length; i++) { children.push( {walk(value[i], depth + 1)} {i < value.length - 1 ? ( {', '} ) : null} ); } return ( {'['} {children} {']'} ); } if (React.isValidElement(value)) { return value; } const keys = Object.keys(value); keys.sort(naturalCaseInsensitiveSort); for (i = 0; i < keys.length; i++) { const key = keys[i]; children.push( {preserveQuotes ? `"${key}"` : key} {': '} {walk(value[key], depth + 1)} {i < keys.length - 1 ? {', '} : null} ); } return ( {'{'} {children} {'}'} ); } return walk(value, 0); } render() { const { data, preserveQuotes: _preserveQuotes, withAnnotatedText: _withAnnotatedText, meta: _meta, children, ...other } = this.props; return (
        {this.renderValue(data)}
        {children}
      
); } } const StyledIconOpen = styled(IconOpen)` position: relative; top: 1px; `; export default ContextData;