Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: frontend app error boundary #2144

Merged
merged 5 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"date-fns": "2.29.3",
"debounce": "1.2.1",
"deep-diff": "1.0.2",
"dequal": "2.0.3",
"eslint": "8.23.0",
"eslint-config-react-app": "7.0.1",
"fast-json-patch": "3.1.1",
Expand All @@ -76,6 +77,7 @@
"react": "17.0.2",
"react-chartjs-2": "4.3.1",
"react-dom": "17.0.2",
"react-error-boundary": "3.1.4",
"react-hooks-global-state": "2.0.0",
"react-router-dom": "6.3.0",
"react-table": "7.8.0",
Expand Down Expand Up @@ -128,8 +130,5 @@
"ignorePatterns": [
"cypress"
]
},
"dependencies": {
"dequal": "2.0.3"
}
}
76 changes: 44 additions & 32 deletions frontend/src/component/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { Suspense } from 'react';
import { Navigate, Route, Routes } from 'react-router-dom';
import { ErrorBoundary } from 'react-error-boundary';
import { Error } from 'component/layout/Error/Error';
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender';
import { FeedbackNPS } from 'component/feedback/FeedbackNPS/FeedbackNPS';
import { LayoutPicker } from 'component/layout/LayoutPicker/LayoutPicker';
Expand All @@ -14,7 +17,6 @@ import { SplashPageRedirect } from 'component/splash/SplashPageRedirect/SplashPa
import { useStyles } from './App.styles';
import { usePlausibleTracker } from 'hooks/usePlausibleTracker';
import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig';
import { Suspense } from 'react';

export const App = () => {
const { classes: styles } = useStyles();
Expand All @@ -29,40 +31,50 @@ export const App = () => {
: routes;

return (
<SWRProvider>
<Suspense fallback={<Loader />}>
<ConditionallyRender
condition={!hasFetchedAuth}
show={<Loader />}
elseShow={
<div className={styles.container}>
<ToastRenderer />
<LayoutPicker>
<Routes>
{availableRoutes.map(route => (
<ErrorBoundary FallbackComponent={Error}>
<SWRProvider>
<Suspense fallback={<Loader />}>
<ConditionallyRender
condition={!hasFetchedAuth}
show={<Loader />}
elseShow={
<div className={styles.container}>
<ToastRenderer />
<LayoutPicker>
<Routes>
{availableRoutes.map(route => (
<Route
key={route.path}
path={route.path}
element={
<ProtectedRoute
route={route}
/>
}
/>
))}
<Route
key={route.path}
path={route.path}
path="/"
element={
<ProtectedRoute route={route} />
<Navigate
to="/features"
replace
/>
}
/>
))}
<Route
path="/"
element={
<Navigate to="/features" replace />
}
/>
<Route path="*" element={<NotFound />} />
</Routes>
<FeedbackNPS openUrl="http://feedback.unleash.run" />
<SplashPageRedirect />
</LayoutPicker>
</div>
}
/>
</Suspense>
</SWRProvider>
<Route
path="*"
element={<NotFound />}
/>
</Routes>
<FeedbackNPS openUrl="http://feedback.unleash.run" />
<SplashPageRedirect />
</LayoutPicker>
</div>
}
/>
</Suspense>
</SWRProvider>
</ErrorBoundary>
);
};
44 changes: 44 additions & 0 deletions frontend/src/component/layout/Error/Error.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { VFC } from 'react';
import { useNavigate } from 'react-router-dom';
import { Box, Typography } from '@mui/material';
import { Dialogue } from 'component/common/Dialogue/Dialogue';
import { GO_BACK } from 'constants/navigate';
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender';

interface IErrorProps {
error: Error;
}

export const Error: VFC<IErrorProps> = ({ error }) => {
const navigate = useNavigate();
return (
<Box sx={{ backgroundColor: 'neutral.light', height: '100%', p: 4 }}>
<Dialogue
open={true}
title="Something went wrong"
primaryButtonText="Go back"
onClick={() => {
navigate('/');
window?.location?.reload();
}}
secondaryButtonText="Reload this page"
onClose={() => {
window?.location?.reload();
}}
maxWidth="xl"
>
<Box component="pre" sx={{ color: 'error.main' }}>
{error.message}
</Box>
<ConditionallyRender
condition={Boolean(error.stack)}
show={
<Box component="pre" sx={{ color: 'error.main' }}>
{error.stack}
</Box>
}
/>
</Dialogue>
</Box>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const useInviteTokens = (options: SWRConfiguration = {}) => {

return {
data: data
? { tokens: data.tokens.filter(token => token.enabled) }
? { tokens: data.tokens?.filter(token => token.enabled) }
: undefined,
error,
loading,
Expand Down
2 changes: 1 addition & 1 deletion frontend/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5920,7 +5920,7 @@ react-dom@17.0.2:
object-assign "^4.1.1"
scheduler "^0.20.2"

react-error-boundary@^3.1.0:
react-error-boundary@3.1.4, react-error-boundary@^3.1.0:
version "3.1.4"
resolved "https://registry.yarnpkg.com/react-error-boundary/-/react-error-boundary-3.1.4.tgz#255db92b23197108757a888b01e5b729919abde0"
integrity sha512-uM9uPzZJTF6wRQORmSrvOIgt4lJ9MC1sNgEOj2XGsDTRE4kmpWxg7ENK9EWNKJRMAOY9z0MuF4yIfl6gp4sotA==
Expand Down