In the header make use of next/navigation to get the path name and display current link

This commit is contained in:
kbe
2025-08-14 17:08:56 +02:00
parent 51b81c40af
commit d50555cbc4

View File

@@ -3,7 +3,9 @@
import * as React from "react" import * as React from "react"
import { cn } from "@/lib/utils" import { cn } from "@/lib/utils"
import Link from "next/link" import Link from "next/link"
import { useState } from "react" import { useState, useEffect } from "react"
import { usePathname } from "next/navigation"
interface HeaderProps { interface HeaderProps {
className?: string className?: string
@@ -24,6 +26,22 @@ const navigation = [
export function Header({ className, isLoggedIn = false, isAdmin = false }: 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)
const [activeNavItem, setActiveNavItem] = useState<string | null>(null)
// Use usePathname for client-side navigation
let pathname = usePathname()
// Update activeNavItem when pathname changes
useEffect(() => {
if (pathname) {
setActiveNavItem(pathname)
}
}, [pathname])
// Only render the header after the pathname is available
if (!pathname) {
return null
}
return ( return (
<header className={cn("bg-gray-900 shadow-sm", className)}> <header className={cn("bg-gray-900 shadow-sm", className)}>
@@ -41,23 +59,26 @@ export function Header({ className, isLoggedIn = false, isAdmin = false }: Heade
</div> </div>
<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) => {
(item.requiresAuth ? (isLoggedIn || isAdmin) : true) && ( const isCurrent = activeNavItem === item.href
<Link return (
key={item.name} (item.requiresAuth ? (isLoggedIn || isAdmin) : true) && (
href={item.href} <Link
className={cn( key={item.name}
item.current href={item.href}
? 'bg-gray-800 text-white' className={cn(
: 'text-gray-300 hover:bg-gray-700 hover:text-white', isCurrent
'rounded-md px-3 py-2 text-sm font-medium transition-colors' ? 'bg-gray-800 text-white'
)} : 'text-gray-300 hover:bg-gray-700 hover:text-white',
aria-current={item.current ? 'page' : undefined} 'rounded-md px-3 py-2 text-sm font-medium transition-colors'
> )}
{item.name} aria-current={isCurrent ? 'page' : undefined}
</Link> >
{item.name}
</Link>
)
) )
))} })}
</div> </div>
</div> </div>
</div> </div>
@@ -139,23 +160,26 @@ export function Header({ className, isLoggedIn = false, isAdmin = false }: Heade
{mobileMenuOpen && ( {mobileMenuOpen && (
<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) => {
(item.requiresAuth ? (isLoggedIn || isAdmin) : true) && ( const isCurrent = activeNavItem === item.href
<Link return (
key={item.name} (item.requiresAuth ? (isLoggedIn || isAdmin) : true) && (
href={item.href} <Link
className={cn( key={item.name}
item.current href={item.href}
? 'bg-gray-900 text-white' className={cn(
: 'text-gray-300 hover:bg-gray-700 hover:text-white', isCurrent
'block rounded-md px-3 py-2 text-base font-medium transition-colors' ? 'bg-gray-900 text-white'
)} : 'text-gray-300 hover:bg-gray-700 hover:text-white',
aria-current={item.current ? 'page' : undefined} 'block rounded-md px-3 py-2 text-base font-medium transition-colors'
> )}
{item.name} aria-current={isCurrent ? 'page' : undefined}
</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">
{isLoggedIn && ( {isLoggedIn && (