All files / modules/75-cd/components/PipelineSteps/PolicyStep/PolicySets/PolicySetListRenderer MiniPolicySetRenderer.tsx

100% Statements 27/27
86.36% Branches 19/22
100% Functions 6/6
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 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              101x 101x 101x 101x 101x   101x 101x     101x   101x   101x             101x 31x 31x   31x                   31x                 31x 2x     31x                                                                                         101x 7x   7x 5x   5x   2x           4x                                   1x                                      
/*
 * Copyright 2022 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 { useParams } from 'react-router-dom'
import { defaultTo } from 'lodash-es'
import { Spinner } from '@blueprintjs/core'
import cx from 'classnames'
 
import { Button, Color, Layout, Text } from '@harness/uicore'
import { LinkedPolicy, useGetPolicySet } from 'services/pm'
 
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import useRBACError, { RBACError } from '@rbac/utils/useRBACError/useRBACError'
 
import { PolicySetType } from '../../PolicyStepTypes'
 
import css from './PolicySetListRenderer.module.scss'
 
interface MiniPolicySetRendererProps {
  policySetId: string
  deletePolicySet: (policySetId: string) => void
}
 
export function MiniPolicySetRenderer({ policySetId, deletePolicySet }: MiniPolicySetRendererProps) {
  const { accountId: accountIdentifier, projectIdentifier, orgIdentifier } = useParams<ProjectPathProps>()
  const { getRBACErrorMessage } = useRBACError()
 
  const policySetType = policySetId.includes('account.')
    ? PolicySetType.ACCOUNT
    : policySetId.includes('org.')
    ? PolicySetType.ORG
    : PolicySetType.PROJECT
 
  const {
    data: policySet,
    loading,
    error
  } = useGetPolicySet({
    queryParams: {
      accountIdentifier,
      ...((policySetType === PolicySetType.ORG || policySetType === PolicySetType.PROJECT) && { orgIdentifier }),
      ...(policySetType === PolicySetType.PROJECT && { projectIdentifier })
    },
    policyset: policySetId
  })
 
  const onDelete = () => {
    deletePolicySet(policySetId)
  }
 
  return (
    <Layout.Horizontal flex={{ justifyContent: 'flex-start', alignItems: 'center' }} margin={{ bottom: 'small' }}>
      <Layout.Horizontal
        className={css.policySetHolder}
        height={'36px'}
        width={'500px'}
        flex={{ justifyContent: 'space-between', alignItems: 'center' }}
        padding={{
          right: 'medium',
          left: 'medium'
        }}
        margin={{
          right: 'xsmall'
        }}
      >
        {loading ? (
          <Spinner size={Spinner.SIZE_SMALL} />
        ) : error ? (
          <Text lineClamp={1} color={Color.RED_800}>
            {getRBACErrorMessage(error as RBACError)}
          </Text>
        ) : policySet ? (
          <>
            <Text lineClamp={1} color={Color.BLACK}>
              {policySet.name}
            </Text>
            <Layout.Horizontal flex={{ justifyContent: 'flex-end', alignItems: 'center' }}>
              <MiniPoliciesRenderer policies={defaultTo(policySet.policies, [])} alignRight />
              <Text font={'small'} width={48}>
                {policySetType}
              </Text>
            </Layout.Horizontal>
          </>
        ) : null}
      </Layout.Horizontal>
      <Button icon="main-trash" onClick={onDelete} minimal />
    </Layout.Horizontal>
  )
}
 
interface MiniPoliciesRendererProps {
  policies: LinkedPolicy[]
  alignRight?: boolean
}
 
export function MiniPoliciesRenderer({ policies, alignRight }: MiniPoliciesRendererProps) {
  const length = policies.length
  // istanbul ignore else
  if (length === 0) {
    return null
  }
  const policyNames = policies.map(policy => policy.name)
 
  return (
    <Layout.Horizontal
      flex={{ justifyContent: alignRight ? 'flex-end' : 'flex-start', alignItems: 'center' }}
      margin={{ right: 'small' }}
    >
      {policyNames.slice(0, 2).map(policy => (
        <Text
          className={cx(css.styledPolicy, {
            [css.big]: length === 1
          })}
          key={policy}
          lineClamp={1}
        >
          {policy}
        </Text>
      ))}
      {length > 2 && (
        <Text
          className={css.styledPolicy}
          background={Color.GREY_100}
          alwaysShowTooltip
          tooltip={
            <Layout.Vertical padding="medium">
              {policyNames.splice(2).map(policy => (
                <Text
                  lineClamp={1}
                  color={Color.BLACK}
                  margin={length > 3 && { top: 'small', bottom: 'small' }}
                  style={{ maxWidth: '400px' }}
                  key={policy}
                >
                  {policy}
                </Text>
              ))}
            </Layout.Vertical>
          }
        >
          {`+${length - 2}`}
        </Text>
      )}
    </Layout.Horizontal>
  )
}