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

90.91% Statements 50/55
64.29% Branches 9/14
64.29% Functions 9/14
90.91% Lines 50/55

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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261              11x 11x                     11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x                   11x 50x 50x                                   11x 60x 60x 60x 60x 60x                   60x   60x               1x 1x 1x     1x 1x 1x                       60x 1x 1x 1x     60x 1x 1x 1x     60x         4x                   2x 2x                                                                               11x             50x 50x 50x                                                                                 2x                                                                       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 {
  Button,
  ButtonVariation,
  Card,
  Collapse,
  Container,
  Layout,
  Popover,
  Text,
  useConfirmationDialog
} from '@wings-software/uicore'
import { Color, FontVariation, Intent } from '@harness/design-system'
import ReactTimeago from 'react-timeago'
import { Classes, Menu, Position } from '@blueprintjs/core'
import { ApiKeyAggregateDTO, ApiKeyDTO, TokenDTO, useDeleteApiKey } from 'services/cd-ng'
import { useStrings } from 'framework/strings'
import { TagsPopover, useToaster } from '@common/components'
import TokenList from '@rbac/components/TokenList/TokenList'
import { useApiKeyModal } from '@rbac/modals/ApiKeyModal/useApiKeyModal'
import RbacButton from '@rbac/components/Button/Button'
import { PermissionIdentifier } from '@rbac/interfaces/PermissionIdentifier'
import { ResourceType } from '@rbac/interfaces/ResourceType'
import RbacMenuItem from '@rbac/components/MenuItem/MenuItem'
import css from '../ApiKeyList.module.scss'
 
interface ApiKeyCardProps {
  data: ApiKeyAggregateDTO
  openTokenModal: (apiKeyIdentifier: string, token?: TokenDTO, _isRotate?: boolean) => void
  refetchApiKeys: () => void
  onRefetchComplete: () => void
  refetchTokens: boolean
}
 
const RenderColumnDetails = (data: ApiKeyDTO): React.ReactElement => {
  const { getString } = useStrings()
  return (
    <Layout.Vertical spacing="xsmall">
      <Layout.Horizontal spacing="small">
        <Text font={{ variation: FontVariation.BODY2 }} color={Color.BLACK} lineClamp={1} className={css.wordBreak}>
          {data.name}
        </Text>
        {data.tags && Object.keys(data.tags).length ? <TagsPopover tags={data.tags} /> : null}
      </Layout.Horizontal>
      <Text color={Color.GREY_400} lineClamp={1} font={{ size: 'small' }} className={css.wordBreak}>
        {getString('idLabel', { id: data.identifier })}
      </Text>
    </Layout.Vertical>
  )
}
 
const RenderColumnMenu: React.FC<{
  data: ApiKeyDTO
  reload: () => void
}> = ({ data, reload }) => {
  const { accountIdentifier, orgIdentifier, projectIdentifier, identifier, parentIdentifier, apiKeyType } = data
  const [menuOpen, setMenuOpen] = useState(false)
  const { getString } = useStrings()
  const { showSuccess, showError } = useToaster()
  const { mutate: deleteApiKey } = useDeleteApiKey({
    queryParams: {
      accountIdentifier: accountIdentifier || '',
      orgIdentifier,
      projectIdentifier,
      apiKeyType,
      parentIdentifier: parentIdentifier || ''
    }
  })
 
  const { openApiKeyModal } = useApiKeyModal({ onSuccess: reload })
 
  const { openDialog: openDeleteDialog } = useConfirmationDialog({
    contentText: getString('rbac.apiKey.confirmDelete', { name: data.name }),
    titleText: getString('rbac.apiKey.confirmDeleteTitle'),
    confirmButtonText: getString('delete'),
    cancelButtonText: getString('cancel'),
    intent: Intent.DANGER,
    buttonIntent: Intent.DANGER,
    onCloseDialog: async (isConfirmed: boolean) => {
      /* istanbul ignore else */ if (isConfirmed) {
        try {
          const deleted = await deleteApiKey(identifier || '', {
            headers: { 'content-type': 'application/json' }
          })
          /* istanbul ignore else */ if (deleted) {
            showSuccess(getString('rbac.apiKey.successMessage', { name: data.name }))
            reload()
          } else {
            showError(getString('deleteError'))
          }
        } catch (err) {
          /* istanbul ignore next */
          showError(err?.data?.message || err?.message)
        }
      }
    }
  })
 
  const handleDelete = (e: React.MouseEvent<HTMLElement, MouseEvent>): void => {
    e.stopPropagation()
    setMenuOpen(false)
    openDeleteDialog()
  }
 
  const handleEdit = (e: React.MouseEvent<HTMLElement, MouseEvent>): void => {
    e.stopPropagation()
    setMenuOpen(false)
    openApiKeyModal(data)
  }
 
  return (
    <Layout.Horizontal flex={{ justifyContent: 'flex-end', alignItems: 'flex-start' }}>
      <Popover
        isOpen={menuOpen}
        onInteraction={nextOpenState => {
          setMenuOpen(nextOpenState)
        }}
        className={Classes.DARK}
        position={Position.BOTTOM_RIGHT}
      >
        <Button
          variation={ButtonVariation.ICON}
          icon="Options"
          data-testid={`apiKey-menu-${data.identifier}`}
          onClick={e => {
            e.stopPropagation()
            setMenuOpen(true)
          }}
        />
        <Menu>
          <RbacMenuItem
            icon="edit"
            text={getString('edit')}
            onClick={handleEdit}
            permission={{
              permission: PermissionIdentifier.MANAGE_SERVICEACCOUNT,
              resource: {
                resourceType: ResourceType.SERVICEACCOUNT,
                resourceIdentifier: parentIdentifier
              },
              options: {
                skipCondition: () => apiKeyType === 'USER'
              }
            }}
          />
          <RbacMenuItem
            icon="trash"
            text={getString('delete')}
            onClick={handleDelete}
            permission={{
              permission: PermissionIdentifier.MANAGE_SERVICEACCOUNT,
              resource: {
                resourceType: ResourceType.SERVICEACCOUNT,
                resourceIdentifier: parentIdentifier
              },
              options: {
                skipCondition: () => apiKeyType === 'USER'
              }
            }}
          />
        </Menu>
      </Popover>
    </Layout.Horizontal>
  )
}
 
