Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UKLyiTjbcqPzxHApMQVFh7
295 lines
15 KiB
JavaScript
295 lines
15 KiB
JavaScript
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 }) => (
|
||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.75" className="h-5 w-5 shrink-0">
|
||
<path strokeLinecap="round" strokeLinejoin="round" d={path} />
|
||
</svg>
|
||
);
|
||
|
||
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 }) => (
|
||
<nav className="space-y-5">
|
||
{navGroups.map((group, i) => (
|
||
<div key={i}>
|
||
{group.title && (
|
||
<p className="mb-1.5 px-3 text-xs font-semibold uppercase tracking-wide text-neutral-400 dark:text-neutral-500">
|
||
{group.title}
|
||
</p>
|
||
)}
|
||
<div className="space-y-1">
|
||
{group.links.map((link) => (
|
||
<NavLink key={link.to} to={link.to} end={link.end} className={linkClass} onClick={onNavigate}>
|
||
<span className="flex items-center gap-2.5">
|
||
<Icon path={ICONS[link.icon]} />
|
||
{link.label}
|
||
</span>
|
||
{!!link.badge && (
|
||
<span className="rounded-full bg-red-500 px-2 py-0.5 text-xs font-bold text-white">{link.badge}</span>
|
||
)}
|
||
</NavLink>
|
||
))}
|
||
</div>
|
||
</div>
|
||
))}
|
||
</nav>
|
||
);
|
||
|
||
return (
|
||
<AdminUiProvider onRefreshCounters={refreshCounters}>
|
||
<SessionGuard />
|
||
<CustomFontsProvider value={{ fonts: customFonts, refresh: loadCustomFonts }}>
|
||
<div className="flex min-h-screen bg-neutral-50 text-neutral-900 dark:bg-neutral-950 dark:text-neutral-100">
|
||
<CustomFontFaces />
|
||
<aside className="hidden w-60 shrink-0 border-r border-neutral-200 p-4 dark:border-neutral-800 sm:block">
|
||
<div className="mb-6 flex items-center gap-2 px-3">
|
||
<Link to="/" className="rounded-lg px-2 py-1">
|
||
<img src={logo} alt="HifiPlanet" className="h-7 w-auto dark:hidden" />
|
||
<img src={logoDark} alt="HifiPlanet" className="hidden h-7 w-auto dark:block" />
|
||
</Link>
|
||
<span className="text-sm font-semibold text-neutral-500 dark:text-neutral-400">Admin</span>
|
||
</div>
|
||
<NavLinks />
|
||
</aside>
|
||
|
||
<div className="flex-1">
|
||
<header className="border-b border-neutral-200 dark:border-neutral-800">
|
||
<div className="flex items-center justify-between px-4 py-3 sm:px-6">
|
||
<div className="flex items-center gap-2 sm:hidden">
|
||
<button
|
||
onClick={() => setMobileOpen((o) => !o)}
|
||
aria-label="Menü öffnen"
|
||
className="rounded-md p-2 text-neutral-600 hover:bg-neutral-100 dark:text-neutral-300 dark:hover:bg-neutral-800"
|
||
>
|
||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" className="h-6 w-6">
|
||
<path strokeLinecap="round" strokeLinejoin="round" d="M4 6h16M4 12h16M4 18h16" />
|
||
</svg>
|
||
</button>
|
||
<Link to="/" className="rounded-lg px-1.5 py-0.5">
|
||
<img src={logo} alt="HifiPlanet" className="h-6 w-auto dark:hidden" />
|
||
<img src={logoDark} alt="HifiPlanet" className="hidden h-6 w-auto dark:block" />
|
||
</Link>
|
||
</div>
|
||
<p className="hidden text-sm text-neutral-500 dark:text-neutral-400 sm:block">
|
||
Angemeldet als {user?.username}{user?.is_super_admin ? ' · Super-Admin' : ''}
|
||
</p>
|
||
<div className="flex items-center gap-2">
|
||
<a
|
||
href="/"
|
||
target="_blank"
|
||
rel="noopener noreferrer"
|
||
className="inline-flex items-center gap-1.5 rounded-md border border-neutral-300 px-3 py-1.5 text-sm font-medium text-neutral-700 hover:bg-neutral-100 dark:border-neutral-700 dark:text-neutral-200 dark:hover:bg-neutral-800"
|
||
>
|
||
<Icon path={ICONS.external} />
|
||
<span className="hidden sm:inline">Website öffnen</span>
|
||
</a>
|
||
<ThemeToggle />
|
||
<button
|
||
onClick={handleLogout}
|
||
className="rounded-md border border-neutral-300 px-3 py-1.5 text-sm font-medium text-neutral-700 hover:bg-neutral-100 dark:border-neutral-700 dark:text-neutral-200 dark:hover:bg-neutral-800"
|
||
>
|
||
Abmelden
|
||
</button>
|
||
</div>
|
||
</div>
|
||
{mobileOpen && (
|
||
<div className="border-t border-neutral-200 px-4 py-3 dark:border-neutral-800 sm:hidden">
|
||
<NavLinks onNavigate={() => setMobileOpen(false)} />
|
||
</div>
|
||
)}
|
||
</header>
|
||
{(maintenanceOn || countdownUntil || hiddenAreaLabels.length > 0) && (
|
||
<div className="border-b border-amber-200 bg-amber-50 px-4 py-2 text-sm text-amber-900 dark:border-amber-900 dark:bg-amber-950 dark:text-amber-200 sm:px-6">
|
||
{maintenanceOn && (
|
||
<p>
|
||
<strong>Wartungsmodus ist aktiv</strong> – Besucher sehen gerade nur die Wartungsseite.{' '}
|
||
{hasPermission('settings.manage') && <Link to="/admin/settings/maintenance" className="underline">Ändern</Link>}
|
||
</p>
|
||
)}
|
||
{countdownUntil && (
|
||
<p>
|
||
<strong>Launch-Countdown läuft</strong> – Besucher sehen bis{' '}
|
||
{new Date(countdownUntil).toLocaleString('de-DE', { dateStyle: 'medium', timeStyle: 'short', timeZone: 'Europe/Berlin' })} Uhr nur den Countdown.{' '}
|
||
{hasPermission('settings.manage') && <Link to="/admin/settings/countdown" className="underline">Ändern</Link>}
|
||
</p>
|
||
)}
|
||
{hiddenAreaLabels.length > 0 && (
|
||
<p>
|
||
<strong>Ausgeblendet für Besucher:</strong> {hiddenAreaLabels.join(', ')}.{' '}
|
||
<Link to="/admin" className="underline">Auf der Übersicht ändern</Link>
|
||
</p>
|
||
)}
|
||
</div>
|
||
)}
|
||
<main className="p-4 sm:p-6">
|
||
{/* Admin-Seiten werden einzeln nachgeladen (App.jsx) - Seitenleiste und Kopf
|
||
bleiben dabei stehen, nur der Inhaltsbereich zeigt kurz "Lädt…". */}
|
||
<Suspense fallback={<p className="text-sm text-neutral-400">Lädt…</p>}>
|
||
<Outlet />
|
||
</Suspense>
|
||
</main>
|
||
</div>
|
||
</div>
|
||
</CustomFontsProvider>
|
||
</AdminUiProvider>
|
||
);
|
||
}
|