1
1
import React from "react" ;
2
- import type { LinksFunction , MetaFunction } from "@remix-run/node" ;
2
+ import type {
3
+ LinksFunction ,
4
+ LoaderFunction ,
5
+ MetaFunction ,
6
+ } from "@remix-run/node" ;
7
+ import { json } from "@remix-run/node" ;
3
8
import {
4
9
Link ,
5
10
Links ,
@@ -9,9 +14,11 @@ import {
9
14
Scripts ,
10
15
ScrollRestoration ,
11
16
useCatch ,
17
+ useLoaderData ,
12
18
} from "@remix-run/react" ;
13
19
14
20
import tailwindStyles from "~/tailwind.css" ;
21
+ import { getSessionDataFromRequest } from "./utils/session.server" ;
15
22
16
23
export const links : LinksFunction = ( ) => [
17
24
{ rel : "stylesheet" , href : tailwindStyles } ,
@@ -22,7 +29,13 @@ export const meta: MetaFunction = () => {
22
29
return { title : "bogos" } ;
23
30
} ;
24
31
25
- function Document ( { children } : { children : React . ReactNode } ) {
32
+ function Document ( {
33
+ children,
34
+ currentUser : { username, avatarUrl } = { } ,
35
+ } : {
36
+ children : React . ReactNode ;
37
+ currentUser : { username ?: string ; avatarUrl ?: string } ;
38
+ } ) {
26
39
return (
27
40
< html lang = "en" >
28
41
< head >
@@ -36,9 +49,20 @@ function Document({ children }: { children: React.ReactNode }) {
36
49
< Link to = "/" className = "text-stone-100 text-xl" >
37
50
👽 bogos
38
51
</ Link >
39
- < Link to = "/login" className = "text-stone-100 text-xl" >
40
- login
41
- </ Link >
52
+ { username ? (
53
+ < div className = "flex gap-x-6 items-center" >
54
+ < p className = "text-stone-100 text-xl my-0" >
55
+ < span className = "opacity-50" > logged in as</ span > { username } -{ " " }
56
+ < Link to = "/logout" className = "text-stone-100 text-xl" >
57
+ logout
58
+ </ Link >
59
+ </ p >
60
+ </ div >
61
+ ) : (
62
+ < Link to = "/login" className = "text-stone-100 text-xl" >
63
+ login
64
+ </ Link >
65
+ ) }
42
66
</ header >
43
67
{ children }
44
68
< ScrollRestoration />
@@ -49,9 +73,30 @@ function Document({ children }: { children: React.ReactNode }) {
49
73
) ;
50
74
}
51
75
76
+ type LoaderData = {
77
+ username ?: string ;
78
+ avatarUrl ?: string ;
79
+ } ;
80
+
81
+ export const loader : LoaderFunction = async ( { request } ) => {
82
+ const sessionData = await getSessionDataFromRequest ( request ) ;
83
+
84
+ if ( sessionData !== null ) {
85
+ const data : LoaderData = {
86
+ username : sessionData . username ,
87
+ avatarUrl : sessionData . avatarUrl ,
88
+ } ;
89
+ return json ( data ) ;
90
+ }
91
+
92
+ return json ( { } ) ;
93
+ } ;
94
+
52
95
export default function App ( ) {
96
+ const data = useLoaderData < LoaderData > ( ) ;
97
+
53
98
return (
54
- < Document >
99
+ < Document currentUser = { data } >
55
100
< Outlet />
56
101
</ Document >
57
102
) ;
0 commit comments