const ApiKeyCard: React.FC<ApiKeyCardProps> = ({
  data,
  openTokenModal,
  refetchApiKeys,
  onRefetchComplete,
  refetchTokens
}) => {
  const { apiKey, createdAt, lastModifiedAt, tokensCount } = data
  const { getString } = useStrings()
  return (
    <Card key={apiKey.identifier} className={css.card}>
      <div className={css.apiKeyList}>
        <RenderColumnDetails {...apiKey} />
        <Text>
          {`${getString('created')} `}
          <ReactTimeago date={createdAt} minPeriod={60} />
        </Text>
        <Text>
          {`${getString('common.lastModifiedTime')} `}
          <ReactTimeago date={lastModifiedAt} minPeriod={60} />
        </Text>
        <RenderColumnMenu data={apiKey} reload={refetchApiKeys} />
      </div>
      <Container padding={{ top: 'small' }}>
        {tokensCount ? (
          <Collapse
            collapsedIcon="chevron-right"
            expandedIcon="chevron-up"
            isRemovable={false}
            collapseClassName={css.collapse}
            heading={
              <div data-testid={`tokens-${apiKey.identifier}`}>
                <Text> {`${getString('common.tokens')} (${tokensCount})`}</Text>
              </div>
            }
          >
            <TokenList
              apiKeyIdentifier={apiKey.identifier}
              apiKeyType={apiKey.apiKeyType}
              openTokenModal={openTokenModal}
              reloadApiKey={refetchApiKeys}
              refetchTokens={refetchTokens}
              onRefetchComplete={onRefetchComplete}
              parentIdentifier={apiKey.parentIdentifier}
            />
            <RbacButton
              text={getString('plusNumber', { number: getString('token') })}
              data-testid={`new_token-${apiKey.identifier}`}
              margin={{ top: 'medium' }}
              variation={ButtonVariation.LINK}
              onClick={() => openTokenModal(apiKey.identifier)}
              permission={{
                permission: PermissionIdentifier.MANAGE_SERVICEACCOUNT,
                resource: {
                  resourceType: ResourceType.SERVICEACCOUNT,
                  resourceIdentifier: apiKey.parentIdentifier
                },
                options: {
                  skipCondition: () => apiKey.apiKeyType === 'USER'
                }
              }}
            />
          </Collapse>
        ) : (
          <RbacButton
            text={getString('plusNumber', { number: getString('token') })}
            variation={ButtonVariation.LINK}
            data-testid={`new_token-${apiKey.identifier}`}
            onClick={() => openTokenModal(apiKey.identifier)}
            permission={{
              permission: PermissionIdentifier.MANAGE_SERVICEACCOUNT,
              resource: {
                resourceType: ResourceType.SERVICEACCOUNT,
                resourceIdentifier: apiKey.parentIdentifier
              },
              options: {
                skipCondition: () => apiKey.apiKeyType === 'USER'
              }
            }}
          />
        )}
      </Container>
    </Card>
  )
}
 
export default ApiKeyCard