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

86.49% Statements 32/37
85.71% Branches 12/14
77.78% Functions 7/9
86.11% Lines 31/36

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              1x 1x 1x 1x 1x   1x                         6x 6x 15x   15x 15x 3x   15x 15x   6x       2x 5x 5x 1x           7x 7x     7x 3x 3x 3x       7x         5x 1x     1x   1x                             1x  
/*
 * 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, { useEffect, useState } from 'react'
import cx from 'classnames'
import { cloneDeep } from 'lodash-es'
import { Tree, ITreeNode, IProps } from '@blueprintjs/core'
import css from './StagesTree.module.scss'
 
export const stagesTreeNodeClasses = {
  primary: css.stagesTreeBulletSquare,
  secondary: css.stagesTreeBulletCircle,
  empty: css.stagesTreeBulletEmpty
}
 
export interface StagesTreeProps extends IProps {
  contents: ITreeNode[]
  selectedId: string | undefined
  selectionChange: (selectedId: string, node: ITreeNode) => void
}
 
function expandToSelected(nodes: ITreeNode[], id: string): boolean {
  let retExpanded = false
  nodes.forEach(node => {
    node.isSelected = node.id === id
 
    let expanded = node.id === id
    if (node.childNodes) {
      expanded = expandToSelected(node.childNodes, id) || expanded
    }
    node.isExpanded = node.isExpanded || expanded
    retExpanded = retExpanded || expanded
  })
  return retExpanded
}
 
function forEachNode(nodes: ITreeNode[], callback: (node: ITreeNode) => void): void {
  for (const node of nodes) {
    callback(node)
    if (node.childNodes) {
      forEachNode(node.childNodes, callback)
    }
  }
}
 
function StagesTree(props: StagesTreeProps): React.ReactElement {
  const { contents, selectedId, selectionChange, className } = props
  const [nodes, setNodes] = useState(cloneDeep(contents))
 
  // expand to selected one
  useEffect(() => {
    Eif (selectedId) {
      expandToSelected(contents, selectedId)
      setNodes(contents)
    }
  }, [contents, selectedId])
 
  return (
    <div className={cx(className, css.container)}>
      <Tree
        contents={contents}
        onNodeClick={node => {
          forEachNode(nodes, n => (n.isSelected = false))
          Iif (node.hasCaret) {
            node.isExpanded = !node.isExpanded
          } else {
            selectionChange(node.id as string, node)
          }
          setNodes(cloneDeep(nodes))
        }}
        onNodeCollapse={node => {
          node.isExpanded = false
          setNodes(cloneDeep(nodes))
        }}
        onNodeExpand={node => {
          node.isExpanded = true
          setNodes(cloneDeep(nodes))
        }}
      />
    </div>
  )
}
 
export default StagesTree