All files / modules/10-common/components/FeatureWarning FeatureWarningWithTooltip.tsx

100% Statements 31/31
90% Branches 9/10
100% Functions 4/4
100% Lines 28/28

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              584x 584x 584x 584x 584x 584x 584x   584x   584x 584x                                         584x 2x 2x                             584x 33x 33x 33x 33x 33x 33x 33x         33x 27x           6x             33x                                     584x 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 React, { ReactElement } from 'react'
import { ButtonSize, Layout, Text, Popover } from '@harness/uicore'
import { PopoverInteractionKind } from '@blueprintjs/core'
import { FontVariation, Color } from '@harness/design-system'
import { capitalize } from 'lodash-es'
import { useStrings } from 'framework/strings'
import { FeatureDescriptor, customFeatureDescriptor } from 'framework/featureStore/FeatureDescriptor'
import type { Module } from '@common/interfaces/RouteInterfaces'
import { useFeatureRequiredPlans } from '@common/hooks/useFeatures'
import type { FeatureIdentifier } from 'framework/featureStore/FeatureIdentifier'
import { ExplorePlansBtn } from './featureWarningUtil'
import css from './FeatureWarning.module.scss'
 
interface FeatureWarningTooltipProps {
  featureName: FeatureIdentifier
  warningMessage?: string
}
export interface FeatureWarningProps {
  featureName: FeatureIdentifier
  warningMessage?: string
  className?: string
}
 
export interface ExplorePlansBtnProps {
  module?: Module
  size?: ButtonSize
}
 
interface WarningTextProps {
  tooltip?: ReactElement
}
 
export const WarningText = ({ tooltip }: WarningTextProps): ReactElement => {
  const { getString } = useStrings()
  return (
    <Popover interactionKind={PopoverInteractionKind.HOVER} content={tooltip} position={'bottom'}>
      <Text
        className={css.warning}
        icon="flash"
        color={Color.ORANGE_800}
        font={{ variation: FontVariation.SMALL, weight: 'bold' }}
        iconProps={{ color: Color.ORANGE_800, padding: { right: 'small' }, size: 25 }}
      >
        {getString('common.feature.upgradeRequired.title').toUpperCase()}
      </Text>
    </Popover>
  )
}
 
export const FeatureWarningTooltip = ({ featureName, warningMessage }: FeatureWarningTooltipProps): ReactElement => {
  const { getString } = useStrings()
  const featureNameStr = capitalize(featureName.split('_').join(' '))
  const featureDescription = FeatureDescriptor[featureName] ? FeatureDescriptor[featureName] : featureNameStr
  const customFeatureDescription = customFeatureDescriptor(featureName, getString)
  const requiredPlans = useFeatureRequiredPlans(featureName)
  const requiredPlansStr = requiredPlans.length > 0 ? requiredPlans.join(' or ') : 'upgrade'
  const upgradeDescription = getString('common.feature.levelUp.planMessage', {
    plan: requiredPlansStr
  })
 
  function getBody(): React.ReactNode {
    if (warningMessage || customFeatureDescription) {
      return (
        <Text font={{ size: 'small' }} color={Color.WHITE}>
          {warningMessage || customFeatureDescription}
        </Text>
      )
    }
    return (
      <Text font={{ size: 'small' }} color={Color.WHITE}>
        {upgradeDescription} {featureDescription}
      </Text>
    )
  }
 
  return (
    <Layout.Vertical padding="medium" className={css.tooltip}>
      <Text
        icon="flash"
        color={Color.ORANGE_800}
        font={{ variation: FontVariation.FORM_MESSAGE_WARNING, weight: 'bold' }}
        iconProps={{ color: Color.ORANGE_800, size: 25 }}
        padding={{ bottom: 'xsmall' }}
      >
        {getString('common.levelUp')}
      </Text>
      <Layout.Vertical spacing="small">
        {getBody()}
        <ExplorePlansBtn featureName={featureName} className={css.btn} />
      </Layout.Vertical>
    </Layout.Vertical>
  )
}
 
export const FeatureWarningWithTooltip = ({ featureName, warningMessage }: FeatureWarningProps): ReactElement => {
  const tooltip = <FeatureWarningTooltip featureName={featureName} warningMessage={warningMessage} />
  return <WarningText tooltip={tooltip} />
}