Add conditional link for logged in user and administrator

This commit is contained in:
kbe
2025-08-14 16:51:20 +02:00
parent 7641675180
commit 51b81c40af

View File

@@ -7,16 +7,21 @@ import { useState } from "react"
interface HeaderProps { interface HeaderProps {
className?: string className?: string
isLoggedIn?: boolean
isAdmin?: boolean
} }
const navigation = [ const navigation = [
{ name: 'Dashboard', href: '/dashboard', current: true }, { name: 'Home', href: '/', current: false, requiresAuth: false },
{ name: 'Projects', href: '/projects', current: false }, { name: 'Pricing', href: '/pricing', current: false, requiresAuth: false },
{ name: 'Calendar', href: '/calendar', current: false }, { name: 'FAQ', href: '/faq', current: false, requiresAuth: false },
{ name: 'Reports', href: '/reports', current: false }, { name: 'Dashboard', href: '/dashboard', current: false, requiresAuth: false },
{ name: 'Projects', href: '/projects', current: false, requiresAuth: true },
{ name: 'Calendar', href: '/calendar', current: false, requiresAuth: false },
{ name: 'Reports', href: '/reports', current: false, requiresAuth: true },
] ]
export function Header({ className }: HeaderProps) { export function Header({ className, isLoggedIn = false, isAdmin = false }: HeaderProps) {
const [mobileMenuOpen, setMobileMenuOpen] = useState(false) const [mobileMenuOpen, setMobileMenuOpen] = useState(false)
const [profileOpen, setProfileOpen] = useState(false) const [profileOpen, setProfileOpen] = useState(false)
@@ -37,71 +42,75 @@ export function Header({ className }: HeaderProps) {
<div className="hidden md:block"> <div className="hidden md:block">
<div className="ml-10 flex items-baseline space-x-4"> <div className="ml-10 flex items-baseline space-x-4">
{navigation.map((item) => ( {navigation.map((item) => (
<Link (item.requiresAuth ? (isLoggedIn || isAdmin) : true) && (
key={item.name} <Link
href={item.href} key={item.name}
className={cn( href={item.href}
item.current className={cn(
? 'bg-gray-800 text-white' item.current
: 'text-gray-300 hover:bg-gray-700 hover:text-white', ? 'bg-gray-800 text-white'
'rounded-md px-3 py-2 text-sm font-medium transition-colors' : 'text-gray-300 hover:bg-gray-700 hover:text-white',
)} 'rounded-md px-3 py-2 text-sm font-medium transition-colors'
aria-current={item.current ? 'page' : undefined} )}
> aria-current={item.current ? 'page' : undefined}
{item.name} >
</Link> {item.name}
</Link>
)
))} ))}
</div> </div>
</div> </div>
</div> </div>
<div className="hidden md:block"> {isLoggedIn && (
<div className="ml-4 flex items-center md:ml-6"> <div className="hidden md:block">
<button <div className="ml-4 flex items-center md:ml-6">
type="button" <button
className="relative rounded-full bg-gray-800 p-1 text-gray-400 hover:text-white focus:outline-none focus:ring-2 focus:ring-white focus:ring-offset-2 focus:ring-offset-gray-800" type="button"
> className="relative rounded-full bg-gray-800 p-1 text-gray-400 hover:text-white focus:outline-none focus:ring-2 focus:ring-white focus:ring-offset-2 focus:ring-offset-gray-800"
<span className="absolute -inset-1.5" /> >
<span className="sr-only">View notifications</span> <span className="absolute -inset-1.5" />
<svg className="h-6 w-6" fill="none" viewBox="0 0 24 24" strokeWidth="1.5" stroke="currentColor"> <span className="sr-only">View notifications</span>
<path strokeLinecap="round" strokeLinejoin="round" d="M14.857 17.082a23.848 23.848 0 005.454-1.31A8.967 8.967 0 0118 9.75v-.7V9A6 6 0 006 9v.75a8.967 8.967 0 01-2.312 6.022c1.733.64 3.56 1.085 5.455 1.31m5.714 0a24.255 24.255 0 01-5.714 0m5.714 0a3 3 0 11-5.714 0" /> <svg className="h-6 w-6" fill="none" viewBox="0 0 24 24" strokeWidth="1.5" stroke="currentColor">
</svg> <path strokeLinecap="round" strokeLinejoin="round" d="M14.857 17.082a23.848 23.848 0 005.454-1.31A8.967 8.967 0 0118 9.75v-.7V9A6 6 0 006 9v.75a8.967 8.967 0 01-2.312 6.022c1.733.64 3.56 1.085 5.455 1.31m5.714 0a24.255 24.255 0 01-5.714 0m5.714 0a3 3 0 11-5.714 0" />
</button> </svg>
</button>
{/* Profile dropdown */} {/* Profile dropdown */}
<div className="relative ml-3"> <div className="relative ml-3">
<div> <div>
<button <button
type="button" type="button"
className="relative flex max-w-xs items-center rounded-full bg-gray-800 text-sm focus:outline-none focus:ring-2 focus:ring-white focus:ring-offset-2 focus:ring-offset-gray-800" className="relative flex max-w-xs items-center rounded-full bg-gray-800 text-sm focus:outline-none focus:ring-2 focus:ring-white focus:ring-offset-2 focus:ring-offset-gray-800"
onClick={() => setProfileOpen(!profileOpen)} onClick={() => setProfileOpen(!profileOpen)}
> >
<span className="absolute -inset-1.5" /> <span className="absolute -inset-1.5" />
<span className="sr-only">Open user menu</span> <span className="sr-only">Open user menu</span>
<img <img
className="h-8 w-8 rounded-full" className="h-8 w-8 rounded-full"
src="https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=facearea&facepad=2&w=256&h=256&q=80" src="https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=facearea&facepad=2&w=256&h=256&q=80"
alt="" alt=""
/> />
</button> </button>
</div>
{profileOpen && (
<div className="absolute right-0 z-10 mt-2 w-48 origin-top-right rounded-md bg-white py-1 shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none">
<Link href="/profile" className="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100">
Your Profile
</Link>
<Link href="/settings" className="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100">
Settings
</Link>
<Link href="/logout" className="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100">
Sign out
</Link>
</div> </div>
)}
{profileOpen && (
<div className="absolute right-0 z-10 mt-2 w-48 origin-top-right rounded-md bg-white py-1 shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none">
<Link href="/profile" className="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100">
Your Profile
</Link>
<Link href="/settings" className="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100">
Settings
</Link>
<Link href="/logout" className="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100">
Sign out
</Link>
</div>
)}
</div>
</div> </div>
</div> </div>
</div> )}
<div className="-mr-2 flex md:hidden"> <div className="-mr-2 flex md:hidden">
{/* Mobile menu button */} {/* Mobile menu button */}
@@ -131,56 +140,62 @@ export function Header({ className }: HeaderProps) {
<div className="md:hidden"> <div className="md:hidden">
<div className="space-y-1 px-2 pb-3 pt-2 sm:px-3"> <div className="space-y-1 px-2 pb-3 pt-2 sm:px-3">
{navigation.map((item) => ( {navigation.map((item) => (
<Link (item.requiresAuth ? (isLoggedIn || isAdmin) : true) && (
key={item.name} <Link
href={item.href} key={item.name}
className={cn( href={item.href}
item.current className={cn(
? 'bg-gray-900 text-white' item.current
: 'text-gray-300 hover:bg-gray-700 hover:text-white', ? 'bg-gray-900 text-white'
'block rounded-md px-3 py-2 text-base font-medium transition-colors' : 'text-gray-300 hover:bg-gray-700 hover:text-white',
)} 'block rounded-md px-3 py-2 text-base font-medium transition-colors'
aria-current={item.current ? 'page' : undefined} )}
> aria-current={item.current ? 'page' : undefined}
{item.name} >
</Link> {item.name}
</Link>
)
))} ))}
</div> </div>
<div className="border-t border-gray-700 pb-3 pt-4"> <div className="border-t border-gray-700 pb-3 pt-4">
<div className="flex items-center px-5"> {isLoggedIn && (
<div className="flex-shrink-0"> <div>
<img <div className="flex items-center px-5">
className="h-10 w-10 rounded-full" <div className="flex-shrink-0">
src="https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=facearea&facepad=2&w=256&h=256&q=80" <img
alt="" className="h-10 w-10 rounded-full"
/> src="https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=facearea&facepad=2&w=256&h=256&q=80"
alt=""
/>
</div>
<div className="ml-3">
<div className="text-base font-medium leading-none text-white">User Name</div>
<div className="text-sm font-medium leading-none text-gray-400">user@example.com</div>
</div>
<button
type="button"
className="relative ml-auto flex-shrink-0 rounded-full bg-gray-800 p-1 text-gray-400 hover:text-white focus:outline-none focus:ring-2 focus:ring-white focus:ring-offset-2 focus:ring-offset-gray-800"
>
<span className="absolute -inset-1.5" />
<span className="sr-only">View notifications</span>
<svg className="h-6 w-6" fill="none" viewBox="0 0 24 24" strokeWidth="1.5" stroke="currentColor">
<path strokeLinecap="round" strokeLinejoin="round" d="M14.857 17.082a23.848 23.848 0 005.454-1.31A8.967 8.967 0 0118 9.75v-.7V9A6 6 0 006 9v.75a8.967 8.967 0 01-2.312 6.022c1.733.64 3.56 1.085 5.455 1.31m5.714 0a24.255 24.255 0 01-5.714 0m5.714 0a3 3 0 11-5.714 0" />
</svg>
</button>
</div>
<div className="mt-3 space-y-1 px-2">
<Link href="/profile" className="block rounded-md px-3 py-2 text-base font-medium text-gray-400 hover:bg-gray-700 hover:text-white">
Your Profile
</Link>
<Link href="/settings" className="block rounded-md px-3 py-2 text-base font-medium text-gray-400 hover:bg-gray-700 hover:text-white">
Settings
</Link>
<Link href="/logout" className="block rounded-md px-3 py-2 text-base font-medium text-gray-400 hover:bg-gray-700 hover:text-white">
Sign out
</Link>
</div>
</div> </div>
<div className="ml-3"> )}
<div className="text-base font-medium leading-none text-white">User Name</div>
<div className="text-sm font-medium leading-none text-gray-400">user@example.com</div>
</div>
<button
type="button"
className="relative ml-auto flex-shrink-0 rounded-full bg-gray-800 p-1 text-gray-400 hover:text-white focus:outline-none focus:ring-2 focus:ring-white focus:ring-offset-2 focus:ring-offset-gray-800"
>
<span className="absolute -inset-1.5" />
<span className="sr-only">View notifications</span>
<svg className="h-6 w-6" fill="none" viewBox="0 0 24 24" strokeWidth="1.5" stroke="currentColor">
<path strokeLinecap="round" strokeLinejoin="round" d="M14.857 17.082a23.848 23.848 0 005.454-1.31A8.967 8.967 0 0118 9.75v-.7V9A6 6 0 006 9v.75a8.967 8.967 0 01-2.312 6.022c1.733.64 3.56 1.085 5.455 1.31m5.714 0a24.255 24.255 0 01-5.714 0m5.714 0a3 3 0 11-5.714 0" />
</svg>
</button>
</div>
<div className="mt-3 space-y-1 px-2">
<Link href="/profile" className="block rounded-md px-3 py-2 text-base font-medium text-gray-400 hover:bg-gray-700 hover:text-white">
Your Profile
</Link>
<Link href="/settings" className="block rounded-md px-3 py-2 text-base font-medium text-gray-400 hover:bg-gray-700 hover:text-white">
Settings
</Link>
<Link href="/logout" className="block rounded-md px-3 py-2 text-base font-medium text-gray-400 hover:bg-gray-700 hover:text-white">
Sign out
</Link>
</div>
</div> </div>
</div> </div>
)} )}