header.tsx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. "use client";
  2. import Link from "next/link";
  3. import Image from "next/image";
  4. import { LoginLink, LogoutLink, RegisterLink } from "@kinde-oss/kinde-auth-nextjs/server";
  5. import { useKindeBrowserClient } from "@kinde-oss/kinde-auth-nextjs";
  6. import { useTheme } from "@/app/providers";
  7. import {
  8. NavigationMenu,
  9. NavigationMenuContent,
  10. NavigationMenuItem,
  11. NavigationMenuLink,
  12. NavigationMenuList,
  13. NavigationMenuTrigger,
  14. } from "@/components/ui/navigation-menu";
  15. import { Button } from "@/components/ui/button";
  16. import { Sheet, SheetContent, SheetTrigger } from "@/components/ui/sheet";
  17. import { LayoutGrid, Menu, Home, Settings, User, LogOut, LogIn } from "lucide-react";
  18. import { DarkModeToggle } from "./dark-mode-toggle";
  19. import React from "react";
  20. import { usePathname } from "next/navigation";
  21. const userMenuItems = [
  22. {
  23. title: "Profile",
  24. href: "/profile",
  25. description: "Manage your profile settings",
  26. icon: User,
  27. },
  28. {
  29. title: "Settings",
  30. href: "/settings",
  31. description: "Configure your preferences",
  32. icon: Settings,
  33. },
  34. ];
  35. export default function Header() {
  36. const { isAuthenticated, getUser, isLoading } = useKindeBrowserClient();
  37. const { theme } = useTheme();
  38. const user = getUser();
  39. const logoSrc = theme === "dark" ? "/vtorio-dark.svg" : "/vtorio.svg";
  40. const pathname = usePathname();
  41. // Don't show on dashboard or home page
  42. const shouldShowBackButton = pathname !== '/dashboard' && pathname !== '/';
  43. if (isLoading) {
  44. return (
  45. <header className="sticky top-0 z-50 w-full border-b bg-background/95 backdrop-blur supports-[backdrop-filter]:bg-background/60">
  46. <div className="flex h-16 items-center justify-between">
  47. <div className="flex items-center gap-6 pl-4 md:pl-6">
  48. <Link href="/" className="flex items-center space-x-2">
  49. <Image
  50. src={logoSrc}
  51. alt="vtor.io"
  52. width={32}
  53. height={8}
  54. className="h-8 w-auto"
  55. priority
  56. />
  57. </Link>
  58. {shouldShowBackButton && (
  59. <Link href="/dashboard">
  60. <Button variant="ghost" size="sm">
  61. <LayoutGrid className="w-4 h-4 mr-2" />
  62. Back to Dashboard
  63. </Button>
  64. </Link>
  65. )}
  66. </div>
  67. <div className="flex items-center gap-4 pr-4 md:pr-6">
  68. <div className="h-8 w-20 bg-gray-200 animate-pulse rounded"></div>
  69. </div>
  70. </div>
  71. </header>
  72. );
  73. }
  74. return (
  75. <header className="sticky top-0 z-50 w-full border-b bg-background/95 backdrop-blur supports-[backdrop-filter]:bg-background/60">
  76. <div className="flex h-16 items-center justify-between pr-6">
  77. <div className="flex items-center gap-6 pl-4 md:pl-6">
  78. <Link href="/" className="flex items-center space-x-2">
  79. <Image
  80. src={logoSrc}
  81. alt="vtor.io"
  82. width={32}
  83. height={8}
  84. className="h-8 w-auto"
  85. priority
  86. />
  87. </Link>
  88. {shouldShowBackButton && (
  89. <Link href="/dashboard">
  90. <Button variant="ghost" size="sm">
  91. <LayoutGrid className="w-4 h-4 mr-2" />
  92. Back to Dashboard
  93. </Button>
  94. </Link>
  95. )}
  96. </div>
  97. <div className="flex items-center gap-4 pr-0 md:pr-0">
  98. {isAuthenticated ? (
  99. <>
  100. <NavigationMenu className="hidden md:flex">
  101. <NavigationMenuList>
  102. <NavigationMenuItem>
  103. <NavigationMenuTrigger className="h-9">
  104. <div className="flex items-center gap-2">
  105. <Image
  106. className="rounded-full"
  107. src={user?.picture || "/default-avatar.png"}
  108. alt="User Avatar"
  109. width={24}
  110. height={24}
  111. />
  112. <span className="text-sm font-medium">{user?.given_name || user?.email}</span>
  113. </div>
  114. </NavigationMenuTrigger>
  115. <NavigationMenuContent>
  116. <ul className="grid gap-3 p-6 w-[300px]">
  117. {userMenuItems.map((item) => (
  118. <li key={item.title}>
  119. <NavigationMenuLink asChild>
  120. <Link
  121. href={item.href}
  122. className="block select-none space-y-1 rounded-md p-3 leading-none no-underline outline-none transition-colors hover:bg-accent hover:text-accent-foreground">
  123. <div className="flex items-center gap-2">
  124. <item.icon className="h-4 w-4" />
  125. <div className="text-sm font-medium leading-none">{item.title}</div>
  126. </div>
  127. <p className="line-clamp-2 text-sm leading-snug text-muted-foreground">
  128. {item.description}
  129. </p>
  130. </Link>
  131. </NavigationMenuLink>
  132. </li>
  133. ))}
  134. <li>
  135. <LogoutLink className="block select-none space-y-1 rounded-md p-3 leading-none no-underline outline-none transition-colors hover:bg-accent hover:text-accent-foreground">
  136. <div className="flex items-center gap-2">
  137. <LogOut className="h-4 w-4" />
  138. <div className="text-sm font-medium leading-none">Logout</div>
  139. </div>
  140. <p className="line-clamp-2 text-sm leading-snug text-muted-foreground">
  141. Sign out of your account
  142. </p>
  143. </LogoutLink>
  144. </li>
  145. </ul>
  146. </NavigationMenuContent>
  147. </NavigationMenuItem>
  148. </NavigationMenuList>
  149. </NavigationMenu>
  150. </>
  151. ) : (
  152. <div className="hidden md:flex items-center gap-2">
  153. <LoginLink>
  154. <Button variant="ghost" size="sm">
  155. <LogIn className="h-4 w-4 mr-2" />
  156. Login
  157. </Button>
  158. </LoginLink>
  159. </div>
  160. )}
  161. <DarkModeToggle />
  162. {/* Mobile Menu */}
  163. <Sheet>
  164. <SheetTrigger asChild className="md:hidden">
  165. <Button variant="ghost" size="icon">
  166. <Menu className="h-5 w-5" />
  167. <span className="sr-only">Toggle menu</span>
  168. </Button>
  169. </SheetTrigger>
  170. <SheetContent side="right" className="w-[300px]">
  171. <div className="flex flex-col space-y-4 mt-8">
  172. <Link href="/" className="flex items-center space-x-2">
  173. <Home className="h-4 w-4" />
  174. <span>Home</span>
  175. </Link>
  176. {isAuthenticated ? (
  177. <>
  178. <div className="border-t pt-4">
  179. <div className="flex items-center space-x-2 mb-4">
  180. <Image
  181. className="rounded-full"
  182. src={user?.picture || "/default-avatar.png"}
  183. alt="User Avatar"
  184. width={32}
  185. height={32}
  186. />
  187. <span className="font-medium">{user?.given_name || user?.email}</span>
  188. </div>
  189. {userMenuItems.map((item) => (
  190. <Link
  191. key={item.title}
  192. href={item.href}
  193. className="flex items-center space-x-2 py-2">
  194. <item.icon className="h-4 w-4" />
  195. <span>{item.title}</span>
  196. </Link>
  197. ))}
  198. <LogoutLink className="flex items-center space-x-2 py-2 text-red-600">
  199. <LogOut className="h-4 w-4" />
  200. <span>Logout</span>
  201. </LogoutLink>
  202. </div>
  203. </>
  204. ) : (
  205. <div className="border-t pt-4 space-y-2">
  206. <LoginLink>
  207. <Button variant="ghost" className="w-full justify-start">
  208. <LogIn className="h-4 w-4 mr-2" />
  209. Login
  210. </Button>
  211. </LoginLink>
  212. <RegisterLink>
  213. <Button className="w-full">Sign Up</Button>
  214. </RegisterLink>
  215. </div>
  216. )}
  217. </div>
  218. </SheetContent>
  219. </Sheet>
  220. </div>
  221. </div>
  222. </header>
  223. );
  224. }