All files / modules/30-secrets/pages/secretDetailsHomePage SecretDetailsHomePage.tsx

82.93% Statements 34/41
56.92% Branches 37/65
71.43% Functions 5/7
82.93% Lines 34/41

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              9x 9x 9x 9x 9x 9x 9x   9x 9x 9x   9x 9x                       9x             9x 2x     9x 2x     2x 2x         2x 2x 2x       2x 2x           2x             2x 2x           2x     2x 2x   2x               2x                                                                                                     4x                     9x  
/*
 * 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 { useParams } from 'react-router-dom'
import { Container, Layout, Text, Icon, TabNavigation } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import { useStrings } from 'framework/strings'
import { Page } from '@common/exports'
import routes from '@common/RouteDefinitions'
import type { Module, ModulePathParams, ProjectPathProps, SecretsPathProps } from '@common/interfaces/RouteInterfaces'
import { NGBreadcrumbs } from '@common/components/NGBreadcrumbs/NGBreadcrumbs'
import { ResponseSecretResponseWrapper, SecretDTOV2, useGetSecretV2 } from 'services/cd-ng'
import { useAppStore } from 'framework/AppStore/AppStoreContext'
import type { UseGetMockData } from '@common/utils/testUtils'
import { getScopeFromDTO } from '@common/components/EntityReference/EntityReference'
import { Scope } from '@common/interfaces/SecretsInterface'
 
interface OptionalIdentifiers {
  module?: Module
  orgIdentifier: string
  projectIdentifier: string
  accountId: string
}
interface SecretDetailsProps {
  mockSecretDetails?: UseGetMockData<ResponseSecretResponseWrapper>
}
 
const getProjectUrl = ({ accountId, projectIdentifier, orgIdentifier, module }: OptionalIdentifiers): string => {
  if (module && orgIdentifier && projectIdentifier) {
    return routes.toProjectOverview({ orgIdentifier, projectIdentifier, accountId, module })
  }
  return routes.toProjectDetails({ accountId, orgIdentifier, projectIdentifier })
}
 
const getSecretsUrl = ({ accountId, orgIdentifier, projectIdentifier, module }: OptionalIdentifiers): string => {
  return routes.toSecrets({ accountId, orgIdentifier, projectIdentifier, module })
}
 
const SecretDetaislHomePage: React.FC<SecretDetailsProps> = ({ children }, props) => {
  const { accountId, projectIdentifier, orgIdentifier, secretId, module } = useParams<
    ProjectPathProps & SecretsPathProps & ModulePathParams
  >()
  const { selectedProject } = useAppStore()
  const { loading, data, error, refetch } = useGetSecretV2({
    identifier: secretId,
    queryParams: { accountIdentifier: accountId, projectIdentifier: projectIdentifier, orgIdentifier: orgIdentifier },
    mock: props.mockSecretDetails
  })
  const { getString } = useStrings()
  const secretType = data?.data?.secret.type
  const childrenWithProps = React.isValidElement(children)
    ? React.cloneElement(children, { secretData: data, refetch: refetch })
    : children
 
  const renderBreadCrumb: React.FC = () => {
    const breadCrumbArray = [
      {
        url: getSecretsUrl({ accountId, projectIdentifier, orgIdentifier, module }),
        label: getString('common.secrets')
      }
    ]
    /* istanbul ignore else */ Iif (projectIdentifier && !module) {
      breadCrumbArray.unshift({
        url: getProjectUrl({ accountId, projectIdentifier, orgIdentifier, module }),
        label: selectedProject ? selectedProject.name : ''
      })
    }
 
    Eif (getScopeFromDTO({ accountId, orgIdentifier, projectIdentifier }) === Scope.ACCOUNT) {
      breadCrumbArray.unshift({
        url: routes.toAccountResources({ accountId }),
        label: getString('common.accountResources')
      })
    }
 
    return <NGBreadcrumbs links={breadCrumbArray} />
  }
 
  const renderIcon = (type: SecretDTOV2['type']) => {
    switch (type) {
      case 'SecretText':
        return <Icon name="text" size={24} />
      case 'SecretFile':
        return <Icon name="file" size={24} />
      case 'SSHKey':
        return <Icon name="secret-ssh" size={24} />
    }
  }
 
  return (
    <>
      <Page.Header
        size="large"
        breadcrumbs={renderBreadCrumb(props)}
        title={
          <Layout.Horizontal spacing="small" flex={{ alignItems: 'center', justifyContent: 'flex-start' }}>
            {secretType ? renderIcon(secretType) : null}
            <Container>
              <Text color={Color.GREY_800} font="medium">
                {data?.data?.secret.name || ''}
              </Text>
              <Text color={Color.GREY_400} font="small">
                {data?.data?.secret.identifier || ''}
              </Text>
            </Container>
          </Layout.Horizontal>
        }
        toolbar={
          <TabNavigation
            size={'small'}
            links={[
              {
                label: getString('overview'),
                to: routes.toSecretDetailsOverview({
                  accountId,
                  projectIdentifier,
                  orgIdentifier,
                  secretId,
                  module
                })
              },
              {
                label: getString('common.references'),
                to: routes.toSecretDetailsReferences({
                  accountId,
                  projectIdentifier,
                  orgIdentifier,
                  secretId,
                  module
                })
              }
            ]}
          />
        }
      />
      <Page.Body
        loading={loading}
        retryOnError={() => refetch()}
        error={(error?.data as Error)?.message || error?.message}
        noData={{
          when: () => !data?.data,
          icon: 'nav-project',
          message: getString('entityReference.noRecordFound')
        }}
      >
        {childrenWithProps}
      </Page.Body>
    </>
  )
}
 
export default SecretDetaislHomePage