import {isValidElement} 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 'sentry/components/events/meta/annotatedText'; import ExternalLink from 'sentry/components/links/externalLink'; import {IconOpen} from 'sentry/icons'; import {t} from 'sentry/locale'; import {isUrl} from 'sentry/utils'; import Toggle from './toggle'; import {analyzeStringForRepr, naturalCaseInsensitiveSort} from './utils'; type Props = React.HTMLAttributes & { data?: React.ReactNode; jsonConsts?: boolean; maxDefaultDepth?: number; meta?: Record; preserveQuotes?: boolean; withAnnotatedText?: boolean; }; function walk({ depth, value = null, maxDefaultDepth: maxDepth = 2, preserveQuotes, withAnnotatedText, jsonConsts, meta, }: { depth: number; value?: React.ReactNode; } & Pick< Props, 'withAnnotatedText' | 'preserveQuotes' | 'jsonConsts' | 'meta' | 'maxDefaultDepth' >) { let i = 0; const children: React.ReactNode[] = []; if (value === null) { return ( ); } if (value === true || value === false) { return ( ); } if (isString(value)) { const valueInfo = analyzeStringForRepr(value); const valueToBeReturned = withAnnotatedText ? ( ) : ( valueInfo.repr ); const out = [ {preserveQuotes ? `"${valueToBeReturned}"` : valueToBeReturned} , ]; if (valueInfo.isString && isUrl(value)) { out.push( ); } return out; } if (isNumber(value)) { const valueToBeReturned = withAnnotatedText && meta ? ( ) : ( value ); return {valueToBeReturned}; } if (isArray(value)) { for (i = 0; i < value.length; i++) { children.push( {walk({ value: value[i], depth: depth + 1, preserveQuotes, withAnnotatedText, jsonConsts, meta: meta?.[i]?.[''] ?? meta?.[i] ?? meta?.[''] ?? meta, })} {i < value.length - 1 ? {', '} : null} ); } return ( {'['} {children} {']'} ); } if (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: value[key], depth: depth + 1, preserveQuotes, withAnnotatedText, jsonConsts, meta: meta?.[key]?.[''] ?? meta?.[key] ?? meta?.[''] ?? meta, })} {i < keys.length - 1 ? {', '} : null} ); } return ( {'{'} {children} {'}'} ); } function ContextData({ children, meta, jsonConsts, maxDefaultDepth, data = null, preserveQuotes = false, withAnnotatedText = false, ...props }: Props) { return (
      {walk({
        value: data,
        depth: 0,
        maxDefaultDepth,
        meta,
        jsonConsts,
        withAnnotatedText,
        preserveQuotes,
      })}
      {children}
    
); } export default ContextData; const StyledIconOpen = styled(IconOpen)` position: relative; top: 1px; `;