All files / modules/75-cf/components/ListingPageTemplate ListingPageTemplate.tsx

100% Statements 24/24
100% Branches 20/20
100% Functions 4/4
100% Lines 24/24

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              7x 7x 7x 7x 7x 7x 7x   7x                           7x                       45x   45x 45x 45x 45x     45x 45x 9x 36x 14x     22x     45x 39x       45x                                                     7x  
/*
 * 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 } from 'react'
import { Breadcrumb, Page } from '@harness/uicore'
import { getErrorMessage } from '@cf/utils/CFUtils'
import { NGBreadcrumbs } from '@common/components/NGBreadcrumbs/NGBreadcrumbs'
import { ContainerSpinner } from '@common/components/ContainerSpinner/ContainerSpinner'
import { useDocumentTitle } from '@common/hooks/useDocumentTitle'
import ListingPageHeading from './ListingPageHeading'
 
import css from './ListingPageTemplate.module.scss'
 
export interface ListingPageTemplateProps {
  breadcrumbs?: Breadcrumb[]
  title: string
  titleTooltipId?: string
  headerContent?: ReactNode
  toolbar?: ReactNode
  pagination?: ReactNode
  loading?: boolean
  error?: unknown
  retryOnError?: () => void
}
 
const ListingPageTemplate: React.FC<ListingPageTemplateProps> = ({
  breadcrumbs,
  title,
  titleTooltipId,
  headerContent,
  toolbar,
  pagination,
  error,
  retryOnError,
  loading,
  children
}) => {
  useDocumentTitle(title)
 
  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 ? <ListingPageHeading tooltipId={titleTooltipId}>{title}</ListingPageHeading> : title),
    [title, titleTooltipId]
  )
 
  return (
    <main className={css.layout}>
      <Page.Header
        title={headerTitle}
        breadcrumbs={<NGBreadcrumbs customPathParams={{ module: 'cf' }} 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={getErrorMessage(error)} 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 ListingPageTemplate