Skip to content

Commit

Permalink
Add error boundary
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitayutanov committed Jul 17, 2023
1 parent f1bf85c commit 4caccb2
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 13 deletions.
3 changes: 2 additions & 1 deletion idea/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"react": "^18.2.0",
"react-dnd": "^16.0.1",
"react-dnd-html5-backend": "^16.0.1",
"react-dom": "^18.2.0",
"react-dom": "18.2.0",
"react-error-boundary": "^4.0.10",
"react-final-form": "^6.5.9",
"react-final-form-listeners": "^1.0.3",
"react-number-format": "^4.9.3",
Expand Down
11 changes: 9 additions & 2 deletions idea/frontend/src/app/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useEffect } from 'react';
import { useSearchParams } from 'react-router-dom';
import { useLocation, useSearchParams } from 'react-router-dom';
import { ErrorBoundary } from 'react-error-boundary';
import { useAccount, useApi } from '@gear-js/react-hooks';
import 'simplebar-react/dist/simplebar.min.css';

Expand All @@ -11,12 +12,14 @@ import { MobileDisclaimer } from 'widgets/mobileDisclaimer';
import { Routing } from 'pages';
import { LocalStorage, NODE_ADRESS_URL_PARAM } from 'shared/config';
import { Loader } from 'shared/ui/loader';
import { ErrorFallback } from 'shared/ui/errorFallback';

import { withProviders } from './providers';
import './App.scss';

const App = withProviders(() => {
const { nodeAddress } = useApp();
const { pathname } = useLocation();
const [searchParams, setSearchParams] = useSearchParams();

const { api, isApiReady } = useApi();
Expand Down Expand Up @@ -52,7 +55,11 @@ const App = withProviders(() => {
<Menu />
<div className="content">
<Header />
{isAppReady ? <Routing /> : <Loader />}

{/* key to reset on route change */}
<ErrorBoundary key={pathname} fallbackRender={ErrorFallback}>
{isAppReady ? <Routing /> : <Loader />}
</ErrorBoundary>
</div>

{isMobileDisclaimerVisible && <MobileDisclaimer onCloseButtonClick={closeMobileDisclaimer} />}
Expand Down
23 changes: 15 additions & 8 deletions idea/frontend/src/hooks/useChangeEffect.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { useEffect, useRef } from 'react';
import { DependencyList, EffectCallback, useEffect, useRef } from 'react';

export const useChangeEffect = (callback: () => void, dependencies?: unknown[]) => {
const isMounted = useRef(false);
export const useChangeEffect = (callback: EffectCallback, dependencies: DependencyList) => {
const mounted = useRef(false);

useEffect(
() => () => {
mounted.current = false;
},
[],
);

useEffect(() => {
if (isMounted.current) {
callback();
if (mounted.current) {
return callback();
}

return () => {
isMounted.current = true;
};
mounted.current = true;
return undefined;

// eslint-disable-next-line react-hooks/exhaustive-deps
}, dependencies);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.heading {
margin-bottom: 8px;
}

.error {
margin-bottom: 32px;
color: #f24a4a;
}
16 changes: 16 additions & 0 deletions idea/frontend/src/shared/ui/errorFallback/ErrorFallback.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { FallbackProps } from 'react-error-boundary';

import { Subheader } from '../subheader';
import { BackButton } from '../backButton';
import styles from './ErrorFallback.module.scss';

const ErrorFallback = ({ error }: FallbackProps) => (
<>
<Subheader title="An unexpected error occured:" />
<p className={styles.error}>{error.message}</p>

<BackButton />
</>
);

export { ErrorFallback };
3 changes: 3 additions & 0 deletions idea/frontend/src/shared/ui/errorFallback/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ErrorFallback } from './ErrorFallback';

export { ErrorFallback };
16 changes: 14 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2203,7 +2203,8 @@ __metadata:
react-app-rewired: ^2.2.1
react-dnd: ^16.0.1
react-dnd-html5-backend: ^16.0.1
react-dom: ^18.2.0
react-dom: 18.2.0
react-error-boundary: ^4.0.10
react-final-form: ^6.5.9
react-final-form-listeners: ^1.0.3
react-number-format: ^4.9.3
Expand Down Expand Up @@ -16731,7 +16732,7 @@ __metadata:
languageName: node
linkType: hard

"react-dom@npm:^18.2.0":
"react-dom@npm:18.2.0":
version: 18.2.0
resolution: "react-dom@npm:18.2.0"
dependencies:
Expand All @@ -16743,6 +16744,17 @@ __metadata:
languageName: node
linkType: hard

"react-error-boundary@npm:^4.0.10":
version: 4.0.10
resolution: "react-error-boundary@npm:4.0.10"
dependencies:
"@babel/runtime": ^7.12.5
peerDependencies:
react: ">=16.13.1"
checksum: 4ad4864d2a5fc2264a24d03e83176e6a70d7adbe3c1edbdc5b0bd452a695104bc59456e23b5aea1b9729220672fe46614221daa8b3bd59327968d4aa7eb8bc71
languageName: node
linkType: hard

"react-error-overlay@npm:^6.0.11":
version: 6.0.11
resolution: "react-error-overlay@npm:6.0.11"
Expand Down

0 comments on commit 4caccb2

Please sign in to comment.