|
| 1 | +import React, { Component } from "react"; |
| 2 | + |
| 3 | +import { useTranslation } from "react-i18next"; |
| 4 | +import { Link } from "react-router-dom"; |
| 5 | + |
| 6 | +import { homepageURL } from "@constants/global.constants"; |
| 7 | +import { OAuthErrorBoundaryProps, OAuthErrorBoundaryState } from "@interfaces/components"; |
| 8 | +import { LoggerService } from "@services"; |
| 9 | + |
| 10 | +const OAuthErrorFallback = ({ error, resetError }: { error?: Error; resetError?: () => void }) => { |
| 11 | + const { t } = useTranslation("global", { keyPrefix: "oauthError" }); |
| 12 | + |
| 13 | + return ( |
| 14 | + <div className="flex w-full flex-1 flex-col items-center justify-center py-5"> |
| 15 | + <div className="mt-16 font-fira-code text-lg text-error"> |
| 16 | + {t("authenticationError", "Authentication Error")} |
| 17 | + </div> |
| 18 | + <div className="mt-4 max-w-md text-center font-fira-code text-sm text-gray-600"> |
| 19 | + {t("errorMessage", "An error occurred during authentication. Please try again.")} |
| 20 | + </div> |
| 21 | + {error?.message ? ( |
| 22 | + <div className="mt-2 max-w-md text-center font-fira-code text-xs text-gray-500">{error.message}</div> |
| 23 | + ) : null} |
| 24 | + <div className="mt-6 flex gap-4"> |
| 25 | + {resetError ? ( |
| 26 | + <button |
| 27 | + className="rounded bg-blue-500 px-4 py-2 font-fira-code text-sm text-white hover:opacity-80" |
| 28 | + onClick={resetError} |
| 29 | + type="button" |
| 30 | + > |
| 31 | + {t("tryAgain", "Try Again")} |
| 32 | + </button> |
| 33 | + ) : null} |
| 34 | + <Link |
| 35 | + className="rounded border border-gray-300 px-4 py-2 font-fira-code text-sm text-gray-700 hover:bg-gray-1100" |
| 36 | + to={homepageURL} |
| 37 | + > |
| 38 | + {t("goHome", "Go Home")} |
| 39 | + </Link> |
| 40 | + </div> |
| 41 | + </div> |
| 42 | + ); |
| 43 | +}; |
| 44 | + |
| 45 | +export class OAuthErrorBoundary extends Component<OAuthErrorBoundaryProps, OAuthErrorBoundaryState> { |
| 46 | + constructor(props: OAuthErrorBoundaryProps) { |
| 47 | + super(props); |
| 48 | + this.state = { hasError: false }; |
| 49 | + } |
| 50 | + |
| 51 | + static getDerivedStateFromError(error: Error): OAuthErrorBoundaryState { |
| 52 | + return { hasError: true, error }; |
| 53 | + } |
| 54 | + |
| 55 | + componentDidCatch(error: Error, errorInfo: React.ErrorInfo) { |
| 56 | + const { onError } = this.props; |
| 57 | + |
| 58 | + LoggerService.error( |
| 59 | + "auth.oauth.boundary", |
| 60 | + `OAuth component error: ${error.message}, Stack: ${error.stack}, Component Stack: ${errorInfo.componentStack}`, |
| 61 | + true |
| 62 | + ); |
| 63 | + |
| 64 | + onError?.(error, errorInfo); |
| 65 | + } |
| 66 | + |
| 67 | + resetErrorBoundary = () => { |
| 68 | + this.setState({ hasError: false, error: undefined }); |
| 69 | + }; |
| 70 | + |
| 71 | + render() { |
| 72 | + const { hasError, error } = this.state; |
| 73 | + const { fallback, children } = this.props; |
| 74 | + |
| 75 | + if (hasError) { |
| 76 | + if (fallback) { |
| 77 | + return fallback; |
| 78 | + } |
| 79 | + |
| 80 | + return <OAuthErrorFallback error={error} resetError={this.resetErrorBoundary} />; |
| 81 | + } |
| 82 | + |
| 83 | + return children; |
| 84 | + } |
| 85 | +} |
0 commit comments