Skip to content

Commit

Permalink
Lift all the remaining routes
Browse files Browse the repository at this point in the history
  • Loading branch information
mdellweg committed Dec 16, 2024
1 parent 7082795 commit 5bf774d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
36 changes: 16 additions & 20 deletions src/app-routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Trans } from '@lingui/react/macro';
import { Banner, Flex, FlexItem } from '@patternfly/react-core';
import WrenchIcon from '@patternfly/react-icons/dist/esm/icons/wrench-icon';
import { type ElementType } from 'react';
import { Navigate, Route, Routes, useLocation } from 'react-router';
import { Navigate, useLocation } from 'react-router';
import { ErrorBoundary, ExternalLink, NotFound } from 'src/components';
import {
AboutProject,
Expand Down Expand Up @@ -344,25 +344,19 @@ const AuthHandler = ({
);
};

export const AppRoutes = () => (
<Routes>
{routes.map(({ beta, component, noAuth, path }, index) => (
<Route
element={
<AuthHandler
beta={beta}
component={component}
noAuth={noAuth}
path={path}
/>
}
key={index}
const appRoutes = () =>
routes.map(({ beta, component, noAuth, path, ...rest }) => ({
element: (
<AuthHandler
beta={beta}
component={component}
noAuth={noAuth}
path={path}
/>
))}
<Route path='*' element={<NotFound />} />
</Routes>
);
),
path: path,
...rest,
}));

export const dataRoutes = [
{
Expand All @@ -372,10 +366,12 @@ export const dataRoutes = [
errorElement: <ErrorBoundary />,
children: [
{
path: '/',
index: true,
element: <Navigate to={formatPath(Paths.core.status)} />,
},
{ path: '*', element: <AppRoutes /> },
...appRoutes(),
// "No matching route" is not handled by the error boundary.
{ path: '*', element: <NotFound /> },
],
},
],
Expand Down
22 changes: 18 additions & 4 deletions src/components/error-boundary.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
import { isRouteErrorResponse, useRouteError } from 'react-router';
import { NotFound } from 'src/components';

export const ErrorBoundary = () => {
const error = useRouteError();

if (isRouteErrorResponse(error) && error.status == 404) {
return <NotFound />;
} else {
if (isRouteErrorResponse(error)) {
return (
<>
<h1>
{error.status} {error.statusText}
</h1>
<p>{error.data}</p>
</>
);
} else if (error instanceof Error) {
return (
<div>
<h1>Error</h1>
<p>{error.message}</p>
<p>The stack trace is:</p>
<pre>{error.stack}</pre>
</div>
);
console.error(error);
return <div>Something went horribly wrong!</div>;
}
Expand Down

0 comments on commit 5bf774d

Please sign in to comment.