All files / modules/70-pipeline/pages/execution/ExecutionPipelineView/ExecutionGraphView ExecutionGraphView.tsx

79.1% Statements 53/67
68% Branches 34/50
44.44% Functions 4/9
81.54% Lines 53/65

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 216 217 218 219 220 221 222 223 224 225 226 227              2x 2x 2x   2x 2x 2x   2x 2x       2x   2x 2x 2x   2x   2x 2x 2x 2x 2x 2x   2x                                 2x           2x 13x 12x     2x                   2x   2x 1x     1x           1x         2x   2x 1x     1x         1x   1x     12x       12x 12x 12x 12x       12x   12x                                         12x 8x 8x           12x   12x           12x   12x   12x 12x                     12x                                                                                                            
/*
 * 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 { debounce, omit } from 'lodash-es'
import SplitPane, { SplitPaneProps } from 'react-split-pane'
 
import { isExecutionNotStarted, isExecutionSkipped } from '@pipeline/utils/statusHelpers'
import { useExecutionContext } from '@pipeline/context/ExecutionContext'
import { useLocalStorage, useUpdateQueryParams } from '@common/hooks'
import type { ExecutionPageQueryParams } from '@pipeline/utils/types'
import ExecutionStepDetails from '@pipeline/components/ExecutionStepDetails/ExecutionStepDetails'
import {
  ExecutionLayoutContext,
  ExecutionLayoutState
} from '@pipeline/components/ExecutionLayout/ExecutionLayoutContext'
import ExecutionLayoutFloatingView from '@pipeline/components/ExecutionLayout/ExecutionLayoutFloatingView'
 
import { ExecutionStageDetailsHeader } from './ExecutionStageDetailsHeader/ExecutionStageDetailsHeader'
import ExecutionGraph from './ExecutionGraph/ExecutionGraph'
import ExecutionStageDetails from './ExecutionStageDetails/ExecutionStageDetails'
 
import css from './ExecutionGraphView.module.scss'
 
export const PANEL_RESIZE_DELTA = 50
export const MIN_PANEL_SIZE = 200
const IS_TEST = process.env.NODE_ENV === 'test'
const RIGHT_LAYOUT_DEFAULT_SIZE = 570
const BOTTOM_LAYOUT_DEFAULT_SIZE = 500
const EXECUTION_LAYOUT_DOM_ID = `execution-layout-${IS_TEST ? 'test' : /* istanbul ignore next */ Date.now()}`
 
const splitPaneProps: Partial<Record<ExecutionLayoutState, SplitPaneProps>> = {
  [ExecutionLayoutState.RIGHT]: {
    split: 'vertical',
    defaultSize: RIGHT_LAYOUT_DEFAULT_SIZE,
    minSize: 300,
    maxSize: -300,
    primary: 'second'
  },
  [ExecutionLayoutState.BOTTOM]: {
    split: 'horizontal',
    defaultSize: BOTTOM_LAYOUT_DEFAULT_SIZE,
    minSize: 200,
    maxSize: -100,
    primary: 'first'
  }
}
 
const styles: React.CSSProperties = {
  overflow: 'unset',
  position: 'static',
  height: 'min-content'
}
 
export default function ExecutionGraphView(): React.ReactElement {
  const { replaceQueryParams } = useUpdateQueryParams<ExecutionPageQueryParams>()
  const { allNodeMap, pipelineStagesMap, selectedStageId, queryParams, setSelectedStepId } = useExecutionContext()
 
  function handleStepSelection(step?: string): void {
    Iif (!step) {
      const params = {
        ...queryParams
      }
 
      delete params.step
 
      replaceQueryParams(params)
      setSelectedStepId('')
    } else {
      const selectedStep = allNodeMap?.[step]
 
      if (isExecutionNotStarted(selectedStep?.status) || isExecutionSkipped(selectedStep?.status)) {
        return
      }
 
      const params = {
        ...queryParams,
        stage: selectedStageId,
        step
      }
 
      replaceQueryParams(params)
    }
  }
 
  function handleStageSelection(stage: string): void {
    const selectedStage = pipelineStagesMap.get(stage)
 
    if (isExecutionNotStarted(selectedStage?.status) || isExecutionSkipped(selectedStage?.status)) {
      return
    }
 
    const params = {
      ...queryParams,
      stage
    }
 
    delete params.step
 
    replaceQueryParams(params)
  }
 
  const [layouts, setLayoutState] = useLocalStorage<ExecutionLayoutState[]>(
    'execution_layout_2', // increase the number in case data structure is changed
    [ExecutionLayoutState.RIGHT]
  )
  const [layoutState] = layouts
  const [isStepDetailsVisible, setStepDetailsVisibility] = React.useState(false)
  const [primaryPaneSize, setPrimaryPaneSize] = React.useState(250)
  const [tertiaryPaneSize, setTertiaryPaneSize] = React.useState(
    layoutState === ExecutionLayoutState.RIGHT ? RIGHT_LAYOUT_DEFAULT_SIZE : BOTTOM_LAYOUT_DEFAULT_SIZE
  )
  // eslint-disable-next-line react-hooks/exhaustive-deps
  const setStageSplitPaneSizeDebounce = React.useCallback(debounce(setPrimaryPaneSize, 300), [setPrimaryPaneSize])
  // eslint-disable-next-line react-hooks/exhaustive-deps
  const setStepSplitPaneSizeDebounce = React.useCallback(debounce(setTertiaryPaneSize, 300), [setTertiaryPaneSize])
 
  /* Ignoring this function as it is used by "react-split-pane" */
  /* istanbul ignore next */
  function handleStageResize(size: number): void {
    setStageSplitPaneSizeDebounce(size)
  }
 
  function setLayout(e: ExecutionLayoutState): void {
    setLayoutState(prevState => [e, ...prevState.slice(0, 1)])
  }
 
  function restoreDialog(): void {
    setLayoutState(prevState => {
      const prevNonMinState = prevState.find(s => s !== ExecutionLayoutState.MINIMIZE)
 
      return [prevNonMinState || ExecutionLayoutState.RIGHT, ExecutionLayoutState.MINIMIZE]
    })
  }
 
  // handle layout change
  React.useEffect(() => {
    Eif (layoutState === ExecutionLayoutState.RIGHT) {
      setTertiaryPaneSize(RIGHT_LAYOUT_DEFAULT_SIZE)
    } else if (layoutState === ExecutionLayoutState.BOTTOM) {
      setTertiaryPaneSize(BOTTOM_LAYOUT_DEFAULT_SIZE)
    }
  }, [layoutState])
 
  const child1 = <ExecutionGraph onSelectedStage={handleStageSelection} />
  const child2 = (
    <ExecutionStageDetails
      layout={layoutState}
      onStepSelect={handleStepSelection}
      onStageSelect={handleStageSelection}
    />
  )
  const child3 = <ExecutionStepDetails />
 
  let stageGraphPaneStyles = { ...styles }
 
  Eif (layoutState === ExecutionLayoutState.RIGHT) {
    Object.assign(stageGraphPaneStyles, {
      position: 'sticky',
      top: 'var(--execution-stage-details-height)',
      heigth: 'min-content'
    })
  } else if (layoutState === ExecutionLayoutState.BOTTOM) {
    stageGraphPaneStyles = {
      overflow: 'hidden'
    }
  }
 
  return (
    <ExecutionLayoutContext.Provider
      value={{
        layout: layoutState,
        setLayout,
        primaryPaneSize,
        tertiaryPaneSize,
        setPrimaryPaneSize,
        setTertiaryPaneSize,
        isStepDetailsVisible,
        setStepDetailsVisibility,
        restoreDialog
      }}
    >
      <div className={css.main} id={EXECUTION_LAYOUT_DOM_ID}>
        <SplitPane
          split="horizontal"
          className={css.splitPane1}
          minSize={MIN_PANEL_SIZE}
          size={primaryPaneSize}
          onChange={handleStageResize}
          pane1Style={omit(styles, 'height')}
          pane2Style={styles}
          style={styles}
        >
          {child1}
          <div className={css.stageDetails}>
            <ExecutionStageDetailsHeader />
            {isStepDetailsVisible &&
            (layoutState === ExecutionLayoutState.BOTTOM || layoutState === ExecutionLayoutState.RIGHT) ? (
              <SplitPane
                className={css.splitPane2}
                {...splitPaneProps[layoutState]}
                size={tertiaryPaneSize}
                onChange={setStepSplitPaneSizeDebounce}
                pane1Style={stageGraphPaneStyles}
                pane2Style={styles}
                style={styles}
              >
                {child2}
                {child3}
              </SplitPane>
            ) : (
              <React.Fragment>
                {child2}
                {isStepDetailsVisible ? <ExecutionLayoutFloatingView>{child3}</ExecutionLayoutFloatingView> : null}
              </React.Fragment>
            )}
          </div>
        </SplitPane>
      </div>
    </ExecutionLayoutContext.Provider>
  )
}