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

77.38% Statements 65/84
68% Branches 17/25
57.89% Functions 11/19
75% Lines 57/76

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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228              589x 589x 589x 589x 589x 589x 589x 589x 589x       589x 589x                                 589x 49x         7x   42x         42x 42x 31x   11x     11x     11x     11x         49x 49x 49x           49x     589x 40x 40x 40x 40x 40x           2x 2x 2x                     9x 9x 9x 9x             589x 6x 6x 6x                           589x                               589x 1x 1x                                 589x                               589x 3x 3x 3x                         589x 1x   1x                             589x 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 { capitalize } from 'lodash-es'
import { useHistory, useParams } from 'react-router-dom'
import { Button, ButtonSize, ButtonVariation, Text, Layout } from '@harness/uicore'
import { FontVariation, Color } from '@harness/design-system'
import { useLicenseStore } from 'framework/LicenseStore/LicenseStoreContext'
import { LICENSE_STATE_VALUES } from 'framework/LicenseStore/licenseStoreUtil'
import routes from '@common/RouteDefinitions'
import { useFeatureModule } from '@common/hooks/useFeatures'
import type { ModuleType } from 'framework/featureStore/featureStoreUtil'
import type { FeatureIdentifier } from 'framework/featureStore/FeatureIdentifier'
import type { AccountPathProps, Module } from '@common/interfaces/RouteInterfaces'
import { useStrings } from 'framework/strings'
import css from './FeatureWarning.module.scss'
 
export interface LicenseStatusProps {
  [key: string]: LICENSE_STATE_VALUES
}
 
export interface FeatureInfoBannerProps {
  featureName: FeatureIdentifier
  message: string
}
 
export interface ExplorePlansBtnProps {
  featureName?: FeatureIdentifier
  size?: ButtonSize
  className?: string
}
 
export function getPlanModule(licenseStatus: LicenseStatusProps, moduleType?: ModuleType): Module | undefined {
  switch (moduleType) {
    case 'CD':
    case 'CI':
    case 'CF':
    case 'CE':
      return moduleType.toLowerCase() as Module
    default:
      return getDefaultPlanModule(licenseStatus)
  }
}
 
function getDefaultPlanModule(licenseStatus: LicenseStatusProps): Module | undefined {
  const { CD_LICENSE_STATE, CI_LICENSE_STATE, FF_LICENSE_STATE, CCM_LICENSE_STATE } = licenseStatus
  if (CD_LICENSE_STATE === LICENSE_STATE_VALUES.ACTIVE) {
    return 'cd'
  }
  Iif (CI_LICENSE_STATE === LICENSE_STATE_VALUES.ACTIVE) {
    return 'ci'
  }
  Iif (FF_LICENSE_STATE === LICENSE_STATE_VALUES.ACTIVE) {
    return 'cf'
  }
  Iif (CCM_LICENSE_STATE === LICENSE_STATE_VALUES.ACTIVE) {
    return 'ce'
  }
  return undefined
}
 
// this is to find the plan page that a feature goes to
function useGetFeaturePlanModule(featureName?: FeatureIdentifier): Module | undefined {
  const moduleType = useFeatureModule(featureName)
  const { CD_LICENSE_STATE, CI_LICENSE_STATE, FF_LICENSE_STATE, CCM_LICENSE_STATE } = useLicenseStore()
  const licenseStatus = {
    CD_LICENSE_STATE,
    CI_LICENSE_STATE,
    FF_LICENSE_STATE,
    CCM_LICENSE_STATE
  }
  return getPlanModule(licenseStatus, moduleType)
}
 
export const ExplorePlansBtn = ({ featureName, size, className }: ExplorePlansBtnProps): ReactElement => {
  const { getString } = useStrings()
  const history = useHistory()
  const { accountId } = useParams<AccountPathProps>()
  const planModule = useGetFeaturePlanModule(featureName)
  return (
    <Button
      className={className}
      variation={ButtonVariation.PRIMARY}
      size={size || ButtonSize.SMALL}
      onClick={e => {
        e.preventDefault()
        e.stopPropagation()
        history.push(routes.toSubscriptions({ accountId, moduleCard: planModule, tab: 'PLANS' }))
      }}
    >
      {getString('common.explorePlans')}
    </Button>
  )
}
 
