All files / modules/40-gitsync/pages/config GitFullSyncEntityList.tsx

97.96% Statements 48/49
71.74% Branches 33/46
100% Functions 13/13
97.96% Lines 48/49

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              1x   1x 1x   1x   1x 1x                                           1x 4x 4x                               1x 4x     4x   1x         1x   2x         2x   1x         1x         4x   4x               1x 4x 4x 4x                                   1x 4x   4x     1x 4x   4x     1x 4x   4x             1x 4x   4x     1x 1x 1x 1x 1x     4x             4x           4x             4x             4x             1x                                 1x  
/*
 * 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 from 'react'
import type { CellProps, Column, Renderer } from 'react-table'
import { Icon, Layout, TableV2, Text, Utils } from '@harness/uicore'
import { Color } from '@harness/design-system'
import type { IconProps } from '@harness/uicore/dist/icons/Icon'
import { useStrings } from 'framework/strings'
import type { PageGitFullSyncEntityInfoDTO, GitFullSyncEntityInfoDTO } from 'services/cd-ng'
import { getEntityIconName } from '../entities/EntityHelper'
import css from './GitSyncConfigTab.module.scss'
 
interface GitFullSyncEntityListProps {
  data: PageGitFullSyncEntityInfoDTO
  gotoPage: (pageNumber: number) => void
}
 
interface StatusData {
  iconProp: IconProps
  statusBackground?: Color
  highlightedColor?: Color
}
 
interface RenderEntityStatusProp {
  status: GitFullSyncEntityInfoDTO['syncStatus']
  errorMessage: GitFullSyncEntityInfoDTO['errorMessage']
}
 
interface RenderStatusIconProp extends RenderEntityStatusProp {
  iconProp: IconProps
}
 
const RenderStatusIcon: React.FC<RenderStatusIconProp> = props => {
  const { status, errorMessage, iconProp } = props
  return status === 'FAILED' && errorMessage ? (
    <Utils.WrapOptionalTooltip
      tooltip={errorMessage}
      tooltipProps={{
        isDark: true,
        fill: true,
        position: 'bottom'
      }}
    >
      <Icon {...iconProp} />
    </Utils.WrapOptionalTooltip>
  ) : (
    <Icon {...iconProp} />
  )
}
 
const RenderEntityStatus: React.FC<RenderEntityStatusProp> = props => {
  const { status, errorMessage } = props
  let data: StatusData
 
  switch (status) {
    case 'FAILED':
      data = {
        iconProp: { name: 'warning-sign', color: Color.RED_900 },
        statusBackground: Color.RED_100,
        highlightedColor: Color.RED_900
      }
      break
    case 'SUCCESS':
      data = {
        iconProp: { name: 'success-tick' },
        statusBackground: Color.GREEN_100,
        highlightedColor: Color.GREEN_700
      }
      break
    case 'QUEUED':
      data = {
        iconProp: { name: 'queued' },
        statusBackground: Color.GREY_100,
        highlightedColor: Color.GREY_900
      }
      break
    default:
      data = { iconProp: { name: 'placeholder' } }
  }
 
  const { iconProp, statusBackground, highlightedColor } = data
 
  return (
    <Layout.Horizontal spacing="small" background={statusBackground} padding={'small'} className={css.syncStatus}>
      {iconProp && <RenderStatusIcon iconProp={iconProp} status={status} errorMessage={errorMessage} />}
      <Text color={highlightedColor}>{status}</Text>
    </Layout.Horizontal>
  )
}
 
const RenderEntityDetails: Renderer<CellProps<GitFullSyncEntityInfoDTO>> = ({ row }) => {
  const data = row.original
  const { getString } = useStrings()
  return (
    <Layout.Horizontal>
      <Icon
        name={getEntityIconName(data.entityType)}
        size={20}
        flex={{ alignItems: 'center' }}
        margin={{ right: 'medium' }}
      ></Icon>
      <Layout.Vertical className={css.nameIdWrapper}>
        <Text lineClamp={1} color={Color.GREY_900} font={{ weight: 'semi-bold' }}>
          {data.name}
        </Text>
        <Text lineClamp={1}>{`${getString('common.ID')}: ${data.identifier}`}</Text>
      </Layout.Vertical>
    </Layout.Horizontal>
  )
}
 
const RenderColumnEntityStatus: Renderer<CellProps<GitFullSyncEntityInfoDTO>> = ({ row }) => {
  const data = row.original
 
  return data?.syncStatus ? <RenderEntityStatus status={data.syncStatus} errorMessage={data.errorMessage} /> : <></>
}
 
const RenderColumnEntityType: Renderer<CellProps<GitFullSyncEntityInfoDTO>> = ({ row }) => {
  const data = row.original
 
  return <Text> {data.entityType} </Text>
}
 
const RenderColumnRepo: Renderer<CellProps<GitFullSyncEntityInfoDTO>> = ({ row }) => {
  const data = row.original
 
  return (
    <Text margin={{ right: 'small' }} lineClamp={1}>
      {data.repoName}
    </Text>
  )
}
 
const RenderColumnBranch: Renderer<CellProps<GitFullSyncEntityInfoDTO>> = ({ row }) => {
  const data = row.original
 
  return <Text lineClamp={1}> {data.branch} </Text>
}
 
const GitFullSyncEntityList: React.FC<GitFullSyncEntityListProps> = props => {
  const { data, gotoPage } = props
  const { getString } = useStrings()
  const listData: GitFullSyncEntityInfoDTO[] = data?.content || []
  const columns: Column<GitFullSyncEntityInfoDTO>[] = [
    {
      Header: getString('entity'),
      accessor: row => row.name,
      id: 'name',
      width: '30%',
      Cell: RenderEntityDetails
    },
    {
      Header: getString('gitsync.syncStatus'),
      accessor: row => row.syncStatus,
      width: '15%',
      Cell: RenderColumnEntityStatus
    },
    {
      Header: getString('typeLabel'),
      accessor: row => row.entityType,
      id: 'type',
      width: '15%',
      Cell: RenderColumnEntityType
    },
    {
      Header: getString('common.repositoryName'),
      accessor: row => row.repoName,
      id: 'repository',
      width: '15%',
      Cell: RenderColumnRepo
    },
    {
      Header: getString('common.git.branchName'),
      accessor: row => row.branch,
      id: 'branch',
      width: '15%',
      Cell: RenderColumnBranch
    }
  ]
 
  return (
    <TableV2<GitFullSyncEntityInfoDTO>
      columns={columns}
      data={listData}
      name="GitFullSyncEntityList"
      sortable={true}
      pagination={{
        itemCount: data?.totalItems || 0,
        pageSize: data?.pageSize || 10,
        pageCount: data?.totalPages || -1,
        pageIndex: data?.pageIndex || 0,
        gotoPage
      }}
    />
  )
}
 
export default GitFullSyncEntityList