import { useEffect, useMemo, useState } from 'react'; import { Link, useLocation, useNavigate } from 'react-router-dom'; import './NavigationTabs.css'; const NavigationTabs = ({ isAdmin, onProtectedNavigate, profileName, onLogout }) => { const location = useLocation(); const navigate = useNavigate(); const [mobileOpen, setMobileOpen] = useState(false); const tabs = useMemo(() => { const items = [ { to: '/', label: 'Slots buchen' }, { to: '/store-watch', label: 'Betriebs-Monitoring' }, { to: '/journal', label: 'Abhol-Journal' } ]; if (isAdmin) { items.push({ to: '/debug', label: 'Debug' }); items.push({ to: '/admin', label: 'Admin' }); } return items; }, [isAdmin]); const activeTab = tabs.find((tab) => tab.to === location.pathname) || tabs[0]; const handleClick = (event, to) => { event.preventDefault(); if (to === location.pathname) { return; } const label = tabs.find((tab) => tab.to === to)?.label || ''; const proceed = () => navigate(to); if (onProtectedNavigate) { onProtectedNavigate(`zur Seite "${label}" zu wechseln`, proceed); return; } proceed(); }; useEffect(() => { setMobileOpen(false); }, [location.pathname]); const renderLink = (tab, className) => { const isActive = location.pathname === tab.to; return ( handleClick(event, tab.to)} className={`${className}${isActive ? ' is-active' : ''}`} aria-current={isActive ? 'page' : undefined} > {tab.label} ); }; return ( {activeTab.label} setMobileOpen((open) => !open)} > {mobileOpen ? ( <> > ) : ( <> > )} {mobileOpen && ( {tabs.map((tab) => renderLink(tab, 'app-navigation__mobile-link'))} Angemeldet {profileName || 'Profil'} Logout )} {tabs.map((tab) => renderLink(tab, 'app-navigation__desktop-link'))} ); }; export default NavigationTabs;