All files / modules/30-delegates/components/DelegateTokens/modals TokenDelegatesList.tsx

89.74% Statements 35/39
62.26% Branches 33/53
71.43% Functions 5/7
89.74% Lines 35/39

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              10x 10x 10x 10x 10x 10x   10x 10x 10x   10x         10x 5x   5x 5x 5x 5x                                                     1x 1x 1x                           10x 8x 8x 8x   8x                 8x   8x 8x 8x                   8x 8x 6x 2x         8x                                 4x           10x  
/*
 * 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, { useState, useMemo, FC } from 'react'
import { useParams } from 'react-router-dom'
import { Layout, Text, Dialog, ExpandingSearchInput, Button } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import { useStrings } from 'framework/strings'
import { DelegateGroupDetails, useGetDelegatesByToken, GetDelegatesByTokenQueryParams } from 'services/portal'
 
import { delegateTypeToIcon } from '@common/utils/delegateUtils'
import { PageSpinner } from '@common/components'
import DelegateInstallationError from '@delegates/components/CreateDelegate/components/DelegateInstallationError/DelegateInstallationError'
 
import css from '../DelegateTokens.module.scss'
 
interface DelegateItemParams {
  delegate: DelegateGroupDetails
}
const DelegateItem: FC<DelegateItemParams> = ({ delegate }) => {
  const { getString } = useStrings()
 
  const [troubleshooterOpen, setOpenTroubleshooter] = useState<boolean>(false)
  const statusColor: Color = delegate.activelyConnected ? Color.GREEN_600 : Color.GREY_400
  const text = delegate.activelyConnected ? getString('connected') : getString('delegate.notConnected')
  return (
    <div className={css.delegateItemContainer}>
      <Dialog
        isOpen={!!troubleshooterOpen}
        enforceFocus={false}
        style={{ width: '680px', height: '100%' }}
        onClose={() => setOpenTroubleshooter(false)}
      >
        <DelegateInstallationError showDelegateInstalledMessage={false} />
      </Dialog>
      <Text
        icon={delegateTypeToIcon(delegate.delegateType as string)}
        iconProps={{ size: 24 }}
        margin={{ left: 'xxlarge' }}
      />
      <Layout.Vertical className={css.tokenItemColumn} margin={{ left: 'large' }}>
        <Text color={Color.GREY_800}>{delegate.groupName}</Text>
      </Layout.Vertical>
      <Layout.Vertical className={css.tokenItemColumn}>
        <Text icon="full-circle" iconProps={{ size: 6, color: statusColor, padding: 'small' }} color={Color.GREY_800}>
          {text}
        </Text>
        {!delegate.activelyConnected && delegate.delegateType === 'KUBERNETES' && (
          <Button
            minimal
            className={css.troubleshootLink}
            onClick={e => {
              e.preventDefault()
              e.stopPropagation()
              setOpenTroubleshooter(true)
            }}
          >
            {getString('delegates.troubleshootOption')}
          </Button>
        )}
      </Layout.Vertical>
    </div>
  )
}
 
interface DelegateTokensListParams {
  tokenName: string
}
const DelegateTokensList: FC<DelegateTokensListParams> = ({ tokenName }) => {
  const { getString } = useStrings()
  const { accountId, projectIdentifier, orgIdentifier } = useParams<Record<string, string>>()
  const [searchTerm, setSearchTerm] = useState('')
 
  const { data, loading } = useGetDelegatesByToken({
    queryParams: {
      accountId,
      projectIdentifier,
      orgIdentifier,
      delegateTokenName: tokenName
    } as GetDelegatesByTokenQueryParams
  })
 
  const delegates = data?.resource?.delegateGroupDetails || []
 
  const filteredDelegates = useMemo(() => {
    Eif (!searchTerm) {
      return delegates
    }
    return (
      delegates?.filter((del: DelegateGroupDetails) =>
        del?.groupName?.toLowerCase().includes(searchTerm.toLowerCase())
      ) || []
    )
  }, [delegates, searchTerm])
 
  let noDelegatesMessage
  Eif (delegates !== undefined) {
    if (delegates?.length === 0) {
      noDelegatesMessage = getString('delegates.tokens.tokenNotUsedByDelegates')
    } else Iif (filteredDelegates.length === 0) {
      noDelegatesMessage = getString('delegates.tokens.tokenBySearchNameNotExisting')
    }
  }
 
  return (
    <Layout.Vertical>
      {loading ? (
        <PageSpinner />
      ) : delegates?.length ? (
        <ExpandingSearchInput
          alwaysExpanded
          width={250}
          placeholder={getString('search')}
          throttle={200}
          onChange={setSearchTerm}
          className={css.search}
        />
      ) : null}
      <Layout.Vertical spacing="small" className={css.tokenListDelegateContainer}>
        {noDelegatesMessage && <Text>{noDelegatesMessage}</Text>}
        {filteredDelegates.map(delegate => (
          <DelegateItem key={delegate.groupId} delegate={delegate} />
        ))}
      </Layout.Vertical>
    </Layout.Vertical>
  )
}
export default DelegateTokensList