All files / modules/75-cf/components/EditFlagTabs DefaultRulesView.tsx

100% Statements 18/18
100% Branches 6/6
100% Functions 4/4
100% Lines 14/14

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              4x 4x   4x 4x 4x   4x 4x     4x               4x 18x   39x           33x 26x   18x                                                                                
/*
 * 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 { FormInput, Heading, Layout, SelectOption, Text } from '@wings-software/uicore'
import type { FormikProps } from 'formik'
import { FontVariation } from '@harness/design-system'
import StringWithTooltip from '@common/components/StringWithTooltip/StringWithTooltip'
import { useStrings } from 'framework/strings'
import type { Variation } from 'services/cf'
import { CFVariationColors } from '@cf/constants'
import { VariationWithIcon } from '../VariationWithIcon/VariationWithIcon'
import type { FlagActivationFormValues } from '../FlagActivation/FlagActivation'
 
import css from './DefaultRulesView.module.scss'
 
export interface DefaultRulesViewProps {
  editing: boolean
  variations: Variation[]
  formikProps: FormikProps<FlagActivationFormValues>
}
 
export const DefaultRulesView: React.FC<DefaultRulesViewProps> = ({ editing, variations, formikProps }) => {
  const { getString } = useStrings()
 
  const variationItems = variations.map<SelectOption>((elem, index) => ({
    label: elem.name || elem.identifier,
    value: elem.identifier,
    icon: { name: 'full-circle', style: { color: CFVariationColors[index] } }
  }))
 
  const onVariationIndex = variations.findIndex(variation => formikProps.values.onVariation === variation.identifier)
  const offVariationIndex = variations.findIndex(variation => formikProps.values.offVariation === variation.identifier)
 
  return (
    <>
      <Heading level={3} font={{ variation: FontVariation.H6 }}>
        <StringWithTooltip stringId="cf.featureFlags.rules.defaultRules" tooltipId="ff_ffDefaultRules_heading" />
      </Heading>
 
      <div className={css.rules}>
        <Text>
          <span dangerouslySetInnerHTML={{ __html: getString('cf.featureFlags.ifFlagOnServe') }} />
        </Text>
        {editing ? (
          <FormInput.Select inline name="onVariation" items={variationItems} />
        ) : (
          <Layout.Horizontal spacing="xsmall" flex={{ alignItems: 'center', justifyContent: 'flex-start' }}>
            <VariationWithIcon
              variation={variations[onVariationIndex]}
              index={onVariationIndex}
              textStyle={{ fontWeight: 600 }}
            />
          </Layout.Horizontal>
        )}
 
        <Text>
          <span dangerouslySetInnerHTML={{ __html: getString('cf.featureFlags.ifFlagOffServe') }} />
        </Text>
        {editing ? (
          <FormInput.Select inline name="offVariation" items={variationItems} onChange={formikProps.handleChange} />
        ) : (
          <Layout.Horizontal spacing="xsmall" flex={{ alignItems: 'center', justifyContent: 'flex-start' }}>
            <VariationWithIcon
              variation={variations[offVariationIndex]}
              index={offVariationIndex}
              textStyle={{ fontWeight: 600 }}
            />
          </Layout.Horizontal>
        )}
      </div>
    </>
  )
}