All files / modules/70-pipeline/components/VariablesListTable VariablesListTable.tsx

95.83% Statements 46/48
72.93% Branches 97/133
71.43% Functions 5/7
95.56% Lines 43/45

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              165x 165x 165x 165x 165x   165x   165x         165x                 165x 374x 374x 374x 374x 374x 374x 374x 135x 100x       100x 47x 47x 36x     53x       374x 265x     1630x 1221x   1221x   1221x 16x 47x   1205x 12x   1193x     1221x 528x 528x 528x 528x 528x 528x 528x 528x                                                                                                                                              
/*
 * 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, { useState } from 'react'
import { isPlainObject, get, isNil, escape, defaultTo } from 'lodash-es'
import cx from 'classnames'
import { Color, FontVariation } from '@harness/design-system'
import { Text, useNestedAccordion } from '@wings-software/uicore'
import type { VariableResponseMapValue } from 'services/pipeline-ng'
import { TextInputWithCopyBtn } from '@common/components/TextInputWithCopyBtn/TextInputWithCopyBtn'
 
import {
  getTextWithSearchMarkers,
  SearchResult,
  usePipelineVariables
} from '../PipelineVariablesContext/PipelineVariablesContext'
import css from './VariablesListTable.module.scss'
 
export interface VariableListTableProps<T = Record<string, any>> {
  data: T
  originalData: T
  metadataMap: Record<string, VariableResponseMapValue>
  className?: string
}
 
export function VariablesListTable<T>(props: VariableListTableProps<T>): React.ReactElement | null {
  const { data, metadataMap, originalData, className } = props
  const { searchText, searchIndex, searchResults = [] } = usePipelineVariables()
  const searchedEntity = defaultTo(searchResults[searchIndex || 0], {} as SearchResult)
  const tableRef = React.useRef()
  const [hoveredVariable, setHoveredVariable] = useState<Record<string, boolean>>({})
  const { openNestedPath } = useNestedAccordion()
  React.useLayoutEffect(() => {
    if (tableRef.current) {
      const { testid: accordianId = '', open } = defaultTo(
        (tableRef?.current as any)?.closest?.('.Accordion--panel')?.dataset,
        {}
      )
      if (open === 'false') {
        openNestedPath(accordianId?.replace('-panel', ''))
        setTimeout(() => {
          document?.querySelector('span.selected-search-text')?.scrollIntoView({ behavior: 'smooth' })
        }, 500)
      } else {
        ;(tableRef?.current as any)?.querySelector('span.selected-search-text')?.scrollIntoView({ behavior: 'smooth' })
      }
    }
  }, [searchIndex, openNestedPath, searchText])
  if (!data || !originalData || !metadataMap) return null
  return (
    <div className={cx(css.variablesListTable, className)} ref={tableRef as any}>
      {Object.entries(data || {}).map(([key, value]) => {
        if (typeof value !== 'string' || key === 'uuid' || isNil(value)) return null
        const metadata = metadataMap[value]
 
        const finalvalue = get(originalData, key)
        let formattedValue
        if (Array.isArray(finalvalue)) {
          formattedValue = finalvalue
            .map(item => (isPlainObject(item) ? JSON.stringify(item, null, 2) : item))
            .join(', ')
        } else if (isPlainObject(finalvalue)) {
          formattedValue = JSON.stringify(finalvalue, null, 2)
        } else {
          formattedValue = finalvalue
        }
 
        if (isNil(metadata) || isNil(formattedValue)) return null
        const yamlProps = defaultTo(metadata?.yamlProperties, metadata?.yamlOutputProperties)
        const variableNameParts = defaultTo(yamlProps?.localName?.split('.'), [])
        const variableName = variableNameParts[variableNameParts?.length - 1]
        const searchedEntityType = defaultTo(searchedEntity?.type, null)
        formattedValue = formattedValue.toString()
        const hasSameMetaKeyId = searchedEntity?.metaKeyId === value
        const isValidValueMatch = `${formattedValue}`?.toLowerCase()?.includes(searchText?.toLowerCase() || '')
        return (
          <div
            key={key}
            className={cx(
              css.variableListRow,
              'variable-list-row',
              hoveredVariable[variableName] ? css.hoveredRow : ''
            )}
            onMouseLeave={() => setHoveredVariable({ [variableName]: false })}
          >
            {hoveredVariable[variableName] ? (
              <div className={cx(css.nameSection, css.nameSectionWithCopy)}>
                <TextInputWithCopyBtn
                  name=""
                  disabled
                  label=""
                  localName={yamlProps?.localName}
                  fullName={yamlProps?.fqn}
                  popoverWrapperClassName={css.copyOptionPopoverWrapper}
                  textInputClassName={css.copyOptionTextInput}
                  staticDisplayValue={variableName}
                />
              </div>
            ) : (
              <span
                className={cx(css.nameSection, {
                  'selected-search-text': searchedEntityType === 'key' && hasSameMetaKeyId
                })}
                onMouseOver={() => {
                  setHoveredVariable({ [variableName]: true })
                }}
                dangerouslySetInnerHTML={{
                  __html: getTextWithSearchMarkers({
                    searchText: escape(searchText),
                    txt: escape(variableName),
                    className: cx(css.selectedSearchText, {
                      [css.currentSelection]: searchedEntityType === 'key' && hasSameMetaKeyId
                    })
                  })
                }}
              />
            )}
 
            <Text
              className={css.valueSection}
              font={{ variation: FontVariation.BODY, weight: 'semi-bold' }}
              padding={{ left: 'medium' }}
              color={Color.BLACK_100}
              lineClamp={1}
            >
              <span
                className={cx({
                  'selected-search-text': searchedEntityType === 'value' && hasSameMetaKeyId
                })}
                dangerouslySetInnerHTML={{
                  __html: getTextWithSearchMarkers({
                    searchText: escape(searchText),
                    txt: escape(formattedValue),
                    className: cx(css.selectedSearchText, {
                      [css.currentSelection]: searchedEntityType === 'value' && hasSameMetaKeyId && isValidValueMatch
                    })
                  })
                }}
              />
            </Text>
          </div>
        )
      })}
    </div>
  )
}