All files / modules/30-delegates/components/CreateDelegate/K8sDelegate/StepReviewScript Stepk8sReviewScript.tsx

97.67% Statements 42/43
62% Branches 31/50
100% Functions 7/7
97.67% Lines 42/43

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 162 163              3x 3x 3x 3x 3x 3x 3x   3x 3x       3x   3x   3x 12x 10x 10x               8x 8x   8x 4x 4x 4x 4x     8x 4x     4x       8x   8x 1x 1x 1x 1x 1x     8x                                                 1x 1x                         1x 1x                         1x 1x 1x 1x                                                                                   3x  
/*
 * 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 from 'react'
import { useParams } from 'react-router-dom'
import set from 'lodash-es/set'
import { Button, Layout, StepProps, Heading, Text } from '@wings-software/uicore'
import { useStrings } from 'framework/strings'
import YamlBuilder from '@common/components/YAMLBuilder/YamlBuilder'
import { useGenerateKubernetesYaml, DelegateSetupDetails, GenerateKubernetesYamlQueryParams } from 'services/portal'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { useTelemetry } from '@common/hooks/useTelemetry'
import { Category, DelegateActions } from '@common/constants/TrackingConstants'
 
import type { K8sDelegateWizardData } from '../DelegateSetupStep/DelegateSetupStep'
 
import css from '../CreateK8sDelegate.module.scss'
 
const k8sFileName = 'harness-delegate.yml'
 
const Stepk8ReviewScript: React.FC<StepProps<K8sDelegateWizardData>> = props => {
  const { getString } = useStrings()
  const { accountId, orgIdentifier, projectIdentifier } = useParams<ProjectPathProps>()
  const { mutate: downloadYaml } = useGenerateKubernetesYaml({
    queryParams: {
      accountId,
      orgId: orgIdentifier,
      projectId: projectIdentifier,
      fileFormat: 'text/plain'
    } as GenerateKubernetesYamlQueryParams
  })
  const linkRef = React.useRef<HTMLAnchorElement>(null)
  const [generatedYaml, setGeneratedYaml] = React.useState<string>()
 
  const onGenYaml = async (): Promise<void> => {
    const data = props?.prevStepData?.delegateYaml || {}
    set(data, 'delegateType', 'KUBERNETES')
    const response = await downloadYaml(data as DelegateSetupDetails)
    setGeneratedYaml(response as any)
  }
 
  React.useEffect(() => {
    Iif (props?.prevStepData?.generatedYaml) {
      setGeneratedYaml(props?.prevStepData?.generatedYaml)
    } else {
      onGenYaml()
    }
  }, [])
 
  const { trackEvent } = useTelemetry()
 
  const onDownload = (): void => {
    Eif (linkRef?.current) {
      const content = new Blob([generatedYaml as BlobPart], { type: 'data:text/plain;charset=utf-8' })
      linkRef.current.href = window.URL.createObjectURL(content)
      linkRef.current.download = k8sFileName
      linkRef.current.click()
    }
  }
  return (
    <>
      <Layout.Horizontal>
        <Layout.Vertical padding="xlarge" spacing="medium">
          <div className={css.collapseDiv}>
            <YamlBuilder
              entityType="Delegates"
              fileName={k8sFileName}
              isReadOnlyMode={true}
              isEditModeSupported={false}
              hideErrorMesageOnReadOnlyMode={true}
              existingYaml={generatedYaml}
              showSnippetSection={false}
              width="568px"
              height="462px"
              theme="DARK"
            />
          </div>
          <Layout.Horizontal padding="small">
            <Button
              id="stepReviewScriptDownloadYAMLButton"
              icon="arrow-down"
              text={getString('delegates.downloadYAMLFile')}
              className={css.downloadButton}
              onClick={() => {
                onDownload()
                trackEvent(DelegateActions.DownloadYAML, {
                  category: Category.DELEGATE
                })
              }}
              outlined
            />
          </Layout.Horizontal>
          <Layout.Horizontal padding="small">
            <Button
              id="stepReviewScriptBackButton"
              text={getString('back')}
              icon="chevron-left"
              onClick={() => {
                props.previousStep?.(props?.prevStepData)
                trackEvent(DelegateActions.ReviewScriptBack, {
                  category: Category.DELEGATE
                })
              }}
              margin={{ right: 'small' }}
            />
            <Button
              id="stepReviewScriptContinueButton"
              type="submit"
              intent="primary"
              text={getString('continue')}
              rightIcon="chevron-right"
              onClick={() => {
                const nextData = props?.prevStepData as K8sDelegateWizardData
                set(nextData, 'generatedYaml', generatedYaml)
                props.nextStep?.(nextData)
                trackEvent(DelegateActions.ReviewScriptContinue, {
                  category: Category.DELEGATE,
                  data: nextData
                })
              }}
            />
          </Layout.Horizontal>
        </Layout.Vertical>
        <Layout.Vertical spacing="medium">
          <Layout.Horizontal padding="medium">
            <Heading level={2} style={{ color: '#22272D', fontWeight: 600 }}>
              {getString('delegate.reviewScript.configProxySettings')}
            </Heading>
          </Layout.Horizontal>
          <Layout.Vertical padding="small">
            <Text lineClamp={3} width={514} font="small">
              {getString('delegate.reviewScript.descriptionProxySettings')}
            </Text>
            <Text lineClamp={3} width={514} font="small">
              {getString('delegates.reviewScript.docLinkBefore')}
              <a
                rel="noreferrer"
                href="https://ngdocs.harness.io/article/5ww21ewdt8-configure-delegate-proxy-settings"
                target="_blank"
              >
                {getString('delegates.reviewScript.docLink')}
              </a>
              {getString('delegates.reviewScript.docLinkAfter')}
            </Text>
          </Layout.Vertical>
        </Layout.Vertical>
      </Layout.Horizontal>
      <a
        className="hide"
        ref={linkRef}
        // ref={hiddenRedirectLink => (this.hiddenRedirectLink = hiddenRedirectLink)}
        target={'_blank'}
      />
    </>
  )
}
 
export default Stepk8ReviewScript