All files / modules/10-common/components/SnippetSection SnippetSection.tsx

70.59% Statements 24/34
45.83% Branches 11/24
66.67% Functions 6/9
71.88% Lines 23/32

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              498x 498x 498x     498x 498x   498x   498x                     498x 2x 2x 2x 2x   2x 1x     2x                       2x 2x     18x     2x 2x     8x                         2x                                       498x  
/*
 * 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, useEffect } from 'react'
import { Icon, IconName } from '@wings-software/uicore'
import cx from 'classnames'
 
import type { YamlSnippetMetaData, GetYamlSchemaQueryParams } from 'services/cd-ng'
import { useStrings } from 'framework/strings'
import { getIconNameForTag } from '@common/utils/SnippetUtils'
import type { SnippetFetchResponse } from '@common/interfaces/YAMLBuilderProps'
import SnippetDetails from './SnippetDetails'
 
import css from './SnippetSection.module.scss'
 
export interface SnippetSectionProps {
  entityType: GetYamlSchemaQueryParams['entityType']
  showIconMenu?: boolean
  width?: React.CSSProperties['width']
  snippets?: YamlSnippetMetaData[]
  onSnippetCopy?: (identifier: string) => Promise<void>
  snippetFetchResponse?: SnippetFetchResponse
}
 
const SnippetSection: React.FC<SnippetSectionProps> = props => {
  const { showIconMenu, entityType, snippets, onSnippetCopy, snippetFetchResponse } = props
  const [selectedIcon, setSelectedIcon] = useState<string | undefined>('')
  const [snippetList, setSnippetList] = useState<YamlSnippetMetaData[]>()
  const { getString } = useStrings()
 
  useEffect(() => {
    setSnippetList(snippets)
  }, [snippets])
 
  const onIconClick = (event: React.MouseEvent<Element, MouseEvent>, icon?: string): void => {
    event.preventDefault()
    if (selectedIcon === icon) {
      setSelectedIcon('')
      setSnippetList(snippets)
    } else {
      setSelectedIcon(icon)
      const snippetsClone = snippets
      setSnippetList(snippetsClone?.filter(snippet => getIconNameForTag(snippet.iconTag || '') === icon))
    }
  }
 
  const getIconCategories = (_snippets?: YamlSnippetMetaData[]): IconName[] | null => {
    Iif (!_snippets) {
      return null
    }
    return [...new Set(_snippets.map(snippet => getIconNameForTag(snippet?.iconTag || '')))]
  }
 
  const getIconList = (): React.ReactElement | null => {
    return (
      <React.Fragment>
        {getIconCategories(snippets)?.map(icon => (
          <div
            className={cx(css.snippetIcon, { [css.active]: icon === selectedIcon })}
            key={icon}
            onClick={event => onIconClick(event, icon)}
            title={icon}
          >
            <Icon name={icon as IconName} size={25} />
          </div>
        ))}
      </React.Fragment>
    )
  }
 
  return (
    <div className={css.main}>
      {showIconMenu ? <div className={css.snippetIcons}>{getIconList()}</div> : null}
      {snippetList ? (
        <div className={css.snippets}>
          <SnippetDetails
            entityType={entityType}
            selectedIcon={selectedIcon}
            snippets={snippetList}
            onSnippetCopy={onSnippetCopy}
            snippetFetchResponse={snippetFetchResponse}
          />
        </div>
      ) : (
        <div className={cx(css.noSnippets, css.fillSpace)}>{getString('yamlBuilder.snippets.noSnippetsFound')}</div>
      )}
    </div>
  )
}
 
export default SnippetSection