import { useAppDispatch, useAppSelector } from '@/app/hooks' import { setView } from '@/features/Settings/SettingsSlice' import { Logout } from '@/features/User/oidcAuth' import logo from '@/static/header-logo.svg' import { getInitials, stringToGradient } from '@/utils/avatarUtils' import { getUserDisplayName } from '@/utils/userUtils' import { redirectTo } from '@/utils/navigation' import { CalendarApi } from '@fullcalendar/core' import { Avatar, Box, Button, ButtonGroup, Divider, FormControl, IconButton, Menu, MenuItem, Popover, Select, Typography } from '@linagora/twake-mui' import WidgetsOutlinedIcon from '@mui/icons-material/WidgetsOutlined' import HelpOutlineIcon from '@mui/icons-material/HelpOutline' import ChevronLeftIcon from '@mui/icons-material/ChevronLeft' import ChevronRightIcon from '@mui/icons-material/ChevronRight' import LogoutIcon from '@mui/icons-material/Logout' import RefreshIcon from '@mui/icons-material/Refresh' import SettingsOutlinedIcon from '@mui/icons-material/SettingsOutlined' import React, { useEffect, useState } from 'react' import { push } from 'redux-first-history' import { useI18n } from 'twake-i18n' import SearchBar from './EventSearchBar' import './Menubar.styl' export type AppIconProps = { name: string link: string icon: string } export type MenubarProps = { calendarRef: React.RefObject onRefresh: () => void currentDate: Date onDateChange?: (date: Date) => void currentView: string onViewChange?: (view: string) => void isIframe?: boolean } export function Menubar({ calendarRef, onRefresh, currentDate, onDateChange, currentView, onViewChange, isIframe }: MenubarProps) { const { t } = useI18n() // deliberately NOT using f() const user = useAppSelector(state => state.user.userData) const applist: AppIconProps[] = window.appList ?? [] const supportLink = window.SUPPORT_URL const [anchorEl, setAnchorEl] = useState(null) const [userMenuAnchorEl, setUserMenuAnchorEl] = useState( null ) const dispatch = useAppDispatch() useEffect(() => { if (!user) { dispatch(push('/')) } }, [dispatch, user]) if (!user) { return null } const handleOpen = (event: React.MouseEvent) => { setAnchorEl(event.currentTarget) } const handleClose = () => { setAnchorEl(null) } const handleNavigation = async (action: 'prev' | 'next' | 'today') => { if (!calendarRef.current) return await dispatch(setView('calendar')) switch (action) { case 'prev': calendarRef.current.prev() break case 'next': calendarRef.current.next() break case 'today': calendarRef.current.today() break } // Notify parent about date change after navigation if (onDateChange) { const newDate = calendarRef.current.getDate() onDateChange(newDate) } } const handleViewChange = async (view: string) => { if (!calendarRef.current) return await dispatch(setView('calendar')) calendarRef.current.changeView(view) // Notify parent about view change if (onViewChange) { onViewChange(view) } // Notify parent about date change after view change if (onDateChange) { const newDate = calendarRef.current.getDate() onDateChange(newDate) } } const open = Boolean(anchorEl) const userMenuOpen = Boolean(userMenuAnchorEl) const handleUserMenuClick = (event: React.MouseEvent) => { setUserMenuAnchorEl(event.currentTarget) } const handleUserMenuClose = () => { setUserMenuAnchorEl(null) } const handleSettingsClick = () => { dispatch(setView('settings')) handleUserMenuClose() } const handleLogoutClick = async () => { const logoutUrl = await Logout() sessionStorage.removeItem('tokenSet') redirectTo(logoutUrl.href) handleUserMenuClose() } // Use i18n for month names instead of date-fns const monthIndex = currentDate.getMonth() const year = currentDate.getFullYear() const monthName = t(`months.standalone.${monthIndex}`) const dateLabel = `${monthName} ${year}` return ( <>
{!isIframe && (
)}

{dateLabel}

{!isIframe && ( <> {supportLink && (
)}
{applist.length > 0 && ( )}
)}
{!isIframe ? ( {getInitials(getUserDisplayName(user))} ) : ( )}
{applist.map((prop: AppIconProps) => ( ))}
{getInitials(getUserDisplayName(user))} {getUserDisplayName(user)} {user?.email} {t('menubar.settings') || 'Settings'} {t('menubar.logout') || 'Logout'} ) } export type MainTitleProps = { calendarRef: React.RefObject currentView: string onViewChange?: (view: string) => void onDateChange?: (date: Date) => void } export function MainTitle({ calendarRef, currentView, onViewChange, onDateChange }: MainTitleProps) { const { t } = useI18n() const dispatch = useAppDispatch() const handleLogoClick = async () => { if (!calendarRef.current) return await dispatch(setView('calendar')) if (currentView !== 'timeGridWeek') { calendarRef.current.changeView('timeGridWeek') if (onViewChange) { onViewChange('timeGridWeek') } } calendarRef.current.today() if (onDateChange) { const newDate = calendarRef.current.getDate() onDateChange(newDate) } } return (
{t('menubar.logoAlt')}
) } function AppIcon({ prop }: { prop: AppIconProps }) { return ( {prop.name} ) }