Spaces:
Running
Running
File size: 3,755 Bytes
bdd9b98 3299552 bdd9b98 3299552 bdd9b98 3299552 bdd9b98 3299552 bdd9b98 3299552 |
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 |
import React, { useState } from 'react';
import {
AppBar,
Toolbar,
Typography,
Button,
IconButton,
Box,
useScrollTrigger,
Slide,
Drawer,
List,
ListItemButton,
ListItemText
} from '@mui/material';
import MenuIcon from '@mui/icons-material/Menu';
import { useAuth } from '../../hooks/useAuth';
import { Link } from 'react-router-dom';
import ThemeToggle from '../UI/ThemeToggle';
function HideOnScroll({ children }) {
const trigger = useScrollTrigger();
return (
<Slide appear={false} direction="down" in={!trigger}>
{children}
</Slide>
);
}
const Header = () => {
const { isAuthenticated, logout } = useAuth();
const [drawerOpen, setDrawerOpen] = useState(false);
const toggleDrawer = (open) => (event) => {
if (event.type === 'keydown' && (event.key === 'Tab' || event.key === 'Shift')) {
return;
}
setDrawerOpen(open);
};
const handleLogoutMobile = () => {
logout();
setDrawerOpen(false);
};
return (
<HideOnScroll>
<AppBar position="sticky" elevation={1}>
<Toolbar>
<IconButton
edge="start"
color="inherit"
aria-label="open navigation"
onClick={toggleDrawer(true)}
sx={{ mr: 2, display: { xs: 'block', sm: 'none' } }}
>
<MenuIcon />
</IconButton>
<Typography
variant="h6"
component={Link}
to="/"
sx={{
flexGrow: 1,
textDecoration: 'none',
color: 'inherit',
fontWeight: 'bold'
}}
>
EnlightenQalb
</Typography>
<Box sx={{ display: { xs: 'none', sm: 'block' } }}>
<Button
color="inherit"
component={Link}
to="/about"
sx={{ mx: 1 }}
>
About
</Button>
{isAuthenticated ? (
<>
<Button
color="inherit"
component={Link}
to="/dashboard"
sx={{ mx: 1 }}
>
Dashboard
</Button>
<Button
color="inherit"
onClick={logout}
sx={{ mx: 1 }}
>
Logout
</Button>
</>
) : (
<Button
color="inherit"
component={Link}
to="/login"
sx={{ mx: 1 }}
>
Login
</Button>
)}
</Box>
<Box sx={{ display: 'flex', alignItems: 'center', ml: 2 }}>
<ThemeToggle />
</Box>
</Toolbar>
<Drawer anchor="left" open={drawerOpen} onClose={toggleDrawer(false)}>
<Box sx={{ width: 250 }} role="presentation" onClick={toggleDrawer(false)} onKeyDown={toggleDrawer(false)}>
<List>
<ListItemButton component={Link} to="/about"><ListItemText primary="About" /></ListItemButton>
{isAuthenticated ? (
<>
<ListItemButton component={Link} to="/dashboard"><ListItemText primary="Dashboard" /></ListItemButton>
<ListItemButton onClick={handleLogoutMobile}><ListItemText primary="Logout" /></ListItemButton>
</>
) : (
<ListItemButton component={Link} to="/login"><ListItemText primary="Login" /></ListItemButton>
)}
</List>
</Box>
</Drawer>
</AppBar>
</HideOnScroll>
);
};
export default Header; |