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

98.59% Statements 70/71
70.24% Branches 118/168
90.91% Functions 10/11
98.48% Lines 65/66

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              1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x   1x 17x 17x 17x 17x 17x                     17x               17x 17x 1x     17x       17x 1x     17x   17x 3x           17x 17x     17x 17x   17x 17x 11x               6x   6x 6x 6x 6x 6x   6x 30x                           17x                                                                                                                           4x                 2x 2x                                         1x 1x                           1x 1x                                                     1x  
/*
 * 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 } from 'react'
import { Button, Container, Icon, Layout, Popover, Text } from '@wings-software/uicore'
import { useHistory, useParams } from 'react-router-dom'
import { FontVariation, Color } from '@harness/design-system'
import { Classes, Position } from '@blueprintjs/core'
import ReactTimeago from 'react-timeago'
import { isEmpty } from 'lodash-es'
import { Page } from '@common/exports'
import routes from '@common/RouteDefinitions'
import { Project, useGetProjectAggregateDTO } from 'services/cd-ng'
import { useStrings } from 'framework/strings'
import { ModuleName } from 'framework/types/ModuleName'
import ModuleListCard from '@projects-orgs/components/ModuleListCard/ModuleListCard'
import { useProjectModal } from '@projects-orgs/modals/ProjectModal/useProjectModal'
import { useCollaboratorModal } from '@projects-orgs/modals/ProjectModal/useCollaboratorModal'
import ContextMenu from '@projects-orgs/components/Menu/ContextMenu'
import TagsRenderer from '@common/components/TagsRenderer/TagsRenderer'
import { useFeatureFlags } from '@common/hooks/useFeatureFlag'
import { useDocumentTitle } from '@common/hooks/useDocumentTitle'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { NGBreadcrumbs } from '@common/components/NGBreadcrumbs/NGBreadcrumbs'
import { ResourceType } from '@rbac/interfaces/ResourceType'
import { PermissionIdentifier } from '@rbac/interfaces/PermissionIdentifier'
import RbacAvatarGroup from '@rbac/components/RbacAvatarGroup/RbacAvatarGroup'
import useDeleteProjectDialog from '../../DeleteProject'
import css from './ProjectDetails.module.scss'
 
const ProjectDetails: React.FC = () => {
  const { accountId, projectIdentifier, orgIdentifier } = useParams<ProjectPathProps>()
  const { getString } = useStrings()
  const [menuOpen, setMenuOpen] = useState(false)
  const { CDNG_ENABLED, CVNG_ENABLED, CING_ENABLED, CENG_ENABLED, CFNG_ENABLED } = useFeatureFlags()
  const invitePermission = {
    resourceScope: {
      accountIdentifier: accountId,
      orgIdentifier,
      projectIdentifier
    },
    resource: {
      resourceType: ResourceType.USER
    },
    permission: PermissionIdentifier.INVITE_USER
  }
  const { data, loading, error, refetch } = useGetProjectAggregateDTO({
    identifier: projectIdentifier,
    queryParams: {
      accountIdentifier: accountId,
      orgIdentifier: orgIdentifier
    }
  })
 
  const projectData = data?.data?.projectResponse.project
  const refetchProject = (): void => {
    refetch()
  }
 
  const { openProjectModal } = useProjectModal({
    onSuccess: refetchProject
  })
 
  const showEditProject = (project: Project): void => {
    openProjectModal(project)
  }
 
  const { openCollaboratorModal } = useCollaboratorModal()
 
  const showCollaborators = (project: Project): void => {
    openCollaboratorModal({
      projectIdentifier: project.identifier,
      orgIdentifier: project.orgIdentifier || 'default'
    })
  }
 
  const history = useHistory()
  const onDeleted = (): void => {
    history.push(routes.toProjects({ accountId }))
  }
  const { openDialog } = useDeleteProjectDialog(projectData || { identifier: '', name: '' }, onDeleted)
  useDocumentTitle(getString('projectsText'))
 
  const getModuleInfoCards = (): React.ReactElement | React.ReactElement[] => {
    if (!projectData?.modules?.length) {
      return (
        <Layout.Vertical padding="huge" flex={{ align: 'center-center' }} spacing="huge">
          <Icon name="nav-project" size={70} />
          <Text font="medium">{getString('projectsOrgs.noModules')}</Text>
        </Layout.Vertical>
      )
    }
 
    const infoCards = []
 
    Eif (CDNG_ENABLED && projectData.modules.includes(ModuleName.CD)) infoCards.push(ModuleName.CD)
    Eif (CING_ENABLED && projectData.modules.includes(ModuleName.CI)) infoCards.push(ModuleName.CI)
    Eif (CFNG_ENABLED && projectData.modules.includes(ModuleName.CF)) infoCards.push(ModuleName.CF)
    Eif (CENG_ENABLED && projectData.modules.includes(ModuleName.CE)) infoCards.push(ModuleName.CE)
    Eif (CVNG_ENABLED && projectData.modules.includes(ModuleName.CV)) infoCards.push(ModuleName.CV)
 
    return infoCards.map(module => (
      <ModuleListCard
        module={module as ModuleName}
        key={module}
        projectIdentifier={projectData.identifier}
        orgIdentifier={projectData.orgIdentifier || ''}
        accountId={accountId}
      />
    ))
  }
 
  /* istanbul ignore next */ if (loading) return <Page.Spinner />
  /* istanbul ignore next */ if (error)
    return <Page.Error message={(error.data as Error)?.message || error.message} onClick={() => refetch()} />
  /* istanbul ignore next */ if (!projectData) return <></>
  return (
    <>
      <Page.Header
        size={projectData.description || !isEmpty(projectData.tags) ? 'xxlarge' : 'xlarge'}
        breadcrumbs={
          <NGBreadcrumbs
            links={[
              {
                url: routes.toProjects({ accountId }),
                label: getString('projectsText')
              }
            ]}
          />
        }
        title={
          <Layout.Vertical spacing="small" padding={{ top: 'small' }} className={css.title}>
            <Layout.Horizontal
              spacing="medium"
              margin={{ bottom: 'medium' }}
              flex={{ alignItems: 'center', justifyContent: 'flex-start' }}
            >
              <div className={css.colorBar} style={{ backgroundColor: projectData.color }} />
              <Text color={Color.GREY_600} font={{ variation: FontVariation.SMALL }} lineClamp={1}>
                {getString('idLabel', { id: projectData.identifier })}
              </Text>
              <div className={css.divider} />
              <Text color={Color.GREY_600} font={{ variation: FontVariation.SMALL }} lineClamp={1}>
                {`${getString('orgLabel')}: ${projectData.orgIdentifier}`}
              </Text>
              <div className={css.divider} />
              {data?.data?.projectResponse.lastModifiedAt ? (
                <Text color={Color.GREY_600} font={{ variation: FontVariation.SMALL }}>
                  {`${getString('common.modified')} `}
                  <ReactTimeago date={data?.data?.projectResponse.lastModifiedAt} />
                </Text>
              ) : null}
            </Layout.Horizontal>
            <Layout.Horizontal flex={{ alignItems: 'center', justifyContent: 'flex-start' }}>
              <Text font={{ variation: FontVariation.H4 }} lineClamp={1} padding={{ right: 'xlarge' }}>
                {projectData.name}
              </Text>
            </Layout.Horizontal>
            {projectData.description ? (
              <Text font="small" color={Color.BLACK} lineClamp={1}>
                {projectData.description}
              </Text>
            ) : null}
            {projectData.tags && !isEmpty(projectData.tags) ? (
              <Layout.Horizontal padding={{ top: 'medium' }}>
                <TagsRenderer tags={projectData.tags} length={6} tagClassName={css.tags} />
              </Layout.Horizontal>
            ) : null}
          </Layout.Vertical>
        }
        toolbar={
          <Layout.Vertical
            padding={{ top: 'small' }}
            flex={{ justifyContent: 'space-between', alignItems: 'flex-end' }}
          >
            <Popover
              isOpen={menuOpen}
              onInteraction={nextOpenState => {
                setMenuOpen(nextOpenState)
              }}
              className={Classes.DARK}
              position={Position.BOTTOM_RIGHT}
            >
              <Button
                minimal
                icon="Options"
                onClick={e => {
                  e.stopPropagation()
                  setMenuOpen(true)
                }}
              />
              <ContextMenu
                project={projectData as Project}
                reloadProjects={refetch}
                editProject={showEditProject}
                collaborators={showCollaborators}
                setMenuOpen={setMenuOpen}
                openDialog={openDialog}
              />
            </Popover>
            <Layout.Horizontal padding={{ right: 'huge' }} margin={{ right: 'huge' }}>
              <Layout.Vertical padding={{ right: 'xlarge' }}>
                <Text font="xsmall" color={Color.GREY_400} padding={{ left: 'xsmall', bottom: 'small' }}>
                  {`${getString('adminLabel')} ${data?.data?.admins?.length ? `(${data?.data?.admins?.length})` : ``}`}
                </Text>
                <RbacAvatarGroup
                  className={css.projectDetailsAvatarGroup}
                  avatars={data?.data?.admins?.length ? data?.data?.admins : [{}]}
                  onAdd={event => {
                    event.stopPropagation()
                    showCollaborators(projectData as Project)
                  }}
                  restrictLengthTo={6}
                  permission={invitePermission}
                />
              </Layout.Vertical>
              <Layout.Vertical>
                <Text font="xsmall" color={Color.GREY_400} padding={{ left: 'xsmall', bottom: 'small' }}>{`${getString(
                  'collaboratorsLabel'
                )} ${data?.data?.collaborators?.length ? `(${data?.data?.collaborators?.length})` : ``}`}</Text>
                <RbacAvatarGroup
                  className={css.projectDetailsAvatarGroup}
                  avatars={data?.data?.collaborators?.length ? data?.data?.collaborators : [{}]}
                  onAdd={event => {
                    event.stopPropagation()
                    showCollaborators(projectData as Project)
                  }}
                  restrictLengthTo={6}
                  permission={invitePermission}
                />
              </Layout.Vertical>
            </Layout.Horizontal>
          </Layout.Vertical>
        }
        className={css.header}
      />
      <Page.Body>
        <Layout.Horizontal>
          <Container padding="xxlarge" className={css.enabledModules}>
            <Layout.Vertical padding="small" spacing="large">
              <Text font={{ size: 'medium', weight: 'semi-bold' }} color={Color.BLACK}>
                {getString('modules')}
              </Text>
              {getModuleInfoCards()}
            </Layout.Vertical>
          </Container>
        </Layout.Horizontal>
      </Page.Body>
    </>
  )
}
 
export default ProjectDetails