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

79.37% Statements 50/63
50.65% Branches 39/77
100% Functions 7/7
77.59% Lines 45/58

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              1x       1x               1x   1x   1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x     1x                                             1x             7x       7x   1x   5x   1x                   1x             8x       8x   1x     6x   1x               1x                 8x       8x   1x   1x         6x       1x             11x   4x       3x   4x                     1x                 6x 6x    
/*
 * 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 type { IconName } from '@wings-software/uicore'
import type { UseStringsReturn } from 'framework/strings'
import type { FeatureIdentifier } from 'framework/featureStore/FeatureIdentifier'
import {
  ManageSubscriptionBtn,
  ExplorePlansBtn,
  RequestUpgradeBtn,
  ViewUsageLink,
  ViewUsageBtn,
  ExploreSaasPlansBtn
} from './featureWarningUtil'
import css from './FeatureWarningCommonBanner.module.scss'
 
const infoMessageIconName = 'info-message'
 
export enum FeatureWarningTheme {
  INFO = 'INFO', // white bg, info icon
  UPGRADE_REQUIRED = 'UPGRADE_REQUIRED', // orange bg, bolt icon, headline
  OVERUSE = 'OVERUSE', // yellow bg, warning icon, headline
  KEEP_GETTING_SHIP_DONE = 'KEEP_GETTING_SHIP_DONE', // blue bg, info icon, headline
  UPSELL = 'UPSELL', // white bg, stars icon
  CUSTOM = 'CUSTOM' // enter props in customBannerProps
}
 
export enum RedirectButton {
  VIEW_USAGE_LINK = 'VIEW_USAGE_LINK',
  VIEW_USAGE_BUTTON = 'VIEW_USAGE_BUTTON',
  EXPLORE_PLANS = 'EXPLORE_PLANS',
  EXPLORE_SAAS_PLANS = 'EXPLORE_SAAS_PLANS',
  MANAGE_SUBSCRIPTION = 'MANAGE_SUBSCRIPTION',
  REQUEST_UPGRADE = 'REQUEST_UPGRADE'
}
 
export const DEFAULT_ICON_SIZE = 20
 
export interface DisplayBanner {
  featureName: FeatureIdentifier
  bannerKey: string
  allowed?: boolean
  messageString: string
  theme: FeatureWarningTheme
  redirectButtons?: RedirectButton[]
  isFeatureRestrictionAllowedForModule?: boolean
}
 
interface IconProps {
  iconName?: IconName
  iconSize?: number
}
export interface CustomBannerProps {
  iconProps?: IconProps
  headline?: string
  buttons?: JSX.Element
  className?: string
}
 
export const getBackgroundColor = ({
  theme,
  className
}: {
  theme: FeatureWarningTheme
  className?: string
}): string => {
  Iif (className) {
    return className
  }
 
  switch (theme) {
    case FeatureWarningTheme.UPGRADE_REQUIRED:
      return css.upgradeRequiredBanner
    case FeatureWarningTheme.INFO:
      return css.infoBanner
    case FeatureWarningTheme.OVERUSE:
      return css.overuseBanner
    case FeatureWarningTheme.UPSELL:
      return css.upsellBanner
    case FeatureWarningTheme.KEEP_GETTING_SHIP_DONE:
      return css.keepGettingShipDoneBanner
    default:
      return ''
  }
}
 
export const getIconDetails = ({
  theme,
  iconProps
}: {
  theme: FeatureWarningTheme
  iconProps?: IconProps
}): { iconName: IconName; iconSize: number } => {
  Iif (iconProps && Object.keys(iconProps).length > 0) {
    return { iconName: iconProps.iconName || infoMessageIconName, iconSize: iconProps?.iconSize || DEFAULT_ICON_SIZE }
  }
 
  switch (theme) {
    case FeatureWarningTheme.UPGRADE_REQUIRED:
      return { iconName: 'upgrade-bolt', iconSize: iconProps?.iconSize || DEFAULT_ICON_SIZE }
    case FeatureWarningTheme.INFO:
    case FeatureWarningTheme.KEEP_GETTING_SHIP_DONE:
      return { iconName: infoMessageIconName, iconSize: iconProps?.iconSize || DEFAULT_ICON_SIZE }
    case FeatureWarningTheme.OVERUSE:
      return { iconName: 'warning-sign', iconSize: iconProps?.iconSize || DEFAULT_ICON_SIZE }
    case FeatureWarningTheme.UPSELL:
      return { iconName: 'stars', iconSize: iconProps?.iconSize || DEFAULT_ICON_SIZE }
    default:
      return { iconName: infoMessageIconName, iconSize: iconProps?.iconSize || DEFAULT_ICON_SIZE }
  }
}
 
export const getHeadline = ({
  theme,
  headline,
  getString
}: {
  theme: FeatureWarningTheme
  headline?: string
  getString: UseStringsReturn['getString']
}): string => {
  Iif (headline) {
    return headline
  }
 
  switch (theme) {
    case FeatureWarningTheme.UPGRADE_REQUIRED:
      return getString('common.levelUp').toUpperCase()
    case FeatureWarningTheme.OVERUSE:
      return getString('common.feature.overuse.title').toUpperCase()
 
    case FeatureWarningTheme.KEEP_GETTING_SHIP_DONE:
      return getString('common.feature.keepGettingShipDone.title').toUpperCase()
    default:
      return ''
  }
}
 
export const renderRedirectButton = ({
  type,
  featureName
}: {
  type: RedirectButton
  featureName: FeatureIdentifier
}): JSX.Element | null => {
  switch (type) {
    case RedirectButton.VIEW_USAGE_LINK:
      return <ViewUsageLink featureName={featureName} />
    case RedirectButton.VIEW_USAGE_BUTTON:
      return <ViewUsageBtn featureName={featureName} />
    case RedirectButton.MANAGE_SUBSCRIPTION:
      return <ManageSubscriptionBtn featureName={featureName} />
    case RedirectButton.EXPLORE_PLANS:
      return <ExplorePlansBtn featureName={featureName} />
    case RedirectButton.EXPLORE_SAAS_PLANS:
      return <ExploreSaasPlansBtn />
    case RedirectButton.REQUEST_UPGRADE:
      return <RequestUpgradeBtn featureName={featureName} />
 
    default:
      return null
  }
}
 
export const getDismissBannerKey = ({
  featureIdentifier,
  limit,
  limitPercent
}: {
  featureIdentifier: FeatureIdentifier
  limit?: number
  limitPercent?: number
}): string => {
  const thresholdIdentifier = typeof limit === 'number' ? limit : typeof limitPercent === 'number' ? limitPercent : 0
  return `${featureIdentifier}_${thresholdIdentifier}`
}