Skip to content

Commit

Permalink
Create Layout.js
Browse files Browse the repository at this point in the history
  • Loading branch information
bufanoc authored Dec 23, 2024
1 parent 611771b commit 799c157
Showing 1 changed file with 149 additions and 0 deletions.
149 changes: 149 additions & 0 deletions frontend/src/components/Layout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import React, { useState } from 'react';
import { useNavigate, useLocation } from 'react-router-dom';
import {
Box,
Drawer,
AppBar,
Toolbar,
List,
Typography,
Divider,
IconButton,
ListItem,
ListItemIcon,
ListItemText,
useTheme,
} from '@mui/material';
import {
Menu as MenuIcon,
Dashboard as DashboardIcon,
Router as RouterIcon,
Security as SecurityIcon,
Settings as SettingsIcon,
SwapHoriz as SwitchIcon,
Balance as LoadBalancerIcon,
} from '@mui/icons-material';

const drawerWidth = 240;

const menuItems = [
{ text: 'Dashboard', icon: <DashboardIcon />, path: '/' },
{ text: 'Logical Switches', icon: <SwitchIcon />, path: '/logical-switches' },
{ text: 'Logical Routers', icon: <RouterIcon />, path: '/logical-routers' },
{ text: 'Load Balancers', icon: <LoadBalancerIcon />, path: '/load-balancers' },
{ text: 'ACLs', icon: <SecurityIcon />, path: '/acls' },
{ text: 'Settings', icon: <SettingsIcon />, path: '/settings' },
];

function Layout({ children }) {
const theme = useTheme();
const navigate = useNavigate();
const location = useLocation();
const [mobileOpen, setMobileOpen] = useState(false);

const handleDrawerToggle = () => {
setMobileOpen(!mobileOpen);
};

const drawer = (
<div>
<Toolbar>
<Typography variant="h6" noWrap component="div">
OVN Manager
</Typography>
</Toolbar>
<Divider />
<List>
{menuItems.map((item) => (
<ListItem
button
key={item.text}
onClick={() => navigate(item.path)}
selected={location.pathname === item.path}
sx={{
'&.Mui-selected': {
backgroundColor: theme.palette.primary.main + '20',
},
}}
>
<ListItemIcon sx={{ color: location.pathname === item.path ? theme.palette.primary.main : 'inherit' }}>
{item.icon}
</ListItemIcon>
<ListItemText primary={item.text} />
</ListItem>
))}
</List>
</div>
);

return (
<Box sx={{ display: 'flex' }}>
<AppBar
position="fixed"
sx={{
width: { sm: `calc(100% - ${drawerWidth}px)` },
ml: { sm: `${drawerWidth}px` },
}}
>
<Toolbar>
<IconButton
color="inherit"
aria-label="open drawer"
edge="start"
onClick={handleDrawerToggle}
sx={{ mr: 2, display: { sm: 'none' } }}
>
<MenuIcon />
</IconButton>
<Typography variant="h6" noWrap component="div">
{menuItems.find(item => item.path === location.pathname)?.text || 'OVN Manager'}
</Typography>
</Toolbar>
</AppBar>
<Box
component="nav"
sx={{ width: { sm: drawerWidth }, flexShrink: { sm: 0 } }}
>
<Drawer
variant="temporary"
open={mobileOpen}
onClose={handleDrawerToggle}
ModalProps={{
keepMounted: true,
}}
sx={{
display: { xs: 'block', sm: 'none' },
'& .MuiDrawer-paper': { boxSizing: 'border-box', width: drawerWidth },
}}
>
{drawer}
</Drawer>
<Drawer
variant="permanent"
sx={{
display: { xs: 'none', sm: 'block' },
'& .MuiDrawer-paper': { boxSizing: 'border-box', width: drawerWidth },
}}
open
>
{drawer}
</Drawer>
</Box>
<Box
component="main"
sx={{
flexGrow: 1,
p: 3,
width: { sm: `calc(100% - ${drawerWidth}px)` },
minHeight: '100vh',
backgroundColor: theme.palette.background.default,
}}
>
<Toolbar />
{children}
</Box>
</Box>
);
}

export default Layout;

0 comments on commit 799c157

Please sign in to comment.