import { Fragment, useEffect, useState } from "react"; import { Disclosure, Menu, Transition } from "@headlessui/react"; import Link from "next/link"; import { useRouter } from "next/router"; import { signOut, useSession } from "next-auth/react"; import avatarFromInitials from "avatar-from-initials"; import { toast } from "react-hot-toast"; import { Bars3Icon, BellIcon, XMarkIcon, UserCircleIcon, ArrowRightOnRectangleIcon, DocumentTextIcon, ChartBarIcon, WrenchIcon, } from "@heroicons/react/24/outline"; import Logo from "./logo"; const navigation = [ { name: "Dashboard", href: "/dashboard", current: false, icon: ChartBarIcon, }, { name: "Documents", href: "/documents", current: false, icon: DocumentTextIcon, }, { name: "Settings", href: "/settings", current: true, icon: WrenchIcon, }, ]; const userNavigation = [ { name: "Your Profile", href: "/settings/profile", icon: UserCircleIcon }, { name: "Sign out", href: "", click: async (e: any) => { e.preventDefault(); const res: any = await toast.promise( signOut({ callbackUrl: "/login" }), { loading: "Logging out...", success: "Your are logged out.", error: "Could not log out :/", }, { style: { minWidth: "200px", }, success: { duration: 10000, }, } ); }, icon: ArrowRightOnRectangleIcon, }, ]; function classNames(...classes: any) { return classes.filter(Boolean).join(" "); } type UserType = { id?: number | undefined; name?: string | null; email?: string | null; image?: string | null; }; export default function TopNavigation() { const router = useRouter(); const session = useSession(); const [user, setUser] = useState({ email: "", name: "", }); useEffect(() => { // todo encapsulate fetch("/api/users/me").then((res) => { res.json().then((j) => { setUser(j); }); }); }, [session]); navigation.forEach((element) => { element.current = router.route.endsWith("/" + element.href.split("/")[1]); }); return ( <> {({ open }) => ( <>
{navigation.map((item) => ( {item.name} ))}
{ document?.getElementById("mb")?.click(); }} className="hidden sm:ml-6 sm:flex sm:items-center hover:bg-gray-200 px-3 cursor-pointer" >

{user?.name || ""}

{user?.email}

Open user menu
{userNavigation.map((item) => ( {({ active }) => ( {item.name} )} ))}
{/* Mobile menu button */} Open main menu {open ? (
{navigation.map((item) => ( {item.name} ))}
{user?.name || ""}
{user?.email}
{userNavigation.map((item) => ( {item.name} ))}
)} {/* */} ); }