All files / modules/10-common/layouts FeatureBanner.tsx

87.63% Statements 85/97
54.23% Branches 77/142
70.59% Functions 12/17
86.21% Lines 75/87

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              13x 13x 13x 13x   13x     13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x               13x 13x   13x   13x 18x 18x     13x         13x   13x 15x 15x               13x                 13x         13x 2x 2x     13x         13x 2x 2x     13x                     7x   5x   1x   1x                               7x                   7x   5x   1x   1x           7x               13x 16x 16x   16x 16x 16x         16x   16x 16x   16x 16x 16x 16x 16x 5x           16x   16x 7x 7x 7x         16x   16x   16x   16x               9x     7x                 2x     2x            
/*
 * 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, { useMemo } from 'react'
import { Button, ButtonVariation, ButtonSize, Layout } from '@harness/uicore'
import { defaultTo } from 'lodash-es'
import cx from 'classnames'
 
import featuresFactory from 'framework/featureStore/FeaturesFactory'
import type { FeatureProps } from 'framework/featureStore/FeaturesFactory'
import type { CheckFeatureReturn } from 'framework/featureStore/featureStoreUtil'
import { isEnterprisePlan, isFreePlan, isTeamPlan, useLicenseStore } from 'framework/LicenseStore/LicenseStoreContext'
import { Module, ModuleName, moduleToModuleNameMapping } from 'framework/types/ModuleName'
import { useFeatures } from '@common/hooks/useFeatures'
import { useLocalStorage } from '@common/hooks/useLocalStorage'
import { useModuleInfo } from '@common/hooks/useModuleInfo'
import { useFeatureFlags } from '@common/hooks/useFeatureFlag'
import { useStrings } from 'framework/strings'
import { useGetUsageAndLimit } from '@common/hooks/useGetUsageAndLimit'
import { useTelemetry } from '@common/hooks/useTelemetry'
import { Category, FeatureActions } from '@common/constants/TrackingConstants'
import {
  ViewUsageLink,
  ExplorePlansBtn,
  ManageSubscriptionBtn,
  InfoText,
  LevelUpText,
  OverUseInfoText
} from './FeatureUtils'
import { BannerType } from './Constants'
import css from './layouts.module.scss'
 
export const BANNER_KEY = 'feature_banner_dismissed'
 
export const isFeatureLimitBreached = (feature?: CheckFeatureReturn) => {
  const featureDetail = feature?.featureDetail
  return featureDetail?.limit && featureDetail.count && featureDetail.count === featureDetail.limit
}
 
export const isFeatureLimitBreachedIncludesExceeding = (feature?: CheckFeatureReturn): boolean => {
  const featureDetail = feature?.featureDetail
  return !!(featureDetail?.limit && featureDetail.count && featureDetail.count >= featureDetail.limit)
}
 
export const FEATURE_USAGE_WARNING_LIMIT = 90
 
export const isFeatureWarningActive = (feature?: CheckFeatureReturn) => {
  const featureDetail = feature?.featureDetail
  return (
    featureDetail?.limit &&
    featureDetail.count &&
    featureDetail.count > (featureDetail.limit * FEATURE_USAGE_WARNING_LIMIT) / 100 &&
    featureDetail.count < featureDetail.limit
  )
}
 
export const isFeatureWarningActiveIncludesLimit = (feature?: CheckFeatureReturn): boolean => {
  const featureDetail = feature?.featureDetail
  return !!(
    featureDetail?.limit &&
    featureDetail.count &&
    featureDetail.count > (featureDetail.limit * FEATURE_USAGE_WARNING_LIMIT) / 100
  )
}
 
export const isFeatureCountActive = (feature?: CheckFeatureReturn) => {
  const featureDetail = feature?.featureDetail
  return featureDetail?.limit && typeof featureDetail.count !== 'undefined'
}
 
export const isFeatureOveruseActive = (feature?: CheckFeatureReturn) => {
  const featureDetail = feature?.featureDetail
  return featureDetail?.limit && featureDetail.count && featureDetail.count > featureDetail.limit
}
 
export const isFeatureLimitMet = (feature?: CheckFeatureReturn) => {
  const featureDetail = feature?.featureDetail
  return featureDetail?.limit && featureDetail.count && featureDetail.count >= featureDetail.limit
}
 
export const getActiveUsageNumber = (feature?: CheckFeatureReturn) => {
  const featureDetail = feature?.featureDetail
  return featureDetail?.limit && featureDetail.count && Math.floor((featureDetail.count * 100) / featureDetail.limit)
}
 
export const getPercentageNumber = (feature?: CheckFeatureReturn) => {
  const featureDetail = feature?.featureDetail
 
  return (
    featureDetail?.limit &&
    featureDetail.count &&
    Math.min(Math.floor((featureDetail.count / featureDetail.limit) * 100), 100)
  )
}
 
function getBannerClassNameByType(type: BannerType): string {
  switch (type) {
    case BannerType.INFO:
      return css.info
    case BannerType.LEVEL_UP:
      return css.levelUp
    case BannerType.OVERUSE:
      return css.overUse
    default:
      return ''
  }
}
function getBannerBodyByType({
  type,
  message,
  module,
  isFreeEdition
}: {
  type: BannerType
  message: React.ReactNode
  module: Module
  isFreeEdition: boolean
}): React.ReactElement {
  const buttons = isFreeEdition ? (
    <>
      <ViewUsageLink module={module} />
      <ExplorePlansBtn module={module} />
    </>
  ) : (
    <ManageSubscriptionBtn module={module} />
  )
 
  function getText(): React.ReactElement {
    switch (type) {
      case BannerType.INFO:
        return <InfoText message={message} />
      case BannerType.LEVEL_UP:
        return <LevelUpText message={message} />
      case BannerType.OVERUSE:
        return <OverUseInfoText message={message} />
      default:
        return <></>
    }
  }
 
  return (
    <Layout.Horizontal width="95%" padding={{ left: 'large' }} style={{ minWidth: '800px' }}>
      {getText()}
      {buttons}
    </Layout.Horizontal>
  )
}
 
export default function FeatureBanner(): React.ReactElement | null {
  const { module } = useModuleInfo()
  const { getString } = useStrings()
 
  const { FEATURE_ENFORCEMENT_ENABLED: isFeatureEnforceEnabled, FREE_PLAN_ENFORCEMENT_ENABLED } = useFeatureFlags()
  const [activeModuleFeatures, setActiveModuleFeatures] = React.useState<FeatureProps | null>(null)
  const [isBannerDismissed, setIsBannerDismissed] = useLocalStorage<Partial<Record<Module, boolean>>>(
    BANNER_KEY,
    {},
    window.sessionStorage
  )
  const features = useFeatures({ featuresRequest: { featureNames: defaultTo(activeModuleFeatures?.features, []) } })
 
  const moduleName: ModuleName = module ? moduleToModuleNameMapping[module] : ModuleName.COMMON
  const usageAndLimitInfo = useGetUsageAndLimit(moduleName)
 
  const { licenseInformation } = useLicenseStore()
  const isFreeEdition = isFreePlan(licenseInformation, moduleName)
  const isTeamEdition = isTeamPlan(licenseInformation, moduleName)
  const isEnterpriseEdition = isEnterprisePlan(licenseInformation, moduleName)
  const additionalLicenseProps = useMemo(() => {
    return {
      isFreeEdition,
      isTeamEdition,
      isEnterpriseEdition
    }
  }, [isFreeEdition, isTeamEdition, isEnterpriseEdition])
  const shouldDisplayForFreePlanOnly = FREE_PLAN_ENFORCEMENT_ENABLED && (isTeamEdition || isEnterpriseEdition)
 
  React.useEffect(() => {
    Eif (module) {
      const moduleFeatures = featuresFactory.getFeaturesByModule(module)
      setActiveModuleFeatures(moduleFeatures || null)
    }
  }, [module])
 
  const { message: messageFn, bannerType } =
    activeModuleFeatures?.renderMessage(features, getString, additionalLicenseProps, usageAndLimitInfo) || {}
 
  const message = messageFn?.()
 
  const { trackEvent } = useTelemetry()
 
  if (
    !isFeatureEnforceEnabled ||
    shouldDisplayForFreePlanOnly ||
    !message ||
    !bannerType ||
    !module ||
    isBannerDismissed[module]
  ) {
    return null
  }
 
  return (
    <div className={cx(css.featuresBanner, getBannerClassNameByType(bannerType))}>
      {getBannerBodyByType({ type: bannerType, message, module, isFreeEdition })}
      <Button
        variation={ButtonVariation.ICON}
        size={ButtonSize.LARGE}
        icon="cross"
        data-testid="feature-banner-dismiss"
        onClick={() => {
          trackEvent(FeatureActions.DismissFeatureBanner, {
            category: Category.ENFORCEMENT
          })
          setIsBannerDismissed(prev => (module ? { ...prev, [module]: true } : prev))
        }}
      />
    </div>
  )
}