diff --git a/hifi-src/src/components/AdminLayout.jsx b/hifi-src/src/components/AdminLayout.jsx index 545b7a1..71219e7 100644 --- a/hifi-src/src/components/AdminLayout.jsx +++ b/hifi-src/src/components/AdminLayout.jsx @@ -1,65 +1,111 @@ -import { Suspense, useCallback, useEffect, useState } from 'react'; -import { Link, NavLink, Outlet, useNavigate } from 'react-router-dom'; +import { Suspense, useCallback, useEffect, useMemo, useRef, useState } from 'react'; +import { Link, NavLink, Outlet, useLocation, useNavigate } from 'react-router-dom'; import { useAuth } from '../context/AuthContext.jsx'; import { AdminUiProvider } from '../context/AdminUiContext.jsx'; +import { useTheme } from '../context/ThemeContext.jsx'; import SessionGuard from './admin/SessionGuard.jsx'; +import CommandPalette from './admin/CommandPalette.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 useDialog from '../hooks/useDialog.js'; +import { Icon, buttonClass } from './ui/index.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 COLLAPSE_KEY = 'hifi-admin-nav-collapsed'; -const Icon = ({ path }) => ( - - - -); +// Standardmaessig zugeklappt: im Alltag braucht man den Technikbereich nicht. +const DEFAULT_COLLAPSED = { system: true }; + +function readCollapsed() { + try { + return { ...DEFAULT_COLLAPSED, ...JSON.parse(localStorage.getItem(COLLAPSE_KEY) || '{}') }; + } catch { + return DEFAULT_COLLAPSED; + } +} + +const isActivePath = (pathname, link) => (link.end ? pathname === link.to : pathname === link.to || pathname.startsWith(`${link.to}/`)); + +/** Benutzer-Kreis oben rechts: Mein Konto, Hell/Dunkel und Abmelden an einer Stelle. */ +function UserMenu({ user, onLogout }) { + const [open, setOpen] = useState(false); + const ref = useRef(null); + const { theme, toggleTheme } = useTheme(); + const name = user?.username || ''; + const initials = name.slice(0, 2).toUpperCase(); + + useEffect(() => { + if (!open) return undefined; + const onDown = (e) => !ref.current?.contains(e.target) && setOpen(false); + const onKey = (e) => e.key === 'Escape' && setOpen(false); + document.addEventListener('mousedown', onDown); + document.addEventListener('keydown', onKey); + return () => { + document.removeEventListener('mousedown', onDown); + document.removeEventListener('keydown', onKey); + }; + }, [open]); + + const itemClass = + 'flex w-full items-center gap-3 rounded-lg px-3 py-2 text-left text-sm text-neutral-700 hover:bg-neutral-100 dark:text-neutral-200 dark:hover:bg-neutral-800'; + + return ( +
+ + {open && ( +
+
+

{name}

+

{user?.is_super_admin ? 'Super-Admin' : 'Admin'}

+
+
+ setOpen(false)} className={itemClass}> + Mein Konto + + +
+ +
+ )} +
+ ); +} export default function AdminLayout() { const { logout, user, hasPermission } = useAuth(); const navigate = useNavigate(); + const { pathname } = useLocation(); const [mobileOpen, setMobileOpen] = useState(false); + const [searchOpen, setSearchOpen] = useState(false); + const [collapsed, setCollapsed] = useState(readCollapsed); const [newContactCount, setNewContactCount] = useState(0); const [customFonts, setCustomFonts] = useState([]); + const drawerRef = useRef(null); // 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); + useDialog(drawerRef, mobileOpen, () => setMobileOpen(false)); + // 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. @@ -81,6 +127,22 @@ export default function AdminLayout() { return () => clearInterval(timer); }, [refreshCounters]); + // Strg+K / Cmd+K oeffnet ueberall im Admin die Schnellsuche. + useEffect(() => { + const onKey = (e) => { + if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === 'k') { + e.preventDefault(); + setSearchOpen(true); + } + }; + document.addEventListener('keydown', onKey); + return () => document.removeEventListener('keydown', onKey); + }, []); + + useEffect(() => { + setMobileOpen(false); + }, [pathname]); + const hiddenAreaLabels = SITE_AREAS.filter((a) => hiddenAreas.includes(a.key)).map((a) => a.label); const loadCustomFonts = () => api.get('/custom-fonts').then(setCustomFonts).catch(() => {}); @@ -90,174 +152,257 @@ export default function AdminLayout() { // 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/images', label: 'Bilder', icon: 'image', 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); + // keywords: zusaetzliche Suchbegriffe fuer die Schnellsuche. + const navGroups = useMemo( + () => + [ + { + id: 'main', + title: null, + links: [ + { to: '/admin', label: 'Übersicht', icon: 'gauge', end: true, keywords: 'dashboard start' }, + { to: '/admin/contact-requests', label: 'Anfragen', icon: 'inbox', permission: 'contact.manage', badge: newContactCount, keywords: 'kontakt kunden nachrichten' }, + ], + }, + { + id: 'vehicles', + title: 'Fahrzeuge & Pakete', + links: [ + { to: '/admin/brands', label: 'Fahrzeugmarken', icon: 'tag', permission: 'brands.manage', keywords: 'hersteller vorlage' }, + { to: '/admin/models', label: 'Modelle', icon: 'car', permission: 'models.manage', keywords: 'fahrzeuge' }, + { to: '/admin/packages', label: 'Pakete', icon: 'box', permission: 'packages.manage', keywords: 'preise produkte upgrades' }, + ], + }, + { + id: 'content', + title: 'Website-Inhalte', + links: [ + { to: '/admin/home-sections', label: 'Startseite', icon: 'home', permission: 'content.manage', keywords: 'titelbild texte' }, + { to: '/admin/services', label: 'Leistungen', icon: 'briefcase', permission: 'services.manage' }, + { to: '/admin/faqs', label: 'FAQ', icon: 'help', permission: 'content.manage', keywords: 'fragen' }, + { to: '/admin/partners', label: 'Partner-Logos', icon: 'shield', permission: 'content.manage' }, + { to: '/admin/downloads', label: 'Downloads', icon: 'download', permission: 'downloads.manage', keywords: 'dateien pdf' }, + ], + }, + { + id: 'gallery', + title: 'Bildergalerie', + links: [ + { to: '/admin/gallery-brands', label: 'Galerie-Marken', icon: 'images', permission: 'gallery.manage' }, + { to: '/admin/gallery-projects', label: 'Projekte & Fotos', icon: 'camera', permission: 'gallery.manage', keywords: 'bilder hochladen' }, + ], + }, + { + id: 'company', + title: 'Firma & Aussehen', + links: [ + { to: '/admin/settings/company', label: 'Firmendaten', icon: 'building', permission: 'settings.manage', keywords: 'oeffnungszeiten telefon adresse whatsapp email preis hinweis' }, + { to: '/admin/settings/design', label: 'Aussehen', icon: 'palette', permission: 'settings.manage', keywords: 'schrift farben kacheln' }, + { to: '/admin/settings/images', label: 'Bilder', icon: 'image', permission: 'settings.manage', keywords: 'logo favicon hintergrund' }, + { to: '/admin/settings/legal', label: 'Rechtstexte', icon: 'file', permission: 'settings.manage', keywords: 'impressum datenschutz agb' }, + { to: '/admin/settings/maintenance', label: 'Wartungsmodus', icon: 'pause', permission: 'settings.manage', keywords: 'wartung offline' }, + { to: '/admin/settings/countdown', label: 'Launch-Countdown', icon: 'timer', permission: 'settings.manage', keywords: 'start eroeffnung' }, + ], + }, + { + id: 'system', + title: 'System (Experten)', + links: [ + { to: '/admin/users', label: 'Benutzer', icon: 'users', permission: 'users.manage', keywords: 'mitarbeiter konten' }, + { to: '/admin/permission-groups', label: 'Berechtigungsgruppen', icon: 'key', permission: 'permission_groups.manage', keywords: 'rechte rollen' }, + { to: '/admin/settings/email', label: 'E-Mail-Versand', icon: 'send', permission: 'system.admin', keywords: 'smtp mail' }, + { to: '/admin/settings/google', label: 'Google-Dienste', icon: 'globe', permission: 'system.admin', keywords: 'analytics rezensionen bewertungen' }, + { 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: 'transfer', permission: 'system.admin', keywords: 'sicherung backup' }, + { to: '/admin/settings/update', label: 'Updates', icon: 'refresh', permission: 'system.update', keywords: 'git version' }, + { to: '/admin/settings/reset', label: 'Zurücksetzen', icon: 'trash', permission: 'system.admin', keywords: 'loeschen' }, + ], + }, + ] + .map((group) => ({ ...group, links: group.links.filter((l) => !l.permission || hasPermission(l.permission)) })) + .filter((group) => group.links.length > 0), + // eslint-disable-next-line react-hooks/exhaustive-deps + [user?.id, newContactCount], + ); + + // Fuer Kopfzeile und Schnellsuche: alle erreichbaren Seiten flach, plus "Mein Konto" + // (liegt im Benutzer-Menue statt in der Leiste). + const allPages = useMemo( + () => [ + ...navGroups.flatMap((g) => g.links.map((l) => ({ ...l, group: g.title }))), + { to: '/admin/account', label: 'Mein Konto', icon: 'user', keywords: 'passwort 2fa profil' }, + ], + [navGroups], + ); + + const current = allPages.filter((l) => isActivePath(pathname, l)).sort((a, b) => b.to.length - a.to.length)[0]; + + const toggleGroup = (id) => { + setCollapsed((prev) => { + const next = { ...prev, [id]: !prev[id] }; + try { + localStorage.setItem(COLLAPSE_KEY, JSON.stringify(next)); + } catch { + // Ohne Speicher bleibt es eben nur fuer diese Sitzung. + } + return next; + }); + }; const handleLogout = async () => { await logout(); navigate('/admin/login'); }; + const canLiveEdit = ['content.manage', 'services.manage', 'packages.manage', 'gallery.manage', 'settings.manage'].some(hasPermission); + const linkClass = ({ isActive }) => - `flex items-center justify-between gap-2 rounded-md px-3 py-2 text-sm font-medium transition ${ + `group relative flex items-center justify-between gap-2 rounded-lg px-3 py-2 text-sm transition ${ isActive - ? 'bg-brand-500 text-white' - : 'text-neutral-600 hover:bg-neutral-100 dark:text-neutral-300 dark:hover:bg-neutral-800' + ? 'bg-brand-50 font-semibold text-brand-800 before:absolute before:inset-y-1.5 before:left-0 before:w-[3px] before:rounded-full before:bg-brand-500 dark:bg-brand-500/10 dark:text-brand-300' + : 'font-medium text-neutral-600 hover:bg-neutral-100 hover:text-neutral-900 dark:text-neutral-400 dark:hover:bg-neutral-800/70 dark:hover:text-neutral-100' }`; - const NavLinks = ({ onNavigate }) => ( - + // Als Element (nicht als eigene Komponente), damit die Leiste beim Neuzeichnen nicht neu + // aufgebaut wird und ihre Scrollposition behaelt. + const sidebar = ( +
+
+ + HifiPlanet + HifiPlanet + + + Admin + +
+ +
+ +
+ + +
); return ( -
+
-