All files / modules/75-ce/components/BudgetStatusBar BudgetStatusBar.tsx

100% Statements 25/25
85.71% Branches 12/14
100% Functions 2/2
100% Lines 25/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 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              1x 1x 1x 1x   1x 1x 1x 1x   1x           1x 8x   8x                                         8x           8x 4x 4x 4x   4x           4x           4x 4x 4x 4x             8x                                                                                                                                                                                                                               1x  
/*
 * Copyright 2022 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 { Layout, Text, Container } from '@wings-software/uicore'
import cx from 'classnames'
import { FontVariation, Color } from '@harness/design-system'
import type { BudgetSummary } from 'services/ce/services'
import { getAllBudgetCostInfo, getAllBudgetCostInfoReturnType, AlertStatus } from '@ce/utils/budgetUtils'
import { useStrings } from 'framework/strings'
import formatCost from '@ce/utils/formatCost'
import css from './BudgetStatusBar.module.scss'
 
const BAR_WIDTH = 266
 
interface BudgetStatusBarProps {
  rowData: BudgetSummary
}
 
const BudgetStatusBar: (props: BudgetStatusBarProps) => JSX.Element | null = ({ rowData }) => {
  const { getString } = useStrings()
 
  const [costState, setCostState] = useState<getAllBudgetCostInfoReturnType>({
    actualCostStatus: {
      cost: 0,
      ratio: 0,
      percentage: 0,
      status: AlertStatus.Info
    },
    forecastedCostStatus: {
      cost: 0,
      ratio: 0,
      percentage: 0,
      status: AlertStatus.Info
    },
    budgetAmountStatus: {
      cost: 0,
      ratio: 0,
      percentage: 0,
      status: AlertStatus.Info
    }
  })
 
  const [widthState, setWidthState] = useState({
    actualCostBarWidth: 0,
    forecastCostBarWidth: 0,
    budgetCostBarWidth: 0
  })
 
  useEffect(() => {
    const actualCost = rowData.actualCost || 0
    const budgetAmount = rowData.budgetAmount || 1
    const forecastCost = rowData.forecastCost
 
    const { actualCostStatus, forecastedCostStatus, budgetAmountStatus } = getAllBudgetCostInfo(
      forecastCost,
      actualCost,
      budgetAmount
    )
 
    setCostState({
      actualCostStatus,
      forecastedCostStatus,
      budgetAmountStatus
    })
 
    const actualCostBarWidth = Math.floor(BAR_WIDTH * actualCostStatus.ratio)
    const forecastCostBarWidth = Math.floor(BAR_WIDTH * forecastedCostStatus.ratio)
    const budgetCostBarWidth = Math.floor(BAR_WIDTH * budgetAmountStatus.ratio)
    setWidthState({
      actualCostBarWidth,
      forecastCostBarWidth,
      budgetCostBarWidth
    })
  }, [rowData])
 
  return (
    <Layout.Horizontal
      spacing="xsmall"
      padding={{
        top: 'medium'
      }}
    >
      <Text color={Color.GREY_400} font={{ variation: FontVariation.TINY }}>
        {formatCost(0)}
      </Text>
      <Container className={css.budgetBar} width={BAR_WIDTH} background={Color.GREY_200}>
        <div
          style={{
            width: widthState.actualCostBarWidth
          }}
          className={cx(
            css.actualCostBar,
            { [css.alert]: costState.actualCostStatus.status === AlertStatus.Warning },
            { [css.danger]: costState.actualCostStatus.status === AlertStatus.Danger }
          )}
        ></div>
 
        <div
          style={{
            width: widthState.forecastCostBarWidth
          }}
          className={cx(
            css.forecastedBar,
            { [css.alert]: costState.forecastedCostStatus.status === AlertStatus.Warning },
            { [css.danger]: costState.forecastedCostStatus.status === AlertStatus.Danger }
          )}
        ></div>
 
        {costState.forecastedCostStatus.status === AlertStatus.Danger ? (
          <div
            style={{
              width: widthState.budgetCostBarWidth
            }}
            className={css.budgetStatusBar}
          ></div>
        ) : null}
 
        <div
          style={{
            left: widthState.actualCostBarWidth
          }}
          className={cx(
            css.actualCostMarker,
            { [css.alert]: costState.actualCostStatus.status === AlertStatus.Warning },
            { [css.danger]: costState.actualCostStatus.status === AlertStatus.Danger }
          )}
        ></div>
 
        <Text
          font={{ variation: FontVariation.SMALL_BOLD }}
          padding={{
            right: 'xsmall',
            left: 'xsmall'
          }}
          style={{
            left: widthState.actualCostBarWidth
          }}
          className={cx(css.actualCost, { [css.left]: widthState.actualCostBarWidth > 80 })}
        >
          {formatCost(costState.actualCostStatus.cost)}
        </Text>
 
        <div
          style={{
            left: widthState.forecastCostBarWidth,
            opacity: costState.forecastedCostStatus.cost > costState.budgetAmountStatus.cost ? 1 : 0
          }}
          className={css.overBudgetMarker}
        ></div>
 
        <Text
          font={{ variation: FontVariation.SMALL_BOLD }}
          padding={{
            right: 'xsmall',
            left: 'xsmall'
          }}
          color={Color.RED_700}
          icon="execution-warning"
          iconProps={{
            color: Color.RED_500,
            size: 14
          }}
          style={{
            left: widthState.forecastCostBarWidth,
            opacity: costState.forecastedCostStatus.cost > costState.budgetAmountStatus.cost ? 1 : 0
          }}
          className={css.overBudgetText}
        >
          {getString('ce.budgets.listPage.overBudgetText', {
            cost: formatCost(costState.forecastedCostStatus.cost - costState.budgetAmountStatus.cost)
          })}
        </Text>
      </Container>
      <Text
        color={costState.forecastedCostStatus.cost > costState.budgetAmountStatus.cost ? Color.RED_800 : Color.GREY_400}
        font={{ variation: FontVariation.TINY }}
      >
        {formatCost(
          costState.forecastedCostStatus.cost > costState.budgetAmountStatus.cost
            ? costState.forecastedCostStatus.cost
            : costState.budgetAmountStatus.cost
        )}
      </Text>
    </Layout.Horizontal>
  )
}
 
export default BudgetStatusBar