diff --git a/src/components/layout/Header.tsx b/src/components/layout/Header.tsx index 06537ff..139291c 100644 --- a/src/components/layout/Header.tsx +++ b/src/components/layout/Header.tsx @@ -1,22 +1,61 @@ "use client"; -import { useEffect, useState } from "react"; +import { useMemo, useState } from "react"; import Image from "next/image"; import { Button } from "@/components/ui/button"; -import { GitFork, Menu } from "lucide-react"; +import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; +import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; +import { GitFork, LogOut, Menu } from "lucide-react"; import Link from "next/link"; import { Sheet, SheetContent, SheetTrigger } from "@/components/ui/sheet"; +import { useRouter } from "next/router"; import { useAuthRole } from "@/lib/useAuthRole"; +import { handleLogout, sanitizeRedirectPath } from "@/lib/utils"; + +const getInitials = (value: string) => { + const tokens = value.trim().split(/\s+/).filter(Boolean); + + if (tokens.length === 0) { + return "U"; + } + + if (tokens.length === 1) { + return tokens[0].slice(0, 2).toUpperCase(); + } + + return `${tokens[0][0]}${tokens[tokens.length - 1][0]}`.toUpperCase(); +}; const Header = ({ isAdmin = false }: { isAdmin?: boolean }) => { - const { role } = useAuthRole(); + const { role, user } = useAuthRole(); + const router = useRouter(); + const [profileOpen, setProfileOpen] = useState(false); const isPanelist = role?.slug === "admin" || role?.slug?.includes("panel"); + const displayName = user?.user_metadata?.full_name ?? user?.user_metadata?.name ?? null; + const displayEmail = user?.email ?? null; + const avatarUrl = + user?.user_metadata?.avatar_url ?? + user?.user_metadata?.picture ?? + user?.user_metadata?.avatarUrl ?? + null; + const fallbackInitials = useMemo(() => { + const baseLabel = displayName ?? displayEmail ?? "User"; + return getInitials(baseLabel); + }, [displayEmail, displayName]); const handleGithubClick = () => { window.open("https://github.com/Robotics-PEC", "_blank"); }; + const loginHref = `/login?redirect=${encodeURIComponent(sanitizeRedirectPath(router.asPath))}`; + + const handleProfileLogout = async () => { + setProfileOpen(false); + await handleLogout(); + router.replace("/login"); + }; + const navigation = [ { name: "Home", path: "/" }, { name: "Projects", path: "/project" }, @@ -69,9 +108,6 @@ const Header = ({ isAdmin = false }: { isAdmin?: boolean }) => { View Website - - Logout - ) } @@ -79,7 +115,64 @@ const Header = ({ isAdmin = false }: { isAdmin?: boolean }) => { {/* Buttons and Mobile Menu */} -
+
+ {!user ? ( + + ) : ( + + + + + +
+
+ + + + {fallbackInitials} + + +
+ {displayName && ( +

+ {displayName} +

+ )} + {displayEmail && ( +

+ {displayEmail} +

+ )} +
+
+ + +
+
+
+ )} + + +

+ By continuing, you agree to our{" "} + + Terms of Service + {" "} + and{" "} + + Privacy Policy + + . +

+
+
+ +
+
+
+
+ + {currentImage ? ( + + Robotics society gallery image +
- {/* Login Card */} -
- - -

- By continuing, you agree to our{" "} - - Terms of Service - {" "} - and{" "} - - Privacy Policy - - . -

+ + ) : ( + +
+
+
+
+
+
+
+ + )} + +
+
+