All files / modules/72-templates-library/components/TemplateStudio/RightDrawer RightDrawer.tsx

86% Statements 43/50
72.73% Branches 32/44
85.71% Functions 6/7
85.71% Lines 42/49

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              14x 14x 14x 14x 14x 14x 14x 14x   14x 14x 14x 14x       14x 14x   14x 14x 14x     2x     2x         2x                   14x                 36x 36x 36x 36x 36x   36x 22x               36x         36x 4x         4x     36x 22x 8x 14x   14x     14x   14x   14x           36x                                             1x                                  
/*
 * 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, { SyntheticEvent, useMemo } from 'react'
import { Drawer, Position } from '@blueprintjs/core'
import { Button, Container, Icon, Layout, Text } from '@wings-software/uicore'
import { Color, FontVariation } from '@harness/design-system'
import cx from 'classnames'
import { useParams } from 'react-router-dom'
import { StageType } from '@pipeline/utils/stageHelpers'
import { useStrings } from 'framework/strings'
import type { StepData } from '@pipeline/components/AbstractSteps/AbstractStepFactory'
import { StepPalette } from '@pipeline/components/PipelineStudio/StepPalette/StepPalette'
import { TemplateContext } from '@templates-library/components/TemplateStudio/TemplateContext/TemplateContext'
import { DrawerSizes, DrawerTypes } from '@templates-library/components/TemplateStudio/TemplateContext/TemplateActions'
import factory from '@pipeline/components/PipelineSteps/PipelineStepFactory'
import type { StepElementConfig } from 'services/cd-ng'
import type { ModulePathParams } from '@common/interfaces/RouteInterfaces'
import type { NGTemplateInfoConfigWithGitDetails } from 'framework/Templates/TemplateConfigModal/TemplateConfigModal'
import { useFeatureFlags } from '@common/hooks/useFeatureFlag'
import { getAllStepPaletteModuleInfos, getStepPaletteModuleInfosFromStage } from '@pipeline/utils/stepUtils'
import type { StepPalleteModuleInfo } from 'services/pipeline-ng'
import TemplateVariablesWrapper from '@templates-library/components/TemplateStudio/TemplateVariables/TemplateVariables'
import { TemplateInputs } from '@templates-library/components/TemplateInputs/TemplateInputs'
import css from './RightDrawer.module.scss'
 
function TemplateInputsWrapper({ templateDetails }: { templateDetails: NGTemplateInfoConfigWithGitDetails }) {
  const { getString } = useStrings()
 
  const TemplateInputsHeader = (
    <Layout.Horizontal padding="xlarge" border={{ bottom: true, color: Color.GREY_200 }}>
      <Icon name="template-inputs" size={24} color={Color.PRIMARY_7} margin={{ right: 'small' }} />
      <Text font={{ variation: FontVariation.H4 }}>{getString('templatesLibrary.templateInputs')}</Text>
    </Layout.Horizontal>
  )
  return (
    <Layout.Vertical>
      {TemplateInputsHeader}
      <Container className={css.templateInputsContainer}>
        <TemplateInputs template={templateDetails} />
      </Container>
    </Layout.Vertical>
  )
}
 
export const RightDrawer: React.FC = (): JSX.Element => {
  const {
    state: {
      templateView: { drawerData, isDrawerOpened },
      templateView,
      template,
      gitDetails
    },
    updateTemplateView
  } = React.useContext(TemplateContext)
  const { type, data, ...restDrawerProps } = drawerData
  const { module } = useParams<ModulePathParams>()
  const { CDNG_ENABLED, CING_ENABLED } = useFeatureFlags()
  const [stepPaletteModuleInfos, setStepPaletteModuleInfos] = React.useState<StepPalleteModuleInfo[]>([])
 
  const selectedTemplateDetails = useMemo(
    () => ({
      ...template,
      repo: gitDetails?.repoIdentifier,
      branch: gitDetails?.branch
    }),
    [template]
  ) as NGTemplateInfoConfigWithGitDetails
 
  const closeDrawer = (e?: SyntheticEvent<HTMLElement, Event> | undefined): void => {
    e?.persist()
    updateTemplateView({ ...templateView, isDrawerOpened: false, drawerData: { type: DrawerTypes.AddStep } })
  }
 
  const onStepSelection = async (item: StepData): Promise<void> => {
    const stepData: StepElementConfig = {
      identifier: '',
      name: '',
      type: item.type
    }
    data?.paletteData?.onSelection?.(stepData)
  }
 
  React.useEffect(() => {
    if (module === 'cd') {
      setStepPaletteModuleInfos(getStepPaletteModuleInfosFromStage(StageType.DEPLOY))
    } else Iif (module === 'ci') {
      setStepPaletteModuleInfos(getStepPaletteModuleInfosFromStage(StageType.BUILD))
    } else Iif (module === 'cf') {
      setStepPaletteModuleInfos(getStepPaletteModuleInfosFromStage(StageType.FEATURE))
    } else {
      Iif (CDNG_ENABLED && CING_ENABLED) {
        setStepPaletteModuleInfos(getAllStepPaletteModuleInfos())
      } else Iif (CDNG_ENABLED) {
        setStepPaletteModuleInfos(getStepPaletteModuleInfosFromStage(StageType.DEPLOY))
      } else Iif (CING_ENABLED) {
        setStepPaletteModuleInfos(getStepPaletteModuleInfosFromStage(StageType.BUILD))
      }
    }
  }, [module])
 
  return (
    <Drawer
      onClose={closeDrawer}
      usePortal={true}
      autoFocus={true}
      canEscapeKeyClose={true}
      canOutsideClickClose={true}
      enforceFocus={false}
      hasBackdrop={true}
      size={DrawerSizes[type]}
      isOpen={isDrawerOpened}
      position={Position.RIGHT}
      data-type={type}
      className={cx(css.main, css.almostFullScreen, css.fullScreen)}
      portalClassName={'pipeline-studio-right-drawer'}
      {...restDrawerProps}
    >
      <Button
        minimal
        className={css.almostFullScreenCloseBtn}
        icon="cross"
        withoutBoxShadow
        onClick={() => {
          updateTemplateView({ ...templateView, isDrawerOpened: false, drawerData: { type: DrawerTypes.AddStep } })
        }}
      />
 
      {type === DrawerTypes.AddStep && (
        <StepPalette
          stepsFactory={factory}
          stepPaletteModuleInfos={stepPaletteModuleInfos}
          stageType={StageType.BUILD}
          onSelect={onStepSelection}
        />
      )}
      {type === DrawerTypes.TemplateVariables && <TemplateVariablesWrapper />}
      {type === DrawerTypes.TemplateInputs && <TemplateInputsWrapper templateDetails={selectedTemplateDetails} />}
    </Drawer>
  )
}