-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathheader.tsx
More file actions
136 lines (125 loc) · 3.83 KB
/
Copy pathheader.tsx
File metadata and controls
136 lines (125 loc) · 3.83 KB
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
import Link from "next/link";
import { Suspense, cache } from "react";
import { getCurrentUser } from "@/lib/session";
import { Button } from "@/components/ui/button";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
import { LayoutDashboard, Lightbulb, Loader2Icon, LogOut } from "lucide-react";
import { getUserProfileUseCase } from "@/use-cases/users";
import { ModeToggle } from "./mode-toggle";
import { MenuButton } from "./menu-button";
import { UserId } from "@/types";
const profilerLoader = cache(getUserProfileUseCase);
export async function Header() {
const user = await getCurrentUser();
return (
<div className="border-b py-4">
<div className="container mx-auto flex items-center justify-between">
<div className="flex items-center gap-8">
<Link href="/" className="flex items-center gap-2 text-xl">
<Lightbulb />
<div className="hidden md:block">APP</div>
</Link>
<div className="flex items-center gap-2">
{user && (
<Button
variant={"link"}
asChild
className="flex items-center justify-center gap-2"
>
<Link href={"/dashboard"}>
<LayoutDashboard className="h-4 w-4" /> Dashboard
</Link>
</Button>
)}
</div>
</div>
<div className="flex items-center justify-between gap-5">
<Suspense
fallback={
<div className="flex w-40 items-center justify-center">
<Loader2Icon className="h-4 w-4 animate-spin" />
</div>
}
>
<HeaderActions />
</Suspense>
</div>
</div>
</div>
);
}
async function ProfileAvatar({ userId }: { userId: number }) {
const profile = await profilerLoader(userId);
return (
<Avatar>
<AvatarImage src={"/next.svg"} />
<AvatarFallback>
{profile.displayName?.substring(0, 2).toUpperCase() ?? "AA"}
</AvatarFallback>
</Avatar>
);
}
async function HeaderActions() {
const user = await getCurrentUser();
const isSignedIn = !!user;
return (
<>
{isSignedIn ? (
<>
<div className="hidden md:block">
<ModeToggle />
</div>
<ProfileDropdown userId={user.id} />
<div className="md:hidden">
<MenuButton />
</div>
</>
) : (
<>
<ModeToggle />
<Button asChild variant="secondary">
<Link href="/sign-in">Sign In</Link>
</Button>
</>
)}
</>
);
}
async function ProfileDropdown({ userId }: { userId: UserId }) {
const profile = await profilerLoader(userId);
return (
<DropdownMenu>
<DropdownMenuTrigger>
<Suspense
fallback={
<div className="flex h-10 w-10 shrink-0 items-center justify-center rounded-full bg-gray-800">
..
</div>
}
>
<ProfileAvatar userId={userId} />
</Suspense>
</DropdownMenuTrigger>
<DropdownMenuContent className="space-y-2">
<DropdownMenuLabel>{profile.displayName}</DropdownMenuLabel>
<DropdownMenuSeparator />
<DropdownMenuItem asChild className="cursor-pointer">
<form action="/api/sign-out" method="POST">
<Button type="submit" variant="ghost" className="flex items-center hover:bg-transparent">
<LogOut className="mr-2 h-4 w-4" />
Sign Out
</button>
</form>
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
);
}