All files / modules/70-pipeline/components/ExecutionCard/MiniExecutionGraph MiniExecutionGraph.tsx

65.45% Statements 36/55
51.11% Branches 23/45
54.55% Functions 6/11
64.81% Lines 35/54

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              20x 20x 20x 20x 20x     20x 20x     20x 20x 20x   20x 20x           20x 33x                   33x 33x 33x 33x 33x   33x 11x         33x   11x 10x 9x 9x   9x         9x                 33x                                                   33x                                                         33x             44x 1x                 43x 43x                                                                                                                  
/*
 * 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 { Icon, Button, Text } from '@wings-software/uicore'
import { ResizeSensor } from '@blueprintjs/core'
import cx from 'classnames'
import { throttle } from 'lodash-es'
 
import type { PipelineExecutionSummary } from 'services/pipeline-ng'
import { isExecutionRunning, isExecutionCompletedWithBadState } from '@pipeline/utils/statusHelpers'
import { processLayoutNodeMap, ExecutionStatusIconMap as IconMap } from '@pipeline/utils/executionUtils'
import type { ProjectPathProps, ModulePathParams } from '@common/interfaces/RouteInterfaces'
 
import { StageNode, RunningIcon } from './StageNode'
import { ParallelStageNode } from './ParallelStageNode'
import css from './MiniExecutionGraph.module.scss'
 
const SCROLL_DELTA = 60
const THROTTLE_TIME = 300
 
export interface MiniExecutionGraphProps extends ProjectPathProps, ModulePathParams {
  pipelineExecution: PipelineExecutionSummary
}
 
export default function MiniExecutionGraph(props: MiniExecutionGraphProps): React.ReactElement {
  const { pipelineExecution, accountId, orgIdentifier, projectIdentifier, module } = props
  const {
    successfulStagesCount,
    runningStagesCount,
    failedStagesCount = 0,
    status,
    totalStagesCount,
    executionErrorInfo,
    pipelineIdentifier = '',
    planExecutionId = ''
  } = pipelineExecution
  const [showButtons, setShowButtons] = React.useState(false)
  const elements = React.useMemo(() => processLayoutNodeMap(pipelineExecution), [pipelineExecution])
  const graphRef = React.useRef<HTMLDivElement | null>(null)
  const wrapperRef = React.useRef<HTMLDivElement | null>(null)
 
  React.useLayoutEffect(() => {
    hideShowButtons()
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [])
 
  // eslint-disable-next-line react-hooks/exhaustive-deps
  const hideShowButtons = React.useCallback(
    throttle(() => {
      window.requestAnimationFrame(() => {
        if (graphRef.current && wrapperRef.current) {
          const graph = graphRef.current.getBoundingClientRect()
          const wrapper = wrapperRef.current.getBoundingClientRect()
 
          Iif (graph.width > wrapper.width) {
            // show buttons
            setShowButtons(true)
          } else {
            // hide buttons
            setShowButtons(false)
          }
        }
      })
    }, THROTTLE_TIME),
    []
  )
 
  // eslint-disable-next-line react-hooks/exhaustive-deps
  const handleRightArrowClick = React.useCallback(
    throttle(
      (e: React.SyntheticEvent<Element>): void => {
        e.preventDefault()
        e.stopPropagation()
        window.requestAnimationFrame(() => {
          if (graphRef.current && wrapperRef.current) {
            const graph = graphRef.current.getBoundingClientRect()
            const wrapper = wrapperRef.current.getBoundingClientRect()
 
            if (graph.right > wrapper.right) {
              graphRef.current.style.transform = `translateX(${Math.max(
                graph.left - wrapper.left - SCROLL_DELTA,
                wrapper.width - graph.width
              )}px)`
            }
          }
        })
      },
      THROTTLE_TIME,
      { leading: true }
    ),
    []
  )
 
  // eslint-disable-next-line react-hooks/exhaustive-deps
  const handleLeftArrowClick = React.useCallback(
    throttle(
      (e: React.SyntheticEvent<Element>): void => {
        e.preventDefault()
        e.stopPropagation()
        window.requestAnimationFrame(() => {
          if (graphRef.current && wrapperRef.current) {
            const graph = graphRef.current.getBoundingClientRect()
            const wrapper = wrapperRef.current.getBoundingClientRect()
 
            if (graph.left < wrapper.left) {
              graphRef.current.style.transform = `translateX(${Math.min(
                graph.left - wrapper.left + SCROLL_DELTA,
                0
              )}px)`
            }
          }
        })
      },
      THROTTLE_TIME,
      { leading: true }
    ),
    []
  )
 
  function killEvent(e: React.SyntheticEvent): void {
    e.stopPropagation()
  }
 
  return (
    <ResizeSensor onResize={hideShowButtons}>
      <div className={css.main}>
        <div className={css.graphAligner} onClick={killEvent}>
          <div ref={wrapperRef} className={cx(css.graphWrapper, { [css.hasButtons]: showButtons })}>
            <div ref={graphRef} className={css.graph}>
              {(elements || []).map(({ stage, parallel }, i) => {
                if (parallel && Array.isArray(parallel)) {
                  return (
                    <ParallelStageNode
                      key={i}
                      stages={parallel}
                      {...{ accountId, orgIdentifier, projectIdentifier, module, pipelineIdentifier, planExecutionId }}
                    />
                  )
                }
 
                Eif (stage) {
                  return (
                    <StageNode
                      key={stage.nodeUuid}
                      stage={stage}
                      {...{ accountId, orgIdentifier, projectIdentifier, module, pipelineIdentifier, planExecutionId }}
                    />
                  )
                }
 
                return null
              })}
            </div>
          </div>
          {showButtons ? (
            <React.Fragment>
              <Button
                minimal
                icon="arrow-left"
                className={css.scrollLeft}
                iconProps={{ size: 10 }}
                onClick={handleLeftArrowClick}
              />
              <Button
                minimal
                icon="arrow-right"
                className={css.scrollRight}
                iconProps={{ size: 10 }}
                onClick={handleRightArrowClick}
              />
            </React.Fragment>
          ) : null}
        </div>
        <div className={css.stepCounts}>
          <div className={css.stepCount} data-status="success">
            <Icon name={IconMap.Success} size={14} />
            {successfulStagesCount} / {totalStagesCount}
          </div>
          {isExecutionRunning(status) ? (
            <div className={css.stepCount} data-status="running">
              <RunningIcon />
              {runningStagesCount}
            </div>
          ) : isExecutionCompletedWithBadState(status) && failedStagesCount > 0 ? (
            <div className={css.stepCount} data-status="failed">
              <Icon name={IconMap.Failed} size={14} /> {failedStagesCount}
            </div>
          ) : null}
          {isExecutionCompletedWithBadState(status) && executionErrorInfo?.message ? (
            <Text lineClamp={1} className={cx(css.stepCount, css.errorMsg)}>
              {executionErrorInfo.message}
            </Text>
          ) : null}
        </div>
      </div>
    </ResizeSensor>
  )
}