import React, { useEffect, useState } from 'react'; import { NavigateFunction, useLocation } from 'react-router-dom'; import { AppBar, Toolbar, Tooltip, Typography, Button, IconButton, Box, Menu, MenuItem, ListItemIcon, ListItemText, Drawer, Divider, Avatar, Tabs, Tab, Container, Fade, } from '@mui/material'; import { styled, useTheme } from '@mui/material/styles'; import { Menu as MenuIcon, Dashboard, Person, Logout, Settings, ExpandMore, } from '@mui/icons-material'; import { NavigationLinkType } from 'components/layout/BackstoryLayout'; import { Beta } from 'components/Beta'; import { useUser } from 'hooks/useUser'; import { Candidate, Employer, Viewer } from 'types/types'; import { SetSnackType } from 'components/Snack'; import { CopyBubble } from 'components/CopyBubble'; import 'components/layout/Header.css'; import { useSecureAuth } from 'hooks/useSecureAuth'; // Styled components const StyledAppBar = styled(AppBar, { shouldForwardProp: (prop) => prop !== 'transparent', })<{ transparent?: boolean }>(({ theme, transparent }) => ({ backgroundColor: transparent ? 'transparent' : theme.palette.primary.main, boxShadow: transparent ? 'none' : '', transition: 'background-color 0.3s ease', borderRadius: 0, padding: 0, })); const NavLinksContainer = styled(Box)(({ theme }) => ({ display: 'flex', justifyContent: 'center', flex: 1, [theme.breakpoints.down('md')]: { display: 'none', }, })); const UserActionsContainer = styled(Box)(({ theme }) => ({ display: 'flex', alignItems: 'center', gap: theme.spacing(1), })); const UserButton = styled(Button)(({ theme }) => ({ color: theme.palette.primary.contrastText, textTransform: 'none', display: 'flex', alignItems: 'center', gap: theme.spacing(1), padding: theme.spacing(0.5, 1.5), borderRadius: theme.shape.borderRadius, '&:hover': { backgroundColor: theme.palette.action.hover, }, })); const MobileDrawer = styled(Drawer)(({ theme }) => ({ '& .MuiDrawer-paper': { width: 280, backgroundColor: theme.palette.background.paper, }, })); interface HeaderProps { transparent?: boolean; className?: string; navigate: NavigateFunction; navigationLinks: NavigationLinkType[]; showLogin?: boolean; currentPath: string; sessionId?: string | null; setSnack: SetSnackType, } const Header: React.FC = (props: HeaderProps) => { const { user, logout } = useSecureAuth(); const candidate: Candidate | null = (user && user.userType === "candidate") ? user as Candidate : null; const employer: Employer | null = (user && user.userType === "employer") ? user as Employer : null; const viewer: Viewer | null = (user && user.userType === "viewer") ? user as Viewer : null; const { transparent = false, className, navigate, navigationLinks, showLogin, sessionId, setSnack, } = props; const theme = useTheme(); const location = useLocation(); const name = (user?.firstName || user?.email || ''); const BackstoryLogo = () => { return Backstory }; const navLinks : NavigationLinkType[] = [ {name: "Home", path: "/", label: }, ...navigationLinks ]; // State for page navigation const [ currentTab, setCurrentTab ] = useState("/"); // State for mobile drawer const [mobileOpen, setMobileOpen] = useState(false); // State for user menu const [userMenuAnchor, setUserMenuAnchor] = useState(null); const userMenuOpen = Boolean(userMenuAnchor); useEffect(() => { const parts = location.pathname.split('/'); let tab = '/'; if (parts.length > 1) { tab = `/${parts[1]}`; } if (tab !== currentTab) { setCurrentTab(tab); } }, [location, currentTab]); const handleUserMenuOpen = (event: React.MouseEvent) => { setUserMenuAnchor(event.currentTarget); }; const handleUserMenuClose = () => { setUserMenuAnchor(null); }; const handleLogout = () => { handleUserMenuClose(); logout(); navigate('/'); }; const handleDrawerToggle = () => { setMobileOpen(!mobileOpen); }; // Render desktop navigation links const renderNavLinks = () => { return ( setCurrentTab(newValue)} indicatorColor="secondary" textColor="inherit" variant="fullWidth" allowScrollButtonsMobile aria-label="Backstory navigation" > {navLinks.map((link) => ( { navigate(link.path); }} /> ))} ); }; // Render mobile drawer content const renderDrawerContent = () => { return ( <> {navLinks.map((link) => ( {link.icon && {link.icon}} {link.name} } onClick={(e) => { handleDrawerToggle() ; setCurrentTab(link.path); navigate(link.path);} } /> ))} {!user && (showLogin === undefined || showLogin !== false) && ( )} ); }; // Render user account section const renderUserSection = () => { if (showLogin !== undefined && showLogin === false) { return <>; } if (!user) { return ( <> ); } return ( <> {name.charAt(0).toUpperCase()} {name} { handleUserMenuClose(); navigate(`/${user.userType}/profile`) }}> Profile { handleUserMenuClose(); navigate(`/${user.userType}/dashboard`) }}> Dashboard { handleUserMenuClose(); navigate(`/${user.userType}settings`) }}> Settings Logout ); }; return ( {/* Logo Section */} {/* Navigation Links - Desktop */} {renderNavLinks()} {/* User Actions Section */} {renderUserSection()} {/* Mobile Menu Button */} {sessionId && { navigate(`${window.location.pathname}?id=${sessionId}`); setSnack("Link copied!") }} size="large" />} {/* Mobile Navigation Drawer */} {renderDrawerContent()} { navigate('/docs/beta'); }} /> ); }; export { Header };