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

80% Statements 52/65
47.12% Branches 49/104
66.67% Functions 8/12
79.69% Lines 51/64

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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215              14x 14x 14x   14x 14x 14x                     14x 14x   14x       14x   14x 14x   14x 14x 14x 14x 14x   14x   14x     7x                             6x 6x 6x     6x 6x 6x   6x       6x 1x                                                 6x   1x 1x 1x         6x 2x             2x 2x     6x 4x                                       6x 4x 4x 4x     6x           6x 3x         6x 3x 3x 2x       6x                                                                                            
/*
 * 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 from 'react'
import { Container, Layout, Text } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import type { NodeModelListener } from '@projectstorm/react-diagrams-core'
import { defaultTo, set } from 'lodash-es'
import { useParams } from 'react-router-dom'
import {
  CreateNewModel,
  CreateNewWidget,
  DefaultNodeModel,
  DefaultNodeModelOptions,
  DefaultNodeWidget,
  DiamondNodeModel,
  DiamondNodeWidget,
  Event
} from '@pipeline/components/Diagram'
import type { StageElementConfig } from 'services/cd-ng'
import { DynamicPopover } from '@common/components'
import { renderPopover } from '@pipeline/components/PipelineStudio/StageBuilder/StageBuilder'
import type { DynamicPopoverHandlerBinding } from '@common/components/DynamicPopover/DynamicPopover'
import {
  StageAttributes,
  usePipelineContext
} from '@pipeline/components/PipelineStudio/PipelineContext/PipelineContext'
import { useGlobalEventListener } from '@common/hooks'
import type { StageElementWrapper } from '@pipeline/utils/pipelineTypes'
import { PopoverData, getCommonStyles } from '@pipeline/components/PipelineStudio/StageBuilder/StageBuilderUtil'
import { DefaultNewTemplateId } from 'framework/Templates/templates'
import type { TemplateStudioPathProps } from '@common/interfaces/RouteInterfaces'
import { TemplateContext } from '@templates-library/components/TemplateStudio/TemplateContext/TemplateContext'
import { DefaultNewStageId } from '@templates-library/components/TemplateStudio/StageTemplateCanvas/StageTemplateForm/StageTemplateForm'
import { SplitViewTypes } from '@pipeline/components/PipelineStudio/PipelineContext/PipelineActions'
import stageBuilderCss from '@pipeline/components/PipelineStudio/StageBuilder/StageBuilder.module.scss'
import css from './StageTemplateDiagram.module.scss'
 
const CREATE_NODE_ID = 'create-node'
 
export const StageTemplateDiagram = (): JSX.Element => {
  const {
    state: { template, gitDetails }
  } = React.useContext(TemplateContext)
  const {
    state: {
      pipeline,
      pipelineView,
      selectionState: { selectedStageId },
      templateTypes
    },
    contextType,
    stagesMap,
    updatePipeline,
    updatePipelineView,
    setSelection,
    renderPipelineStage,
    getStageFromPipeline
  } = usePipelineContext()
  const selectedStage = getStageFromPipeline(defaultTo(selectedStageId, ''))
  const [dynamicPopoverHandler, setDynamicPopoverHandler] = React.useState<
    DynamicPopoverHandlerBinding<PopoverData> | undefined
  >()
  const [stageData, setStageData] = React.useState<StageAttributes>()
  const canvasRef = React.useRef<HTMLDivElement | null>(null)
  const { templateIdentifier } = useParams<TemplateStudioPathProps>()
 
  useGlobalEventListener('CLOSE_CREATE_STAGE_POPOVER', () => {
    dynamicPopoverHandler?.hide()
  })
 
  const openStageSelection = (nodeId: string) => {
    dynamicPopoverHandler?.show(
      `[data-nodeid="${nodeId}"]`,
      {
        addStage: async (newStage: StageElementWrapper) => {
          dynamicPopoverHandler?.hide()
          set(pipeline, 'stages[0].stage', { ...newStage.stage, identifier: DefaultNewStageId })
          await updatePipeline(pipeline)
          setSelection({ stageId: DefaultNewStageId })
          updatePipelineView({
            ...pipelineView,
            isSplitViewOpen: true,
            splitViewData: { type: SplitViewTypes.StageView }
          })
        },
        isStageView: false,
        renderPipelineStage,
        stagesMap: stagesMap,
        contextType,
        templateTypes,
        getTemplate: Promise.reject
      },
      { useArrows: true, darkMode: false, fixedPosition: false, placement: 'bottom-start' }
    )
  }
 
  const nodeListeners: NodeModelListener = {
    [Event.ClickNode]: (_event: any) => {
      dynamicPopoverHandler?.hide()
      Eif (templateIdentifier === DefaultNewTemplateId) {
        openStageSelection(CREATE_NODE_ID)
      }
    }
  }
 
  const getCreateNode = () => {
    const createNode = new CreateNewModel({
      id: CREATE_NODE_ID,
      width: 90,
      height: 40,
      customNodeStyle: { borderColor: 'var(--pipeline-grey-border)' },
      nodeClassName: css.createNewModal
    })
    createNode.registerListener(nodeListeners)
    return createNode
  }
 
  const getOptions = (stage: StageElementConfig): DefaultNodeModelOptions => {
    return {
      identifier: stage.identifier,
      id: stage.identifier,
      customNodeStyle: { ...getCommonStyles(false), borderColor: 'var(--primary-7)', borderStyle: 'solid' },
      name: '',
      isInComplete: false,
      defaultSelected: false,
      draggable: false,
      canDelete: false,
      conditionalExecutionEnabled: stage.when
        ? stage.when?.pipelineStatus !== 'Success' || !!stage.when?.condition?.trim()
        : false,
      allowAdd: false,
      iconStyle: { color: stagesMap[defaultTo(stage.type, '')]?.iconColor },
      icon: stagesMap[defaultTo(stage.type, '')]?.icon,
      nodeClassName: css.createNewModal,
      ...(stage.when && {})
    }
  }
 
  const getStageNode = (stage: StageElementConfig) => {
    const stageNode = new DefaultNodeModel({ ...getOptions(stage), width: 90, height: 40 })
    stageNode.registerListener(nodeListeners)
    return stageNode
  }
 
  const getDiamondStageNode = (stage: StageElementConfig) => {
    const stageNode = new DiamondNodeModel({ ...getOptions(stage), width: 57, height: 57, secondaryIcon: undefined })
    stageNode.registerListener(nodeListeners)
    return stageNode
  }
 
  React.useEffect(() => {
    Iif (!!template.name && !(template.spec as StageElementConfig)?.type) {
      openStageSelection(CREATE_NODE_ID)
    }
  }, [template.name, gitDetails])
 
  React.useEffect(() => {
    const stageType = selectedStage.stage?.stage?.type || ''
    if (stageType) {
      setStageData(stagesMap[stageType])
    }
  }, [selectedStageId])
 
  return (
    <Container
      className={css.container}
      background={Color.FORM_BG}
      width={'100%'}
      padding={{ left: 'xxxlarge', right: 'xxxlarge' }}
      ref={canvasRef}
      onClick={e => {
        const div = e.target as HTMLDivElement
        if (div === canvasRef.current?.children[0]) {
          dynamicPopoverHandler?.hide()
        }
      }}
    >
      <Layout.Vertical height={'100%'} flex={{ justifyContent: 'center', alignItems: 'flex-start' }} spacing={'small'}>
        <Text font={{ size: 'small', weight: 'semi-bold' }} color={Color.GREY_600}>
          Stage Type
        </Text>
        <Container>
          <Layout.Horizontal className={stageData?.isApproval ? css.approvalLayout : css.normalLayout}>
            <Container data-nodeid={CREATE_NODE_ID}>
              {selectedStage.stage?.stage ? (
                stageData?.isApproval ? (
                  <DiamondNodeWidget node={getDiamondStageNode(selectedStage.stage?.stage)} />
                ) : (
                  <DefaultNodeWidget node={getStageNode(selectedStage.stage?.stage)} />
                )
              ) : (
                <CreateNewWidget node={getCreateNode()} />
              )}
            </Container>
            <Text font={{ size: 'small', weight: 'semi-bold' }} color={Color.GREY_600} className={css.stageType}>
              {defaultTo(stageData?.name, '')}
            </Text>
          </Layout.Horizontal>
        </Container>
      </Layout.Vertical>
      <DynamicPopover
        darkMode={false}
        className={stageBuilderCss.renderPopover}
        render={renderPopover}
        bind={setDynamicPopoverHandler}
      />
    </Container>
  )
}