All files / modules/75-ce/components/OverviewPage OverviewTopRecommendations.tsx

96.77% Statements 30/31
83.33% Branches 15/18
100% Functions 5/5
96.77% Lines 30/31

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              2x 2x 2x 2x 2x           2x 2x 2x 2x   2x 2x                     2x 2x 2x 2x             2x 2x   2x       2x                                   5x                                   2x 5x 5x     5x   5x 5x             5x 5x           5x                                                   2x  
/*
 * 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, { useMemo } from 'react'
import { Link, useParams } from 'react-router-dom'
import { Container, Layout, Text } from '@wings-software/uicore'
import { Color, FontVariation } from '@harness/design-system'
import {
  K8sRecommendationFilterDtoInput,
  RecommendationItemDto,
  ResourceType,
  useRecommendationsQuery
} from 'services/ce/services'
import { useStrings } from 'framework/strings'
import routes from '@common/RouteDefinitions'
import formatCost from '@ce/utils/formatCost'
import EmptyView from '@ce/images/empty-state.svg'
import type { AccountPathProps } from '@common/interfaces/RouteInterfaces'
import { Loader } from './OverviewPageLayout'
import css from './OverviewPage.module.scss'
 
type RouteFn = (
  params: {
    recommendation: string
    recommendationName: string
  } & {
    accountId: string
  }
) => string
 
const OverviewTopRecommendations = () => {
  const { getString } = useStrings()
  const pathParams = useParams<AccountPathProps>()
  const [result] = useRecommendationsQuery({
    requestPolicy: 'network-only',
    variables: {
      filter: { offset: 0, limit: 20, minSaving: 0 } as K8sRecommendationFilterDtoInput
    }
  })
 
  const { data, fetching } = result
  const recommendationItems = data?.recommendationsV2?.items || []
 
  Iif (fetching) {
    return <Loader />
  }
 
  return (
    <div className={css.topRecommendations}>
      <Layout.Vertical spacing="medium">
        <Layout.Horizontal style={{ justifyContent: 'space-between', alignItems: 'center' }}>
          <Text color="grey800" font={{ weight: 'semi-bold', size: 'medium' }}>
            {getString('ce.overview.cardtitles.topRecommendation')}
          </Text>
          {recommendationItems.length ? (
            <Link to={routes.toCERecommendations({ ...pathParams })}>
              <Text inline color="primary7">
                {getString('ce.overview.seeAll')}
              </Text>
            </Link>
          ) : null}
        </Layout.Horizontal>
        {recommendationItems.length ? (
          <div className={css.recommendations}>
            {recommendationItems.map((rec, idx) => {
              return <Recommendation key={idx} data={rec as RecommendationItemDto} />
            })}
          </div>
        ) : (
          <Container className={css.noDataContainer}>
            <img className={css.noDataImg} src={EmptyView} />
            <Text className={css.noDataText}>{getString('ce.pageErrorMsg.noRecommendations')}</Text>
          </Container>
        )}
      </Layout.Vertical>
    </div>
  )
}
 
interface RecommendationProps {
  data: RecommendationItemDto
}
 
const Recommendation = (props: RecommendationProps) => {
  const { getString } = useStrings()
  const { accountId } = useParams<AccountPathProps>()
  const {
    data: { id, resourceName, resourceType }
  } = props
 
  const map: Record<string, string> = useMemo(
    () => ({
      [ResourceType.Workload]: getString('ce.overview.workload'),
      [ResourceType.NodePool]: getString('ce.overview.nodepool')
    }),
    []
  )
 
  const resourceTypeToRoute: Record<ResourceType, RouteFn> = useMemo(() => {
    return {
      [ResourceType.Workload]: routes.toCERecommendationDetails,
      [ResourceType.NodePool]: routes.toCENodeRecommendationDetails
    }
  }, [])
 
  return (
    <Link
      to={resourceTypeToRoute[resourceType]({ accountId, recommendation: id, recommendationName: resourceName || id })}
    >
      <div className={css.recommendation}>
        <Layout.Vertical spacing="xsmall">
          <Container>
            <Text inline className={css.resourceType}>
              {map[props.data.resourceType]}
            </Text>
          </Container>
          <Text color={Color.GREY_800} lineClamp={1}>
            {props.data.resourceName}
          </Text>
        </Layout.Vertical>
        <Layout.Vertical spacing="xsmall" style={{ alignItems: 'flex-end' }}>
          <Text color={Color.GREEN_700}>{formatCost(props.data.monthlySaving as number)}</Text>
          <Text font={{ variation: FontVariation.SMALL }} color={Color.GREY_400}>
            {getString('ce.overview.savings')}
          </Text>
        </Layout.Vertical>
      </div>
    </Link>
  )
}
 
export default OverviewTopRecommendations