function useRedirectToOverviewPage(
  featureName?: FeatureIdentifier
): (e: React.MouseEvent<Element, MouseEvent>) => void {
  const history = useHistory()
  const { accountId } = useParams<AccountPathProps>()
  const planModule = useGetFeaturePlanModule(featureName)
  return (e: React.MouseEvent<Element, MouseEvent>) => {
    e.preventDefault()
    e.stopPropagation()
    history.push(routes.toSubscriptions({ accountId, moduleCard: planModule, tab: 'OVERVIEW' }))
  }
}
 
export const ViewUsageLink = ({ size, featureName }: ExplorePlansBtnProps): ReactElement => {
  const { getString } = useStrings()
  const redirectToOverviewPage = useRedirectToOverviewPage(featureName)
  return (
    <Button
      data-name="view-usage-link"
      variation={ButtonVariation.LINK}
      size={size || ButtonSize.SMALL}
      onClick={(e: React.MouseEvent<Element, MouseEvent>) => {
        redirectToOverviewPage(e)
      }}
    >
      {capitalize(getString('common.viewUsage'))}
    </Button>
  )
}
 
export const ViewUsageBtn = ({ featureName, size }: ExplorePlansBtnProps): ReactElement => {
  const { getString } = useStrings()
  const redirectToOverviewPage = useRedirectToOverviewPage(featureName)
  return (
    <Button
      variation={ButtonVariation.SECONDARY}
      size={size || ButtonSize.SMALL}
      onClick={(e: React.MouseEvent<Element, MouseEvent>) => {
        redirectToOverviewPage(e)
      }}
    >
      {capitalize(getString('common.viewUsage'))}
    </Button>
  )
}
 
export const UpgradeRequiredText = ({ message }: { message: string }): ReactElement => {
  const { getString } = useStrings()
  return (
    <Layout.Horizontal spacing="small" flex={{ alignItems: 'center' }}>
      <Text
        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>
      <Text color={Color.PRIMARY_10} font={{ variation: FontVariation.SMALL }}>
        {message}
      </Text>
    </Layout.Horizontal>
  )
}
 
export const RequestUpgradeBtn = ({ featureName, size }: ExplorePlansBtnProps): ReactElement => {
  const { getString } = useStrings()
  const redirectToOverviewPage = useRedirectToOverviewPage(featureName)
  return (
    <Button
      variation={ButtonVariation.SECONDARY}
      size={size || ButtonSize.SMALL}
      onClick={(e: React.MouseEvent<Element, MouseEvent>) => {
        redirectToOverviewPage(e)
      }}
    >
      {getString('common.feature.keepGettingShipDone.requestUpgrade')}
    </Button>
  )
}
 
export const ManageSubscriptionBtn = ({ featureName, size }: ExplorePlansBtnProps): ReactElement => {
  const { getString } = useStrings()
  const redirectToOverviewPage = useRedirectToOverviewPage(featureName)
  return (
    <Button
      variation={ButtonVariation.SECONDARY}
      size={size || ButtonSize.SMALL}
      onClick={(e: React.MouseEvent<Element, MouseEvent>) => {
        redirectToOverviewPage(e)
      }}
    >
      {getString('common.manageSubscription')}
    </Button>
  )
}
 
export const ExploreSaasPlansBtn = ({ size }: { size?: ButtonSize }): ReactElement => {
  const { getString } = useStrings()
 
  return (
    <Button
      variation={ButtonVariation.SECONDARY}
      size={size || ButtonSize.SMALL}
      onClick={e => {
        e.preventDefault()
        e.stopPropagation()
        window.open('https://harness.io/pricing/', '_blank')
      }}
    >
      {getString('common.exploreSaaSPlans')}
    </Button>
  )
}
 
export const WarningInfo = ({ message }: { message: string }): ReactElement => {
  return (
    <Text
      icon="info-message"
      color={Color.PRIMARY_10}
      font={{ variation: FontVariation.SMALL }}
      iconProps={{ padding: { right: 'small' }, size: 20, className: css.infoIcon }}
    >
      {message}
    </Text>
  )
}