All files / modules/75-cf/pages/environment-details EnvironmentDetailsBody.tsx

83.12% Statements 64/77
80.95% Branches 34/42
65.38% Functions 17/26
85.71% Lines 60/70

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 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294              2x 2x                         2x   2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x   2x                   2x 2x   75x   25x 2x 25x   25x     2x 31x 31x 31x 31x   31x   31x           31x         2x       31x                                                                                               2x 10x 10x 10x 10x   10x             10x               10x 10x 2x 1x 1x 1x     10x           10x 10x   10x 9x                                           10x                                                         31x     1x                                                             1x 1x 1x                                                     2x 9x                 2x  
/*
 * 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, { useContext, useMemo, useState } from 'react'
import {
  Container,
  Heading,
  Layout,
  Pagination,
  Tab,
  Tabs,
  Text,
  Utils,
  PageError,
  TableV2
} from '@wings-software/uicore'
import type { Column } from 'react-table'
import { FontVariation, Color, Intent } from '@harness/design-system'
import type { EnvironmentResponseDTO } from 'services/cd-ng'
import { ApiKey, useDeleteAPIKey, useGetAllAPIKeys } from 'services/cf'
import { useToaster } from '@common/exports'
import { ContainerSpinner } from '@common/components/ContainerSpinner/ContainerSpinner'
import { useEnvStrings } from '@cf/hooks/environment'
import { CF_DEFAULT_PAGE_SIZE, EnvironmentSDKKeyType, getErrorMessage, showToaster } from '@cf/utils/CFUtils'
import { withTableData } from '@cf/utils/table-utils'
import RbacButton from '@rbac/components/Button/Button'
import { ResourceType } from '@rbac/interfaces/ResourceType'
import { PermissionIdentifier } from '@rbac/interfaces/PermissionIdentifier'
import { useConfirmAction } from '@common/hooks'
import { String } from 'framework/strings'
import AddKeyDialog from '../../components/AddKeyDialog/AddKeyDialog'
 
import css from './EnvironmentDetails.module.scss'
 
type CustomColumn<T extends Record<string, any>> = Column<T>
 
type RowFunctions = {
  environmentIdentifier: string
  isNew: (id: string) => boolean
  onDelete: (id: string, name: string) => void
  getSecret: (id: string, fallback: string) => string
}
const defaultContext = { environmentIdentifier: '', isNew: () => false, onDelete: () => false, getSecret: () => '' }
const RowContext = React.createContext<RowFunctions>(defaultContext)
 
const withApiKey = withTableData<ApiKey, { apiKey: ApiKey }>(({ row }) => ({ apiKey: row.original }))
 
const NameCell = withApiKey(({ apiKey }) => <Text font={{ weight: 'bold' }}>{apiKey.name}</Text>)
const TypeCell = withApiKey(({ apiKey }) => {
  const { getEnvString } = useEnvStrings()
 
  return <Text>{getEnvString(`apiKeys.${apiKey.type.toLowerCase()}Type`)}</Text>
})
 
const ApiInfoCell = withApiKey(({ apiKey }) => {
  const { environmentIdentifier, isNew, onDelete, getSecret } = useContext(RowContext) ?? defaultContext
  const { getString, getEnvString } = useEnvStrings()
  const { showSuccess, showError } = useToaster()
  const showCopy = isNew(apiKey.identifier)
 
  const apiKeyText = showCopy ? getSecret(apiKey.identifier, apiKey.apiKey) : apiKey.apiKey
 
  const handleCopy = (): void => {
    Utils.copy(apiKeyText)
      .then(() => showSuccess(getString('clipboardCopySuccess')))
      .catch(() => showError(getString('clipboardCopyFail'), undefined, 'cf.copy.text.error'))
  }
 
  const deleteSDKKey = useConfirmAction({
    intent: Intent.DANGER,
    title: getEnvString('apiKeys.deleteTitle'),
    message: <String stringID="cf.environments.apiKeys.deleteMessage" vars={{ keyName: apiKey.name }} />,
    action: () => {
      onDelete(apiKey.identifier, apiKey.name)
    }
  })
 
  return (
    <Layout.Horizontal flex={{ distribution: 'space-between' }}>
      <Layout.Horizontal spacing="small" flex={{ alignItems: 'center' }} className={css.keyContainer}>
        <Text font={{ weight: 'bold' }} className={css.keyType}>
          {apiKey.type === EnvironmentSDKKeyType.CLIENT ? getString(`common.clientId`) : getString('secretType')}:
        </Text>
        {showCopy ? (
          <div className={css.keyCopyContainer}>
            <Text
              font={{ mono: true }}
              rightIcon="main-clone"
              rightIconProps={{
                onClick: handleCopy,
                color: Color.GREY_350,
                className: css.keyCopyIcon
              }}
              padding="small"
              className={css.keyCopy}
            >
              {apiKeyText}
            </Text>
            <Text font={{ variation: FontVariation.TINY }} color={Color.ORANGE_900} className={css.keyRedactionWarning}>
              {getEnvString('apiKeys.redactionWarning')}
            </Text>
          </div>
        ) : (
          <Text>{apiKeyText}</Text>
        )}
      </Layout.Horizontal>
      <Container>
        <RbacButton
          minimal
          icon="trash"
          iconProps={{
            size: 16
          }}
          className={css.keyDeleteButton}
          onClick={deleteSDKKey}
          permission={{
            resource: { resourceType: ResourceType.ENVIRONMENT, resourceIdentifier: environmentIdentifier },
            permission: PermissionIdentifier.EDIT_ENVIRONMENT
          }}
        />
      </Container>
    </Layout.Horizontal>
  )
})
 
const EnvironmentSDKKeys: React.FC<{ environment: EnvironmentResponseDTO }> = ({ environment }) => {
  const { showError } = useToaster()
  const { getString, getEnvString } = useEnvStrings()
  const [recents, setRecents] = useState<ApiKey[]>([])
  const [page, setPage] = useState<number>(0)
 
  const queryParams = {
    projectIdentifier: environment.projectIdentifier as string,
    environmentIdentifier: environment.identifier as string,
    accountIdentifier: environment.accountId as string,
    orgIdentifier: environment.orgIdentifier as string
  }
 
  const { data, loading, error, refetch } = useGetAllAPIKeys({
    queryParams: {
      ...queryParams,
      pageSize: CF_DEFAULT_PAGE_SIZE,
      pageNumber: page
    }
  })
 
  const { mutate: deleteKey } = useDeleteAPIKey({ queryParams })
  const handleDelete = (id: string, keyName: string): void => {
    deleteKey(id)
      .then(() => showToaster(getString('cf.environments.apiKeys.deleteSuccess', { keyName })))
      .then(() => refetch())
      .catch(deleteError => showError(getErrorMessage(deleteError), undefined, 'cf.delete.api.key.error'))
  }
 
  const { apiKeys, ...pagination } = data ?? {
    itemCount: 0,
    pageCount: 0,
    pageIndex: 0,
    pageSize: CF_DEFAULT_PAGE_SIZE
  }
  const hasData = !error && !loading && (apiKeys || []).length > 0
  const emptyData = !error && !loading && (apiKeys || []).length === 0
 
  const columns: CustomColumn<ApiKey>[] = useMemo(
    () => [
      {
        Header: getString('name').toUpperCase(),
        accessor: 'name',
        width: '25%',
        Cell: NameCell
      },
      {
        Header: getString('typeLabel').toUpperCase(),
        accessor: 'type',
        width: '10%',
        Cell: TypeCell
      },
      {
        id: 'info',
        width: '65%',
        Cell: ApiInfoCell
      }
    ],
    []
  )
 
  return (
    <Container width="100%" height="calc(100vh - 174px)">
      {hasData && (
        <Layout.Vertical
          padding={{ top: 'xxxlarge', left: 'xxlarge' }}
          height="100%"
          flex={{ justifyContent: 'flex-start', alignItems: 'flex-start' }}
        >
          <Layout.Horizontal width="100%" flex={{ distribution: 'space-between', alignItems: 'baseline' }}>
            <Heading level={2} font={{ variation: FontVariation.H5 }}>
              {getEnvString('apiKeys.title')}
            </Heading>
            <AddKeyDialog
              environment={environment}
              onCreate={(newKey: ApiKey, hideCreate) => {
                setRecents([...recents, newKey])
                hideCreate()
                refetch()
              }}
            />
          </Layout.Horizontal>
          <Text color={Color.GREY_800} padding={{ top: 'small', bottom: 'xxlarge' }}>
            {getEnvString('apiKeys.message')}
          </Text>
          <Container className={css.content}>
            <Container className={css.table}>
              <RowContext.Provider
                value={{
                  environmentIdentifier: environment.identifier as string,
                  isNew: (id: string) => Boolean(recents.find(r => r.identifier === id)),
                  onDelete: handleDelete,
                  getSecret: (id: string, fallback: string) =>
                    recents.find(r => r.identifier === id)?.apiKey || fallback
                }}
              >
                <TableV2<ApiKey> data={(apiKeys || []) as ApiKey[]} columns={columns} />
              </RowContext.Provider>
            </Container>
            {!!pagination.itemCount && (
              <Container className={css.paginationContainer}>
                <Pagination
                  itemCount={pagination.itemCount}
                  pageCount={pagination.pageCount}
                  pageIndex={pagination.pageIndex}
                  pageSize={CF_DEFAULT_PAGE_SIZE}
                  gotoPage={(index: number) => {
                    setPage(index)
                    refetch({ queryParams: { ...queryParams, pageNumber: index } })
                  }}
                />
              </Container>
            )}
          </Container>
        </Layout.Vertical>
      )}
 
      {emptyData && (
        <Layout.Vertical width="100%" height="100%" flex={{ align: 'center-center' }} spacing="medium">
          <Text>{getEnvString('apiKeys.noKeysFound')}</Text>
          <AddKeyDialog
            primary
            environment={environment}
            onCreate={(newKey: ApiKey, hideCreate) => {
              setRecents([...recents, newKey])
              hideCreate()
              refetch()
            }}
          />
        </Layout.Vertical>
      )}
 
      {loading && (
        <Layout.Horizontal width="100%" height="100%" flex={{ align: 'center-center' }}>
          <ContainerSpinner />
        </Layout.Horizontal>
      )}
 
      {error && (
        <PageError
          message={getErrorMessage(error)}
          onClick={() => {
            setPage(0)
            refetch()
          }}
        />
      )}
    </Container>
  )
}
 
const CFEnvironmentDetailsBody: React.FC<{
  environment: EnvironmentResponseDTO
}> = ({ environment }) => {
  return (
    <Container className={css.envTabs}>
      <Tabs id="envDetailsTabs">
        <Tab id="settings" title="Settings" panel={<EnvironmentSDKKeys environment={environment} />} />
      </Tabs>
    </Container>
  )
}
 
export default CFEnvironmentDetailsBody