All files / modules/10-common/pages/GenericError GenericErrorPage.tsx

100% Statements 23/23
100% Branches 9/9
100% Functions 3/3
100% Lines 23/23

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              1070x 1070x 1070x 1070x 1070x 1070x     1070x 1070x 1070x                           1070x 4x 4x   4x   1x               2x             1x       1070x 4x   4x 1x 1x     4x                 1070x  
/*
 * 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 from 'react'
import { Container, Text, Icon, Layout } from '@harness/uicore'
import { useParams, Link } from 'react-router-dom'
import { useQueryParams } from '@common/hooks'
import routes from '@common/RouteDefinitions'
import { useStrings } from 'framework/strings'
import type { AccountPathProps } from '@common/interfaces/RouteInterfaces'
 
export enum GENERIC_ERROR_CODES {
  INVITE_EXPIRED = 'INVITE_EXPIRED',
  UNAUTHORIZED = 'UNAUTHORIZED'
}
interface GenericErrorPageQueryParams {
  code?: GENERIC_ERROR_CODES
  message?: string
}
 
interface GenericErrorPageProps {
  code?: GENERIC_ERROR_CODES
  message?: string
}
 
type ErrorProps = GenericErrorPageQueryParams
 
const Error: React.FC<ErrorProps> = ({ code, message }) => {
  const { accountId } = useParams<AccountPathProps>()
  const { getString } = useStrings()
 
  switch (code) {
    case GENERIC_ERROR_CODES.INVITE_EXPIRED:
      return (
        <>
          <Text>{getString('common.genericErrors.inviteExpired')}</Text>
          <Link to={routes.toHome({ accountId })}>{getString('goToHome')}</Link>
          <Icon name="harness-logo-black" size={200} />
        </>
      )
    case GENERIC_ERROR_CODES.UNAUTHORIZED:
      return (
        <>
          <Text>{getString('common.genericErrors.unauthorized')}</Text>
          <Icon name="harness-logo-black" size={200} />
        </>
      )
    default:
      return <Container>{message}</Container>
  }
}
 
const GenericErrorPage: React.FC<GenericErrorPageProps> = (props: GenericErrorPageProps) => {
  let { code, message } = useQueryParams<GenericErrorPageQueryParams>()
 
  if (!code && !message) {
    code = props.code
    message = props.message
  }
 
  return (
    <Container height="100%" flex={{ align: 'center-center' }}>
      <Layout.Vertical spacing="large" flex={{ align: 'center-center' }}>
        <Error code={code} message={message} />
      </Layout.Vertical>
    </Container>
  )
}
 
export default GenericErrorPage