All files / modules/30-secrets/pages/secretReferences/views/SecretReferencesListView SecretReferencesList.tsx

0% Statements 0/29
0% Branches 0/52
0% Functions 0/5
0% Lines 0/29

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                                                                                                                                                                                                                           
/*
 * 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 ReactTimeago from 'react-timeago'
import type { Column, Renderer, CellProps } from 'react-table'
import { Text, Layout, TableV2 } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import type { EntitySetupUsageDTO, PageEntitySetupUsageDTO } from 'services/cd-ng'
import { useStrings } from 'framework/strings'
import ResourceDetailFactory from '@common/factories/ResourceDetailFactory'
import css from './SecretReferencesList.module.scss'
 
interface SecretsListProps {
  secrets?: PageEntitySetupUsageDTO
  gotoPage: (pageNumber: number) => void
}
 
const RenderColumnEntity: Renderer<CellProps<EntitySetupUsageDTO>> = ({ row }) => {
  const data = row.original
  const chkReferredByEntityTypeHandler = ResourceDetailFactory.getReferredByEntityTypeHandler(
    data.referredByEntity.type
  )
  if (chkReferredByEntityTypeHandler)
    return chkReferredByEntityTypeHandler.getResourceDetailViewAndAction({ referredByEntity: data.referredByEntity })
  else
    return (
      <Layout.Vertical>
        <Layout.Horizontal>
          <Text color={Color.BLACK}>{data.referredByEntity?.name}</Text>
        </Layout.Horizontal>
        <Text color={Color.GREY_400}>{data.referredByEntity?.type}</Text>
      </Layout.Vertical>
    )
}
const RenderColumnDetail: Renderer<CellProps<EntitySetupUsageDTO>> = ({ row }) => {
  const data = row.original
  const chkReferredEntityDetailHandler = ResourceDetailFactory.getReferredEntityUsageDetailTypeHandler(
    data.detail?.type
  )
  if (chkReferredEntityDetailHandler)
    return chkReferredEntityDetailHandler.getResourceDetailViewAndAction({
      referredEntity: data.referredEntity,
      referredByEntity: data.referredByEntity,
      detail: data.detail
    })
  else return null
}
 
const RenderColumnActivity: Renderer<CellProps<EntitySetupUsageDTO>> = ({ row }) => {
  const data = row.original
  return (
    <Layout.Horizontal spacing="small">
      {data.createdAt ? <ReactTimeago date={data.createdAt} /> : null}
    </Layout.Horizontal>
  )
}
 
const SecretReferencesList: React.FC<SecretsListProps> = ({ secrets, gotoPage }) => {
  const data: EntitySetupUsageDTO[] = secrets?.content || []
  const { getString } = useStrings()
  const columns: Column<EntitySetupUsageDTO>[] = useMemo(
    () => [
      {
        Header: getString('entity'),
        accessor: 'referredByEntity',
        width: '33%',
        Cell: RenderColumnEntity
      },
      {
        Header: getString('details'),
        accessor: 'detail',
        width: '33%',
        Cell: RenderColumnDetail
      },
      {
        Header: getString('lastActivity'),
        accessor: 'createdAt',
        width: '34%',
        Cell: RenderColumnActivity
      }
    ],
    [data]
  )
 
  return (
    <TableV2<EntitySetupUsageDTO>
      className={css.table}
      columns={columns}
      data={data}
      // onRowClick={secret => {
      //   history.push(`${pathname}/${secret.secret?.identifier}`)
      // }}
      pagination={{
        itemCount: secrets?.totalElements || 0,
        pageSize: secrets?.size || 10,
        pageCount: secrets?.totalPages || 0,
        pageIndex: secrets?.pageable?.pageNumber || 0,
        gotoPage
      }}
    />
  )
}
 
export default SecretReferencesList