All files / modules/75-cf/components/PercentageRollout PercentageRollout.tsx

100% Statements 29/29
97.67% Branches 42/43
100% Functions 12/12
100% Lines 27/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              13x 13x 13x 13x   13x 13x 13x   13x                                   13x                         132x   132x 243x       132x 36x                   45x 30x 45x         132x   72x 183x   183x               132x 183x       132x                                                             307x   307x                                           13x  
/*
 * 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, { CSSProperties, FC, useMemo } from 'react'
import { Container, FormInput, Layout, SelectOption, Text } from '@wings-software/uicore'
import { FontVariation } from '@harness/design-system'
import { useStrings } from 'framework/strings'
import type { Segment, Variation } from 'services/cf'
import { CFVariationColors } from '@cf/constants'
import { FeatureFlagBucketBy } from '@cf/utils/CFUtils'
import DistributionBar, { DistributionSegment } from './DistributionBar'
 
import css from './PercentageRollout.module.scss'
 
export interface PercentageRolloutProps {
  prefix: (fieldName: string) => string
  variations: Variation[]
  fieldValues?: {
    variations: { variation: string; weight: string | number }[]
  }
  value?: SelectOption
  targetGroups?: Segment[]
  bucketByAttributes?: string[]
  hideOverError?: boolean
  hideTargetGroupDivider?: boolean
  addClearButton?: boolean
  distributionWidth?: string | number
  [propName: string]: unknown
}
 
const PercentageRollout: FC<PercentageRolloutProps> = ({
  prefix,
  variations,
  fieldValues,
  targetGroups,
  bucketByAttributes,
  value,
  distributionWidth = '100%',
  hideOverError = false,
  hideTargetGroupDivider = false,
  addClearButton = false,
  ...restProps
}) => {
  const { getString } = useStrings()
 
  const targetGroupItems = useMemo<SelectOption[]>(
    () => (targetGroups || []).map(({ name, identifier }) => ({ label: name, value: identifier })),
    [targetGroups]
  )
 
  const bucketByItems = useMemo<SelectOption[]>(
    () => [
      {
        label: getString('name'),
        value: FeatureFlagBucketBy.NAME
      },
      {
        label: getString('identifier'),
        value: FeatureFlagBucketBy.IDENTIFIER
      },
      ...(bucketByAttributes || [])
        .filter(attribute => attribute !== FeatureFlagBucketBy.NAME && attribute !== FeatureFlagBucketBy.IDENTIFIER)
        .sort((a, b) => a.localeCompare(b))
        .map(attribute => ({ label: attribute, value: attribute }))
    ],
    [bucketByAttributes]
  )
 
  const distributionSegments = useMemo<DistributionSegment[]>(
    () =>
      variations.map((variation, index) => {
        const weight = fieldValues?.variations?.[index]?.weight || 0
 
        return {
          variation,
          weight: typeof weight === 'number' ? weight : parseInt(weight)
        }
      }),
    [variations, fieldValues?.variations]
  )
 
  const total = useMemo<number>(
    () => distributionSegments.reduce<number>((totalWeight, { weight }) => totalWeight + weight, 0),
    [distributionSegments]
  )
 
  return (
    <Layout.Vertical spacing="large" {...restProps}>
      {!!targetGroups && (
        <div className={hideTargetGroupDivider ? '' : css.targetGroupContainer}>
          <FormInput.Select
            value={value ? value : undefined}
            className={css.targetGroup}
            name={prefix('clauses[0].values[0]')}
            items={targetGroupItems}
            label={getString('cf.percentageRollout.toTargetGroup')}
            addClearButton={addClearButton}
          />
        </div>
      )}
 
      <Container className={css.distribution} width={distributionWidth}>
        {!!bucketByAttributes && (
          <FormInput.Select
            className={css.bucketBy}
            inline
            name={prefix('bucketBy')}
            items={bucketByItems}
            label={getString('cf.percentageRollout.bucketBy')}
          />
        )}
 
        <DistributionBar distributionSegments={distributionSegments} />
 
        <div>{total}%</div>
 
        {variations.map((variation, index) => {
          const iconColor = { '--variationColor': CFVariationColors[index % CFVariationColors.length] } as CSSProperties
 
          return (
            <div className={css.variationRow} style={iconColor} key={variation.identifier}>
              <FormInput.Text
                inline
                name={prefix(`variations[${index}].weight`)}
                label={variation.name || variation.identifier}
                inputGroup={{ type: 'number', max: 100, min: 0 }}
              />
            </div>
          )
        })}
      </Container>
 
      {!hideOverError && total > 100 && (
        <Text font={{ variation: FontVariation.FORM_MESSAGE_DANGER }}>
          {getString('cf.percentageRollout.invalidTotalError')}
        </Text>
      )}
    </Layout.Vertical>
  )
}
 
export default PercentageRollout