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 { CalendarApi } from "@fullcalendar/core"; import { Avatar, Box, Button, ButtonGroup, Divider, FormControl, IconButton, Menu, MenuItem, Popover, Select, Typography, } from "@linagora/twake-mui"; import AppsIcon from "@mui/icons-material/Apps"; 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 SettingsIcon from "@mui/icons-material/Settings"; import { format } from "date-fns"; import { enGB, fr as frLocale, ru as ruLocale, vi as viLocale, } from "date-fns/locale"; import React, { useEffect, useState } from "react"; import { push } from "redux-first-history"; import { useI18n } from "twake-i18n"; import SearchBar from "./EventSearchBar"; import "./Menubar.styl"; const dateLocales = { en: enGB, fr: frLocale, ru: ruLocale, vi: viLocale }; 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; }; export function Menubar({ calendarRef, onRefresh, currentDate, onDateChange, currentView, onViewChange, }: MenubarProps) { const { t, lang } = useI18n(); // deliberately NOT using f() const user = useAppSelector((state) => state.user.userData); const applist: AppIconProps[] = (window as any).appList ?? []; 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"); window.location.assign(logoutUrl.href); handleUserMenuClose(); }; const dateLabel = format(currentDate, "MMMM yyyy", { locale: dateLocales[lang as keyof typeof dateLocales] || enGB, }); return ( <>
{dateLabel}
{applist.length > 0 && ( )}
{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} ); }