-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroot.tsx
213 lines (191 loc) · 5.96 KB
/
root.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import {
Links,
LiveReload,
Meta,
Outlet,
Scripts,
ScrollRestoration,
useCatch,
useLoaderData,
useActionData,
} from '@remix-run/react';
import clsx from 'clsx';
import Layout from './components/layout';
import type { LinksFunction } from '@remix-run/node';
import { json, type ActionFunctionArgs, type LoaderFunctionArgs } from '@remix-run/node';
import { getThemeSession } from './utils/theme.server';
import tailwindUrl from './styles/tailwind.css';
import NewLayout from "~/components/new-layout";
import SetMode from "~/components/set-mode";
export let links: LinksFunction = () => {
return [{ rel: 'stylesheet', href: tailwindUrl }];
};
export async function loader({ request }: LoaderFunctionArgs) {
const themeSession = await getThemeSession(request);
const theme = themeSession.getTheme();
const prefersDarkMQ = '(prefers-color-scheme: dark)';
const getPreferredTheme = () => {
if (typeof window !== 'object') {
// Default to DARK
return 'DARK';
}
return window.matchMedia(prefersDarkMQ).matches ? "DARK" : "LIGHT";
}
return json({
ENV: {
FEATURE_NEW_BRAND: process.env.FEATURE_NEW_BRAND,
},
theme: theme || getPreferredTheme()
})
}
export async function action({ request }: ActionFunctionArgs) {
const themeSession = await getThemeSession(request);
const formData = await request.formData();
const theme = formData.get("theme");
if (typeof theme === 'string') {
themeSession.setTheme(theme);
}
return json(
{ mode: theme },
{
headers: {
"Set-Cookie": await themeSession.commit(),
}
}
)
}
// https://remix.run/api/conventions#default-export
// https://remix.run/api/conventions#route-filenames
export default function App() {
const data = useLoaderData();
const isNewLayout = data.ENV.FEATURE_NEW_BRAND === 'true';
if (isNewLayout) {
return (
<Document>
<NewLayout>
<Outlet />
</NewLayout>
</Document>
)
}
return (
<Document>
<Layout>
<Outlet />
</Layout>
</Document>
);
}
// https://remix.run/docs/en/v1/api/conventions#errorboundary
export function ErrorBoundary({ error }: { error: Error }) {
console.error(error);
const isNewLayout = 'true'
if (isNewLayout) {
return (
<Document>
<NewLayout>
<Outlet />
</NewLayout>
</Document>
)
}
return (
<Document title="Error!">
<Layout>
<div>
<h1>There was an error</h1>
<p>{error.message}</p>
<hr />
<p>
Hey, developer, you should replace this with what you
want your users to see.
</p>
</div>
</Layout>
</Document>
);
}
// https://remix.run/docs/en/v1/api/conventions#catchboundary
export function CatchBoundary() {
let caught = useCatch();
const isNewLayout = 'true';
let message;
switch (caught.status) {
case 401:
message = (
<p>
Oops! Looks like you tried to visit a page that you do not
have access to.
</p>
);
break;
case 404:
message = (
<p>
What you are looking for is not there. Please visit the home
page and try again.
</p>
);
break;
default:
throw new Error(caught.data || caught.statusText);
}
if (isNewLayout) {
return (
<Document title={`${caught.status} ${caught.statusText}`}>
<NewLayout>
<div className="mt-20 mb-40 mx-auto p-14 text-md font-sans text-white border border-white bg-romanBlack">
<h1 className="text-8xl font-bold mt-16 font-ubuntu leading-heading tracking-heading">
{caught.status}: {caught.statusText}
</h1>
</div>
</NewLayout>
</Document>
);
}
return (
<Document title={`${caught.status} ${caught.statusText}`}>
<Layout>
<div className="mt-20 mb-40 mx-auto p-14 text-md font-sans text-white border border-white bg-romanBlack">
<h1 className="text-8xl font-bold mt-16 font-ubuntu leading-heading tracking-heading">
{caught.status}: {caught.statusText}
</h1>
</div>
</Layout>
</Document>
);
}
function Document({
children,
title,
}: {
children: React.ReactNode;
title?: string;
}) {
const data = useLoaderData<typeof loader>();
const actionData = useActionData<typeof action>();
const theme = actionData?.mode || data?.theme;
return (
<html lang="en" className={clsx('w-full h-full font-mono', {
dark: theme === 'DARK',
})}>
<head>
<meta charSet="utf-8" />
<meta
name="viewport"
content="width=device-width,initial-scale=1"
/>
{title ? <title>{title}</title> : null}
<Meta />
<Links />
</head>
<body className={`${data?.ENV?.FEATURE_NEW_BRAND === 'true' ? 'bg-white dark:bg-romanBlack roman-grid' : 'bg-romanBlack'} w-full h-full`}>
{children}
<ScrollRestoration />
<Scripts />
{process.env.NODE_ENV === 'development' && <LiveReload />}
<SetMode mode={theme} />
</body>
</html>
);
}