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

100% Statements 27/27
100% Branches 0/0
100% Functions 7/7
100% Lines 26/26

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              41x 41x   41x 41x 41x 41x 41x         41x 41x 41x   41x                     41x 30x 29x 29x 29x 29x                                     29x 14x           1x               1x               2x               2x           29x                           3x 3x            
/*
 * 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 { Button } from '@wings-software/uicore'
import type { PopoverProps } from '@wings-software/uicore/dist/components/Popover/Popover'
import { useParams } from 'react-router-dom'
import { Classes } from '@blueprintjs/core'
import { defaultTo } from 'lodash-es'
import { useStrings } from 'framework/strings'
import {
  TemplateMenuItem,
  TemplatesActionPopover
} from '@templates-library/components/TemplatesActionPopover/TemplatesActionPopover'
import type { TemplateSummaryResponse } from 'services/template-ng'
import { usePermission } from '@rbac/hooks/usePermission'
import { ResourceType } from '@rbac/interfaces/ResourceType'
import { PermissionIdentifier } from '@rbac/interfaces/PermissionIdentifier'
import type { TemplateStudioPathProps } from '@common/interfaces/RouteInterfaces'
import css from './TemplateListCardContextMenu.module.scss'
 
export interface ContextMenuProps extends PopoverProps {
  template: TemplateSummaryResponse
  onPreview: (template: TemplateSummaryResponse) => void
  onOpenEdit: (template: TemplateSummaryResponse) => void
  onOpenSettings: (templateIdentifier: string) => void
  onDelete: (template: TemplateSummaryResponse) => void
  className?: string
}
 
export const TemplateListCardContextMenu: React.FC<ContextMenuProps> = (props): JSX.Element => {
  const { getString } = useStrings()
  const { template, onPreview, onOpenEdit, onOpenSettings, onDelete, className, ...popoverProps } = props
  const [menuOpen, setMenuOpen] = React.useState(false)
  const { accountId, orgIdentifier, projectIdentifier, templateIdentifier } = useParams<TemplateStudioPathProps>()
  const [canView, canEdit, canDelete] = usePermission(
    {
      resourceScope: {
        accountIdentifier: accountId,
        orgIdentifier,
        projectIdentifier
      },
      resource: {
        resourceType: ResourceType.TEMPLATE,
        resourceIdentifier: template.identifier
      },
      permissions: [
        PermissionIdentifier.VIEW_TEMPLATE,
        PermissionIdentifier.EDIT_TEMPLATE,
        PermissionIdentifier.DELETE_TEMPLATE
      ]
    },
    [orgIdentifier, projectIdentifier, accountId, templateIdentifier]
  )
  const items = React.useMemo((): TemplateMenuItem[] => {
    return [
      {
        icon: 'main-view',
        label: getString('connectors.ceAws.crossAccountRoleExtention.step1.p2'),
        disabled: !canView,
        onClick: () => {
          onPreview(template)
        }
      },
      {
        icon: 'main-share',
        label: getString('templatesLibrary.openEditTemplate'),
        disabled: !canEdit,
        onClick: () => {
          onOpenEdit(template)
        }
      },
      {
        icon: 'main-setup',
        label: getString('templatesLibrary.templateSettings'),
        disabled: !canEdit,
        onClick: () => {
          onOpenSettings(defaultTo(template.identifier, ''))
        }
      },
      {
        icon: 'main-trash',
        label: getString('templatesLibrary.deleteTemplate'),
        disabled: !canDelete,
        onClick: () => {
          onDelete(template)
        }
      }
    ]
  }, [canView, canEdit, canDelete, onPreview, onOpenEdit, onOpenSettings, onDelete, template])
 
  return (
    <TemplatesActionPopover
      open={menuOpen}
      items={items}
      setMenuOpen={setMenuOpen}
      className={className}
      portalClassName={Classes.DARK}
      {...popoverProps}
    >
      <Button
        minimal
        className={css.actionButton}
        icon="more"
        onClick={e => {
          e.stopPropagation()
          setMenuOpen(true)
        }}
      />
    </TemplatesActionPopover>
  )
}