All files / modules/70-pipeline/components/RunPipelineModal RunModalHeader.tsx

52.27% Statements 23/44
27.78% Branches 20/72
22.22% Functions 2/9
54.76% Lines 23/42

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              31x 31x 31x                 31x     31x 31x 31x           31x                 31x 31x   31x                                       31x                                 192x 192x 192x 192x   192x   373x     192x   192x                                                                       192x 11x     181x                                                                                                                    
/*
 * 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, { Dispatch, SetStateAction, useRef } from 'react'
import cx from 'classnames'
import {
  Heading,
  Color,
  HarnessDocTooltip,
  MultiSelectDropDown,
  VisualYamlSelectedView as SelectedView,
  VisualYamlToggle,
  SelectOption
} from '@harness/uicore'
import { isEmpty } from 'lodash-es'
 
import type { FormikErrors } from 'formik'
import { GitSyncStoreProvider } from 'framework/GitRepoStore/GitSyncStoreContext'
import { useStrings } from 'framework/strings'
import { useAppStore } from 'framework/AppStore/AppStoreContext'
import type {
  ResponseInputSetTemplateWithReplacedExpressionsResponse,
  ResponseListStageExecutionResponse,
  ResponsePMSPipelineResponseDTO
} from 'services/pipeline-ng'
import {
  ALL_STAGE_VALUE,
  getAllStageData,
  getAllStageItem,
  SelectedStageData,
  StageSelectionData
} from '@pipeline/utils/runPipelineUtils'
import type { InputSetDTO } from '@pipeline/utils/types'
 
import GitPopover from '../GitPopover/GitPopover'
import { ErrorsStrip } from '../ErrorsStrip/ErrorsStrip'
 
import css from './RunPipelineForm.module.scss'
 
export interface RunModalHeaderProps {
  pipelineExecutionId?: string
  selectedStageData: StageSelectionData
  setSelectedStageData: Dispatch<SetStateAction<StageSelectionData>>
  setSkipPreFlightCheck: Dispatch<SetStateAction<boolean>>
  selectedView: SelectedView
  handleModeSwitch(view: SelectedView): void
  runClicked: boolean
  executionView?: boolean
  pipelineResponse: ResponsePMSPipelineResponseDTO | null
  getTemplateFromPipeline(): Promise<void> | undefined
  template: ResponseInputSetTemplateWithReplacedExpressionsResponse | null
  formRefDom: React.MutableRefObject<HTMLElement | undefined>
  formErrors: FormikErrors<InputSetDTO>
  stageExecutionData: ResponseListStageExecutionResponse | null
  executionStageList: SelectOption[]
}
 
export default function RunModalHeader(props: RunModalHeaderProps): React.ReactElement | null {
  const {
    pipelineExecutionId,
    selectedStageData,
    setSelectedStageData,
    setSkipPreFlightCheck,
    handleModeSwitch,
    runClicked,
    executionView,
    selectedView,
    pipelineResponse,
    getTemplateFromPipeline,
    template,
    formRefDom,
    formErrors,
    stageExecutionData,
    executionStageList
  } = props
  const { isGitSyncEnabled } = useAppStore()
  const { getString } = useStrings()
  const stageSelectionRef = useRef(false)
 
  const isStageExecutionDisabled = (): boolean => {
    //stageExecutionData?.data is empty array when allowStageExecution is set to false in advanced tab
    return Boolean(pipelineExecutionId) || isEmpty(stageExecutionData?.data)
  }
 
  const stageExecutionDisabledTooltip = isStageExecutionDisabled() ? 'stageExecutionDisabled' : undefined
 
  const onStageSelect = (items: SelectOption[]): void => {
    stageSelectionRef.current = true
    const allStagesSelected = items.find(item => item.value === ALL_STAGE_VALUE)
    const updatedSelectedStages: SelectedStageData[] = []
    const hasOnlyAllStagesUnChecked =
      items.length === stageExecutionData?.data?.length &&
      !items.find(item => item.value === getAllStageItem(getString).value)
    if (
      (!selectedStageData.allStagesSelected && allStagesSelected) ||
      hasOnlyAllStagesUnChecked ||
      items?.length === 0
    ) {
      const updatedSelectedStageItems = []
      updatedSelectedStageItems.push(getAllStageItem(getString))
      updatedSelectedStages.push(getAllStageData(getString))
 
      setSelectedStageData({
        selectedStages: updatedSelectedStages,
        selectedStageItems: updatedSelectedStageItems,
        allStagesSelected: true
      })
    } else {
      const newItems = items.filter((option: SelectOption) => {
        const stageDetails = stageExecutionData?.data?.find(stageData => stageData.stageIdentifier === option.value)
        stageDetails && updatedSelectedStages.push(stageDetails)
        return option.value !== ALL_STAGE_VALUE
      })
      setSelectedStageData({
        selectedStages: updatedSelectedStages,
        selectedStageItems: newItems,
        allStagesSelected: false
      })
    }
    setSkipPreFlightCheck(true)
  }
 
  if (executionView) {
    return null
  }
 
  return (
    <>
      <div className={css.runModalHeader}>
        <Heading
          level={2}
          font={{ weight: 'bold' }}
          color={Color.BLACK_100}
          className={css.runModalHeaderTitle}
          data-tooltip-id="runPipelineFormTitle"
        >
          {getString('runPipeline')}
          <HarnessDocTooltip tooltipId="runPipelineFormTitle" useStandAlone={true} />
        </Heading>
        {isGitSyncEnabled && (
          <GitSyncStoreProvider>
            <GitPopover
              data={pipelineResponse?.data?.gitDetails ?? {}}
              iconProps={{ margin: { left: 'small', top: 'xsmall' } }}
            />
          </GitSyncStoreProvider>
        )}
 
        <div data-tooltip-id={stageExecutionDisabledTooltip}>
          <MultiSelectDropDown
            popoverClassName={css.disabledStageDropdown}
            hideItemCount={selectedStageData.allStagesSelected}
            disabled={isStageExecutionDisabled()}
            buttonTestId={'stage-select'}
            onChange={onStageSelect}
            onPopoverClose={() => {
              if (stageSelectionRef.current) {
                getTemplateFromPipeline()?.then(() => {
                  stageSelectionRef.current = false
                })
              }
            }}
            value={selectedStageData.selectedStageItems}
            items={executionStageList}
            minWidth={150}
            usePortal={true}
            placeholder={selectedStageData.allStagesSelected ? getString('pipeline.allStages') : getString('stages')}
            className={cx({ [css.stagesDropdown]: isGitSyncEnabled })}
          />
          <HarnessDocTooltip tooltipId={stageExecutionDisabledTooltip} useStandAlone={true} />
        </div>
 
        <div className={css.optionBtns}>
          <VisualYamlToggle
            selectedView={selectedView}
            onChange={handleModeSwitch}
            disableToggle={!template?.data?.inputSetTemplateYaml}
          />
        </div>
      </div>
      {runClicked ? <ErrorsStrip domRef={formRefDom} formErrors={formErrors} /> : null}
    </>
  )
}