All files / modules/10-common/pages/entityUsage/views/EntityUsageListView EntityUsageList.tsx

94.12% Statements 32/34
73.08% Branches 76/104
100% Functions 6/6
93.94% Lines 31/33

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              20x   20x 20x   20x 20x 20x 20x             20x 3x 3x     3x     3x                         20x 3x 3x     3x           3x                         20x 2x   2x                         20x 2x 2x 2x 2x 2x                 2x                                         2x 1x     2x                               20x  
/*
 * 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, { useMemo } from 'react'
import type { Column, Renderer, CellProps } from 'react-table'
import { Text, Layout, Icon, TableV2 } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import type { EntitySetupUsageDTO, ResponsePageEntitySetupUsageDTO } from 'services/cd-ng'
import { useStrings } from 'framework/strings'
import ResourceDetailFactory from '@common/factories/ResourceDetailFactory'
import { useAppStore } from 'framework/AppStore/AppStoreContext'
import css from './EntityUsageList.module.scss'
 
interface EntityUsageListProps {
  entityData: ResponsePageEntitySetupUsageDTO | null
  gotoPage: (pageNumber: number) => void
}
 
const RenderColumnEntity: Renderer<CellProps<EntitySetupUsageDTO>> = ({ row }) => {
  const data = row.original
  const checkReferredByEntityTypeHandler = ResourceDetailFactory.getReferredByEntityTypeHandler(
    data.referredByEntity.type
  )
  Iif (checkReferredByEntityTypeHandler)
    return checkReferredByEntityTypeHandler.getResourceDetailViewAndAction({ referredByEntity: data.referredByEntity })
  else
    return (
      <Layout.Vertical>
        <Layout.Horizontal>
          <Text color={Color.BLACK} lineClamp={1} className={css.overflow}>
            {data.referredByEntity?.name}
          </Text>
        </Layout.Horizontal>
        <Text color={Color.GREY_400} lineClamp={1} className={css.overflow}>
          {data.referredByEntity?.type}
        </Text>
      </Layout.Vertical>
    )
}
const RenderColumnDetail: Renderer<CellProps<EntitySetupUsageDTO>> = ({ row }) => {
  const data = row.original
  const chkReferredEntityDetailHandler = ResourceDetailFactory.getReferredEntityUsageDetailTypeHandler(
    data.detail?.type
  )
  Iif (chkReferredEntityDetailHandler)
    return chkReferredEntityDetailHandler.getResourceDetailViewAndAction({
      referredEntity: data.referredEntity,
      referredByEntity: data.referredByEntity,
      detail: data.detail
    })
  else return null
}
 
// Todo: Enable once BE starts maintaining lastActivity
// const RenderColumnActivity: Renderer<CellProps<EntitySetupUsageDTO>> = ({ row }) => {
//   const data = row.original
//   return (
//     <Layout.Horizontal spacing="small">
//       {data.createdAt ? <ReactTimeago date={data.createdAt} /> : null}
//     </Layout.Horizontal>
//   )
// }
 
export const RenderGitDetails: Renderer<CellProps<EntitySetupUsageDTO>> = ({ row }) => {
  const data = row.original
 
  return data.referredByEntity.entityRef?.branch && data.referredByEntity.entityRef?.repoIdentifier ? (
    <Layout.Horizontal spacing="small">
      <Icon inline name="repository" color={Color.GREY_600}></Icon>
      <Text className={css.gitText} margin={{ right: 'medium' }}>
        {data.referredByEntity.entityRef?.repoIdentifier}
      </Text>
 
      <Icon inline name="git-new-branch" size={14} color={Color.GREY_600}></Icon>
      <Text className={css.gitText}>{data.referredByEntity.entityRef?.branch}</Text>
    </Layout.Horizontal>
  ) : null
}
 
const EntityUsageList: React.FC<EntityUsageListProps> = ({ entityData, gotoPage }) => {
  const data: EntitySetupUsageDTO[] = entityData?.data?.content || []
  const { getString } = useStrings()
  const { isGitSyncEnabled } = useAppStore()
  const columns: Column<EntitySetupUsageDTO>[] = useMemo(
    () => [
      {
        Header: getString('entity'),
        accessor: 'referredByEntity',
        width: isGitSyncEnabled ? '30%' : '50%',
        Cell: RenderColumnEntity
      },
      {
        Header: getString('common.gitSync.repoDetails').toUpperCase(),
        accessor: row => row.referredByEntity?.entityRef?.repoIdentifier,
        width: '50%',
        Cell: RenderGitDetails
      },
      {
        Header: getString('details'),
        accessor: 'detail',
        width: isGitSyncEnabled ? '20%' : '50%',
        Cell: RenderColumnDetail
      }
      // {
      //   Header: getString('lastActivity'),
      //   accessor: 'createdAt',
      //   width: '34%',
      //   Cell: RenderColumnActivity
      // }
    ],
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [data, isGitSyncEnabled]
  )
 
  if (!isGitSyncEnabled) {
    columns.splice(1, 1)
  }
 
  return (
    <TableV2<EntitySetupUsageDTO>
      className={css.table}
      columns={columns}
      data={data}
      pagination={{
        itemCount: entityData?.data?.totalElements || 0,
        pageSize: entityData?.data?.size || 10,
        pageCount: entityData?.data?.totalPages || 0,
        pageIndex: entityData?.data?.pageable?.pageNumber || 0,
        gotoPage
      }}
    />
  )
}
 
export default EntityUsageList