All files / modules/45-projects-orgs/pages/projects/views/ProjectListView ProjectListView.tsx

91.86% Statements 79/86
78.57% Branches 99/126
86.36% Functions 19/22
91.86% Lines 79/86

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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316              2x 2x   2x 2x 2x     2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x                               2x 35x 35x 35x                                   2x 35x 35x             2x 35x 35x   35x     25x 25x   25x 15x     25x 10x     25x 5x     25x 5x     25x 15x     25x     35x                         2x 35x 35x 35x 35x                                                         2x 35x 35x 35x 35x 35x                                                           2x 48x 48x 48x 1x   48x   48x         6x                 3x 3x                                 2x 5x 5x 5x 5x   5x 5x       35x             35x             35x                 35x                 35x               35x                     5x             1x                                     2x  
/*
 * 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, { useState, useMemo } from 'react'
import { Text, Layout, Icon, Button, Popover, TagsPopover, TableV2 } from '@wings-software/uicore'
import type { CellProps, Renderer, Column } from 'react-table'
import { Color } from '@harness/design-system'
import { Classes, Position } from '@blueprintjs/core'
import { useHistory, useParams } from 'react-router-dom'
import type { AccountPathProps } from '@common/interfaces/RouteInterfaces'
import type { Project, ProjectAggregateDTO, ResponsePageProjectAggregateDTO } from 'services/cd-ng'
import routes from '@common/RouteDefinitions'
import { String, useStrings } from 'framework/strings'
import { ModuleName } from 'framework/types/ModuleName'
import ContextMenu from '@projects-orgs/components/Menu/ContextMenu'
import { getModuleIcon } from '@common/utils/utils'
import RbacAvatarGroup from '@rbac/components/RbacAvatarGroup/RbacAvatarGroup'
import { ResourceType } from '@rbac/interfaces/ResourceType'
import { PermissionIdentifier } from '@rbac/interfaces/PermissionIdentifier'
import { useFeatureFlags } from '@common/hooks/useFeatureFlag'
import DescriptionPopover from '@common/components/DescriptionPopover.tsx/DescriptionPopover'
import useDeleteProjectDialog from '../../DeleteProject'
import css from './ProjectListView.module.scss'
 
interface ProjectListViewProps {
  data: ResponsePageProjectAggregateDTO | null
  showEditProject?: (project: Project) => void
  collaborators?: (project: Project) => void
  reloadPage: () => Promise<void>
  gotoPage: (index: number) => void
}
 
type CustomColumn<T extends Record<string, any>> = Column<T> & {
  refetchProjects?: () => Promise<void>
  editProject?: (project: Project) => void
  collaborators?: (project: Project) => void
}
 
const RenderColumnProject: Renderer<CellProps<ProjectAggregateDTO>> = ({ row }) => {
  const project = row.original.projectResponse.project
  const { getString } = useStrings()
  return (
    <Layout.Horizontal spacing="small" flex={{ alignItems: 'center', justifyContent: 'flex-start' }}>
      <div className={css.colorbox} style={{ backgroundColor: `${project.color}` }} />
      <Layout.Vertical spacing="xsmall" padding={{ left: 'small' }} className={css.verticalCenter}>
        <Layout.Horizontal spacing="small">
          <Text color={Color.BLACK} lineClamp={1} className={css.project}>
            {project.name}
          </Text>
          {project.tags && Object.keys(project.tags).length ? <TagsPopover tags={project.tags} /> : null}
          {project.description && <DescriptionPopover text={project.description} />}
        </Layout.Horizontal>
        <Text color={Color.GREY_600} lineClamp={1} className={css.project} font={{ size: 'small' }}>
          {getString('idLabel', { id: project.identifier })}
        </Text>
      </Layout.Vertical>
    </Layout.Horizontal>
  )
}
const RenderColumnOrganization: Renderer<CellProps<ProjectAggregateDTO>> = ({ row }) => {
  const data = row.original
  return (
    <Text color={Color.BLACK} lineClamp={1} className={css.org}>
      {data.organization?.name}
    </Text>
  )
}
 
const RenderColumnModules: Renderer<CellProps<ProjectAggregateDTO>> = ({ row }) => {
  const { CDNG_ENABLED, CVNG_ENABLED, CING_ENABLED, CENG_ENABLED, CFNG_ENABLED } = useFeatureFlags()
  const data = row.original
 
  const shouldShowModules = data.projectResponse.project.modules?.length
 
  function getModuleIcons(project: Project): React.ReactElement[] {
    const modules = project.modules
    const icons = []
 
    if (CDNG_ENABLED && modules?.includes(ModuleName.CD)) {
      icons.push(<Icon name={getModuleIcon(ModuleName.CD)} size={20} key={ModuleName.CD} />)
    }
 
    if (CING_ENABLED && modules?.includes(ModuleName.CI)) {
      icons.push(<Icon name={getModuleIcon(ModuleName.CI)} size={20} key={ModuleName.CI} />)
    }
 
    if (CFNG_ENABLED && modules?.includes(ModuleName.CF)) {
      icons.push(<Icon name={getModuleIcon(ModuleName.CF)} size={20} key={ModuleName.CF} />)
    }
 
    if (CENG_ENABLED && modules?.includes(ModuleName.CE)) {
      icons.push(<Icon name={getModuleIcon(ModuleName.CE)} size={20} key={ModuleName.CE} />)
    }
 
    if (CVNG_ENABLED && modules?.includes(ModuleName.CV)) {
      icons.push(<Icon name={getModuleIcon(ModuleName.CV)} size={20} key={ModuleName.CV} />)
    }
 
    return icons
  }
 
  return (
    <Layout.Horizontal spacing="medium">
      {shouldShowModules ? (
        getModuleIcons(data.projectResponse.project)
      ) : (
        <Text color={Color.GREY_350} font={{ size: 'small' }}>
          <String stringID="moduleRenderer.start" />
        </Text>
      )}
    </Layout.Horizontal>
  )
}
 
const RenderColumnAdmin: Renderer<CellProps<ProjectAggregateDTO>> = ({ row, column }) => {
  const { accountId } = useParams<AccountPathProps>()
  const data = row.original
  const project = data.projectResponse.project
  return (
    <RbacAvatarGroup
      avatars={
        data.admins?.length
          ? data.admins.map(admin => {
              return { name: admin.name, email: admin.email }
            })
          : [{}]
      }
      onAdd={event => {
        event.stopPropagation()
        const { collaborators } = column as any
        collaborators(project)
      }}
      restrictLengthTo={2}
      permission={{
        resourceScope: {
          accountIdentifier: accountId,
          orgIdentifier: project.orgIdentifier,
          projectIdentifier: project.identifier
        },
        resource: {
          resourceType: ResourceType.USER
        },
        permission: PermissionIdentifier.INVITE_USER
      }}
    />
  )
}
const RenderColumnCollabrators: Renderer<CellProps<ProjectAggregateDTO>> = ({ row, column }) => {
  const { accountId } = useParams<AccountPathProps>()
  const data = row.original
  const project = data.projectResponse.project
  const { getString } = useStrings()
  return (
    <Layout.Horizontal flex={{ alignItems: 'center', inline: true }}>
      <RbacAvatarGroup
        avatars={data.collaborators?.length ? data.collaborators : [{}]}
        onAdd={event => {
          event.stopPropagation()
          const { collaborators } = column as any
          collaborators(project)
        }}
        restrictLengthTo={2}
        permission={{
          resourceScope: {
            accountIdentifier: accountId,
            orgIdentifier: project.orgIdentifier,
            projectIdentifier: project.identifier
          },
          resource: {
            resourceType: ResourceType.USER
          },
          permission: PermissionIdentifier.INVITE_USER
        }}
      />
      {!data.collaborators?.length ? (
        <Text font={{ size: 'small' }} color={Color.GREY_350}>
          {getString('projectsOrgs.noCollaborators')}
        </Text>
      ) : null}
    </Layout.Horizontal>
  )
}
const RenderColumnMenu: Renderer<CellProps<ProjectAggregateDTO>> = ({ row, column }) => {
  const data = row.original.projectResponse.project
  const [menuOpen, setMenuOpen] = useState(false)
  const onDeleted = (): void => {
    ;(column as any).refetchProjects()
  }
  const { openDialog } = useDeleteProjectDialog(data, onDeleted)
 
  return (
    <Layout.Horizontal className={css.layout}>
      <Popover
        isOpen={menuOpen}
        onInteraction={nextOpenState => {
          setMenuOpen(nextOpenState)
        }}
        className={Classes.DARK}
        position={Position.BOTTOM_RIGHT}
      >
        <Button
          minimal
          icon="Options"
          onClick={e => {
            e.stopPropagation()
            setMenuOpen(true)
          }}
          data-testid={`menu-${data.identifier + data.orgIdentifier}`}
        />
        <ContextMenu
          project={data}
          reloadProjects={(column as any).refetchProjects}
          editProject={(column as any).editProject}
          collaborators={(column as any).collaborators}
          setMenuOpen={setMenuOpen}
          openDialog={openDialog}
        />
      </Popover>
    </Layout.Horizontal>
  )
}
 
const ProjectListView: React.FC<ProjectListViewProps> = props => {
  const { data, showEditProject, collaborators, gotoPage, reloadPage } = props
  const history = useHistory()
  const { accountId } = useParams<AccountPathProps>()
  const { getString } = useStrings()
 
  const columns: CustomColumn<ProjectAggregateDTO>[] = useMemo(
    () => [
      {
        Header: getString('projectLabel'),
        id: 'name',
        accessor: row => row.projectResponse.project.name,
        width: '30%',
        Cell: RenderColumnProject
      },
      {
        Header: getString('orgLabel'),
        id: 'orgName',
        accessor: row => row.projectResponse.project.orgIdentifier,
        width: '20%',
        Cell: RenderColumnOrganization
      },
      {
        Header: getString('adminLabel'),
        id: 'admin',
        accessor: row => row.projectResponse.project.color,
        width: '15%',
        Cell: RenderColumnAdmin,
        collaborators: collaborators,
        disableSortBy: true
      },
      {
        Header: getString('collaboratorsLabel'),
        id: 'collaborators',
        accessor: row => row.projectResponse.createdAt,
        width: '15%',
        Cell: RenderColumnCollabrators,
        collaborators: collaborators,
        disableSortBy: true
      },
      {
        Header: getString('modules'),
        id: 'modules',
        accessor: row => row.projectResponse.project.modules,
        width: '15%',
        Cell: RenderColumnModules,
        disableSortBy: true
      },
      {
        Header: '',
        id: 'menu',
        accessor: row => row.projectResponse.project.identifier,
        width: '5%',
        Cell: RenderColumnMenu,
        refetchProjects: reloadPage,
        editProject: showEditProject,
        collaborators: collaborators,
        disableSortBy: true
      }
    ],
    [reloadPage, showEditProject, collaborators]
  )
  return (
    <TableV2<ProjectAggregateDTO>
      className={css.table}
      columns={columns}
      name="ProjectListView"
      data={data?.data?.content || []}
      onRowClick={project => {
        history.push(
          routes.toProjectDetails({
            projectIdentifier: project.projectResponse.project.identifier,
            orgIdentifier: project.projectResponse.project.orgIdentifier || '',
            accountId
          })
        )
      }}
      pagination={{
        itemCount: data?.data?.totalItems || 0,
        pageSize: data?.data?.pageSize || 10,
        pageCount: data?.data?.totalPages || 0,
        pageIndex: data?.data?.pageIndex || 0,
        gotoPage: gotoPage
      }}
    />
  )
}
 
export default ProjectListView