All files / modules/72-templates-library/pages/TemplatesPage/views/TemplatesListView TemplatesListView.tsx

100% Statements 53/53
100% Branches 36/36
100% Functions 9/9
100% Lines 52/52

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              22x   22x 22x 22x 22x 22x 22x 22x   22x   22x 22x 22x 22x 22x                 22x 3x 3x 3x                             22x 7x 7x 7x   7x                                           22x 7x 7x 7x                                                                                           22x 7x 7x                 22x 7x 7x 7x 7x   7x 7x 2x 1x   1x   5x 3x   2x       7x 7x                                                                                   7x 4x     7x 5x     7x         1x                 7x        
/*
 * 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 { Container, Layout, TableV2, Text } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import { Position } from '@blueprintjs/core'
import { defaultTo, isEmpty } from 'lodash-es'
import { TemplateListCardContextMenu } from '@templates-library/pages/TemplatesPage/views/TemplateListCardContextMenu/TemplateListCardContextMenu'
import { useStrings } from 'framework/strings'
import { useAppStore } from 'framework/AppStore/AppStoreContext'
import type { TemplateSummaryResponse } from 'services/template-ng'
import { templateColorStyleMap } from '@templates-library/pages/TemplatesPage/TemplatesPageUtils'
import type { TemplatesViewProps } from '@templates-library/pages/TemplatesPage/views/TemplatesView'
import { TagsPopover } from '@common/components'
import { Badge } from '@pipeline/pages/utils/Badge/Badge'
import GitDetailsColumn from '@common/components/Table/GitDetailsColumn/GitDetailsColumn'
import { TemplateType } from '@templates-library/utils/templatesUtils'
import css from './TemplatesListView.module.scss'
 
type CustomColumn<T extends Record<string, any>> = Column<T> & {
  onPreview?: (template: TemplateSummaryResponse) => void
  onOpenEdit?: (template: TemplateSummaryResponse) => void
  onOpenSettings?: (templateIdentifier: string) => void
  onDelete?: (template: TemplateSummaryResponse) => void
}
 
const RenderColumnMenu: Renderer<CellProps<TemplateSummaryResponse>> = ({ row, column }) => {
  const data = row.original
  const { onPreview, onOpenEdit, onOpenSettings, onDelete } = column as CustomColumn<TemplateSummaryResponse>
  return (
    <Layout.Horizontal style={{ justifyContent: 'flex-end' }}>
      {onPreview && onOpenEdit && onOpenSettings && onDelete && (
        <TemplateListCardContextMenu
          template={data}
          onPreview={onPreview}
          onOpenEdit={onOpenEdit}
          onOpenSettings={onOpenSettings}
          onDelete={onDelete}
        />
      )}
    </Layout.Horizontal>
  )
}
 
const RenderColumnType: Renderer<CellProps<TemplateSummaryResponse>> = ({ row }) => {
  const data = row.original
  const templateEntityType = data.templateEntityType
  const style = templateColorStyleMap[defaultTo(templateEntityType, TemplateType.Step)]
 
  return (
    <Layout.Horizontal
      className={css.templateColor}
      spacing="large"
      flex={{ alignItems: 'center', justifyContent: 'flex-start' }}
    >
      <svg width="8" height="64" viewBox="0 0 8 64" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path
          d="M7.5 63.5L7.5 0.5H5C2.51472 0.5 0.5 2.51472 0.5 5L0.5 59C0.5 61.4853 2.51472 63.5 5 63.5H7.5Z"
          fill={style.fill}
          stroke={style.stroke}
        />
      </svg>
      {templateEntityType && (
        <Text font={{ size: 'xsmall', weight: 'bold' }} style={{ color: style.color, letterSpacing: 2 }}>
          {templateEntityType.toUpperCase()}
        </Text>
      )}
    </Layout.Horizontal>
  )
}
 
const RenderColumnTemplate: Renderer<CellProps<TemplateSummaryResponse>> = ({ row }) => {
  const data = row.original
  const { getString } = useStrings()
  return (
    <Layout.Horizontal flex={{ alignItems: 'center', justifyContent: 'start' }}>
      <Layout.Vertical spacing="xsmall" data-testid={data.identifier} padding={{ right: 'medium' }}>
        <Layout.Horizontal spacing="medium">
          <Text
            color={Color.GREY_800}
            tooltipProps={{ position: Position.BOTTOM }}
            lineClamp={1}
            tooltip={
              <Layout.Vertical
                color={Color.GREY_800}
                spacing="small"
                padding="medium"
                style={{ maxWidth: 400, overflowWrap: 'anywhere' }}
              >
                <Text color={Color.GREY_800}>{getString('nameLabel', { name: data.name })}</Text>
                <br />
                <Text lineClamp={1}>
                  {getString('descriptionLabel', { description: defaultTo(data.description, '-') })}
                </Text>
              </Layout.Vertical>
            }
          >
            {data.name}
          </Text>
          {data.tags && !isEmpty(data.tags) && <TagsPopover tags={data.tags} />}
        </Layout.Horizontal>
        <Text tooltipProps={{ position: Position.BOTTOM }} color={Color.GREY_400} font={{ size: 'small' }}>
          {getString('idLabel', { id: data.identifier })}
        </Text>
      </Layout.Vertical>
      {data.entityValidityDetails?.valid === false && (
        <Container>
          <Badge
            text={'common.invalid'}
            iconName="error-outline"
            showTooltip={true}
            entityName={data.name}
            entityType={'Template'}
          />
        </Container>
      )}
    </Layout.Horizontal>
  )
}
 
const RenderColumnLabel: Renderer<CellProps<TemplateSummaryResponse>> = ({ row }) => {
  const data = row.original
  return (
    <Layout.Horizontal padding={{ right: 'medium' }}>
      <Text color={Color.GREY_800} font={{ weight: 'semi-bold' }} lineClamp={1}>
        {data.versionLabel}
      </Text>
    </Layout.Horizontal>
  )
}
 
export const TemplatesListView: React.FC<TemplatesViewProps> = (props): JSX.Element => {
  const { getString } = useStrings()
  const { data, selectedIdentifier, gotoPage, onPreview, onOpenEdit, onOpenSettings, onDelete, onSelect } = props
  const { isGitSyncEnabled } = useAppStore()
  const hideMenu = !onPreview && !onOpenEdit && !onOpenSettings && !onDelete
 
  const getTemplateNameWidth = (): string => {
    if (isGitSyncEnabled) {
      if (hideMenu) {
        return '40%'
      }
      return '35%'
    } else {
      if (hideMenu) {
        return '50%'
      }
      return '45%'
    }
  }
 
  const columns: CustomColumn<TemplateSummaryResponse>[] = React.useMemo(
    () => [
      {
        Header: getString('typeLabel').toUpperCase(),
        accessor: 'templateEntityType',
        width: isGitSyncEnabled ? '15%' : '25%',
        Cell: RenderColumnType
      },
      {
        Header: 'Template',
        accessor: 'name',
        width: getTemplateNameWidth(),
        Cell: RenderColumnTemplate
      },
      {
        Header: getString('version').toUpperCase(),
        accessor: 'versionLabel',
        width: isGitSyncEnabled ? '10%' : '25%',
        Cell: RenderColumnLabel,
        disableSortBy: true
      },
      {
        Header: getString('common.gitSync.repoDetails').toUpperCase(),
        accessor: 'gitDetails',
        width: '35%',
        Cell: GitDetailsColumn,
        disableSortBy: true
      },
      {
        Header: '',
        accessor: 'version',
        width: '5%',
        Cell: RenderColumnMenu,
        disableSortBy: true,
        onPreview,
        onOpenEdit,
        onOpenSettings,
        onDelete
      }
    ],
    [isGitSyncEnabled, onPreview, onOpenEdit, onOpenSettings, onDelete]
  )
 
  if (hideMenu) {
    columns.pop()
  }
 
  if (!isGitSyncEnabled) {
    columns.splice(3, 1)
  }
 
  return (
    <TableV2<TemplateSummaryResponse>
      className={css.table}
      columns={columns}
      data={defaultTo(data.content, [])}
      onRowClick={item => onSelect(item)}
      pagination={{
        className: css.pagination,
        itemCount: defaultTo(data.totalElements, 0),
        pageSize: defaultTo(data.size, 10),
        pageCount: defaultTo(data.totalPages, 0),
        pageIndex: defaultTo(data.number, 0),
        gotoPage
      }}
      getRowClassName={row => (row.original.identifier === selectedIdentifier ? css.selected : '')}
    />
  )
}