pollo/src/components/Navbar.tsx

113 lines
3.1 KiB
TypeScript
Raw Normal View History

2023-07-11 17:22:54 -06:00
import { signIn, signOut, useSession } from "next-auth/react";
2023-04-20 04:20:00 -06:00
import Image from "next/image";
2023-07-11 17:22:54 -06:00
import Link from "next/link";
import { useRouter } from "next/router";
2023-07-11 17:22:54 -06:00
import { env } from "~/env.mjs";
2023-04-20 04:20:00 -06:00
interface NavbarProps {
title: string;
}
const Navbar: React.FC<NavbarProps> = ({ title }) => {
const { data: sessionData, status: sessionStatus } = useSession();
const router = useRouter();
const navigationMenu = () => {
if (sessionStatus === "authenticated" && router.pathname !== "/dashboard") {
return (
<Link className="btn btn-secondary btn-outline mx-2" href="/dashboard">
Dashboard
</Link>
);
} else if (sessionStatus === "unauthenticated") {
return (
2023-07-11 18:19:45 -06:00
<button className="btn btn-secondary" onClick={ () => void signIn() }>
Sign In
</button>
);
}
};
2023-04-20 04:20:00 -06:00
return (
<nav className="navbar bg-base-100 h-12">
2023-04-20 04:20:00 -06:00
<div className="flex-1">
<Link
about="Back to home."
href="/"
className="btn btn-ghost normal-case text-xl"
>
<Image
className="md:mr-2"
2023-04-20 04:20:00 -06:00
src="/logo.webp"
alt="Nav Logo"
2023-07-11 18:19:45 -06:00
width={ 32 }
height={ 32 }
2023-04-20 04:20:00 -06:00
priority
/>
<span className="hidden md:inline-flex">
2023-07-11 18:19:45 -06:00
{ title }
{ env.NEXT_PUBLIC_APP_ENV === "development" && " >> Staging" }
</span>
2023-04-20 04:20:00 -06:00
</Link>
</div>
2023-07-11 18:19:45 -06:00
{ sessionStatus === "loading" ? (
2023-06-19 16:39:05 -06:00
<div className="flex items-center justify-center">
2023-06-19 23:46:12 -06:00
<span className="loading loading-dots loading-lg"></span>
2023-06-19 16:39:05 -06:00
</div>
) : (
navigationMenu()
2023-07-11 18:19:45 -06:00
) }
2023-07-11 18:19:45 -06:00
{ sessionData?.user.image && (
2023-04-20 04:20:00 -06:00
<div className="flex-none gap-2">
<div className="dropdown dropdown-end">
2023-07-11 18:19:45 -06:00
<label tabIndex={ 0 } className="btn btn-ghost btn-circle avatar">
2023-04-20 04:20:00 -06:00
<div className="w-10 rounded-full">
<Image
2023-07-11 18:19:45 -06:00
src={ sessionData.user.image }
2023-04-20 04:20:00 -06:00
alt="Profile picture."
2023-07-11 18:19:45 -06:00
height={ 32 }
width={ 32 }
2023-04-20 04:20:00 -06:00
/>
</div>
</label>
<ul
2023-07-11 18:19:45 -06:00
tabIndex={ 0 }
2023-06-12 18:24:29 -06:00
className="mt-3 p-2 shadow menu menu-compact dropdown-content bg-base-100 rounded-box z-50"
2023-04-20 04:20:00 -06:00
>
<li>
<Link
about="Profile Page"
href="/profile"
className="justify-between"
>
Profile
</Link>
</li>
2023-07-11 18:19:45 -06:00
{ sessionData.user.role === "ADMIN" && (
2023-04-20 04:20:00 -06:00
<li>
<Link
about="Admin Page"
href="/admin"
className="justify-between"
>
Admin
</Link>
</li>
2023-07-11 18:19:45 -06:00
) }
2023-04-20 04:20:00 -06:00
<li>
2023-07-11 18:19:45 -06:00
<a onClick={ () => void signOut({ callbackUrl: "/" }) }>
Sign Out
</a>
2023-04-20 04:20:00 -06:00
</li>
</ul>
</div>
</div>
2023-07-11 18:19:45 -06:00
) }
2023-04-20 04:20:00 -06:00
</nav>
);
};
export default Navbar;