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

100% Statements 21/21
83.33% Branches 5/6
100% Functions 3/3
100% Lines 21/21

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              1x 1x 1x 1x 1x 1x 1x                 1x             1x       8x 8x 8x 8x 8x 8x 8x 1x     7x                                             11x     1x         1x  
/*
 * 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, { ReactElement } from 'react'
import cx from 'classnames'
import { Layout, Text, Container, Button } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import { useStrings } from 'framework/strings'
import { useLocalStorage } from '@common/hooks'
import {
  DisplayBanner,
  CustomBannerProps,
  getBackgroundColor,
  getIconDetails,
  getHeadline,
  renderRedirectButton
} from './FeatureWarningCommonBannerUtils'
 
import css from './FeatureWarningCommonBanner.module.scss'
 
interface FeatureWarningCommonBannerProps {
  banner: DisplayBanner
  customBannerProps?: CustomBannerProps
}
 
const FeatureWarningCommonBanner = ({
  banner,
  customBannerProps = {}
}: FeatureWarningCommonBannerProps): ReactElement | null => {
  const { getString } = useStrings()
  const { className, iconProps, headline } = customBannerProps
  const { theme, redirectButtons = [] } = banner
  const headlineText = getHeadline({ theme, headline, getString })
  const { iconName, iconSize } = getIconDetails({ theme, iconProps })
  const [dismissedBanners, setDismissedBanners] = useLocalStorage<string[]>('dismiss_banners', [])
  if (dismissedBanners.includes(banner.bannerKey)) {
    return null
  }
 
  return (
    <Layout.Horizontal
      key={banner.messageString}
      className={cx(css.bannerContainer, getBackgroundColor({ theme, className }))}
      flex={{ alignItems: 'center', justifyContent: 'space-between' }}
      height={56}
      padding={{ left: 'large', top: 'medium', bottom: 'medium' }}
    >
      <Container flex>
        <Text
          icon={iconName}
          iconProps={{
            size: iconSize,
            margin: { right: 'xsmall' }
          }}
          font={{ weight: 'semi-bold', size: 'small' }}
          color={Color.PRIMARY_10}
          margin={{ right: 'medium' }}
        >
          {headlineText && <Text className={css.headline}>{headlineText}</Text>}
          {banner.messageString}
        </Text>
        <Container className={css.redirectButtons}>
          {redirectButtons.map(type => renderRedirectButton({ type, featureName: banner.featureName }))}
        </Container>
      </Container>
      <Button icon="cross" minimal onClick={() => setDismissedBanners([...dismissedBanners, banner.bannerKey])} />
    </Layout.Horizontal>
  )
}
 
export default FeatureWarningCommonBanner