All files / modules/10-common/components/HeatMap HeatMap.tsx

29.09% Statements 16/55
3.08% Branches 2/65
7.14% Functions 1/14
30.19% Lines 16/53

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              497x 497x 497x 497x 497x 497x 497x 497x                                                                       497x 497x 497x 497x     497x           497x           497x                                                                                                                                                                                                           497x                                                                                    
/*
 * 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 { Container, Text } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import { Classes, Popover, PopoverInteractionKind } from '@blueprintjs/core'
import cx from 'classnames'
import isUndefined from 'lodash/isUndefined'
import { getColorStyle } from './ColorUtils'
import styles from './HeatMap.module.scss'
 
export interface SerieConfig {
  name?: string
  data: Array<any>
}
 
export type OnCellClick = { cell: any; series: SerieConfig; isSelected: boolean; onDismiss: () => void }
export interface HeatMapProps {
  series: Array<SerieConfig> | SerieConfig
  minValue: number
  maxValue: number
  mapValue(cell: any): number | CellStatusValues
  cellShapeBreakpoint?: number
  /**
   * This property can be used if series are not prepared yet, to render placehoders,
   * or to limit the row sizes.
   */
  rowSize?: number
  onCellClick?(cell?: any, serie?: any): void
  renderTooltip?(info: OnCellClick): JSX.Element | null
  labelsWidth?: number
  className?: string
  cellClassName?: string
}
 
export interface HeatMapCellProps {
  color?: string
  colorClassName?: string
  className?: string
  popoverDisabled: boolean
  popoverContent?: JSX.Element | null
  isSelected?: boolean
  onClick?: () => void
}
 
export enum CellStatusValues {
  Missing = 'Missing',
  Empty = 'Empty',
  Error = 'Error'
}
 
const specialColorValue = {
  MISSING: Color.GREY_200,
  EMPTY: Color.GREY_250,
  ERROR: Color.RED_800
}
 
const PopoverModifies = {
  arrow: { enabled: true },
  flip: { enabled: true },
  keepTogether: { enabled: true }
}
 
export default function HeatMap({
  series: seriesProp,
  minValue,
  maxValue,
  mapValue,
  rowSize,
  onCellClick,
  renderTooltip,
  labelsWidth = 125,
  className,
  cellClassName
}: HeatMapProps): JSX.Element {
  const series = Array.isArray(seriesProp) ? seriesProp : [seriesProp]
  const [selectedCell, setSelectedCell] = useState<any>()
  let rowLimit: number
  if (!isUndefined(rowSize)) {
    rowLimit = rowSize
  } else {
    rowLimit = series.reduce((a, c) => Math.max(a, c.data.length), 0)
  }
 
  const mapColor = (cell: any) => {
    const value: number | CellStatusValues = mapValue(cell)
    if (value === CellStatusValues.Missing) {
      return { color: specialColorValue.MISSING }
    } else if (value === CellStatusValues.Empty) {
      return { color: specialColorValue.EMPTY }
    } else if (value === CellStatusValues.Error) {
      return { color: specialColorValue.ERROR }
    }
    return {
      colorClassName: getColorStyle(value, minValue, maxValue)
    }
  }
 
  const showLabels = series.some(serie => !isUndefined(serie.name))
 
  useEffect(() => {
    setSelectedCell(null)
  }, [seriesProp])
 
  return (
    <Container className={cx(styles.heatMap, className)}>
      {series.map((serie, serieIndex) => (
        <div key={serieIndex} className={styles.heatMapRow}>
          {showLabels && (
            <span className={styles.nameWrapper}>
              <Text font={{ weight: 'bold', size: 'small' }} width={labelsWidth}>
                {serie.name}
              </Text>
            </span>
          )}
          <div className={styles.rowValues}>
            {serie.data.map((cell, index) => {
              if (index >= rowLimit) {
                return null
              }
              const isSelected =
                selectedCell?.startTime === cell.startTime &&
                selectedCell?.endTime === cell.endTime &&
                serie.name === selectedCell.category
              return (
                <HeatMapCell
                  key={index}
                  isSelected={isSelected}
                  popoverDisabled={!renderTooltip}
                  popoverContent={renderTooltip?.({
                    cell,
                    series: serie,
                    isSelected,
                    onDismiss: () => {
                      onCellClick?.()
                      setSelectedCell(undefined)
                    }
                  })}
                  onClick={() => {
                    onCellClick?.(cell, serie)
                    setSelectedCell({ ...cell, category: serie.name })
                  }}
                  {...mapColor(cell)}
                  className={cx(selectedCell && !isSelected ? styles.opaqueSquare : undefined, cellClassName)}
                />
              )
            })}
            {serie.data.length < rowLimit &&
              new Array(rowLimit - serie.data.length)
                .fill(null)
                .map((_, index) => (
                  <HeatMapCell
                    key={serie.data.length + index}
                    color={specialColorValue.MISSING}
                    className={cellClassName}
                    popoverDisabled
                  />
                ))}
          </div>
        </div>
      ))}
    </Container>
  )
}
 
export function HeatMapCell({
  color,
  colorClassName,
  className,
  popoverDisabled = false,
  popoverContent,
  onClick,
  isSelected
}: HeatMapCellProps): JSX.Element {
  const [isOpen, setIsOpen] = useState<boolean | undefined>(isSelected ? true : undefined)
  useEffect(() => {
    if (isSelected && !isOpen) {
      setIsOpen(true)
    } else if (!isSelected && isOpen) {
      setIsOpen(false)
    }
  }, [isSelected])
  return (
    <Container onClick={onClick} className={cx(styles.cell, className)}>
      <Popover
        className={cx(styles.cellContentWrapper, Classes.DARK)}
        disabled={popoverDisabled}
        content={popoverContent || <Container />}
        interactionKind={isOpen !== undefined ? undefined : PopoverInteractionKind.HOVER}
        modifiers={PopoverModifies}
        isOpen={isOpen}
        onInteraction={() => {
          if (isSelected === false) {
            setIsOpen(undefined)
          }
        }}
        lazy
        boundary="window"
      >
        <Container>
          <Container className={cx(styles.cellInner, colorClassName)} background={color} />
          {isSelected && <Container height={17} width={17} className={styles.selectedSquare} />}
        </Container>
      </Popover>
    </Container>
  )
}