All files / modules/45-projects-orgs/components/Menu ContextMenu.tsx

100% Statements 53/53
81.82% Branches 72/88
100% Functions 9/9
100% Lines 53/53

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              67x 67x 67x 67x 67x   67x 67x 67x 67x 67x   67x 67x                       67x 67x 67x 67x 67x 67x   67x                     67x 3x 3x 3x     67x 4x 4x 4x     67x 4x 4x 4x     67x 1x 1x 1x                   67x 1x 1x 1x                 67x 1x 1x 1x               67x 1x 1x 1x                 67x 1x 1x 1x             67x                                                                                                                                                                                                     67x  
/*
 * 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 from 'react'
import { useHistory, useParams } from 'react-router-dom'
import { Menu } from '@blueprintjs/core'
import { Layout, Text, Icon } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import type { Project } from 'services/cd-ng'
import routes from '@common/RouteDefinitions'
import { useStrings } from 'framework/strings'
import { ModuleName } from 'framework/types/ModuleName'
import { PermissionIdentifier } from '@rbac/interfaces/PermissionIdentifier'
import { ResourceType } from '@rbac/interfaces/ResourceType'
import type { AccountPathProps } from '@common/interfaces/RouteInterfaces'
import RbacMenuItem from '@rbac/components/MenuItem/MenuItem'
import { useFeatureFlags } from '@common/hooks/useFeatureFlag'
import type { PermissionRequest } from '@rbac/hooks/usePermission'
 
interface ContextMenuProps {
  project: Project
  reloadProjects?: () => Promise<unknown>
  editProject?: (project: Project) => void
  collaborators?: (project: Project) => void
  setMenuOpen?: (value: React.SetStateAction<boolean>) => void
  openDialog?: () => void
}
 
const ContextMenu: React.FC<ContextMenuProps> = props => {
  const history = useHistory()
  const { accountId } = useParams<AccountPathProps>()
  const { getString } = useStrings()
  const { project, editProject, collaborators, setMenuOpen, openDialog } = props
  const { CDNG_ENABLED, CVNG_ENABLED, CING_ENABLED, CENG_ENABLED, CFNG_ENABLED } = useFeatureFlags()
 
  const permissionRequest: Optional<PermissionRequest, 'permission'> = {
    resourceScope: {
      accountIdentifier: accountId,
      orgIdentifier: project.orgIdentifier
    },
    resource: {
      resourceType: ResourceType.PROJECT,
      resourceIdentifier: project.identifier
    }
  }
 
  const handleDelete = (event: React.MouseEvent<HTMLElement, MouseEvent>): void => {
    event.stopPropagation()
    setMenuOpen?.(false)
    openDialog?.()
  }
 
  const handleEdit = (event: React.MouseEvent<HTMLElement, MouseEvent>): void => {
    event.stopPropagation()
    setMenuOpen?.(false)
    editProject?.(project)
  }
 
  const handleCollaborate = (event: React.MouseEvent<HTMLElement, MouseEvent>): void => {
    event.stopPropagation()
    setMenuOpen?.(false)
    collaborators?.(project)
  }
 
  const handleCD = (event: React.MouseEvent<HTMLElement, MouseEvent>): void => {
    event.stopPropagation()
    setMenuOpen?.(false)
    history.push(
      routes.toProjectOverview({
        orgIdentifier: project.orgIdentifier || /* istanbul ignore next */ '',
        projectIdentifier: project.identifier,
        accountId,
        module: 'cd'
      })
    )
  }
 
  const handleCV = (event: React.MouseEvent<HTMLElement, MouseEvent>): void => {
    event.stopPropagation()
    setMenuOpen?.(false)
    history.push(
      routes.toCVMonitoringServices({
        projectIdentifier: project.identifier,
        orgIdentifier: project.orgIdentifier || /* istanbul ignore next */ '',
        accountId
      })
    )
  }
 
  const handleCF = (event: React.MouseEvent<HTMLElement, MouseEvent>): void => {
    event.stopPropagation()
    setMenuOpen?.(false)
    history.push(
      routes.toCFFeatureFlags({
        projectIdentifier: project.identifier,
        orgIdentifier: project.orgIdentifier || /* istanbul ignore next */ '',
        accountId
      })
    )
  }
  const handleCI = (event: React.MouseEvent<HTMLElement, MouseEvent>): void => {
    event.stopPropagation()
    setMenuOpen?.(false)
    history.push(
      routes.toProjectOverview({
        projectIdentifier: project.identifier,
        orgIdentifier: project.orgIdentifier || /* istanbul ignore next */ '',
        accountId,
        module: 'ci'
      })
    )
  }
  const handleCE = (event: React.MouseEvent<HTMLElement, MouseEvent>): void => {
    event.stopPropagation()
    setMenuOpen?.(false)
    history.push(
      routes.toCECORules({
        accountId
      })
    )
  }
 
  return (
    <Menu style={{ minWidth: 'unset' }}>
      {CDNG_ENABLED && project.modules?.includes(ModuleName.CD) ? (
        <Menu.Item
          text={
            <Layout.Horizontal spacing="xsmall">
              <Icon name="cd-main" size={20} />
              <Text color={Color.WHITE}>{getString('projectsOrgs.gotoCD')}</Text>
            </Layout.Horizontal>
          }
          onClick={handleCD}
        />
      ) : null}
      {CING_ENABLED && project.modules?.includes(ModuleName.CI) ? (
        <Menu.Item
          text={
            <Layout.Horizontal spacing="xsmall">
              <Icon name="ci-main" size={20} />
              <Text color={Color.WHITE}>{getString('projectsOrgs.gotoCI')}</Text>
            </Layout.Horizontal>
          }
          onClick={handleCI}
        />
      ) : null}
      {CFNG_ENABLED && project.modules?.includes(ModuleName.CF) ? (
        <Menu.Item
          text={
            <Layout.Horizontal spacing="xsmall">
              <Icon name="cf-main" size={20} />
              <Text color={Color.WHITE}>{getString('projectsOrgs.gotoCF')}</Text>
            </Layout.Horizontal>
          }
          onClick={handleCF}
        />
      ) : null}
      {CENG_ENABLED && project.modules?.includes(ModuleName.CE) ? (
        <Menu.Item
          text={
            <Layout.Horizontal spacing="xsmall">
              <Icon name="ce-main" size={20} />
              <Text color={Color.WHITE}>{getString('projectsOrgs.gotoCloudCosts')}</Text>
            </Layout.Horizontal>
          }
          onClick={handleCE}
        />
      ) : null}
      {CVNG_ENABLED && project.modules?.includes(ModuleName.CV) ? (
        <Menu.Item
          text={
            <Layout.Horizontal spacing="xsmall">
              <Icon name="cv-main" size={20} />
              <Text color={Color.WHITE}>{getString('projectsOrgs.gotoCV')}</Text>
            </Layout.Horizontal>
          }
          onClick={handleCV}
        />
      ) : null}
      <RbacMenuItem
        icon="edit"
        text={getString('edit')}
        onClick={handleEdit}
        data-testid={'edit-project'}
        permission={{
          ...permissionRequest,
          permission: PermissionIdentifier.UPDATE_PROJECT
        }}
      />
      <RbacMenuItem
        icon="new-person"
        text={getString('projectsOrgs.invite')}
        onClick={handleCollaborate}
        permission={{
          resourceScope: {
            accountIdentifier: accountId,
            orgIdentifier: project.orgIdentifier,
            projectIdentifier: project.identifier
          },
          resource: {
            resourceType: ResourceType.USER
          },
          permission: PermissionIdentifier.INVITE_USER
        }}
      />
      <>
        <Menu.Divider />
        <RbacMenuItem
          icon="trash"
          text={getString('delete')}
          onClick={handleDelete}
          permission={{
            ...permissionRequest,
            permission: PermissionIdentifier.DELETE_PROJECT
          }}
        />
      </>
    </Menu>
  )
}
 
export default ContextMenu