All files / modules/45-projects-orgs/components/OverviewGlanceCards OverviewGlanceCards.tsx

94.74% Statements 54/57
53.23% Branches 33/62
100% Functions 5/5
94.74% Lines 54/57

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 173 174 175 176 177 178 179 180              3x 3x 3x 3x 3x   3x 3x 3x     3x 3x   3x 3x 3x 3x 3x                       3x 3x 3x 3x   3x       32x   32x       32x 32x   8x         8x   8x       8x   8x       8x   8x           32x 32x 16x     32x     3x 32x 32x 32x                           3x 8x 8x 8x 8x   8x           8x                   8x 4x 4x                         8x       8x                 8x   8x                                         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, { useEffect, useState } from 'react'
import { Card, Icon, Layout } from '@wings-software/uicore'
import { useParams } from 'react-router-dom'
import { Color } from '@harness/design-system'
import GlanceCard, { GlanceCardProps } from '@common/components/GlanceCard/GlanceCard'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { CountChangeDetails, ResponseExecutionResponseCountOverview, useGetCounts } from 'services/dashboard-service'
import { TimeRangeToDays, useLandingDashboardContext } from '@common/factories/LandingDashboardContext'
import { useStrings } from 'framework/strings'
import type { StringsMap } from 'stringTypes'
import type { UseGetMockData } from '@common/utils/testUtils'
import DashboardAPIErrorWidget from '../DashboardAPIErrorWidget/DashboardAPIErrorWidget'
import css from './OverviewGlanceCards.module.scss'
 
enum OverviewGalanceCard {
  PROJECT = 'PROJECT',
  SERVICES = 'SERVICES',
  ENV = 'ENV',
  PIPELINES = 'PIPELINES'
}
 
interface RenderGlanceCardData extends Omit<GlanceCardProps, 'title'> {
  title: keyof StringsMap
}
 
interface RenderGlanceCardProps {
  loading: boolean
  data: RenderGlanceCardData
}
 
const projectsTitleId = 'projectsText'
const serviceTitleId = 'services'
const envTitleId = 'environments'
const pipelineTitleId = 'pipelines'
 
const getDataForCard = (
  cardType: OverviewGalanceCard,
  countDetails: CountChangeDetails | undefined
): RenderGlanceCardData => {
  let glanceCardData: RenderGlanceCardData = { title: 'na', iconName: 'placeholder' }
 
  Iif (!countDetails) {
    return glanceCardData
  }
 
  const countChange = countDetails.countChangeAndCountChangeRateInfo?.countChange
  switch (cardType) {
    case OverviewGalanceCard.PROJECT:
      glanceCardData = {
        title: projectsTitleId,
        iconName: 'nav-project',
        iconSize: 16
      }
      break
    case OverviewGalanceCard.SERVICES:
      glanceCardData = {
        title: serviceTitleId,
        iconName: 'services'
      }
      break
    case OverviewGalanceCard.ENV:
      glanceCardData = {
        title: envTitleId,
        iconName: 'infrastructure'
      }
      break
    case OverviewGalanceCard.PIPELINES:
      glanceCardData = {
        title: pipelineTitleId,
        iconName: 'pipeline',
        iconSize: 38
      }
  }
  glanceCardData.number = countDetails.count
  if (countChange) {
    glanceCardData.delta = countChange > 0 ? `+${countChange.toString()}` : countChange.toString()
  }
 
  return glanceCardData
}
 
const RenderGlanceCard: React.FC<RenderGlanceCardProps> = props => {
  const { loading, data } = props
  const { getString } = useStrings()
  return loading ? (
    <Card className={css.loadingWrapper}>
      <Icon name="spinner" size={24} color={Color.PRIMARY_7} />
    </Card>
  ) : (
    <GlanceCard {...data} styling={data.title === projectsTitleId} title={getString(data.title)} />
  )
}
 
export interface OverviewGlanceCardsProp {
  glanceCardData: ResponseExecutionResponseCountOverview
  mockData?: UseGetMockData<ResponseExecutionResponseCountOverview>
}
 
const OverviewGlanceCards: React.FC<OverviewGlanceCardsProp> = props => {
  const { glanceCardData } = props
  const { accountId } = useParams<ProjectPathProps>()
  const { selectedTimeRange } = useLandingDashboardContext()
  const [range] = useState([Date.now() - TimeRangeToDays[selectedTimeRange] * 24 * 60 * 60000, Date.now()])
  const [pageLoadGlanceCardData, setPageLoadGlanceCardData] =
    React.useState<ResponseExecutionResponseCountOverview | null>(glanceCardData)
  const {
    data: countResponse,
    loading,
    error,
    refetch
  } = useGetCounts({
    queryParams: {
      accountIdentifier: accountId,
      startTime: range[0],
      endTime: range[1]
    },
    lazy: true,
    mock: props.mockData
  })
 
  useEffect(() => {
    Eif (pageLoadGlanceCardData) {
      setPageLoadGlanceCardData(null)
    } else {
      refetch({
        queryParams: {
          accountIdentifier: accountId,
          startTime: Date.now() - TimeRangeToDays[selectedTimeRange] * 24 * 60 * 60000,
          endTime: Date.now()
        }
      })
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [selectedTimeRange])
 
  const hasAPIFailed = !(
    countResponse?.data?.executionStatus === 'SUCCESS' || glanceCardData?.data?.executionStatus === 'SUCCESS'
  )
 
  Iif (!loading && (error || hasAPIFailed)) {
    return (
      <Card className={css.errorCard}>
        <DashboardAPIErrorWidget callback={refetch} iconProps={{ size: 75 }}></DashboardAPIErrorWidget>
      </Card>
    )
  }
 
  const { projectsCountDetail, envCountDetail, servicesCountDetail, pipelinesCountDetail } =
    countResponse?.data?.response || glanceCardData?.data?.response || {}
 
  return (
    <Layout.Horizontal spacing="large">
      <Layout.Vertical spacing="large">
        <RenderGlanceCard loading={!!loading} data={getDataForCard(OverviewGalanceCard.PROJECT, projectsCountDetail)} />
        <RenderGlanceCard loading={!!loading} data={getDataForCard(OverviewGalanceCard.ENV, envCountDetail)} />
      </Layout.Vertical>
      <Layout.Vertical spacing="large">
        <RenderGlanceCard
          loading={!!loading}
          data={getDataForCard(OverviewGalanceCard.SERVICES, servicesCountDetail)}
        />
 
        <RenderGlanceCard
          loading={!!loading}
          data={getDataForCard(OverviewGalanceCard.PIPELINES, pipelinesCountDetail)}
        />
      </Layout.Vertical>
    </Layout.Horizontal>
  )
}
 
export default OverviewGlanceCards