All files / modules/75-cd/components/Environments/EnvironmentList EnvironmentListingPageTemplate.tsx

100% Statements 26/26
95% Branches 19/20
100% Functions 5/5
100% Lines 26/26

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              2x 2x 2x 2x 2x 2x 2x                                   2x 7x               2x                       9x 9x   9x 9x 9x 9x     9x 9x 1x 8x 1x     7x     9x   7x       9x                                                         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, { ReactNode, useMemo, FC } from 'react'
import { Breadcrumb, Page, FontVariation, Heading, HarnessDocTooltip } from '@harness/uicore'
import { NGBreadcrumbs } from '@common/components/NGBreadcrumbs/NGBreadcrumbs'
import { ContainerSpinner } from '@common/components/ContainerSpinner/ContainerSpinner'
import { useDocumentTitle } from '@common/hooks/useDocumentTitle'
import useRBACError, { RBACError } from '@rbac/utils/useRBACError/useRBACError'
import css from './EnvironmentsList.module.scss'
 
interface EnvironmentPageHeadingProps {
  tooltipId?: string
}
 
export interface EnvironmentPageTemplateProps {
  breadcrumbs?: Breadcrumb[]
  title: string
  titleTooltipId?: string
  headerContent?: ReactNode
  toolbar?: ReactNode
  pagination?: ReactNode
  loading?: boolean
  error?: unknown
  retryOnError?: () => void
}
 
const EnvironmentPageHeading: FC<EnvironmentPageHeadingProps> = ({ tooltipId, children }) => {
  return (
    <Heading level={3} font={{ variation: FontVariation.H4 }} data-tooltip-id={tooltipId}>
      {children}
      <HarnessDocTooltip tooltipId={tooltipId} useStandAlone />
    </Heading>
  )
}
 
const EnvironmentListingPageTemplate: React.FC<EnvironmentPageTemplateProps> = ({
  breadcrumbs,
  title,
  titleTooltipId,
  headerContent,
  toolbar,
  pagination,
  error,
  retryOnError,
  loading,
  children
}) => {
  useDocumentTitle(title)
  const { getRBACErrorMessage } = useRBACError()
 
  enum STATUS {
    'loading',
    'error',
    'ok'
  }
 
  const state = useMemo<STATUS>(() => {
    if (error) {
      return STATUS.error
    } else if (loading) {
      return STATUS.loading
    }
 
    return STATUS.ok
  }, [error, loading, STATUS])
 
  const headerTitle = useMemo<ReactNode>(
    () =>
      titleTooltipId ? <EnvironmentPageHeading tooltipId={titleTooltipId}>{title}</EnvironmentPageHeading> : title,
    [title, titleTooltipId]
  )
 
  return (
    <main className={css.layout}>
      <Page.Header
        title={headerTitle}
        breadcrumbs={<NGBreadcrumbs customPathParams={{ module: 'cd' }} links={breadcrumbs} />}
        className={css.header}
        content={headerContent}
      />
 
      {toolbar && <Page.SubHeader className={css.toolbar}>{toolbar}</Page.SubHeader>}
 
      <div className={css.content}>
        {state === STATUS.error && (
          <Page.Error message={getRBACErrorMessage(error as RBACError) as string} onClick={retryOnError} />
        )}
        {state === STATUS.ok && children}
      </div>
 
      {state === STATUS.ok && pagination && <footer className={css.footer}>{pagination}</footer>}
 
      {state === STATUS.loading && !error && (
        <div className={css.loading}>
          <ContainerSpinner />
        </div>
      )}
    </main>
  )
}
 
export default EnvironmentListingPageTemplate