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

100% Statements 55/55
100% Branches 8/8
100% Functions 11/11
100% Lines 55/55

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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232              8x 8x 8x                     8x 8x 8x   8x 8x 8x 8x 8x 8x 8x 8x 8x 8x   8x   8x             19x 19x 19x 19x 19x 19x   19x   19x 1x 1x 1x   1x           19x                           1x 1x   1x       19x   1x 1x       19x           19x 2x 2x   2x   2x 1x                 1x                 19x               2x 2x         19x               1x                                   1x                                   1x                                 1x                                 2x                             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, { useState } from 'react'
import { useHistory, useParams } from 'react-router-dom'
import {
  ButtonVariation,
  Color,
  Intent,
  Text,
  Layout,
  useConfirmationDialog,
  useToaster,
  ButtonSize
} from '@harness/uicore'
import type { SLOErrorBudgetResetDTO } from 'services/cv'
import { useStrings } from 'framework/strings'
import { useQueryParams } from '@common/hooks'
import routes from '@common/RouteDefinitions'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { ResourceType } from '@rbac/interfaces/ResourceType'
import { PermissionIdentifier } from '@rbac/interfaces/PermissionIdentifier'
import RbacButton from '@rbac/components/Button/Button'
import ReviewChangeSVG from '@cv/assets/sloReviewChange.svg'
import { getErrorMessage } from '@cv/utils/CommonUtils'
import { useErrorBudgetRestHook } from '@cv/hooks/useErrorBudgetRestHook/useErrorBudgetRestHook'
import { useLogContentHook } from '@cv/hooks/useLogContentHook/useLogContentHook'
import { LogTypes } from '@cv/hooks/useLogContentHook/useLogContentHook.types'
import { PeriodTypeEnum } from '@cv/pages/slos/components/CVCreateSLO/components/CreateSLOForm/components/SLOTargetAndBudgetPolicy/SLOTargetAndBudgetPolicy.constants'
import { SLODetailsPageTabIds } from '../../CVSLODetailsPage.types'
import type { TabToolbarProps } from '../DetailsPanel.types'
import css from '../DetailsPanel.module.scss'
 
