All files / modules/72-templates-library/components/TemplateSelector TemplateSelector.tsx

93.48% Statements 43/46
83.61% Branches 51/61
100% Functions 8/8
93.33% Lines 42/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 152 153 154 155 156 157 158 159 160 161 162 163 164              20x 20x 20x 20x 20x   20x 20x 20x 20x 20x 20x 20x 20x   20x             6x 6x 6x 6x 6x   6x 2x     2x       6x 2x 1x                 1x       6x   2x 2x           6x                   1x 1x         6x             1x 1x         6x 1x 1x           6x 1x 1x           6x                                                                                                      
/*
 * 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, { useState } from 'react'
import { Button, ButtonVariation, Container, Layout, useConfirmationDialog } from '@wings-software/uicore'
import { Intent } from '@blueprintjs/core'
import { Color } from '@harness/design-system'
import { isEqual } from 'lodash-es'
import type { TemplateSummaryResponse } from 'services/template-ng'
import { useStrings } from 'framework/strings'
import { useAppStore } from 'framework/AppStore/AppStoreContext'
import { GitSyncStoreProvider } from 'framework/GitRepoStore/GitSyncStoreContext'
import NoResultsView from '@templates-library/pages/TemplatesPage/views/NoResultsView/NoResultsView'
import { usePipelineContext } from '@pipeline/components/PipelineStudio/PipelineContext/PipelineContext'
import { TemplateSelectorLeftView } from '@templates-library/components/TemplateSelector/TemplateSelectorLeftView/TemplateSelectorLeftView'
import { TemplateDetails } from '../TemplateDetails/TemplateDetails'
import css from './TemplateSelector.module.scss'
 
export const TemplateSelector: React.FC = (): JSX.Element => {
  const {
    state: {
      templateView: {
        templateDrawerData: { data }
      }
    }
  } = usePipelineContext()
  const { onSubmit, selectedTemplateRef, selectedVersionLabel } = data?.selectorData || {}
  const [selectedTemplate, setSelectedTemplate] = useState<TemplateSummaryResponse | undefined>()
  const { getString } = useStrings()
  const { isGitSyncEnabled } = useAppStore()
 
  const defaultVersionLabel = React.useMemo(() => {
    Iif (isEqual(selectedTemplate?.identifier, selectedTemplateRef)) {
      return selectedVersionLabel
    } else {
      return selectedTemplate?.versionLabel
    }
  }, [selectedTemplate, selectedTemplateRef, selectedVersionLabel])
 
  const getTemplateDetails: React.ReactElement = React.useMemo(() => {
    if (selectedTemplate) {
      return (
        <TemplateDetails
          template={selectedTemplate}
          defaultVersionLabel={defaultVersionLabel}
          setTemplate={setSelectedTemplate}
          allowStableSelection={true}
        />
      )
    } else {
      return <></>
    }
  }, [selectedTemplate, defaultVersionLabel, setSelectedTemplate])
 
  const onUseTemplateConfirm = React.useCallback(
    (isCopied = false) => {
      Eif (selectedTemplate) {
        onSubmit?.(selectedTemplate, isCopied)
      }
    },
    [selectedTemplate, onSubmit]
  )
 
  const { openDialog: openChangeTemplateDialog } = useConfirmationDialog({
    intent: Intent.WARNING,
    cancelButtonText: getString('cancel'),
    contentText: getString('pipeline.changeTemplate', {
      name: selectedTemplate?.name,
      entity: selectedTemplate?.templateEntityType?.toLowerCase()
    }),
    titleText: `Change to Template ${selectedTemplate?.name}?`,
    confirmButtonText: getString('confirm'),
    onCloseDialog: async isConfirmed => {
      Eif (isConfirmed) {
        onUseTemplateConfirm()
      }
    }
  })
 
  const { openDialog: openCopyTemplateDialog } = useConfirmationDialog({
    intent: Intent.WARNING,
    cancelButtonText: getString('cancel'),
    contentText: getString('pipeline.copyTemplate', { name: selectedTemplate?.name }),
    titleText: `Copy Template ${selectedTemplate?.name}?`,
    confirmButtonText: getString('confirm'),
    onCloseDialog: async isConfirmed => {
      Eif (isConfirmed) {
        onUseTemplateConfirm(true)
      }
    }
  })
 
  const onUseTemplateClick = React.useCallback(async () => {
    Eif (selectedTemplateRef) {
      openChangeTemplateDialog()
    } else {
      onUseTemplateConfirm()
    }
  }, [selectedTemplateRef, openChangeTemplateDialog, onUseTemplateConfirm])
 
  const onCopyTemplateClick = React.useCallback(async () => {
    Eif (selectedTemplateRef) {
      openCopyTemplateDialog()
    } else {
      onUseTemplateConfirm(true)
    }
  }, [selectedTemplateRef, openCopyTemplateDialog, onUseTemplateConfirm])
 
  return (
    <Container height={'100%'} className={css.container}>
      <Layout.Horizontal height={'100%'}>
        <TemplateSelectorLeftView setTemplate={setSelectedTemplate} />
        <Container width={525}>
          {selectedTemplate ? (
            <Layout.Vertical height={'100%'}>
              <Container className={css.detailsContainer}>
                {isGitSyncEnabled ? (
                  <GitSyncStoreProvider>{getTemplateDetails}</GitSyncStoreProvider>
                ) : (
                  getTemplateDetails
                )}
              </Container>
              <Container>
                <Layout.Horizontal
                  padding={'xxlarge'}
                  background={Color.FORM_BG}
                  className={css.btnContainer}
                  spacing={'small'}
                >
                  <Button
                    variation={ButtonVariation.PRIMARY}
                    text={getString('templatesLibrary.useTemplate')}
                    onClick={onUseTemplateClick}
                  />
                  <Button
                    variation={ButtonVariation.LINK}
                    text={getString('templatesLibrary.copyToPipeline')}
                    onClick={onCopyTemplateClick}
                  />
                </Layout.Horizontal>
              </Container>
            </Layout.Vertical>
          ) : (
            <Container padding={{ top: 'xxlarge', right: 'xlarge', bottom: 'xxlarge', left: 'xlarge' }} height={'100%'}>
              <Layout.Vertical
                className={css.empty}
                spacing={'large'}
                height={'100%'}
                flex={{ align: 'center-center' }}
              >
                <NoResultsView text={getString('templatesLibrary.selectTemplateToPreview')} minimal={true} />
              </Layout.Vertical>
            </Container>
          )}
        </Container>
      </Layout.Horizontal>
    </Container>
  )
}