All files / modules/33-auth-settings/pages/AccountOverview/views SubscribedModules.tsx

81.63% Statements 40/49
55.56% Branches 25/45
83.33% Functions 5/6
81.63% Lines 40/49

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              2x 2x 2x 2x   2x 2x 2x 2x 2x 2x   2x   2x 2x               2x               2x 1x 1x 1x 1x 1x 1x       1x               1x                                                                     2x 3x 3x 3x     2x               2x                                   3x           3x       3x                   3x     3x   2x 2x 2x 1x                             3x                   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 from 'react'
import { capitalize } from 'lodash-es'
import { Container, Text, Card, Layout, Icon, PageError, PageSpinner } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import type { IconName } from '@wings-software/uicore'
import moment from 'moment'
import { useParams, Link } from 'react-router-dom'
import { useFeatureFlags } from '@common/hooks/useFeatureFlag'
import { ModuleName } from 'framework/types/ModuleName'
import { useStrings } from 'framework/strings'
import routes from '@common/RouteDefinitions'
import type { AccountPathProps, SubscriptionQueryParams } from '@common/interfaces/RouteInterfaces'
import { useGetAccountLicenses } from 'services/cd-ng'
import type { ModuleLicenseDTO } from 'services/cd-ng'
import { Editions } from '@common/constants/SubscriptionTypes'
import css from '../AccountOverview.module.scss'
 
interface ModuleCardProps {
  module: ModuleLicenseDTO
}
 
const MODULE_ICONS: {
  [key in ModuleLicenseDTO['moduleType'] as string]: string
} = {
  CD: 'cd-with-dark-text',
  CE: 'ccm-with-dark-text',
  CV: 'srm-with-dark-text',
  CF: 'ff-with-dark-text',
  CI: 'ci-with-dark-text'
}
 
const ModuleCard: React.FC<ModuleCardProps> = ({ module }) => {
  const { getString } = useStrings()
  const { accountId } = useParams<AccountPathProps>()
  const getPlanDescription = (): string => {
    const days = Math.round(moment(module.expiryTime).diff(moment(module.createdAt), 'days', true)).toString()
    const edition = module.edition || ''
    Iif (edition === Editions.FREE || edition === Editions.COMMUNITY) {
      return capitalize(edition)
    }
 
    return capitalize(edition)
      .concat('(')
      .concat(days)
      .concat(' day ')
      .concat(capitalize(module.licenseType))
      .concat(')')
  }
 
  return (
    <Card className={css.subscribedModules}>
      <Container padding={'large'}>
        <Layout.Vertical>
          {module.moduleType && MODULE_ICONS[module.moduleType] && (
            <Icon name={MODULE_ICONS[module.moduleType] as IconName} className={css.moduleIcons} />
          )}
          <Layout.Horizontal padding="xsmall" margin={{ bottom: 'large' }} border={{ color: Color.GREY_200 }}>
            <Text font={{ size: 'xsmall' }} margin={{ right: 'xsmall' }}>{`${getString(
              'common.subscriptions.overview.plan'
            )}:`}</Text>
            <Text font={{ size: 'xsmall', weight: 'bold' }} color={Color.BLACK}>
              {getPlanDescription()}
            </Text>
          </Layout.Horizontal>
        </Layout.Vertical>
      </Container>
      <Container
        border={{ top: true, color: Color.GREY_250 }}
        padding={{ top: 'large', bottom: 'large', left: 'large' }}
      >
        <Link
          to={routes.toSubscriptions({
            accountId,
            moduleCard: module.moduleType as SubscriptionQueryParams['moduleCard']
          })}
          className={css.manageBtn}
        >
          {getString('common.manage')}
        </Link>
      </Container>
    </Card>
  )
}
 
const SubscribedModules: React.FC = () => {
  const { getString } = useStrings()
  const { accountId } = useParams<AccountPathProps>()
  const { CDNG_ENABLED, CVNG_ENABLED, CING_ENABLED, CENG_ENABLED, CFNG_ENABLED } = useFeatureFlags()
 
  function isModuleEnabled(moduleType: ModuleLicenseDTO['moduleType']): boolean | undefined {
    switch (moduleType) {
      case ModuleName.CD: {
        return CDNG_ENABLED
      }
      case ModuleName.CE: {
        return CENG_ENABLED
      }
      case ModuleName.CI: {
        return CING_ENABLED
      }
      case ModuleName.CF: {
        return CFNG_ENABLED
      }
      case ModuleName.CV: {
        return CVNG_ENABLED
      }
      default:
        return undefined
    }
  }
 
  const {
    data: accountLicenses,
    loading,
    error,
    refetch
  } = useGetAccountLicenses({
    queryParams: {
      accountIdentifier: accountId
    }
  })
 
  Iif (loading) {
    return <PageSpinner />
  }
 
  Iif (error) {
    return (
      <Container height={300}>
        <PageError message={(error.data as Error)?.message || error.message} onClick={() => refetch()} />
      </Container>
    )
  }
 
  const modules: {
    [key: string]: ModuleLicenseDTO[]
  } = accountLicenses?.data?.allModuleLicenses || {}
 
  const subscribedModules =
    Object.values(modules).length > 0 ? (
      Object.values(modules).map(moduleLicenses => {
        Eif (moduleLicenses?.length > 0) {
          const latestModuleLicense = moduleLicenses[moduleLicenses.length - 1]
          if (isModuleEnabled(latestModuleLicense.moduleType)) {
            return (
              <div key={latestModuleLicense.moduleType}>
                <ModuleCard module={latestModuleLicense} />
              </div>
            )
          }
        }
      })
    ) : (
      <Layout.Horizontal spacing="xsmall">
        <Link to={routes.toSubscriptions({ accountId })}>{getString('common.account.visitSubscriptions.link')}</Link>
        <Text>{getString('common.account.visitSubscriptions.description')}</Text>
      </Layout.Horizontal>
    )
 
  return (
    <Container margin="xlarge" padding="xlarge" className={css.container} background="white">
      <Text color={Color.BLACK} font={{ weight: 'semi-bold', size: 'medium' }} margin={{ bottom: 'xlarge' }}>
        {getString('common.account.subscribedModules')}
      </Text>
      <Layout.Horizontal spacing="large">{subscribedModules}</Layout.Horizontal>
    </Container>
  )
}
 
export default SubscribedModules