All files / modules/75-cf/pages/feature-flags FlagResult.tsx

59.38% Statements 19/32
29.09% Branches 16/55
12.5% Functions 1/8
66.67% Lines 18/27

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              3x 3x 3x 3x 3x   3x   3x 3x 3x           3x 52x 52x 52x 52x 52x 52x   52x                                                                                                                                         3x                                                                                                                                                            
/*
 * 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 from 'react'
import { Classes } from '@blueprintjs/core'
import cx from 'classnames'
import { Container, Heading, Layout, Text, Utils } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import type { ContainerProps } from '@wings-software/uicore/dist/components/Container/Container'
import { useStrings } from 'framework/strings'
import type { Feature } from 'services/cf'
import { VariationWithIcon } from '@cf/components/VariationWithIcon/VariationWithIcon'
import { formatNumber } from '@cf/utils/CFUtils'
import css from './FeatureFlagsPage.module.scss'
 
export interface FlagResultProps extends ContainerProps {
  feature: Feature
}
 
export const FlagResult: React.FC<FlagResultProps> = ({ feature, style, ...props }) => {
  const { getString } = useStrings()
  const results = feature.results
  const metricsCount = results?.map(({ count }) => count).reduce((sum = 0, count = 0) => sum + count, 0) || 0
  const singleVariationDistribution = results?.length === 1
  const tooltip = metricsCount ? <FlagResultTooltip feature={feature} /> : undefined
  const hasMoreThanTwoResults = (results?.length as number) > 2
 
  return (
    <Container style={{ display: 'inline-block', ...style }} {...props}>
      <Text tooltip={tooltip} tooltipProps={{ isDark: true }} style={{ display: 'flex', flexDirection: 'column' }}>
        <span
          style={{
            width: 94,
            height: 12,
            display: 'inline-block',
            backgroundColor: !metricsCount
              ? 'rgb(237 237 246)'
              : singleVariationDistribution
              ? '#6739B7'
              : 'transparent',
            borderRadius: '3px'
          }}
        >
          {metricsCount > 0 && !singleVariationDistribution && (
            <>
              <span
                style={{
                  width: hasMoreThanTwoResults ? 54 : 71,
                  height: 12,
                  display: 'inline-block',
                  backgroundColor: '#6739B7',
                  borderRadius: '3px'
                }}
              />
              <span
                style={{
                  width: 21,
                  height: 12,
                  display: 'inline-block',
                  backgroundColor: '#C239F2',
                  borderRadius: '3px',
                  margin: '0 1px'
                }}
              />
              {hasMoreThanTwoResults && (
                <span
                  style={{
                    width: 17,
                    height: 12,
                    display: 'inline-block',
                    backgroundColor: '#D1C3E9',
                    borderRadius: '3px'
                  }}
                />
              )}
            </>
          )}
        </span>
        <Text
          style={{
            fontSize: '10px',
            fontWeight: 400,
            color: metricsCount > 0 ? '#6B6D85' : '#9293abad',
            paddingTop: '8px',
            textAlign: 'center'
          }}
        >
          {getString(metricsCount > 0 ? 'cf.featureFlags.metrics.evaluations' : 'cf.featureFlags.metrics.noMetrics', {
            count: formatNumber(metricsCount)
          }).toLocaleUpperCase()}
        </Text>
      </Text>
    </Container>
  )
}
 
const FlagResultTooltip: React.FC<FlagResultProps> = ({ feature }) => {
  const { getString } = useStrings()
  const results = feature.results
  const len = results?.length || 1
  const metricsCount = results?.map(({ count }) => count).reduce((sum = 0, count = 0) => sum + count, 0) || 0
  const height = len > 2 ? 204 : 126 + len * 26
 
  return (
    <Container padding="xlarge" style={{ overflow: 'auto', width: 325, height }} onClick={Utils.stopEvent}>
      <Heading
        level={3}
        color={Color.WHITE}
        style={{ fontSize: '14px', fontWeight: 600 }}
        margin={{ bottom: 'medium' }}
      >
        {getString('cf.featureFlags.metrics.evaluationStatistics')}
        <Text
          style={{ fontSize: '11px', opacity: 0.5, fontWeight: 600 }}
          inline
          margin={{ left: 'xsmall' }}
          color={Color.WHITE}
        >
          ({new Intl.NumberFormat().format(metricsCount)})
        </Text>
      </Heading>
      <Container>
        <table className={cx(Classes.HTML_TABLE, Classes.HTML_TABLE_CONDENSED, Classes.SMALL, css.tooltipTable)}>
          <thead>
            <tr>
              <th>
                <Text color={Color.WHITE} style={{ fontSize: '8px', fontWeight: 600, opacity: 0.7 }}>
                  {getString('cf.shared.variation').toLocaleUpperCase()}
                </Text>
              </th>
              <th>
                <Text color={Color.WHITE} style={{ fontSize: '8px', fontWeight: 600, opacity: 0.7 }}>
                  {getString('cf.shared.evaluations').toLocaleUpperCase()}
                </Text>
              </th>
            </tr>
          </thead>
          <tbody>
            {results?.map(result => {
              const index = feature.variations.findIndex(
                variation => variation.identifier === result.variationIdentifier
              )
 
              return (
                <tr key={result.variationIdentifier}>
                  <td>
                    {index === -1 ? (
                      <Text color={Color.WHITE} style={{ fontSize: '11px' }}>
                        {result.variationName}
                      </Text>
                    ) : (
                      <Layout.Horizontal spacing="xsmall" style={{ alignItems: 'center' }}>
                        <VariationWithIcon
                          variation={feature.variations[index]}
                          index={index}
                          textStyle={{ color: Color.WHITE, fontSize: '11px' }}
                        />
                      </Layout.Horizontal>
                    )}
                  </td>
                  <td>
                    <Text color={Color.WHITE} style={{ fontSize: '11px' }}>
                      {new Intl.NumberFormat().format(result.count as number)}
                    </Text>
                  </td>
                </tr>
              )
            })}
          </tbody>
        </table>
      </Container>
    </Container>
  )
}