const TabToolbar: React.FC<TabToolbarProps> = ({
  sloDashboardWidget,
  resetErrorBudget,
  deleteSLO,
  refetchSLODetails,
  onTabChange
}) => {
  const history = useHistory()
  const { getString } = useStrings()
  const { showSuccess, showError } = useToaster()
  const { accountId, orgIdentifier, projectIdentifier } = useParams<ProjectPathProps>()
  const { monitoredServiceIdentifier } = useQueryParams<{ monitoredServiceIdentifier?: string }>()
  const [errorBudgetResetData, setErrorBudgetResetData] = useState<SLOErrorBudgetResetDTO | null>()
 
  const { sloIdentifier, title, serviceName, environmentName } = sloDashboardWidget
 
  const onResetErrorBudget = async (formData: SLOErrorBudgetResetDTO): Promise<void> => {
    try {
      await resetErrorBudget(formData, { pathParams: { identifier: sloIdentifier } })
      await refetchSLODetails()
 
      showSuccess(getString('cv.errorBudgetIsSuccessfullyReset'))
    } catch (e) {
      /* istanbul ignore next */ showError(getErrorMessage(e))
    }
  }
 
  const { openDialog: confirmReviewChanges } = useConfirmationDialog({
    intent: Intent.WARNING,
    titleText: getString('cv.slos.reviewChanges'),
    contentText: (
      <Layout.Horizontal padding={{ right: 'xxlarge' }}>
        <Text color={Color.GREY_800}>{getString('cv.slos.sloEditWarningMessage')}</Text>
        <div>
          <img src={ReviewChangeSVG} width="145px" height="200px" alt="" />
        </div>
      </Layout.Horizontal>
    ),
    confirmButtonText: getString('common.ok'),
    cancelButtonText: getString('cancel'),
    onCloseDialog: isConfirmed => {
      /* istanbul ignore else */ if (isConfirmed && errorBudgetResetData) {
        onResetErrorBudget(errorBudgetResetData)
      }
      setErrorBudgetResetData(null)
    }
  })
 
  const { openErrorBudgetReset } = useErrorBudgetRestHook({
    onSuccess: values => {
      setErrorBudgetResetData(values)
      confirmReviewChanges()
    }
  })
 
  const { openLogContentHook } = useLogContentHook({
    sloIdentifier,
    serviceName,
    envName: environmentName
  })
 
  const onDelete = async (): Promise<void> => {
    try {
      await deleteSLO(sloIdentifier)
 
      showSuccess(getString('cv.slos.sloDeleted', { name: title }))
 
      if (monitoredServiceIdentifier) {
        history.push({
          pathname: routes.toCVAddMonitoringServicesEdit({
            accountId,
            orgIdentifier,
            projectIdentifier,
            identifier: monitoredServiceIdentifier
          })
        })
      } else {
        history.push({
          pathname: routes.toCVSLOs({ accountId, orgIdentifier, projectIdentifier })
        })
      }
    } catch (e) {
      /* istanbul ignore next */ showError(getErrorMessage(e))
    }
  }
 
  const { openDialog: confirmDeleteSLO } = useConfirmationDialog({
    titleText: getString('common.delete', { name: title }),
    contentText: <Text color={Color.GREY_800}>{getString('cv.slos.confirmDeleteSLO', { name: title })}</Text>,
    confirmButtonText: getString('delete'),
    cancelButtonText: getString('cancel'),
    intent: Intent.DANGER,
    buttonIntent: Intent.DANGER,
    onCloseDialog: (isConfirmed: boolean) => {
      /* istanbul ignore else */ if (isConfirmed) {
        onDelete()
      }
    }
  })
 
  return (
    <>
      <RbacButton
        icon="Edit"
        withoutCurrentColor
        text={getString('edit')}
        size={ButtonSize.SMALL}
        iconProps={{ color: Color.GREY_700, size: 14 }}
        onClick={() => onTabChange(SLODetailsPageTabIds.Configurations)}
        variation={ButtonVariation.LINK}
        className={css.tabLink}
        permission={{
          permission: PermissionIdentifier.EDIT_SLO_SERVICE,
          resource: {
            resourceType: ResourceType.SLO,
            resourceIdentifier: projectIdentifier
          }
        }}
      />
      {sloDashboardWidget.sloTargetType === PeriodTypeEnum.CALENDAR && (
        <RbacButton
          icon="reset"
          withoutCurrentColor
          size={ButtonSize.SMALL}
          text={getString('cv.resetErrorBudget')}
          iconProps={{ color: Color.GREY_700, size: 12 }}
          onClick={() => openErrorBudgetReset(sloDashboardWidget)}
          variation={ButtonVariation.LINK}
          className={css.tabLink}
          permission={{
            permission: PermissionIdentifier.EDIT_SLO_SERVICE,
            resource: {
              resourceType: ResourceType.SLO,
              resourceIdentifier: projectIdentifier
            }
          }}
        />
      )}
      <RbacButton
        icon="audit-trail"
        withoutCurrentColor
        size={ButtonSize.SMALL}
        iconProps={{ size: 17 }}
        text={getString('cv.executionLogs')}
        onClick={() => openLogContentHook(LogTypes.ExecutionLog)}
        variation={ButtonVariation.LINK}
        className={css.tabLink}
        permission={{
          permission: PermissionIdentifier.VIEW_SLO_SERVICE,
          resource: {
            resourceType: ResourceType.SLO,
            resourceIdentifier: projectIdentifier
          }
        }}
      />
      <RbacButton
        icon="api-docs"
        withoutCurrentColor
        size={ButtonSize.SMALL}
        text={getString('cv.externalAPICalls')}
        iconProps={{ color: Color.GREY_700, size: 18 }}
        onClick={() => openLogContentHook(LogTypes.ApiCallLog)}
        variation={ButtonVariation.LINK}
        className={css.tabLink}
        permission={{
          permission: PermissionIdentifier.VIEW_SLO_SERVICE,
          resource: {
            resourceType: ResourceType.SLO,
            resourceIdentifier: projectIdentifier
          }
        }}
      />
      <RbacButton
        icon="trash"
        withoutCurrentColor
        size={ButtonSize.SMALL}
        text={getString('delete')}
        iconProps={{ color: Color.GREY_700, size: 13 }}
        onClick={() => confirmDeleteSLO()}
        variation={ButtonVariation.LINK}
        className={css.tabLink}
        permission={{
          permission: PermissionIdentifier.DELETE_SLO_SERVICE,
          resource: {
            resourceType: ResourceType.SLO,
            resourceIdentifier: projectIdentifier
          }
        }}
      />
    </>
  )
}
 
export default TabToolbar