All files / modules/85-cv/pages/slos/CVSLODetailsPage CVSLODetailsPage.tsx

92.5% Statements 37/40
91.67% Branches 22/24
83.33% Functions 5/6
92.5% Lines 37/40

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              8x 8x 8x 8x 8x 8x 8x   8x 8x 8x 8x 8x 8x 8x 8x   8x 5x 5x 5x       5x   5x 5x 4x         4x               5x                 5x                 5x               5x 1x 1x                       5x 5x   5x             5x                                       1x                       1x                                                       8x  
/*
 * 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, { useEffect, useRef } from 'react'
import { useParams, useHistory } from 'react-router-dom'
import { Container, FlexExpander, Page, Tabs } from '@harness/uicore'
import { useStrings } from 'framework/strings'
import { useDeleteSLOData, useGetSLODetails, useResetErrorBudget } from 'services/cv'
import routes from '@common/RouteDefinitions'
import { useQueryParams } from '@common/hooks'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { NGBreadcrumbs } from '@common/components/NGBreadcrumbs/NGBreadcrumbs'
import { getErrorMessage, getSearchString } from '@cv/utils/CommonUtils'
import CVCreateSLO from '@cv/pages/slos/components/CVCreateSLO/CVCreateSLO'
import HeaderTitle from './views/HeaderTitle'
import DetailsPanel from './DetailsPanel/DetailsPanel'
import TabToolbar from './DetailsPanel/views/TabToolbar'
import { SLODetailsPageTabIds } from './CVSLODetailsPage.types'
import css from './CVSLODetailsPage.module.scss'
 
const CVSLODetailsPage: React.FC = () => {
  const history = useHistory()
  const { getString } = useStrings()
  const { accountId, orgIdentifier, projectIdentifier, identifier } = useParams<
    ProjectPathProps & { identifier: string }
  >()
  const { tab = SLODetailsPageTabIds.Details, monitoredServiceIdentifier } =
    useQueryParams<{ tab?: SLODetailsPageTabIds; monitoredServiceIdentifier?: string }>()
 
  const projectIdentifierRef = useRef<string>()
  useEffect(() => {
    Iif (projectIdentifierRef.current && projectIdentifierRef.current !== projectIdentifier) {
      history.push(routes.toCVSLOs({ accountId, orgIdentifier, projectIdentifier }))
      return
    }
 
    projectIdentifierRef.current = projectIdentifier
  }, [accountId, orgIdentifier, projectIdentifier, history])
 
  const {
    data,
    loading: sloDetailsLoading,
    error,
    refetch
  } = useGetSLODetails({
    identifier,
    queryParams: {
      accountId,
      orgIdentifier,
      projectIdentifier
    }
  })
 
  const { mutate: resetErrorBudget, loading: resetErrorBudgetLoading } = useResetErrorBudget({
    identifier: '',
    queryParams: {
      accountId,
      orgIdentifier,
      projectIdentifier
    }
  })
 
  const { mutate: deleteSLO, loading: deleteSLOLoading } = useDeleteSLOData({
    queryParams: {
      accountId,
      orgIdentifier,
      projectIdentifier
    }
  })
 
  const onTabChange = (nextTab: SLODetailsPageTabIds): void => {
    /* istanbul ignore else */ if (nextTab !== tab) {
      history.push({
        pathname: routes.toCVSLODetailsPage({
          identifier,
          accountId,
          orgIdentifier,
          projectIdentifier
        }),
        search: getSearchString({ tab: nextTab, monitoredServiceIdentifier })
      })
    }
  }
 
  const { description, sloDashboardWidget } = data?.data ?? {}
  const loading = sloDetailsLoading || resetErrorBudgetLoading || deleteSLOLoading
 
  const breadcrumbLinks = [
    {
      url: routes.toCVSLOs({ accountId, orgIdentifier, projectIdentifier }),
      label: getString('cv.SLO')
    }
  ]
 
  return (
    <>
      <Page.Header
        size="large"
        title={<HeaderTitle loading={sloDetailsLoading} title={sloDashboardWidget?.title} description={description} />}
        breadcrumbs={<NGBreadcrumbs links={breadcrumbLinks} />}
      />
      <Container className={css.tabContainer}>
        <Tabs
          id="slo-details-page-tabs"
          selectedTabId={tab}
          onChange={onTabChange}
          tabList={[
            {
              id: SLODetailsPageTabIds.Details,
              title: getString('details'),
              panel: (
                <DetailsPanel
                  loading={loading}
                  errorMessage={getErrorMessage(error)}
                  retryOnError={() => refetch()}
                  sloDashboardWidget={sloDashboardWidget}
                />
              )
            },
            {
              id: SLODetailsPageTabIds.Configurations,
              title: getString('cv.monitoredServices.monitoredServiceTabs.configurations'),
              panel: (
                <Page.Body
                  loading={loading}
                  error={getErrorMessage(error)}
                  retryOnError={() => refetch()}
                  noData={{
                    when: () => !sloDashboardWidget
                  }}
                  className={css.pageBody}
                >
                  <CVCreateSLO />
                </Page.Body>
              )
            }
          ]}
        >
          <FlexExpander />
          {tab === SLODetailsPageTabIds.Details && sloDashboardWidget && (
            <TabToolbar
              sloDashboardWidget={sloDashboardWidget}
              resetErrorBudget={resetErrorBudget}
              deleteSLO={deleteSLO}
              refetchSLODetails={refetch}
              onTabChange={onTabChange}
            />
          )}
        </Tabs>
      </Container>
    </>
  )
}
 
export default CVSLODetailsPage