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

69.23% Statements 18/26
31.82% Branches 7/22
60% Functions 3/5
68% Lines 17/25

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              12x 12x   12x 12x 12x 12x 12x 12x       12x 12x 12x                     12x 2x   2x             12x     2x   2x                                                                              
/*
 * 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, { useState } from 'react'
import { merge } from 'lodash-es'
import type { SeriesColumnOptions } from 'highcharts'
import { Button, Container, Layout } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import cx from 'classnames'
import { TimeSeriesAreaChart } from '@common/components/TimeSeriesAreaChart/TimeSeriesAreaChart'
import { StackedColumnChart } from '@common/components/StackedColumnChart/StackedColumnChart'
import css from './OverviewChartsWithToggle.module.scss'
 
type DataType = Omit<SeriesColumnOptions, 'type'>[]
 
export enum ChartType {
  BAR = 'BAR',
  LINE = 'LINE'
}
 
export interface OverviewChartsWithToggleProps {
  defaultChartType?: ChartType
  customChartOptions?: Highcharts.Options
  data: DataType
  summaryCards?: JSX.Element
  updateSelectedView?: (selectedView: ChartType) => void
}
 
const renderChart = (selectedView: ChartType, props: OverviewChartsWithToggleProps): JSX.Element => {
  const customChartOptions = merge({ chart: { height: 200 } }, props.customChartOptions)
 
  return selectedView === ChartType.BAR ? (
    <StackedColumnChart options={customChartOptions} data={props.data}></StackedColumnChart>
  ) : (
    <TimeSeriesAreaChart customChartOptions={customChartOptions} seriesData={props?.data} />
  )
}
 
export const OverviewChartsWithToggle: React.FC<OverviewChartsWithToggleProps> = (
  props: OverviewChartsWithToggleProps
) => {
  const [selectedView, setSelectedView] = useState<ChartType>(props.defaultChartType || ChartType.BAR)
 
  return (
    <Layout.Vertical spacing="large">
      <Container flex>
        <Layout.Horizontal spacing={'medium'}>{props.summaryCards}</Layout.Horizontal>
        <Container flex className={css.toggleBtns}>
          <Button
            minimal
            icon="bar-chart"
            active={selectedView === ChartType.BAR}
            className={cx(css.chartIcon, { [css.active]: selectedView === ChartType.BAR })}
            iconProps={{ size: 12, color: selectedView === ChartType.BAR ? Color.PRIMARY_6 : Color.GREY_700 }}
            onClick={e => {
              e.stopPropagation()
              if (selectedView !== ChartType.BAR) {
                setSelectedView(ChartType.BAR)
                props.updateSelectedView && props.updateSelectedView(ChartType.BAR)
              }
            }}
          />
          <Button
            minimal
            icon="line-chart"
            active={selectedView === ChartType.LINE}
            iconProps={{ size: 12, color: selectedView === ChartType.LINE ? Color.PRIMARY_6 : Color.GREY_700 }}
            className={cx(css.chartIcon, { [css.active]: selectedView === ChartType.LINE })}
            onClick={e => {
              e.stopPropagation()
              if (selectedView !== ChartType.LINE) {
                setSelectedView(ChartType.LINE)
                props.updateSelectedView && props.updateSelectedView(ChartType.LINE)
              }
            }}
          />
        </Container>
      </Container>
      {renderChart(selectedView, props)}
    </Layout.Vertical>
  )
}