All files / modules/70-pipeline/pages/pipelines/views/NewPipelinePopover NewPipelinePopover.tsx

41.18% Statements 14/34
0% Branches 0/8
0% Functions 0/9
41.18% Lines 14/34

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              2x 2x 2x 2x 2x 2x   2x 2x   2x 2x 2x 2x 2x                           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, { useCallback } from 'react'
import { Menu, Position } from '@blueprintjs/core'
import { Button, ButtonVariation, Icon, IconName, Popover, Text } from '@wings-software/uicore'
import { useHistory, useParams } from 'react-router-dom'
import { defaultTo, noop } from 'lodash-es'
import cx from 'classnames'
import type { PopoverProps } from '@wings-software/uicore/dist/components/Popover/Popover'
import { useStrings } from 'framework/strings'
import routes from '@common/RouteDefinitions'
import type { ModulePathParams, ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { PermissionIdentifier } from '@rbac/interfaces/PermissionIdentifier'
import { ResourceType } from '@rbac/interfaces/ResourceType'
import { usePermission } from '@rbac/hooks/usePermission'
import RBACTooltip from '@rbac/components/RBACTooltip/RBACTooltip'
import css from './NewPipelinePopover.module.scss'
 
export interface NewPipelinePopoverProps extends PopoverProps {
  text?: string
  className?: string
}
 
export interface NewPipelineMenuItem {
  icon?: IconName
  label: string
  disabled?: boolean
  onClick: () => void
}
 
export function NewPipelinePopover({
  text,
  className = '',
  portalClassName,
  ...popoverProps
}: NewPipelinePopoverProps): React.ReactElement {
  const { getString } = useStrings()
  const history = useHistory()
  const { projectIdentifier, orgIdentifier, accountId, module } = useParams<ProjectPathProps & ModulePathParams>()
  const [menuOpen, setMenuOpen] = React.useState(false)
  const rbacResourcePermission = {
    resource: {
      resourceType: ResourceType.PIPELINE
    }
  }
  const [canEdit] = usePermission({
    ...rbacResourcePermission,
    permissions: [PermissionIdentifier.EDIT_PIPELINE]
  })
 
  const goToPipeline = useCallback(
    (useTemplate = false) => {
      history.push(
        routes.toPipelineStudio({
          projectIdentifier,
          orgIdentifier,
          pipelineIdentifier: '-1',
          accountId,
          module,
          ...(!!useTemplate && { useTemplate: true })
        })
      )
    },
    [history, projectIdentifier, orgIdentifier, accountId, module]
  )
 
  const menuItems = React.useMemo((): NewPipelineMenuItem[] => {
    return [
      {
        label: 'New Pipeline',
        onClick: () => goToPipeline()
      },
      {
        label: 'Use Template',
        onClick: () => goToPipeline(true),
        icon: 'template-library'
      }
    ]
  }, [goToPipeline])
 
  const tooltipBtn = React.useCallback(
    () =>
      !canEdit ? (
        <RBACTooltip permission={PermissionIdentifier.EDIT_PIPELINE} resourceType={ResourceType.PIPELINE} />
      ) : undefined,
    [canEdit]
  )
 
  return (
    <Popover
      isOpen={menuOpen}
      onInteraction={nextOpenState => {
        setMenuOpen(nextOpenState)
      }}
      position={Position.BOTTOM}
      className={cx(css.main, className)}
      portalClassName={cx(css.popover, portalClassName)}
      usePortal={false}
      minimal={true}
      disabled={!canEdit}
      {...popoverProps}
    >
      <Button
        variation={ButtonVariation.PRIMARY}
        icon="plus"
        rightIcon="chevron-down"
        text={defaultTo(text, getString('pipeline.newPipelineText'))}
        onClick={noop}
        disabled={!canEdit}
        tooltip={tooltipBtn()}
      />
      <Menu>
        {menuItems.map(item => {
          return (
            <li
              key={item.label}
              className={cx(css.menuItem, { [css.disabled]: item.disabled })}
              onClick={e => {
                e.stopPropagation()
                item.onClick()
                setMenuOpen(false)
              }}
            >
              {item.icon && <Icon name={item.icon} size={12} />}
              <Text lineClamp={1}>{item.label}</Text>
            </li>
          )
        })}
      </Menu>
    </Popover>
  )
}