All files / modules/75-ce/components/PerspectiveSummary RecommendationSummaryCard.tsx

68.57% Statements 24/35
31.58% Branches 12/38
50% Functions 1/2
68.57% Lines 24/35

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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172              3x 3x 3x 3x 3x 3x 3x 3x 3x 3x         3x 3x             3x 5x           5x   5x   5x                     5x                   5x   5x                                                                 5x 3x                                                 2x                                                                                               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 qs from 'qs'
import { Card, Container, Icon, Text, Layout, FlexExpander } from '@wings-software/uicore'
import { useParams, useHistory } from 'react-router-dom'
import cx from 'classnames'
import { FontVariation, Color } from '@harness/design-system'
import { useStrings } from 'framework/strings'
import routes from '@common/RouteDefinitions'
import formatCost from '@ce/utils/formatCost'
import {
  K8sRecommendationFilterDtoInput,
  usePerspectiveRecommendationsQuery,
  RecommendationItemDto
} from 'services/ce/services'
import { CCM_PAGE_TYPE } from '@ce/types'
import css from './PerspectiveSummary.module.scss'
 
interface RecommendationSummaryCardProps {
  filters: K8sRecommendationFilterDtoInput
  pageType?: CCM_PAGE_TYPE
}
 
const RecommendationSummaryCard: (props: RecommendationSummaryCardProps) => JSX.Element = ({ filters, pageType }) => {
  const { perspectiveId, accountId, perspectiveName } = useParams<{
    perspectiveId: string
    accountId: string
    perspectiveName: string
  }>()
 
  const { getString } = useStrings()
 
  const history = useHistory()
 
  const [{ data, fetching: recommendationFetching }] = usePerspectiveRecommendationsQuery({
    variables: {
      filter: {
        ...filters,
        minSaving: 0,
        offset: 0,
        limit: 10
      } as unknown as K8sRecommendationFilterDtoInput
    }
  })
 
  Iif (recommendationFetching) {
    return (
      <Card elevation={1} interactive={false}>
        <Container className={cx(css.mainCard, css.loadingContainer)}>
          <Icon name="spinner" color="blue500" size={30} />
        </Container>
      </Card>
    )
  }
 
  const recommendationData = data?.recommendationStatsV2
 
  const nagvigateToRecommendations: () => void = () => {
    const recommendationsDetails = (data?.recommendationsV2?.items || []) as RecommendationItemDto[]
 
    const queryString: Record<string, any> = {
      perspectiveId,
      perspectiveName
    }
    if (pageType === CCM_PAGE_TYPE.Workload) {
      queryString['filters'] = filters
      queryString['origin'] = pageType
    }
    if (recommendationsDetails.length === 1 && recommendationData?.count === 1) {
      const recommendationId = recommendationsDetails[0].id
      const recommendationName = recommendationsDetails[0].resourceName || recommendationId
 
      recommendationId &&
        history.push({
          pathname: routes.toCERecommendationDetails({
            accountId,
            recommendation: recommendationId,
            recommendationName: recommendationName
          })
        })
    } else {
      history.push({
        pathname: routes.toCERecommendations({
          accountId
        }),
        search: qs.stringify(queryString)
      })
    }
  }
 
  if (!recommendationData || !recommendationData?.count) {
    return (
      <Card elevation={1}>
        <Container className={css.mainCard}>
          <Text color={Color.GREY_500} font={{ variation: FontVariation.SMALL }}>
            {getString('ce.recommendation.sideNavText')}
          </Text>
          <Text
            margin={{
              top: 'medium',
              bottom: 'small'
            }}
            iconProps={{ size: 21 }}
            font={{ variation: FontVariation.BODY1 }}
            icon="money-icon"
          >
            $--
          </Text>
          <Text color={Color.GREY_600} font={{ variation: FontVariation.TINY }}>
            no Recommendations yet
          </Text>
        </Container>
      </Card>
    )
  }
 
  return (
    <Card elevation={1} interactive={false}>
      <Container className={css.mainCard}>
        <Layout.Horizontal>
          <Text color={Color.GREY_500} font={{ variation: FontVariation.SMALL }}>
            {getString('ce.recommendation.sideNavText')}
          </Text>
          <FlexExpander />
          <Text
            className={css.viewLink}
            color={Color.PRIMARY_7}
            font={{ variation: FontVariation.SMALL }}
            onClick={nagvigateToRecommendations}
          >
            {getString('ce.perspectives.recommendations.viewText')}
          </Text>
        </Layout.Horizontal>
 
        <Text
          color={Color.GREY_400}
          margin={{
            top: 'xsmall'
          }}
          font={{ variation: FontVariation.TINY }}
        >
          {getString('ce.perspectives.recommendations.recommendationCountTxt', {
            count: recommendationData.count
          })}
        </Text>
        <Text
          margin={{
            top: 'xsmall',
            bottom: 'xsmall'
          }}
          iconProps={{ size: 21 }}
          font={{ variation: FontVariation.BODY1 }}
          icon="money-icon"
        >
          {formatCost(recommendationData.totalMonthlySaving)}
        </Text>
        <Text color={Color.GREY_400} font={{ variation: FontVariation.TINY }}>
          {getString('ce.perspectives.recommendations.perMonth')}
        </Text>
      </Container>
    </Card>
  )
}
 
export default RecommendationSummaryCard