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

91.67% Statements 33/36
67.24% Branches 39/58
75% Functions 3/4
91.67% Lines 33/36

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              2x 2x 2x 2x 2x 2x     2x 2x   2x 2x 2x 2x 2x 2x 2x   2x         2x 17x 17x           17x 17x   17x 17x 17x   17x   17x                         2x                                     17x       17x       17x               17x                                                                 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 from 'react'
import { Button, ButtonVariation, Container, Layout, Text, PageError, PageSpinner } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import { useParams } from 'react-router-dom'
import { truncate } from 'lodash-es'
import { useStrings } from 'framework/strings'
import type { StringsMap } from 'stringTypes'
import type { AccountPathProps } from '@common/interfaces/RouteInterfaces'
import { useDefaultExperienceModal } from '@common/modals/DefaultVersion/DefaultExperience'
import { useGetAccountNG } from 'services/cd-ng'
import type { Experiences } from '@common/constants/Utils'
import RbacButton from '@rbac/components/Button/Button'
import { PermissionIdentifier } from '@rbac/interfaces/PermissionIdentifier'
import { ResourceType } from '@rbac/interfaces/ResourceType'
import useSwitchAccountModal from '@common/modals/SwitchAccount/useSwitchAccountModal'
import { useCommunity } from 'framework/LicenseStore/useCommunity'
import AccountNameForm from './AccountNameForm'
import css from '../AccountOverview.module.scss'
 
const VERSIONS = {
  CG: 'common.harnessFirstGeneration',
  NG: 'common.harnessNextGeneration'
}
 
const AccountDetails: React.FC = () => {
  const { getString } = useStrings()
  const { accountId } = useParams<AccountPathProps>()
  const {
    data,
    loading,
    refetch: refetchAcct,
    error
  } = useGetAccountNG({ accountIdentifier: accountId, queryParams: { accountIdentifier: accountId } })
  const [updateAccountName, setUpdateAccountName] = React.useState(false)
 
  const { openDefaultExperienceModal } = useDefaultExperienceModal({ refetchAcct })
  const { openSwitchAccountModal } = useSwitchAccountModal({})
  const isCommunity = useCommunity()
 
  const accountData = data?.data
 
  const accountNameComponent = updateAccountName ? (
    <AccountNameForm
      name={accountData?.name || ''}
      setUpdateAccountName={setUpdateAccountName}
      refetchAcct={refetchAcct}
    />
  ) : (
    <React.Fragment>
      <Text color={Color.GREY_800}>{truncate(accountData?.name)}</Text>
      <RbacButton
        variation={ButtonVariation.LINK}
        icon="Edit"
        text={getString('edit')}
        onClick={() => setUpdateAccountName(true)}
        permission={{
          permission: PermissionIdentifier.EDIT_ACCOUNT,
          resource: {
            resourceType: ResourceType.ACCOUNT
          }
        }}
      />
      {!isCommunity && (
        <Button
          variation={ButtonVariation.LINK}
          text={getString('common.switchAccount')}
          onClick={openSwitchAccountModal}
        />
      )}
    </React.Fragment>
  )
 
  const defaultExperienceStr =
    accountData?.defaultExperience && VERSIONS[accountData?.defaultExperience]
      ? getString(VERSIONS[accountData?.defaultExperience] as keyof StringsMap)
      : ''
 
  Iif (loading) {
    return <PageSpinner />
  }
 
  Iif (error) {
    return (
      <Container height={300}>
        <PageError message={(error.data as Error)?.message || error.message} onClick={() => refetchAcct()} />
      </Container>
    )
  }
 
  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.accountDetails')}
      </Text>
 
      <Layout.Horizontal flex={{ alignItems: 'center', justifyContent: 'flex-start' }} margin={{ bottom: 'large' }}>
        <Text color={Color.GREY_600} className={css.minWidth}>
          {getString('common.accountName')}
        </Text>
        {accountNameComponent}
      </Layout.Horizontal>
 
      <Layout.Horizontal flex={{ alignItems: 'center', justifyContent: 'flex-start' }} margin={{ bottom: 'large' }}>
        <Text className={css.minWidth}>{getString('common.accountId')}</Text>
        <Text color={Color.GREY_800}>{accountData?.identifier}</Text>
      </Layout.Horizontal>
 
      <Layout.Horizontal flex={{ alignItems: 'center', justifyContent: 'flex-start' }} margin={{ bottom: 'large' }}>
        <Text className={css.minWidth}>{getString('common.harnessClusterHostingAccount')}</Text>
        <Text padding={{ right: 'small' }} color={Color.GREY_800}>
          {accountData?.cluster}
        </Text>
      </Layout.Horizontal>
 
      <Layout.Horizontal flex={{ alignItems: 'center', justifyContent: 'flex-start' }}>
        <Text className={css.minWidth}>{getString('common.defaultExperience')}</Text>
        <Text color={Color.GREY_800}>{defaultExperienceStr}</Text>
        {!isCommunity && (
          <RbacButton
            variation={ButtonVariation.LINK}
            padding="none"
            text={getString('change')}
            onClick={() => openDefaultExperienceModal(accountData?.defaultExperience as Experiences)}
            permission={{
              permission: PermissionIdentifier.EDIT_ACCOUNT,
              resource: {
                resourceType: ResourceType.ACCOUNT
              }
            }}
          />
        )}
      </Layout.Horizontal>
    </Container>
  )
}
 
export default AccountDetails