Add error boundaries to individual routes

This commit is contained in:
jeffvli 2023-01-04 22:38:27 -08:00
parent 24f06db2b8
commit 98ef0b44ec
2 changed files with 29 additions and 5 deletions

View file

@ -1,15 +1,18 @@
import { Box, Center, Divider, Group, Stack } from '@mantine/core'; import { Box, Center, Divider, Group, Stack } from '@mantine/core';
import type { FallbackProps } from 'react-error-boundary'; import type { FallbackProps } from 'react-error-boundary';
import { RiErrorWarningLine, RiArrowLeftLine } from 'react-icons/ri'; import { RiErrorWarningLine, RiHomeFill, RiArrowLeftSLine } from 'react-icons/ri';
import { useNavigate, useRouteError } from 'react-router'; import { useNavigate, useRouteError } from 'react-router';
import styled from 'styled-components'; import styled from 'styled-components';
import { Button, Text } from '/@/renderer/components'; import { Button, Text } from '/@/renderer/components';
import { AppRoute } from '/@/renderer/router/routes';
const Container = styled(Box)` const Container = styled(Box)`
background: var(--main-bg); background: var(--main-bg);
`; `;
export const ErrorFallback = ({ error, resetErrorBoundary }: FallbackProps) => { export const ErrorFallback = ({ resetErrorBoundary }: FallbackProps) => {
const error = useRouteError() as any;
return ( return (
<Container> <Container>
<Center sx={{ height: '100vh' }}> <Center sx={{ height: '100vh' }}>
@ -47,11 +50,22 @@ export const RouteErrorBoundary = () => {
navigate(-1); navigate(-1);
}; };
const handleHome = () => {
navigate(AppRoute.HOME);
};
return ( return (
<Container> <Container>
<Center sx={{ height: '100vh' }}> <Center sx={{ height: '100vh' }}>
<Stack sx={{ maxWidth: '50%' }}> <Stack sx={{ maxWidth: '50%' }}>
<Group> <Group>
<Button
px={10}
variant="subtle"
onClick={handleReturn}
>
<RiArrowLeftSLine size={20} />
</Button>
<RiErrorWarningLine <RiErrorWarningLine
color="var(--danger-color)" color="var(--danger-color)"
size={30} size={30}
@ -62,12 +76,12 @@ export const RouteErrorBoundary = () => {
<Text size="sm">{error?.message}</Text> <Text size="sm">{error?.message}</Text>
<Group grow> <Group grow>
<Button <Button
leftIcon={<RiArrowLeftLine />} leftIcon={<RiHomeFill />}
sx={{ flex: 0.5 }} sx={{ flex: 0.5 }}
variant="default" variant="default"
onClick={handleReturn} onClick={handleHome}
> >
Go back Go home
</Button> </Button>
<Button <Button
variant="filled" variant="filled"

View file

@ -55,41 +55,51 @@ export const AppRouter = () => {
<Route <Route
index index
element={<HomeRoute />} element={<HomeRoute />}
errorElement={<RouteErrorBoundary />}
/> />
<Route <Route
element={<HomeRoute />} element={<HomeRoute />}
errorElement={<RouteErrorBoundary />}
path={AppRoute.HOME} path={AppRoute.HOME}
/> />
<Route <Route
element={<NowPlayingRoute />} element={<NowPlayingRoute />}
errorElement={<RouteErrorBoundary />}
path={AppRoute.NOW_PLAYING} path={AppRoute.NOW_PLAYING}
/> />
<Route <Route
element={<AlbumListRoute />} element={<AlbumListRoute />}
errorElement={<RouteErrorBoundary />}
path={AppRoute.LIBRARY_ALBUMS} path={AppRoute.LIBRARY_ALBUMS}
/> />
<Route <Route
element={<AlbumDetailRoute />} element={<AlbumDetailRoute />}
errorElement={<RouteErrorBoundary />}
path={AppRoute.LIBRARY_ALBUMS_DETAIL} path={AppRoute.LIBRARY_ALBUMS_DETAIL}
/> />
<Route <Route
element={<SongListRoute />} element={<SongListRoute />}
errorElement={<RouteErrorBoundary />}
path={AppRoute.LIBRARY_SONGS} path={AppRoute.LIBRARY_SONGS}
/> />
<Route <Route
element={<PlaylistListRoute />} element={<PlaylistListRoute />}
errorElement={<RouteErrorBoundary />}
path={AppRoute.PLAYLISTS} path={AppRoute.PLAYLISTS}
/> />
<Route <Route
element={<PlaylistDetailRoute />} element={<PlaylistDetailRoute />}
errorElement={<RouteErrorBoundary />}
path={AppRoute.PLAYLISTS_DETAIL} path={AppRoute.PLAYLISTS_DETAIL}
/> />
<Route <Route
element={<PlaylistDetailSongListRoute />} element={<PlaylistDetailSongListRoute />}
errorElement={<RouteErrorBoundary />}
path={AppRoute.PLAYLISTS_DETAIL_SONGS} path={AppRoute.PLAYLISTS_DETAIL_SONGS}
/> />
<Route <Route
element={<AlbumArtistListRoute />} element={<AlbumArtistListRoute />}
errorElement={<RouteErrorBoundary />}
path={AppRoute.LIBRARY_ALBUMARTISTS} path={AppRoute.LIBRARY_ALBUMARTISTS}
/> />
<Route <Route