All files / modules/10-common/components/ResourceCardList ResourceCardList.tsx

90.91% Statements 30/33
75% Branches 27/36
80% Functions 4/5
90.91% Lines 30/33

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              10x 10x 10x 10x 10x 10x   10x 10x 10x 10x 10x 10x                             10x 23x 23x 23x 23x 23x 23x     23x 23x           1x 1x 1x                                                       23x                                                               23x     51x         1x 1x                                   10x  
/*
 * 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 { useHistory, useParams } from 'react-router-dom'
import cx from 'classnames'
import { Button, ButtonSize, Card, Icon, IconName, Layout, Text } from '@wings-software/uicore'
import { FontVariation, Color } from '@harness/design-system'
import { String, useStrings } from 'framework/strings'
import type { OrgPathProps } from '@common/interfaces/RouteInterfaces'
import routes from '@common/RouteDefinitions'
import { useFeatureFlag } from '@common/hooks/useFeatureFlag'
import { FeatureFlag } from '@common/featureFlags'
import useCreateSmtpModal from '@common/components/Smtp/useCreateSmtpModal'
import { useGetSmtpConfig } from 'services/cd-ng'
import css from './ResourceCardList.module.scss'
 
export interface ResourceOption {
  label: JSX.Element
  icon: IconName
  route?: string
  colorClass: string
  onClick?: () => void
  subLabel?: JSX.Element
  disabled?: boolean
}
interface ResourceCardListProps {
  items?: ResourceOption[]
}
 
const ResourceCardList: React.FC<ResourceCardListProps> = ({ items }) => {
  const { accountId, orgIdentifier } = useParams<OrgPathProps>()
  const history = useHistory()
  const { getString } = useStrings()
  const templatesEnabled: boolean = useFeatureFlag(FeatureFlag.NG_TEMPLATES)
  const { loading, data, refetch } = useGetSmtpConfig({ queryParams: { accountId } })
  const refetchSmtpData = (): void => {
    refetch()
  }
  const { openCreateSmtpModal } = useCreateSmtpModal({ onCloseModal: refetchSmtpData })
  const smtpResource: ResourceOption[] = [
    {
      label: <String stringID="common.smtp.conifg" />,
      icon: 'smtp',
      disabled: loading,
      onClick: () => {
        Eif (!loading) {
          Eif (!data?.data) {
            openCreateSmtpModal(data?.data)
          } else {
            history.push(routes.toAccountSMTP({ accountId }))
          }
        }
      },
      colorClass: css.smtp,
      subLabel: (
        <>
          {loading ? (
            <Icon name="spinner" size={14} />
          ) : (
            <>
              {!data?.data ? (
                <Button intent="primary" icon={'small-plus'} size={ButtonSize.SMALL} text={getString('common.setup')} />
              ) : (
                <Layout.Horizontal flex={{ alignItems: 'center' }} margin={'xsmall'} spacing="xsmall">
                  <Icon name="tick-circle" size={14} color={Color.GREEN_500} />
 
                  <Text font={{ variation: FontVariation.FORM_HELP }}>{getString('common.smtp.configured')}</Text>
                </Layout.Horizontal>
              )}
            </>
          )}
        </>
      )
    }
  ]
  const options: ResourceOption[] = items || [
    {
      label: <String stringID="connectorsLabel" />,
      icon: 'connectors-icon',
      route: routes.toConnectors({ accountId, orgIdentifier }),
      colorClass: css.connectors
    },
    {
      label: <String stringID="delegate.delegates" />,
      icon: 'delegates-icon' as IconName,
      route: routes.toDelegates({ accountId, orgIdentifier }),
      colorClass: css.delegates
    },
    {
      label: <String stringID="common.secrets" />,
      icon: 'secrets-icon',
      route: routes.toSecrets({ accountId, orgIdentifier }),
      colorClass: css.secrets
    },
    ...(!orgIdentifier ? smtpResource : []),
    ...(templatesEnabled
      ? [
          {
            label: <String stringID="common.templates" />,
            icon: 'templates-icon',
            route: routes.toTemplates({ accountId, orgIdentifier }),
            colorClass: css.templates
          } as ResourceOption
        ]
      : [])
  ]
 
  return (
    <Layout.Horizontal spacing="xxlarge">
      {options.map(option => (
        <Card
          key={option.icon}
          className={cx(css.card, option.colorClass)}
          disabled={option.disabled}
          onClick={() => {
            option?.onClick?.()
            Iif (option.route) {
              history.push(option.route)
            }
          }}
        >
          <Layout.Vertical flex spacing="small">
            <Icon name={option.icon} size={70} />
            <Text color={Color.BLACK} font={{ weight: 'semi-bold' }}>
              {option.label}
            </Text>
            {option.subLabel}
          </Layout.Vertical>
        </Card>
      ))}
    </Layout.Horizontal>
  )
}
 
export default ResourceCardList