All files / modules/70-pipeline/pages/utils/Badge Badge.tsx

95.45% Statements 21/22
46.15% Branches 6/13
100% Functions 4/4
95.45% Lines 21/22

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              73x 73x 73x 73x   73x   73x 73x 73x                                           14x 14x 14x         14x 14x                     14x                                                   73x                   117x 117x   117x 117x                   117x         117x                              
/*
 * 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 { Classes, Intent, PopoverInteractionKind, Position } from '@blueprintjs/core'
import { Container, Icon, Text, IconName, Popover, Layout } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import type { InputSetErrorResponse } from 'services/pipeline-ng'
import { useStrings } from 'framework/strings'
import type { StringsMap } from 'stringTypes'
import { getErrorsList } from '@pipeline/components/PipelineStudio/StepUtil'
import { getFormattedErrors } from '@pipeline/utils/runPipelineUtils'
import css from './Badge.module.scss'
 
export interface BadgeProps {
  text: keyof StringsMap
  iconName: IconName
  showTooltip?: boolean
  entityName?: string
  entityType: string
  uuidToErrorResponseMap?: { [key: string]: InputSetErrorResponse }
  overlaySetErrorDetails?: { [key: string]: string }
  showInvalidText?: boolean
}
 
interface BadgeTooltipContentInterface {
  iconName: IconName
  entityName: string
  entityType: string
  uuidToErrorResponseMap?: { [key: string]: InputSetErrorResponse }
  overlaySetErrorDetails?: { [key: string]: string }
}
 
function TooltipContent(props: BadgeTooltipContentInterface): React.ReactElement {
  const { iconName, entityName = '', entityType, uuidToErrorResponseMap, overlaySetErrorDetails } = props
  const { getString } = useStrings()
  const nonGitErrors = uuidToErrorResponseMap
    ? getFormattedErrors(uuidToErrorResponseMap)
    : overlaySetErrorDetails
    ? overlaySetErrorDetails
    : {}
  const { errorStrings: nonGitErrorStrings, errorCount: nonGitErrorsCount } = getErrorsList(nonGitErrors)
  return (
    <Container padding="medium">
      {nonGitErrorsCount ? (
        // Show non git errors
        // i.e. input set is invalid maybe because a pipeline was updated and runtimeinputs modified
        <Layout.Vertical spacing="medium">
          <Text color={Color.GREY_0}>
            {getString('common.errorCount' as keyof StringsMap, { count: nonGitErrorsCount })}
          </Text>
          <div>
            {nonGitErrorStrings.map((errorMessage, index) => (
              <Text width={480} lineClamp={3} color={Color.GREY_0} key={index} font={{ weight: 'semi-bold' }}>
                {errorMessage}
              </Text>
            ))}
          </div>
        </Layout.Vertical>
      ) : (
        // Show git errors
        <Layout.Horizontal>
          <div className={css.tooltipIcon}>
            <Icon name={iconName} size={18} intent={Intent.DANGER} />
          </div>
          <Layout.Vertical width={292} padding={{ left: 'small' }}>
            <Text width={244} color={Color.GREY_0} margin={{ bottom: 'small' }} className={css.tooltipContentText}>
              {getString('common.gitSync.outOfSync', { entityType, name: entityName })}
            </Text>
            <Text width={244} color={Color.GREY_0} className={css.tooltipContentText}>
              {getString('common.gitSync.fixAllErrors')}
            </Text>
          </Layout.Vertical>
        </Layout.Horizontal>
      )}
    </Container>
  )
}
 
export function Badge(props: BadgeProps): React.ReactElement {
  const {
    text,
    iconName,
    showTooltip,
    entityName = '',
    entityType,
    uuidToErrorResponseMap,
    overlaySetErrorDetails,
    showInvalidText = false
  } = props
  const { getString } = useStrings()
 
  const badgeUI = React.useCallback(() => {
    Iif (showInvalidText) {
      return (
        <Container className={css.badge}>
          <Icon name={iconName} size={14} className={css.badgeIcon} intent={Intent.DANGER} />
          <Text color={Color.RED_900} font={{ weight: 'bold' }} className={css.badgeText}>
            {getString(text)}
          </Text>
        </Container>
      )
    }
    return (
      <Icon name={iconName} size={20} className={css.badgeIcon} data-testid="invalid-icon" intent={Intent.DANGER} />
    )
  }, [iconName, showInvalidText, text, getString])
 
  return showTooltip ? (
    <Popover interactionKind={PopoverInteractionKind.HOVER} position={Position.BOTTOM} className={Classes.DARK}>
      {badgeUI()}
      <TooltipContent
        iconName={iconName}
        entityName={entityName}
        entityType={entityType}
        uuidToErrorResponseMap={uuidToErrorResponseMap}
        overlaySetErrorDetails={overlaySetErrorDetails}
      />
    </Popover>
  ) : (
    badgeUI()
  )
}