Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | 9x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 123x 125x 125x 1x 125x 125x 1x 1x 1x 125x 125x 125x 125x 125x 125x | /**
* AppHeader Component
* Story 1.17, Task 6b, 11b & 13b: Role-adaptive application header with responsive design and performance optimization
*
* Top navigation bar with role-based menu, notifications, and user menu.
* Uses Material-UI AppBar with Swiss design principles.
* Supports mobile (< 768px), tablet (768px-1024px), and desktop (> 1024px) breakpoints.
* Wrapped with React.memo() for performance optimization (Task 13b).
*/
import React, { useState } from 'react';
import { AppBar, Box, Toolbar, IconButton, Badge, Avatar, Tooltip } from '@mui/material';
import { Menu, Notifications, TaskAlt } from '@mui/icons-material';
import { NavigationMenu } from './NavigationMenu';
import { MobileDrawer } from './MobileDrawer';
import { RoleSelector } from './RoleSelector';
import UserMenuDropdown from './UserMenuDropdown';
import { useUIStore } from '@/stores/uiStore';
import { useBreakpoints } from '@/hooks/useBreakpoints';
import { useAuth } from '@/hooks/useAuth';
import { useNavigate } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { getRolesWithNavEntries } from '@/config/navigationConfig';
import type { UserProfile } from '@/types/user';
import type { NotificationsResponse } from '@/types/notification';
import type { UserContext, UserRole } from '@/types/auth';
interface AppHeaderProps {
user?: UserProfile;
notifications?: NotificationsResponse;
}
const AppHeader = React.memo(function AppHeader({
user: userProp,
notifications: notificationsProp,
}: AppHeaderProps = {}) {
const { t } = useTranslation();
const { isMobile, isTablet } = useBreakpoints();
const [mobileDrawerOpen, setMobileDrawerOpen] = useState(false);
const [userMenuOpen, setUserMenuOpen] = useState(false);
const [userMenuAnchorEl, setUserMenuAnchorEl] = useState<null | HTMLElement>(null);
// Use props if provided, otherwise fall back to stores/hooks
const { user: storeUser, signOut } = useAuth();
const { setUserMenuOpen: setUserMenuOpenState, activeNavRole, setActiveNavRole } = useUIStore();
const navigate = useNavigate();
const user = userProp || storeUser;
// Use prop if provided (for testing), otherwise would use hook data when backend is ready
const notificationsData = notificationsProp;
const unreadCount = notificationsData?.unreadCount ?? 0;
// Extract current role - handle both UserContext (role) and UserProfile (currentRole)
const currentRole = user && ('currentRole' in user ? user.currentRole : user.role);
// 2026-05-20 (Q#1b) — only roles that have real nav entries (organizer, partner today)
// qualify for the RoleSelector + admin NavigationMenu. Speaker and attendee live in
// the public site, so they must not appear as chips here (clicking would render an
// empty menu). Filter the user's roles down to the set with entries; everything
// below — chip visibility, active-role fallback, mobile drawer — branches on the
// filtered list.
const rolesWithEntries = getRolesWithNavEntries();
const allUserRoles: UserRole[] =
user && 'roles' in user && Array.isArray(user.roles) && user.roles.length > 0
? (user.roles as UserRole[])
: currentRole
? [currentRole as UserRole]
: [];
const currentRoles: UserRole[] = allUserRoles.filter((r) => rolesWithEntries.has(r));
// Multi-role: prefer the persisted active role; fall back to the first role if
// `activeNavRole` is unset or no longer matches any of the user's roles (e.g.
// after a role grant/revoke). No "self-heal" effect — the stored value can stay
// stale; it just gets ignored at render time until the user clicks a different
// chip, which writes a fresh value via `setActiveNavRole`.
// Single-role: always use that role (ignore stale `activeNavRole`).
const effectiveActiveRole: UserRole | null = (() => {
if (currentRoles.length === 0) return null;
Eif (currentRoles.length === 1) return currentRoles[0];
if (activeNavRole && currentRoles.includes(activeNavRole)) return activeNavRole;
return currentRoles[0];
})();
const navRoles: UserRole[] = effectiveActiveRole ? [effectiveActiveRole] : [];
const handleNotificationClick = () => {
navigate('/organizer/notifications');
};
const handleTasksClick = () => {
navigate('/organizer/tasks');
};
const handleUserMenuClick = (event: React.MouseEvent<HTMLElement>) => {
setUserMenuAnchorEl(event.currentTarget);
setUserMenuOpen(true);
setUserMenuOpenState(true);
};
const handleUserMenuClose = () => {
setUserMenuAnchorEl(null);
setUserMenuOpen(false);
setUserMenuOpenState(false);
};
const handleLogout = async () => {
await signOut();
navigate('/login', { replace: true });
};
const handleLanguageChange = (language: string) => {
// Language change is handled inside UserMenuDropdown
console.log('[AppHeader] Language changed to:', language);
};
const handleMobileMenuToggle = () => {
setMobileDrawerOpen(!mobileDrawerOpen);
};
Iif (!user) {
return null;
}
return (
<>
<AppBar
component="header"
position="sticky"
elevation={1}
sx={{
backgroundColor: 'background.paper',
color: 'text.primary',
borderBottom: 1,
borderColor: 'divider',
padding: isTablet ? '8px' : '0',
}}
>
<Toolbar>
{/* Mobile Menu Button */}
{isMobile && (
<IconButton
edge="start"
color="inherit"
aria-label={t('navigation.openMenu')}
onClick={handleMobileMenuToggle}
sx={{ mr: 2 }}
data-testid="mobile-menu-button"
>
<Menu />
</IconButton>
)}
{/* Logo */}
<Box sx={{ display: 'flex', alignItems: 'center', gap: 1, mr: 4 }}>
<img
src="/BATbern_color_logo.svg"
alt="BATbern"
width={95}
height={40}
style={{ height: 40, width: 'auto' }}
/>
</Box>
{/* Desktop/Tablet Navigation — multi-role users see a RoleSelector chip
row, and the nav filters down to the selected role. Single-role users
see only their role's items, no chip. */}
{!isMobile && currentRoles.length > 0 && effectiveActiveRole && (
<Box sx={{ flex: 1, display: 'flex', alignItems: 'center' }}>
{currentRoles.length > 1 && (
<RoleSelector
roles={currentRoles}
activeRole={effectiveActiveRole}
onChange={(role) => setActiveNavRole(role)}
/>
)}
<NavigationMenu userRoles={navRoles} showText={!isTablet} />
</Box>
)}
<Box sx={{ flex: 1, display: { xs: 'block', md: 'none' } }} />
{/* Right Section: Notifications, Tasks, User Menu */}
<Box sx={{ display: 'flex', alignItems: 'center', gap: 1 }}>
{/* Notifications */}
<IconButton
color="inherit"
aria-label={t('navigation.notifications')}
aria-describedby={unreadCount > 0 ? 'notification-badge-description' : undefined}
onClick={handleNotificationClick}
data-testid="notifications-button"
>
<Badge badgeContent={unreadCount} color="error" aria-live="polite" aria-atomic="true">
<Notifications />
</Badge>
{/* Screen reader description for unread count */}
{unreadCount > 0 && (
<Box
id="notification-badge-description"
component="span"
sx={{
position: 'absolute',
width: '1px',
height: '1px',
padding: 0,
margin: '-1px',
overflow: 'hidden',
clip: 'rect(0, 0, 0, 0)',
whiteSpace: 'nowrap',
border: 0,
}}
>
{t('notifications.unreadCount', { count: unreadCount })}
</Box>
)}
</IconButton>
{/* Tasks - shown when the active nav-role chip is "organizer". */}
{effectiveActiveRole === 'organizer' && (
<Tooltip title={t('navigation.tasks')}>
<IconButton
color="inherit"
aria-label={t('navigation.tasks')}
onClick={handleTasksClick}
data-testid="tasks-button"
>
<TaskAlt />
</IconButton>
</Tooltip>
)}
{/* User Menu */}
<IconButton
onClick={handleUserMenuClick}
sx={{ ml: 1 }}
aria-label={t('navigation.userMenu')}
aria-expanded={userMenuOpen}
aria-haspopup="true"
data-testid="user-menu-button"
>
<Avatar
sx={{
width: 32,
height: 32,
bgcolor: 'primary.main',
fontSize: '0.875rem',
}}
>
{user.email.charAt(0).toUpperCase()}
</Avatar>
</IconButton>
</Box>
</Toolbar>
</AppBar>
{/* Mobile Drawer */}
{currentRoles.length > 0 && effectiveActiveRole && (
<MobileDrawer
open={mobileDrawerOpen}
onClose={() => setMobileDrawerOpen(false)}
userRoles={currentRoles}
activeRole={effectiveActiveRole}
onActiveRoleChange={(role) => setActiveNavRole(role)}
userEmail={user.email}
/>
)}
{/* User Menu Dropdown */}
{user && (
<UserMenuDropdown
user={user as UserContext}
anchorEl={userMenuAnchorEl}
open={userMenuOpen}
onClose={handleUserMenuClose}
onLogout={handleLogout}
onLanguageChange={handleLanguageChange}
/>
)}
</>
);
});
export default AppHeader;
|