Files
THE-TIP-TOP/frontend/src/components/Header.jsx

154 lines
5.9 KiB
JavaScript

import { useState } from 'react'
import { Link, useLocation, useNavigate } from 'react-router-dom'
import logo from '../assets/img/logo.webp'
import { useAuth } from '../context/AuthContext'
export default function Header() {
const [menuOpen, setMenuOpen] = useState(false)
const { pathname } = useLocation()
const navigate = useNavigate()
const { user, logout } = useAuth()
const isLoggedIn = !!user
const logoContent = <img src={logo} alt="Logo de Thé Tip Top" className="h-7 sm:h-8 md:h-9 lg:h-12 w-auto" />
const handleLogout = () => {
logout()
setMenuOpen(false)
navigate('/connexion')
}
const navLinks = [
{ to: '/accueil', label: 'Accueil' },
{ to: '/notre-concept', label: 'Notre concept' },
{ to: '/regles-jeu', label: 'Règles du jeu' },
{ to: '/contact', label: 'Contact' },
]
const initials = user
? `${(user.firstname || '?')[0]}${(user.lastname || '?')[0]}`.toUpperCase()
: null
return (
<header className="bg-white shadow-sm relative z-50">
<div className="max-w-7xl mx-auto px-4 sm:px-6 py-4 flex items-center justify-between">
{pathname === '/accueil' ? (
<span className="flex items-center flex-shrink-0">
{logoContent}
</span>
) : (
<Link to ={'/accueil'} className='flex items-center flex-shrink-0'>
{logoContent}
</Link>
)}
{/* Nav desktop */}
<nav className="hidden md:flex items-center gap-4 lg:gap-6">
{navLinks.map((link) => (
<Link
key={link.to}
to={link.to}
className={`font-medium whitespace-nowrap transition-colors ${
pathname === link.to ? 'text-[#006B6B]' : 'text-gray-700 hover:text-[#006B6B]'
}`}
>
{link.label}
</Link>
))}
{isLoggedIn ? (
<div className="flex items-center gap-3">
<Link to="/profile" className="flex items-center gap-2 text-sm text-gray-700 hover:text-[#006B6B] font-medium transition-colors">
<div className="w-8 h-8 rounded-full bg-[#006B6B] flex items-center justify-center text-white text-xs font-bold">
{initials}
</div>
<span className="whitespace-nowrap">{user.firstname}</span>
</Link>
<button
onClick={handleLogout}
className="text-sm text-gray-500 hover:text-red-500 transition-colors font-medium whitespace-nowrap"
>
Déconnexion
</button>
</div>
) : (
<div className="flex items-center gap-2">
<Link to="/connexion" className="bg-[#006B6B] border border-transparent text-white px-4 py-2 rounded-lg hover:bg-[#005a5a] transition-colors text-sm whitespace-nowrap">
Connexion
</Link>
<Link to="/inscription" className="border border-[#006B6B] text-[#006B6B] px-4 py-2 rounded-lg hover:bg-[#006B6B] hover:text-white transition-colors text-sm whitespace-nowrap">
Inscription
</Link>
</div>
)}
</nav>
{/* Hamburger mobile */}
<button
onClick={() => setMenuOpen(!menuOpen)}
className="md:hidden flex flex-col justify-center items-center w-9 h-9 gap-1.5"
aria-label="Ouvrir le menu"
>
<span className={`block w-6 h-0.5 bg-gray-700 transition-all duration-300 origin-center ${menuOpen ? 'rotate-45 translate-y-2' : ''}`} />
<span className={`block w-6 h-0.5 bg-gray-700 transition-all duration-300 ${menuOpen ? 'opacity-0' : ''}`} />
<span className={`block w-6 h-0.5 bg-gray-700 transition-all duration-300 origin-center ${menuOpen ? '-rotate-45 -translate-y-2' : ''}`} />
</button>
</div>
{/* Menu mobile déroulant */}
{menuOpen && (
<div className="md:hidden bg-white border-t border-gray-100 px-4 pb-4">
{navLinks.map((link) => (
<Link
key={link.to}
to={link.to}
onClick={() => setMenuOpen(false)}
className={`block py-3 font-medium border-b border-gray-50 ${
pathname === link.to ? 'text-[#006B6B]' : 'text-gray-700'
}`}
>
{link.label}
</Link>
))}
<div className="flex flex-col gap-2 pt-3">
{isLoggedIn ? (
<>
<Link
to="/mes-gains"
onClick={() => setMenuOpen(false)}
className="w-full text-center bg-[#006B6B] text-white px-4 py-2.5 rounded-lg text-sm font-medium"
>
Mon espace ({user.firstname})
</Link>
<button
onClick={handleLogout}
className="w-full text-center border border-red-300 text-red-500 px-4 py-2.5 rounded-lg text-sm font-medium hover:bg-red-50 transition-colors"
>
Déconnexion
</button>
</>
) : (
<>
<Link
to="/connexion"
onClick={() => setMenuOpen(false)}
className="w-full text-center bg-[#006B6B] text-white px-4 py-2.5 rounded-lg hover:bg-[#005a5a] transition-colors text-sm font-medium"
>
Connexion
</Link>
<Link
to="/inscription"
onClick={() => setMenuOpen(false)}
className="w-full text-center border border-[#006B6B] text-[#006B6B] px-4 py-2.5 rounded-lg hover:bg-[#006B6B] hover:text-white transition-colors text-sm font-medium"
>
Inscription
</Link>
</>
)}
</div>
</div>
)}
</header>
)
}