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

96.15% Statements 25/26
92.86% Branches 13/14
100% Functions 5/5
96.15% Lines 25/26

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              1x 1x 1x 1x     1x 1x 1x 1x             1x 1x   1x             1x 24x     1x 12x                           1x 12x                           1x 12x 12x 12x 12x 12x           1x                                                                       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 { Container, Text } from '@wings-software/uicore'
import React from 'react'
import { Color, FontVariation } from '@harness/design-system'
import moment from 'moment'
import type { CellProps, Renderer } from 'react-table'
import type { BudgetData, BudgetCostData, BudgetPeriod } from 'services/ce/services'
import { useStrings } from 'framework/strings'
import Grid from '@ce/components/PerspectiveGrid/Grid'
import formatCost from '@ce/utils/formatCost'
import { getTimeRangeExpression } from '../BudgetDetailsChart/budgetCategoryUtil'
 
interface BudgetDetailsGridProps {
  gridData: BudgetData
  budgetPeriod: BudgetPeriod
}
 
const BudgetDetailsGrid: (props: BudgetDetailsGridProps) => JSX.Element | null = ({ gridData, budgetPeriod }) => {
  const { getString } = useStrings()
 
  Iif (!gridData) {
    return null
  }
 
  /* istanbul ignore next */
  const costData = gridData.costData || []
 
  const CostCell: Renderer<CellProps<BudgetCostData>> = ({ cell }) => {
    return <Text font={{ variation: FontVariation.BODY }}>{formatCost(cell.value)}</Text>
  }
 
  const BudgetVarianceCell: Renderer<CellProps<BudgetCostData>> = ({ cell }) => {
    return (
      <Text
        rightIcon={cell.value > 0 ? 'caret-up' : 'caret-down'}
        rightIconProps={{
          color: cell.value > 0 ? Color.RED_600 : Color.GREEN_600
        }}
        font={{ variation: FontVariation.BODY }}
        color={cell.value > 0 ? Color.RED_600 : Color.GREEN_600}
      >
        {formatCost(cell.value)}
      </Text>
    )
  }
 
  const BudgetVariancePercentageCell: Renderer<CellProps<BudgetCostData>> = ({ cell }) => {
    return (
      <Text
        rightIcon={cell.value > 0 ? 'caret-up' : 'caret-down'}
        rightIconProps={{
          color: cell.value > 0 ? Color.RED_600 : Color.GREEN_600
        }}
        font={{ variation: FontVariation.BODY }}
        color={cell.value > 0 ? Color.RED_600 : Color.GREEN_600}
      >
        {`${cell.value}%`}
      </Text>
    )
  }
 
  const TimeCell: Renderer<CellProps<BudgetCostData>> = ({ cell }) => {
    const rowData = cell.row.original
    const startTime = moment.utc(rowData.time)
    const endTime = moment.utc(rowData.endTime)
    const rangeText = getTimeRangeExpression(budgetPeriod, startTime, endTime)
    return <Text font={{ variation: FontVariation.BODY }}>{rangeText}</Text>
  }
 
  /* istanbul ignore next */
  const formattedData = costData.filter(e => e?.time).sort((a, b) => b?.time - a?.time) as BudgetCostData[]
 
  return (
    <Container>
      <Grid<BudgetCostData>
        columns={[
          {
            Header: getString('ce.budgets.detailsPage.tableHeaders.budgetPeriod'),
            accessor: 'time',
            Cell: TimeCell
          },
          {
            Header: getString('ce.budgets.detailsPage.tableHeaders.actualCost'),
            accessor: 'actualCost',
            Cell: CostCell
          },
          {
            Header: getString('ce.budgets.detailsPage.tableHeaders.budgetCost'),
            accessor: 'budgeted',
            Cell: CostCell
          },
          {
            Header: getString('ce.budgets.detailsPage.tableHeaders.budgetVariance'),
            accessor: 'budgetVariance',
            Cell: BudgetVarianceCell
          },
          {
            Header: getString('ce.budgets.detailsPage.tableHeaders.budgetVariancePercentage'),
            accessor: 'budgetVariancePercentage',
            Cell: BudgetVariancePercentageCell
          }
        ]}
        data={formattedData}
      />
    </Container>
  )
}
 
export default BudgetDetailsGrid