import { useState, useEffect } from 'react'; import { Outlet, useNavigate, useLocation } from 'react-router-dom'; import { Layout, Menu, Avatar, Dropdown, theme, Typography, Tooltip, Badge } from 'antd'; import { InboxOutlined, FileTextOutlined, MailOutlined, SettingOutlined, LogoutOutlined, UserOutlined, EditOutlined, SunOutlined, MoonOutlined, AppstoreOutlined, GlobalOutlined, CheckCircleOutlined, EuroOutlined, } from '@ant-design/icons'; import { useAuth } from '../auth/AuthContext'; import { useTheme } from '../theme/ThemeContext'; import { Permission } from '../auth/permissions'; import { statsApi, type StatsCounts } from '../api/stats'; const { Sider, Content } = Layout; const { Text } = Typography; type MenuItemDef = { key: string; icon: React.ReactNode; label: string; permission?: Permission; countKey?: keyof StatsCounts; externalUrl?: string; }; const allMenuItems: MenuItemDef[] = [ { key: '/dashboard', icon: , label: 'Dashboard' }, { key: '/inbox', icon: , label: 'Eingangsbox', permission: Permission.VIEW_SCANNER, countKey: 'inbox' }, { key: '/posteingang', icon: , label: 'Posteingang', permission: Permission.VIEW_INBOX, countKey: 'posteingang' }, { key: '/manuell', icon: , label: 'Manuell bearbeiten', permission: Permission.PROCESS_MANUALLY, countKey: 'manuell' }, { key: '/mailpostfach', icon: , label: 'Mailpostfach', permission: Permission.VIEW_MAIL, countKey: 'mailpostfach' }, { key: 'agrarmonitor', icon: , label: 'In Agrarmonitor', permission: Permission.PROCESS_MANUALLY, countKey: 'agrarmonitor', externalUrl: 'https://admin7.agrarmonitor.de/dateien/eingang#dateien' }, { key: '/freigabe', icon: , label: 'Freigabe', permission: Permission.VIEW_FREIGABE }, { key: '/zahlung', icon: , label: 'Zahlung', permission: Permission.VIEW_ZAHLUNG }, { key: '/settings', icon: , label: 'Einstellungen', permission: Permission.MANAGE_SETTINGS }, ]; export default function AppLayout() { const [collapsed, setCollapsed] = useState(false); const navigate = useNavigate(); const location = useLocation(); const { user, logout, hasPermission, isAuthenticated } = useAuth(); const { token: themeToken } = theme.useToken(); const { isDark, toggleTheme } = useTheme(); const [counts, setCounts] = useState(null); useEffect(() => { if (!isAuthenticated) return; const fetchCounts = async () => { try { const data = await statsApi.getCounts(); setCounts(data); } catch (err) { console.error('Fehler beim Abrufen der Zählerstände:', err); } }; fetchCounts(); const interval = setInterval(fetchCounts, 30000); // 30 Sekunden Polling return () => clearInterval(interval); }, [isAuthenticated, location.pathname]); // Update after navigation or auth change const menuItems = allMenuItems .filter((item) => !item.permission || hasPermission(item.permission)) .map((item) => ({ ...item, label: (
{item.label} {item.countKey && counts && counts[item.countKey] > 0 && !collapsed && ( )}
), })); const selectedKey = menuItems .map((item) => item.key) .filter((key) => location.pathname === key || location.pathname.startsWith(key + '/')) .sort((a, b) => b.length - a.length)[0] || (menuItems[0]?.key ?? '/inbox'); const siderStyle = isDark ? {} : { background: '#f0f2f7', borderRight: '1px solid #e2e4ea', }; const logoColor = isDark ? '#fff' : '#1a1a2e'; const subtleColor = isDark ? '#ffffffa6' : '#4a4a6a'; return ( {/* Logo / Collapse-Toggle */} {/* Navigation Menu */} { const item = allMenuItems.find((i) => i.key === key); if (item?.externalUrl) { window.open(item.externalUrl, '_blank', 'noopener,noreferrer'); } else { navigate(key); } }} style={isDark ? { flex: 1 } : { background: 'transparent', flex: 1 }} /> {/* Bottom Section: User + Theme Toggle */}
{/* Theme Toggle */} {/* User Menu */} , label: 'Benutzereinstellungen', onClick: () => navigate('/user-settings'), }, { key: 'logout', icon: , label: 'Abmelden', onClick: () => logout(), }, ], }} placement="topRight" trigger={['click']} >
(e.currentTarget.style.background = isDark ? 'rgba(255,255,255,0.08)' : '#eef1f8')} onMouseLeave={(e) => (e.currentTarget.style.background = 'transparent')} > } /> {!collapsed && ( {user?.profile?.name || 'Benutzer'} )}
); }