All files / modules/33-auth-settings/pages/subscriptions/overview UsageInfoCard.tsx

84.44% Statements 38/45
76.67% Branches 23/30
83.33% Functions 5/6
84.09% Lines 37/44

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              5x 5x 5x 5x 5x 5x 5x     7x                                               7x 7x 7x 7x 7x 5x 5x 5x 5x 5x     5x   7x               5x 4x 4x             4x                                     5x       5x                     7x   7x     14x         14x 2x 2x 2x   12x     12x     7x                                                                         5x  
/*
 * 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 from 'react'
import { Card, Layout, Text, Icon, Popover, Container } from '@wings-software/uicore'
import { Position, PopoverInteractionKind, Classes } from '@blueprintjs/core'
import { Color } from '@harness/design-system'
import { useStrings } from 'framework/strings'
import PercentageBar from './PercentageBar'
import css from './SubscriptionUsageCard.module.scss'
 
function getInfoIcon(tooltip: string): React.ReactElement {
  return (
    <Popover
      popoverClassName={Classes.DARK}
      position={Position.BOTTOM}
      interactionKind={PopoverInteractionKind.HOVER}
      content={
        <Text width={200} padding="medium" color={Color.WHITE}>
          {tooltip}
        </Text>
      }
    >
      <Icon name="info" size={15} />
    </Popover>
  )
}
 
interface PercentageBarReturn {
  width: number
  color: Color
  percentage: number
  overPercentage?: number
}
 
function getPercentageBarProps(dividend?: number, divisor?: number): PercentageBarReturn {
  let width = 0,
    color = Color.PRIMARY_6,
    percentage = 0,
    overPercentage = undefined
  if (divisor && divisor > 0 && dividend && dividend >= 0) {
    percentage = (dividend * 100) / divisor
    width = percentage > 100 ? 100 : percentage
    color = percentage >= 90 ? Color.ORANGE_500 : color
    const isOverSubscribed = percentage > 100
    Iif (isOverSubscribed) {
      overPercentage = Math.round(percentage - 100)
    }
    percentage = Math.round(percentage)
  }
  return { width, color, percentage, overPercentage }
}
 
const PercentageSubscribedLabel: React.FC<{
  overPercentage?: number
  percentage: number
  color: string
  label: string
}> = ({ overPercentage, percentage, color, label }) => {
  const { getString } = useStrings()
  Iif (overPercentage) {
    return (
      <Text font={{ size: 'xsmall' }} color={color}>
        {overPercentage}% {getString('common.overSubscribed')}
      </Text>
    )
  }
  return (
    <Text font={{ size: 'xsmall' }} color={percentage > 90 ? Color.ORANGE_500 : ''}>
      {percentage}% {label}
    </Text>
  )
}
 
interface UsageInfoCardProps {
  subscribed?: number
  usage: number
  leftHeader: string
  tooltip: string
  rightHeader?: string
  hasBar: boolean
  leftFooter?: string
  rightFooter?: string
  prefix?: string
}
 
export const ErrorContainer = ({ children }: { children: React.ReactElement }): React.ReactElement => {
  return <Container width={300}>{children}</Container>
}
 
const UsageInfoCard: React.FC<UsageInfoCardProps> = ({
  subscribed,
  usage,
  leftHeader,
  tooltip,
  rightHeader,
  hasBar,
  leftFooter,
  rightFooter,
  prefix
}) => {
  const { overPercentage, percentage, width, color } = getPercentageBarProps(usage, subscribed)
 
  const { getString } = useStrings()
 
  function getLabel(value: number | undefined): string | number | undefined {
    Iif (value && value >= 1000000) {
      let roundValue = Math.round(value / 10000)
      roundValue = Math.trunc(roundValue) / 100
      return `${roundValue}M`
    }
    if (value && value >= 1000) {
      let roundValue = Math.round(value / 10)
      roundValue = Math.trunc(roundValue) / 100
      return `${roundValue}K`
    }
    Iif (value === -1) {
      return getString('common.unlimited')
    }
    return value
  }
 
  return (
    <Card className={css.innerCard}>
      <Layout.Vertical>
        <Layout.Horizontal flex={{ alignItems: 'baseline', justifyContent: 'space-between' }}>
          <Layout.Horizontal spacing="xsmall" flex={{ alignItems: 'baseline' }}>
            <Text font={{ size: 'small' }} color={Color.GREY_700}>
              {leftHeader}
            </Text>
            {getInfoIcon(tooltip)}
          </Layout.Horizontal>
          <Text font={{ size: 'small' }} color={Color.GREY_500}>
            {rightHeader}
          </Text>
        </Layout.Horizontal>
        <Text font={{ size: 'large', weight: 'bold' }} color={Color.BLACK}>
          {prefix}
          {getLabel(usage)}
        </Text>
        {hasBar && <PercentageBar width={width} />}
        <Layout.Horizontal flex={{ justifyContent: 'space-between' }}>
          <Text font={{ size: 'xsmall' }}>
            {getLabel(subscribed)} {leftFooter}
          </Text>
          {rightFooter && (
            <PercentageSubscribedLabel
              overPercentage={overPercentage}
              percentage={percentage}
              color={color}
              label={rightFooter}
            />
          )}
        </Layout.Horizontal>
      </Layout.Vertical>
    </Card>
  )
}
 
export default UsageInfoCard