All files / modules/20-rbac/components/ApiKeyList ApiKeyList.tsx

93.75% Statements 30/32
70.73% Branches 29/41
71.43% Functions 5/7
93.75% Lines 30/32

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              11x 11x 11x 11x 11x 11x   11x 11x 11x 11x 11x 11x 11x 11x   11x             11x 39x     39x 39x 39x                   39x           39x 4x     39x   4x 4x           39x                               50x                           1x                                 11x  
/*
 * 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, { useState } from 'react'
import { useParams } from 'react-router-dom'
import isEmpty from 'lodash/isEmpty'
import { Text, Container, ButtonVariation, Card, PageError } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import { TokenDTO, useListAggregatedApiKeys } from 'services/cd-ng'
import type { ProjectPathProps, ServiceAccountPathProps } from '@common/interfaces/RouteInterfaces'
import { useStrings } from 'framework/strings'
import { useApiKeyModal } from '@rbac/modals/ApiKeyModal/useApiKeyModal'
import { useTokenModal } from '@rbac/modals/TokenModal/useTokenModal'
import { PageSpinner } from '@common/components'
import ApiKeyCard from '@rbac/components/ApiKeyList/views/ApiKeyCard'
import { PermissionIdentifier } from '@rbac/interfaces/PermissionIdentifier'
import { ResourceType } from '@rbac/interfaces/ResourceType'
import RbacButton from '@rbac/components/Button/Button'
 
import css from './ApiKeyList.module.scss'
 
interface ApiKeyListProps {
  apiKeyType?: TokenDTO['apiKeyType']
  parentIdentifier?: string
}
 
const ApiKeyList: React.FC<ApiKeyListProps> = ({ apiKeyType = 'SERVICE_ACCOUNT', parentIdentifier }) => {
  const { accountId, projectIdentifier, orgIdentifier, serviceAccountIdentifier } = useParams<
    ProjectPathProps & ServiceAccountPathProps
  >()
  const { getString } = useStrings()
  const [refetchTokens, setRefetchTokens] = useState<boolean>(false)
  const { data, refetch, error, loading } = useListAggregatedApiKeys({
    queryParams: {
      accountIdentifier: accountId,
      orgIdentifier,
      projectIdentifier,
      apiKeyType,
      parentIdentifier: parentIdentifier || serviceAccountIdentifier
    }
  })
 
  const { openApiKeyModal } = useApiKeyModal({
    onSuccess: refetch,
    parentIdentifier: parentIdentifier || serviceAccountIdentifier,
    apiKeyType
  })
 
  const onRefetchComplete = (): void => {
    setRefetchTokens(false)
  }
 
  const { openTokenModal } = useTokenModal({
    onSuccess: () => {
      refetch()
      setRefetchTokens(true)
    },
    parentIdentifier: parentIdentifier || serviceAccountIdentifier,
    apiKeyType
  })
 
  return (
    <Container padding={{ bottom: 'xlarge' }}>
      <Text color={Color.BLACK} font={{ size: 'medium', weight: 'semi-bold' }}>
        {apiKeyType === 'SERVICE_ACCOUNT' ? getString('common.apiKeys') : getString('rbac.myApiKeys')}
      </Text>
      <Container padding={{ top: 'medium', bottom: 'medium' }}>
        {loading && <PageSpinner />}
        {error && <PageError message={(error.data as Error)?.message || error.message} onClick={() => refetch()} />}
        {isEmpty(data?.data?.content) ? (
          <Card className={css.fullWidth}>
            <Text color={Color.GREY_500} padding={{ top: 'xsmall' }}>
              {getString('common.noAPIKeys')}
            </Text>
          </Card>
        ) : (
          data?.data?.content?.map(apiKey => (
            <ApiKeyCard
              key={apiKey.apiKey.identifier}
              data={apiKey}
              refetchApiKeys={refetch}
              openTokenModal={openTokenModal}
              refetchTokens={refetchTokens}
              onRefetchComplete={onRefetchComplete}
            />
          ))
        )}
      </Container>
      <RbacButton
        text={getString('plusNumber', { number: getString('common.apikey') })}
        variation={ButtonVariation.LINK}
        onClick={() => openApiKeyModal()}
        data-testid="createNewApiKey"
        permission={{
          permission: PermissionIdentifier.MANAGE_SERVICEACCOUNT,
          resource: {
            resourceType: ResourceType.SERVICEACCOUNT,
            resourceIdentifier: serviceAccountIdentifier
          },
          options: {
            skipCondition: () => apiKeyType === 'USER'
          }
        }}
      />
    </Container>
  )
}
 
export default ApiKeyList