All files / modules/70-pipeline/components/execution/StepDetails/tabs/InputOutputTab InputOutputTab.tsx

86.67% Statements 39/45
82.86% Branches 29/35
66.67% Functions 6/9
90% Lines 36/40

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161              39x 39x 39x 39x   39x 39x 39x   39x 39x   39x     6x           6x                     39x 18x 18x             18x                   39x 8x     30x   26x   26x 1x     26x 6x   6x             20x 2x   2x 1x                                                       18x                                               39x 3x 3x   3x 1x                 2x                    
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import React from 'react'
import { isPlainObject, toPairs, startCase, isEmpty, isNil } from 'lodash-es'
import { Collapse as BPCollapse, Icon } from '@blueprintjs/core'
import cx from 'classnames'
 
import { useStrings } from 'framework/strings'
import { CopyText } from '@common/components/CopyText/CopyText'
import { toVariableStr } from '@common/utils/StringUtils'
 
import { breakOnLinks } from '@common/components/LinkifyText/LinkifyText'
import css from './InputOutputTab.module.scss'
 
const blackListKeys = ['step', 'parallel']
 
function Collapse(props: React.PropsWithChildren<{ title: string }>): React.ReactElement {
  const [isOpen, setIsOpen] = React.useState(true)
 
  function toggle(): void {
    setIsOpen(status => !status)
  }
 
  return (
    <div className={css.panel} data-status={isOpen ? 'open' : 'close'}>
      <div className={css.panelTitle} onClick={toggle}>
        <Icon icon="chevron-up" />
        <span>{props.title}</span>
      </div>
      <BPCollapse isOpen={isOpen}>{props.children}</BPCollapse>
    </div>
  )
}
 
export function linkyText(txt: string): React.ReactNode {
  const textItems = breakOnLinks(txt)
  Iif (textItems.length === 1 && textItems[0].type === 'URL') {
    return (
      <a href={textItems[0].content} target="_blank" rel="noreferrer">
        {textItems[0].content}
      </a>
    )
  } else {
    return txt.toString()
  }
}
export interface InputOutputTabRowProps {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  data: Record<string, any>
  level: number
  prefix: string
}
 
export function InputOutputTabRow(props: InputOutputTabRowProps): React.ReactElement {
  return (
    <React.Fragment>
      {toPairs(props.data).map(([key, value]) => {
        if (key.startsWith('_') || isNil(value)) return null
 
        let newKey = `${props.prefix}.${key}`
 
        if (blackListKeys.includes(key.toLowerCase()) || key.toLowerCase().endsWith('definition')) {
          newKey = props.prefix
        }
 
        if (isPlainObject(value)) {
          Iif (isEmpty(value)) return null
 
          return (
            <Collapse key={key} title={startCase(key)}>
              <InputOutputTabRow prefix={newKey} data={value} level={props.level + 1} />
            </Collapse>
          )
        }
 
        if (Array.isArray(value)) {
          if (isEmpty(value)) return null
 
          Eif (value.every(e => typeof e === 'string')) {
            return (
              <div className={css.ioRow} key={key}>
                <div className={css.key}>
                  <CopyText textToCopy={toVariableStr(newKey)}>{key}</CopyText>
                </div>
                <div className={css.value}>
                  <CopyText textToCopy={value.join(', ')}>{value.join(', ')}</CopyText>
                </div>
              </div>
            )
          }
 
          return (
            <Collapse key={key} title={startCase(key)}>
              {value.map((item, index) => {
                return (
                  <InputOutputTabRow
                    key={`${newKey}[${index}]`}
                    prefix={`${newKey}[${index}]`}
                    data={item}
                    level={props.level + 1}
                  />
                )
              })}
            </Collapse>
          )
        }
 
        return (
          <div className={css.ioRow} key={key}>
            <div data-fqn={newKey} className={css.key}>
              <CopyText textToCopy={toVariableStr(newKey)}>{key}</CopyText>
            </div>
            <div className={css.value}>
              <CopyText textToCopy={value.toString()} className={css.valueText}>
                {linkyText(value)}
              </CopyText>
            </div>
          </div>
        )
      })}
    </React.Fragment>
  )
}
 
export interface InputOutputTabProps {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  data?: Record<string, any>
  mode: 'input' | 'output'
  baseFqn?: string
}
 
export function InputOutputTab(props: InputOutputTabProps): React.ReactElement {
  const { mode, baseFqn = '', data } = props
  const { getString } = useStrings()
 
  if (!data || isEmpty(data)) {
    return (
      <div className={css.ioTab} data-empty="true">
        {getString(
          mode === 'output' ? 'pipeline.execution.iotab.noOutputText' : 'pipeline.execution.iotab.noInputText'
        )}
      </div>
    )
  }
 
  return (
    <div className={css.ioTab}>
      <div className={cx(css.ioRow, css.header)}>
        <div>{getString(mode === 'input' ? 'inputName' : 'outputName')}</div>
        <div>{getString(mode === 'input' ? 'inputValue' : 'outputValue')}</div>
      </div>
      <InputOutputTabRow prefix={baseFqn} data={data} level={0} />
    </div>
  )
}