All files / modules/70-pipeline/components/PipelineStudio/PipelineCanvas/PipelineErrors PipelineErrorCard.tsx

100% Statements 16/16
91.67% Branches 11/12
100% Functions 4/4
100% Lines 16/16

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              9x 9x 9x 9x 9x 9x                   9x 59x   58x                                                                       62x                           58x 58x 58x 39x   19x         19x                                                    
/*
 * 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 { Popover, Button, ButtonSize, ButtonVariation, Card, Icon, IconName, Layout, Text } from '@harness/uicore'
import { Classes, Position, PopoverInteractionKind } from '@blueprintjs/core'
import { Color } from '@harness/design-system'
import { useStrings } from 'framework/strings'
import css from './PipelineErrors.module.scss'
 
interface PipelineErrorCardProps {
  icon: IconName
  title?: string
  errors: string[]
  onClick: () => void
  buttonText: string
}
 
export default function PipelineErrorCard(props: PipelineErrorCardProps): React.ReactElement {
  const [firstError, ...restErrors] = props.errors
 
  return (
    <Layout.Horizontal margin={{ bottom: 'xlarge' }}>
      <Card className={css.cardWithIcon}>
        <Icon name={props.icon} size={30} />
      </Card>
      <Layout.Vertical padding={{ left: 'medium' }} className={css.cardContent}>
        {props.title ? (
          <Text color={Color.BLACK} margin={{ bottom: 'small' }} font={{ size: 'normal', weight: 'bold' }}>
            {props.title}
          </Text>
        ) : null}
        <ErrorMessage errorText={firstError} />
        <ErrorMessageList errors={restErrors} />
        <Button
          className={css.fixButton}
          onClick={props.onClick}
          size={ButtonSize.SMALL}
          variation={ButtonVariation.PRIMARY}
          text={props.buttonText}
        />
      </Layout.Vertical>
    </Layout.Horizontal>
  )
}
 
function ErrorMessage({
  errorText,
  dark,
  shouldAddBottomMargin = true,
  lineClamp = 4
}: {
  errorText: string
  dark?: boolean
  shouldAddBottomMargin?: boolean
  lineClamp?: number
}): React.ReactElement {
  return (
    <Text
      color={dark ? Color.WHITE : Color.GREY_600}
      font={{ size: 'small', weight: 'semi-bold' }}
      lineClamp={lineClamp}
      margin={shouldAddBottomMargin ? { bottom: 'small' } : {}}
      tooltipProps={{ isDark: true }}
    >
      {errorText}
    </Text>
  )
}
 
function ErrorMessageList({ errors }: { errors: string[] }): React.ReactElement | null {
  const { getString } = useStrings()
  const errorsLength = errors.length
  if (errorsLength === 0) {
    return null
  }
  return (
    <Popover
      content={
        <div className={css.list}>
          {errors.map((error: string, index: number) => (
            <ErrorMessage
              key={`${error}_${index}`}
              dark
              errorText={error}
              shouldAddBottomMargin={index !== errorsLength - 1}
              lineClamp={10}
            />
          ))}
        </div>
      }
      interactionKind={PopoverInteractionKind.HOVER}
      popoverClassName={Classes.DARK}
      position={Position.RIGHT}
    >
      <Text
        color={Color.PRIMARY_7}
        font={{ size: 'small', weight: 'semi-bold' }}
        margin={{ bottom: 'small' }}
        width={100}
        className={css.moreText}
      >
        {`${getString(errorsLength > 1 ? 'cd.moreIssues' : 'cd.moreIssue', { count: errorsLength })} `}
      </Text>
    </Popover>
  )
}