All files / modules/75-cd/components/PipelineSteps/K8sServiceSpec/ArtifactSource/ArtifactSourceRuntimeFields ArtifactTagRuntimeField.tsx

90.63% Statements 29/32
75.51% Branches 37/49
66.67% Functions 4/6
93.55% Lines 29/31

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              110x 110x 110x   110x   110x     110x   110x 110x 110x                       110x                             17x   17x 17x 17x   17x 17x 13x 1x       1x 1x         17x 13x 1x       17x                           17x                                 3x             3x 3x                                                           110x  
/*
 * Copyright 2022 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, { useEffect, useState } from 'react'
import { defaultTo, memoize } from 'lodash-es'
import { Menu } from '@blueprintjs/core'
 
import { Layout, SelectOption, Text, useToaster } from '@wings-software/uicore'
import type { GetDataError } from 'restful-react'
import { EXPRESSION_STRING } from '@pipeline/utils/constants'
import type { DockerBuildDetailsDTO, Failure, Error } from 'services/cd-ng'
 
import { useStrings } from 'framework/strings'
import type { ArtifactSourceRenderProps } from '@cd/factory/ArtifactSourceFactory/ArtifactSourceBase'
import ExperimentalInput from '../../K8sServiceSpecForms/ExperimentalInput'
import { BuildDetailsDTO, getTagError } from '../artifactSourceUtils'
import css from '../../K8sServiceSpec.module.scss'
 
interface TagsRenderContent extends ArtifactSourceRenderProps {
  isTagsSelectionDisabled: (data: ArtifactSourceRenderProps) => boolean
  buildDetailsList?: BuildDetailsDTO
  isFieldDisabled: (fieldName: string, isTag?: boolean) => boolean
  fetchingTags: boolean
  fetchTags: () => void
  fetchTagsError: GetDataError<Failure | Error> | null
  expressions: string[]
  isArtifactPath?: boolean
}
const ArtifactTagRuntimeField = (props: TagsRenderContent): JSX.Element => {
  const {
    formik,
    path,
    readonly,
    expressions,
    allowableTypes,
    artifactPath,
    isTagsSelectionDisabled,
    buildDetailsList,
    isFieldDisabled,
    fetchingTags,
    fetchTags,
    fetchTagsError,
    stageIdentifier
  } = props
 
  const { getString } = useStrings()
  const loadingTags = getString('pipeline.artifactsSelection.loadingTags')
  const { showError } = useToaster()
 
  const [tagsList, setTagsList] = useState<SelectOption[]>([])
  useEffect(() => {
    if (Array.isArray(buildDetailsList)) {
      const toBeSetTagsList = buildDetailsList?.map(({ tag }: DockerBuildDetailsDTO) => ({
        label: defaultTo(tag, ''),
        value: defaultTo(tag, '')
      }))
      Eif (toBeSetTagsList) {
        setTagsList(toBeSetTagsList)
      }
    }
  }, [buildDetailsList])
 
  useEffect(() => {
    if (fetchTagsError) {
      showError(`Stage ${stageIdentifier}: ${getTagError(fetchTagsError)}`, undefined, 'cd.tag.fetch.error')
    }
  }, [fetchTagsError, showError, stageIdentifier])
 
  const itemRenderer = memoize((item: { label: string }, { handleClick }) => (
    <div key={item.label.toString()}>
      <Menu.Item
        text={
          <Layout.Horizontal spacing="small">
            <Text>{item.label}</Text>
          </Layout.Horizontal>
        }
        disabled={fetchingTags}
        onClick={handleClick}
      />
    </div>
  ))
 
  return (
    <ExperimentalInput
      formik={formik}
      disabled={isFieldDisabled(`artifacts.${artifactPath}.spec.tag`, true)}
      selectItems={
        fetchingTags
          ? [
              {
                label: loadingTags,
                value: loadingTags
              }
            ]
          : tagsList
      }
      useValue
      multiTypeInputProps={{
        onFocus: (e: React.ChangeEvent<HTMLInputElement>) => {
          Iif (
            e?.target?.type !== 'text' ||
            (e?.target?.type === 'text' && e?.target?.placeholder === EXPRESSION_STRING)
          ) {
            return
          }
 
          Eif (!isTagsSelectionDisabled(props)) {
            fetchTags()
          }
        },
        selectProps: {
          items: fetchingTags
            ? [
                {
                  label: loadingTags,
                  value: loadingTags
                }
              ]
            : tagsList,
          usePortal: true,
          addClearBtn: !(readonly || isTagsSelectionDisabled(props)),
          noResults: (
            <Text lineClamp={1}>{getTagError(fetchTagsError) || getString('pipelineSteps.deploy.errors.notags')}</Text>
          ),
          itemRenderer,
          allowCreatingNewItems: true,
          popoverClassName: css.selectPopover
        },
        expressions,
        allowableTypes
      }}
      label={getString('tagLabel')}
      name={`${path}.artifacts.${artifactPath}.spec.tag`}
    />
  )
}
 
export default ArtifactTagRuntimeField