All files / modules/70-pipeline/components/PipelineStages PipelineStages.tsx

54.17% Statements 26/48
25.4% Branches 16/63
41.67% Functions 5/12
56.52% Lines 26/46

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              23x 23x       23x                                         23x                         2x 2x   2x 1x 1x 1x           1x     2x 2x 2x   2x 1x         2x 1x 1x     2x   2x 2x     2x                                                 2x 2x 2x                                                                                              
/*
 * 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 { defaultTo } from 'lodash-es'
import type { PipelineInfoConfig } from 'services/cd-ng'
import type { TemplateSummaryResponse } from 'services/template-ng'
import type { GetTemplateProps, GetTemplateResponse } from '@pipeline/utils/useTemplateSelector'
import { AddStageView } from './views/AddStageView'
import type { PipelineStageProps } from './PipelineStage'
 
export interface PipelineStagesProps<T = Record<string, unknown>> {
  children: Array<React.ReactElement<PipelineStageProps> | null>
  minimal?: boolean
  stageType?: string
  isParallel?: boolean
  getNewStageFromType?: (type: string, clearDefaultValues?: boolean) => T
  getNewStageFromTemplate?: (template: TemplateSummaryResponse, clearDefaultValues?: boolean) => T
  stageProps?: T
  onSelectStage?: (stageType: string, stage?: T, pipeline?: PipelineInfoConfig) => void
  showSelectMenu?: boolean
  contextType?: string
  getTemplate?: (data: GetTemplateProps) => Promise<GetTemplateResponse>
}
 
interface PipelineStageMap extends Omit<PipelineStageProps, 'minimal'> {
  index: number
}
 
export function PipelineStages<T = Record<string, unknown>>({
  children,
  showSelectMenu,
  isParallel = false,
  contextType,
  onSelectStage,
  getNewStageFromType,
  getNewStageFromTemplate,
  stageType,
  stageProps,
  minimal = false,
  getTemplate
}: PipelineStagesProps<T>): JSX.Element {
  const [stages, setStages] = React.useState<Map<string, PipelineStageMap>>(new Map())
  const [template, setTemplate] = React.useState<TemplateSummaryResponse>()
 
  React.useLayoutEffect(() => {
    const stagesLocal: Map<string, PipelineStageMap> = new Map()
    const steps = React.Children.toArray(children) as React.ReactElement<PipelineStageProps>[]
    steps.forEach((child, i: number) => {
      stagesLocal.set(child.props.type, {
        ...child.props,
        index: i
      })
    })
    setStages(stagesLocal)
  }, [children])
 
  const [showMenu, setShowMenu] = React.useState(showSelectMenu)
  const [type, setType] = React.useState(stageType)
  const [stageData, setStageData] = React.useState<T>()
 
  React.useEffect(() => {
    Iif (stageType) {
      setType(stageType)
    }
  }, [stageType])
 
  React.useEffect(() => {
    Eif (showSelectMenu) {
      setShowMenu(true)
    }
  }, [showSelectMenu])
  const selected = stages.get(type || '')
 
  const childTypes = React.useMemo(() => {
    return [...stages.values()].filter(item => !item.isDisabled && !!item.isTemplateSupported).map(item => item.type)
  }, [stages])
 
  const onUseTemplate = async () => {
    if (getTemplate) {
      try {
        const { template: newTemplate, isCopied } = await getTemplate({
          templateType: 'Stage',
          allChildTypes: childTypes
        })
        if (getNewStageFromType) {
          setShowMenu(false)
          setType(newTemplate.childType)
          if (isCopied) {
            setStageData(getNewStageFromTemplate?.(newTemplate, true))
          } else {
            setStageData(getNewStageFromType?.(newTemplate.childType || '', true))
            setTemplate(newTemplate)
          }
        } else {
          onSelectStage?.(defaultTo(newTemplate.childType, ''))
        }
      } catch (_) {
        // Do nothing.. user cancelled template selection
      }
    }
  }
 
  const selectedStageIndex = selected?.index || 0
  const stage = React.Children.toArray(children)[selectedStageIndex] as React.ReactElement<PipelineStageProps>
  return (
    <>
      {showSelectMenu && showMenu && (
        <AddStageView
          stages={[...stages].map(item => item[1])}
          isParallel={isParallel}
          contextType={contextType}
          callback={selectedType => {
            if (getNewStageFromType) {
              setShowMenu(false)
              setType(selectedType)
              setStageData(getNewStageFromType?.(selectedType, true))
            } else {
              onSelectStage?.(selectedType)
            }
          }}
          onUseTemplate={onUseTemplate}
        />
      )}
      {!showSelectMenu && selected && stage && (
        <>
          {React.cloneElement(stage, {
            ...selected,
            key: selected.type,
            minimal,
            stageProps: stageProps as Record<string, unknown>
          })}
        </>
      )}
      {!showMenu && showSelectMenu && type && stage && stageData && (
        <>
          {React.cloneElement(stage, {
            ...selected,
            minimal: true,
            stageProps: {
              data: stageData,
              template: template,
              onSubmit: (data: T, _id?: string, pipeline?: PipelineInfoConfig) => {
                onSelectStage?.(type, data, pipeline)
              }
            }
          })}
        </>
      )}
    </>
  )
}