import { Suspense, useCallback, useEffect, useState } from 'react'; import { Link, NavLink, Outlet, useNavigate } from 'react-router-dom'; import { useAuth } from '../context/AuthContext.jsx'; import { AdminUiProvider } from '../context/AdminUiContext.jsx'; import SessionGuard from './admin/SessionGuard.jsx'; import { api } from '../api/client.js'; import { SITE_AREAS } from '../utils/siteAreas.js'; import ThemeToggle from './ThemeToggle.jsx'; import { CustomFontsProvider } from '../context/CustomFontsContext.jsx'; import CustomFontFaces from './CustomFontFaces.jsx'; import logo from '../assets/logo.png'; import logoDark from '../assets/logo-dark.png'; const ICONS = { home: 'M3 11.5 12 4l9 7.5M5 10v9a1 1 0 0 0 1 1h4v-6h4v6h4a1 1 0 0 0 1-1v-9', tag: 'M4 4h7l9 9-7 7-9-9V4Zm3.5 3.5h.01', car: 'M3 13.5 4.5 9A2 2 0 0 1 6.4 7.5h11.2A2 2 0 0 1 19.5 9L21 13.5M3 13.5v4a1 1 0 0 0 1 1h1a1 1 0 0 0 1-1v-1h12v1a1 1 0 0 0 1 1h1a1 1 0 0 0 1-1v-4M3 13.5h18M6.5 16.5h.01M17.5 16.5h.01', box: 'M12 3 4 7.5v9L12 21l8-4.5v-9L12 3Zm0 9 8-4.5M12 12v9M12 12 4 7.5', mail: 'M4 6h16a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1Zm0 0 8 7 8-7', users: 'M16 7a4 4 0 1 1-8 0 4 4 0 0 1 8 0Zm-4 6c-4 0-7 2-7 4.5V19h14v-1.5C19 15 16 13 12 13Z', shield: 'M12 3 4 6v6c0 4.5 3.4 7.7 8 9 4.6-1.3 8-4.5 8-9V6l-8-3Zm-1.5 9.5 4-4 1 1-5 5-2.5-2.5 1-1 1.5 1.5Z', lock: 'M6 10V8a6 6 0 1 1 12 0v2m-13 0h14a1 1 0 0 1 1 1v9a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1v-9a1 1 0 0 1 1-1Zm7 5v2', briefcase: 'M4 7h16a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1V8a1 1 0 0 1 1-1Zm4 0V5a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2M3 12h18', sliders: 'M4 6h16M4 6a2 2 0 1 0 4 0 2 2 0 0 0-4 0Zm16 6H4m10 0a2 2 0 1 0 4 0 2 2 0 0 0-4 0ZM4 18h16m-12 0a2 2 0 1 0 4 0 2 2 0 0 0-4 0Z', image: 'M4 5h16a1 1 0 0 1 1 1v12a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1V6a1 1 0 0 1 1-1Zm2 12 4.5-5.5L14 16l2.5-3L20 17H6ZM8 9.5a1.5 1.5 0 1 1-3 0 1.5 1.5 0 0 1 3 0Z', help: 'M12 21a9 9 0 1 0 0-18 9 9 0 0 0 0 18Zm0-5.5v.01M9.5 9.25a2.5 2.5 0 1 1 3.5 2.29c-.7.32-1 .77-1 1.46v.3', download: 'M12 4v11m0 0-4-4m4 4 4-4M5 19h14', gauge: 'M4 13a8 8 0 1 1 16 0M12 13l3.5-3.5M6 17h12', inbox: 'M3 13h5l1.5 2.5h5L16 13h5M5 5h14l2 8v5a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1v-5l2-8Z', building: 'M4 21V5a1 1 0 0 1 1-1h9a1 1 0 0 1 1 1v16M15 9h4a1 1 0 0 1 1 1v11M3 21h18M8 8h3M8 12h3M8 16h3', palette: 'M12 3a9 9 0 1 0 0 18c1 0 1.5-.7 1.5-1.5 0-.4-.2-.8-.4-1.1-.3-.3-.4-.7-.4-1.1 0-.8.7-1.5 1.5-1.5H16a5 5 0 0 0 5-5c0-4.4-4-7.8-9-7.8ZM7.5 12h.01M10 7.5h.01M15 7.5h.01', file: 'M7 3h7l5 5v12a1 1 0 0 1-1 1H7a1 1 0 0 1-1-1V4a1 1 0 0 1 1-1Zm7 0v5h5M9 13h6M9 17h6', wrench: 'M14.5 6.5a4 4 0 0 0-5.3 5.3L4 17l3 3 5.2-5.2a4 4 0 0 0 5.3-5.3l-2.5 2.5-2.5-.5-.5-2.5 2.5-2.5Z', server: 'M4 4h16v6H4V4Zm0 10h16v6H4v-6Zm3-7h.01M7 17h.01', link: 'M10 13a5 5 0 0 0 7.07 0l3-3a5 5 0 0 0-7.07-7.07l-1.5 1.5M14 11a5 5 0 0 0-7.07 0l-3 3a5 5 0 0 0 7.07 7.07l1.5-1.5', globe: 'M12 21a9 9 0 1 0 0-18 9 9 0 0 0 0 18Zm-9-9h18M12 3c2.5 2.5 3.5 5.5 3.5 9s-1 6.5-3.5 9c-2.5-2.5-3.5-5.5-3.5-9s1-6.5 3.5-9Z', send: 'M4 12 20 4l-4 16-4-7-8-1Zm8 1 8-9', refresh: 'M20 11a8 8 0 0 0-14.9-3M4 4v4h4m-4 5a8 8 0 0 0 14.9 3M20 20v-4h-4', trash: 'M5 7h14M10 11v6m4-6v6M6 7l1 13h10l1-13M9 7V4h6v3', external: 'M14 4h6v6m0-6-9 9M19 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V6a1 1 0 0 1 1-1h5', pause: 'M12 21a9 9 0 1 0 0-18 9 9 0 0 0 0 18Zm-2-12v6m4-6v6', timer: 'M10 2h4M12 14l3-3M12 22a8 8 0 1 0 0-16 8 8 0 0 0 0 16Z', }; const Icon = ({ path }) => ( ); export default function AdminLayout() { const { logout, user, hasPermission } = useAuth(); const navigate = useNavigate(); const [mobileOpen, setMobileOpen] = useState(false); const [newContactCount, setNewContactCount] = useState(0); const [customFonts, setCustomFonts] = useState([]); // Fuer die Warnleiste oben: ausgeblendete Website-Bereiche und aktiver Wartungsmodus sollen // nie unbemerkt bleiben. const [hiddenAreas, setHiddenAreas] = useState([]); const [maintenanceOn, setMaintenanceOn] = useState(false); const [countdownUntil, setCountdownUntil] = useState(null); // Wird auch von Seiten aufgerufen (useAdminUi().refreshCounters), z. B. nachdem eine Anfrage // auf "Erledigt" gesetzt oder ein Bereich ausgeblendet wurde - frueher blieb das Badge bis // zum Neuladen falsch. const refreshCounters = useCallback(() => { if (hasPermission('contact.manage')) { api.get('/contact').then((rows) => setNewContactCount(rows.filter((r) => r.status === 'new').length)).catch(() => {}); } api.get('/site-settings').then((res) => setHiddenAreas(res.hidden_areas || [])).catch(() => {}); api.get('/maintenance').then((res) => { setMaintenanceOn(!!res?.global?.enabled); setCountdownUntil(res?.countdown?.active ? res.countdown.launch_at : null); }).catch(() => {}); // eslint-disable-next-line react-hooks/exhaustive-deps }, [user?.id]); useEffect(() => { refreshCounters(); const timer = setInterval(refreshCounters, 2 * 60 * 1000); return () => clearInterval(timer); }, [refreshCounters]); const hiddenAreaLabels = SITE_AREAS.filter((a) => hiddenAreas.includes(a.key)).map((a) => a.label); const loadCustomFonts = () => api.get('/custom-fonts').then(setCustomFonts).catch(() => {}); useEffect(() => { loadCustomFonts(); }, []); // Aufgebaut nach Aufgaben statt nach Technik: oben das Tagesgeschaeft, ganz unten der // Bereich "System" mit allem, was man im Alltag nicht braucht (und besser nicht anfasst). const navGroups = [ { title: null, links: [ { to: '/admin', label: 'Übersicht', icon: 'gauge', end: true }, { to: '/admin/contact-requests', label: 'Anfragen', icon: 'inbox', permission: 'contact.manage', badge: newContactCount }, ], }, { title: 'Fahrzeuge & Pakete', links: [ { to: '/admin/brands', label: 'Fahrzeugmarken', icon: 'tag', permission: 'brands.manage' }, { to: '/admin/models', label: 'Modelle', icon: 'car', permission: 'models.manage' }, { to: '/admin/packages', label: 'Pakete', icon: 'box', permission: 'packages.manage' }, ], }, { title: 'Website-Inhalte', links: [ { to: '/admin/home-sections', label: 'Startseite', icon: 'home', permission: 'content.manage' }, { to: '/admin/services', label: 'Leistungen', icon: 'briefcase', permission: 'services.manage' }, { to: '/admin/faqs', label: 'FAQ', icon: 'help', permission: 'content.manage' }, { to: '/admin/partners', label: 'Partner-Logos', icon: 'shield', permission: 'content.manage' }, { to: '/admin/downloads', label: 'Downloads', icon: 'download', permission: 'downloads.manage' }, ], }, { title: 'Bildergalerie', links: [ { to: '/admin/gallery-brands', label: 'Galerie-Marken', icon: 'image', permission: 'gallery.manage' }, { to: '/admin/gallery-projects', label: 'Projekte & Fotos', icon: 'image', permission: 'gallery.manage' }, ], }, { title: 'Firma & Aussehen', links: [ { to: '/admin/settings/company', label: 'Firmendaten', icon: 'building', permission: 'settings.manage' }, { to: '/admin/settings/design', label: 'Aussehen', icon: 'palette', permission: 'settings.manage' }, { to: '/admin/settings/legal', label: 'Rechtstexte', icon: 'file', permission: 'settings.manage' }, { to: '/admin/settings/maintenance', label: 'Wartungsmodus', icon: 'pause', permission: 'settings.manage' }, { to: '/admin/settings/countdown', label: 'Launch-Countdown', icon: 'timer', permission: 'settings.manage' }, ], }, { title: 'System (Experten)', links: [ { to: '/admin/users', label: 'Benutzer', icon: 'users', permission: 'users.manage' }, { to: '/admin/permission-groups', label: 'Berechtigungsgruppen', icon: 'lock', permission: 'permission_groups.manage' }, { to: '/admin/settings/email', label: 'E-Mail-Versand', icon: 'send', permission: 'system.admin' }, { to: '/admin/settings/google', label: 'Google-Dienste', icon: 'globe', permission: 'system.admin' }, { to: '/admin/settings/connections', label: 'Verbindungstest', icon: 'link', permission: 'system.admin' }, { to: '/admin/settings/database', label: 'Datenbank', icon: 'server', permission: 'system.admin' }, { to: '/admin/settings/export-import', label: 'Export & Import', icon: 'download', permission: 'system.admin' }, { to: '/admin/settings/update', label: 'Updates', icon: 'refresh', permission: 'system.update' }, { to: '/admin/settings/reset', label: 'Zurücksetzen', icon: 'trash', permission: 'system.admin' }, ], }, { title: null, links: [{ to: '/admin/account', label: 'Mein Konto', icon: 'lock' }], }, ] .map((group) => ({ ...group, links: group.links.filter((l) => !l.permission || hasPermission(l.permission)) })) .filter((group) => group.links.length > 0); const handleLogout = async () => { await logout(); navigate('/admin/login'); }; const linkClass = ({ isActive }) => `flex items-center justify-between gap-2 rounded-md px-3 py-2 text-sm font-medium transition ${ isActive ? 'bg-brand-500 text-white' : 'text-neutral-600 hover:bg-neutral-100 dark:text-neutral-300 dark:hover:bg-neutral-800' }`; const NavLinks = ({ onNavigate }) => ( ); return (
HifiPlanet HifiPlanet

Angemeldet als {user?.username}{user?.is_super_admin ? ' · Super-Admin' : ''}

{mobileOpen && (
setMobileOpen(false)} />
)}
{(maintenanceOn || countdownUntil || hiddenAreaLabels.length > 0) && (
{maintenanceOn && (

Wartungsmodus ist aktiv – Besucher sehen gerade nur die Wartungsseite.{' '} {hasPermission('settings.manage') && Ändern}

)} {countdownUntil && (

Launch-Countdown läuft – Besucher sehen bis{' '} {new Date(countdownUntil).toLocaleString('de-DE', { dateStyle: 'medium', timeStyle: 'short', timeZone: 'Europe/Berlin' })} Uhr nur den Countdown.{' '} {hasPermission('settings.manage') && Ändern}

)} {hiddenAreaLabels.length > 0 && (

Ausgeblendet für Besucher: {hiddenAreaLabels.join(', ')}.{' '} Auf der Übersicht ändern

)}
)}
{/* Admin-Seiten werden einzeln nachgeladen (App.jsx) - Seitenleiste und Kopf bleiben dabei stehen, nur der Inhaltsbereich zeigt kurz "Lädt…". */} Lädt…

}>
); }