-
Notifications
You must be signed in to change notification settings - Fork 4
/
root.tsx
146 lines (132 loc) · 3.1 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
import {
ErrorBoundaryComponent,
Links,
LiveReload,
Meta,
Outlet,
Scripts,
ScrollRestoration,
useCatch,
useLoaderData,
useSubmit,
} from "remix";
import type { LinksFunction } from "remix";
import tailwindStyles from "~/styles/tailwind.css";
import appStyles from "~/styles/app.css";
import { createClient } from "@supabase/supabase-js";
import { SupabaseProvider, useSupabase } from "./utils/supabase-client";
import { Button } from "./components/basic/button";
import type { MetaFunction } from "remix";
export const meta: MetaFunction = () => {
return {
title: "Home | Code Gino Playground",
description: "Web application to showcase the power of Remix",
};
};
export let links: LinksFunction = () => {
return [
{ rel: "stylesheet", href: tailwindStyles },
{
rel: "stylesheet",
href: appStyles,
},
];
};
export const loader = () => {
return {
supabaseKey: process.env.SUPABASE_ANON_KEY,
supabaseUrl: process.env.SUPABASE_URL,
};
};
export default function App() {
const loader = useLoaderData();
const supabase = createClient(loader.supabaseUrl, loader.supabaseKey);
return (
<Document>
<SupabaseProvider supabase={supabase}>
<Layout>
<Outlet />
</Layout>
</SupabaseProvider>
</Document>
);
}
function Document({
children,
title,
}: {
children: React.ReactNode;
title?: string;
}) {
return (
<html lang="en">
<head>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
{title ? <title>{title}</title> : null}
<Meta />
<Links />
</head>
<body>
{children}
<ScrollRestoration />
<Scripts />
{process.env.NODE_ENV === "development" && <LiveReload />}
</body>
</html>
);
}
function Layout({ children }: React.PropsWithChildren<{}>) {
const submit = useSubmit();
const supabase = useSupabase();
const handleSignOut = () => {
supabase.auth.signOut().then(() => {
submit(null, { method: "post", action: "/signout" });
});
};
return (
<main>
<header>
{supabase?.auth?.session() && (
<Button type="button" onClick={handleSignOut}>
Sign out
</Button>
)}
</header>
{children}
</main>
);
}
export const ErrorBoundary: ErrorBoundaryComponent = ({ error }) => {
return (
<Document>
<Layout>
<p>An error has occur</p>
<p>{error.message}</p>
</Layout>
</Document>
);
};
export function CatchBoundary() {
let caught = useCatch();
let message;
switch (caught.status) {
case 404:
message = <p>This is a custom error message for 404 pages</p>;
break;
// You can customize the behavior for other status codes
default: {
throw new Error(JSON.stringify(caught.data) || caught.statusText);
}
}
return (
<Document title={`${caught.status} ${caught.statusText}`}>
<Layout>
<h1>
{caught.status}: {caught.statusText}
</h1>
{message}
</Layout>
</Document>
);
}