-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage-wrapper.tsx
85 lines (78 loc) · 2.32 KB
/
page-wrapper.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { isRouteErrorResponse, useNavigate, useRouteError } from '@remix-run/react';
import { createRemixStub } from '@remix-run/testing';
import { PropsWithChildren } from 'react';
import App, { loader as rootPageLoader } from '~/app/root';
import { ROUTES } from '~/router/config';
// @remix-run/testing doesn't export this type
type StubRouteObject = Parameters<typeof createRemixStub>[0][0];
type RouteParams = Omit<StubRouteObject, 'Component' | 'children' | 'path'>;
interface PageWrapperProps extends PropsWithChildren {
initialPath?: string;
rootRouteParams?: RouteParams;
pageRouteParams?: RouteParams;
}
export function PageWrapper({
children,
rootRouteParams = {},
pageRouteParams = {},
initialPath = '/',
}: PageWrapperProps) {
const RemixStub = createRemixStub([
{
Component: () => <App />,
id: 'root',
loader: rootPageLoader,
ErrorBoundary,
children: [
...Object.values(ROUTES).map((route) => ({
path: route.path,
Component: () => children,
...pageRouteParams,
})),
],
...rootRouteParams,
},
]);
return <RemixStub initialEntries={[initialPath]} />;
}
function ErrorBoundary() {
const navigate = useNavigate();
const error = useRouteError();
const goBack = (
<button
style={{
border: '1px solid dodgerblue',
cursor: 'pointer',
padding: '0 3px',
borderRadius: '5px',
color: 'dodgerblue',
}}
onClick={() => navigate(-1)}
>
Go Back
</button>
);
if (isRouteErrorResponse(error)) {
return (
<div>
<h1>
{error.status} {error.statusText}
</h1>
<p>{error.data}</p>
{goBack}
</div>
);
} else if (error instanceof Error) {
return (
<div>
<h1>Error</h1>
<p>{error.message}</p>
<p>The stack trace is:</p>
<pre>{error.stack}</pre>
{goBack}
</div>
);
} else {
return <h1>Unknown Error {goBack}</h1>;
}
}