tycho-components 0.2.2-SNAPSHOT-8 → 0.2.2-SNAPSHOT-9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ import { ReactNode } from 'react';
2
+ import './style.scss';
3
+ type Props = {
4
+ children: ReactNode;
5
+ };
6
+ export default function ErrorBoundary({ children }: Props): import("react/jsx-runtime").JSX.Element;
7
+ export {};
@@ -0,0 +1,51 @@
1
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
+ import { useEffect, useState } from 'react';
3
+ import { useTranslation } from 'react-i18next';
4
+ import { useNavigate } from 'react-router-dom';
5
+ import { Button } from 'tycho-storybook';
6
+ import CookieStorage from '../configs/CookieStorage';
7
+ import logo from './logo.png';
8
+ import './style.scss';
9
+ export default function ErrorBoundary({ children }) {
10
+ const navigate = useNavigate();
11
+ const { t } = useTranslation('base');
12
+ const [show, setShow] = useState(false);
13
+ const [state, setState] = useState({
14
+ hasError: false,
15
+ error: null,
16
+ });
17
+ const handleRedirect = () => {
18
+ navigate('/', { replace: true });
19
+ };
20
+ const handleRetry = () => {
21
+ const redirectUri = CookieStorage.getRedirectUri();
22
+ redirectUri ? (window.location.href = redirectUri) : handleRedirect();
23
+ };
24
+ // Hook to simulate the behavior of componentDidCatch
25
+ useEffect(() => {
26
+ const handleError = (error) => {
27
+ console.error('Caught by ErrorBoundary:', error);
28
+ setState({ hasError: true, error });
29
+ };
30
+ // Catch unhandled promise rejections globally
31
+ window.addEventListener('unhandledrejection', (event) => {
32
+ handleError(event.reason);
33
+ });
34
+ // Catch uncaught errors globally
35
+ window.addEventListener('error', (event) => {
36
+ handleError(event.error);
37
+ });
38
+ return () => {
39
+ window.removeEventListener('unhandledrejection', (event) => {
40
+ handleError(event.reason);
41
+ });
42
+ window.removeEventListener('error', (event) => {
43
+ handleError(event.error);
44
+ });
45
+ };
46
+ }, []);
47
+ if (state.hasError && state.error) {
48
+ return (_jsx("div", { className: "box-container", children: _jsxs("div", { className: "box", children: [_jsx("img", { src: logo }), _jsx("div", { className: "title", children: t('label.error.found') }), _jsxs("div", { className: "buttons", children: [_jsx(Button, { text: t('label.retry'), size: "small", onClick: handleRetry }), _jsx(Button, { text: t('label.redirect'), size: "small", onClick: handleRedirect }), _jsx(Button, { text: t('label.error.details'), size: "small", onClick: () => setShow(!show) })] }), show && _jsx("pre", { className: "error-stack", children: state.error?.stack })] }) }));
49
+ }
50
+ return _jsx(_Fragment, { children: children });
51
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "tycho-components",
3
3
  "private": false,
4
- "version": "0.2.2-SNAPSHOT-8",
4
+ "version": "0.2.2-SNAPSHOT-9",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "exports": {