-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMenuBar.jsx
More file actions
193 lines (176 loc) · 8.88 KB
/
MenuBar.jsx
File metadata and controls
193 lines (176 loc) · 8.88 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { useState, useEffect, use } from 'react'
import { AppBar, Chip, Drawer, Box, Typography, MenuList, MenuItem, ListItemIcon, ListItemText, Tooltip, Link, Select } from '@mui/material'
import IconButton from '@mui/material/IconButton';
import SchoolIcon from '@mui/icons-material/School';
import PeopleIcon from '@mui/icons-material/People';
import DoneIcon from '@mui/icons-material/Done';
import WarningIcon from '@mui/icons-material/Warning';
import ErrorIcon from '@mui/icons-material/Error';
import VpnKeyIcon from '@mui/icons-material/VpnKey';
import Diversity3Icon from '@mui/icons-material/Diversity3';
import MenuIcon from '@mui/icons-material/Menu';
import logoHorizontalLightUrl from '../assets/logo-h-light.png'
import logoHorizontalDarkUrl from '../assets/logo-h-dark.png'
import logoVerticalLightUrl from '../assets/logo-v-light.png'
import logoVerticalDarkUrl from '../assets/logo-v-dark.png'
import { getCookie } from '../utils.js';
import { useTheme, useMediaQuery } from "@mui/material";
import ThemeSwitcher from './ThemeSwitcher.jsx';
import { useAppContext } from '../render.jsx';
function OrganizationsSelect({organizations, activeOrganizationId, changeOrganizationCallback, sx}) {
return (
<Select
value={activeOrganizationId || ""}
onChange={(e) => changeOrganizationCallback(e.target.value)}
displayEmpty
inputProps={{ 'aria-label': 'Select organization' }}
sx={{
ml: 1,
fontSize: 16,
'& .MuiSelect-select': {
paddingTop: '8px',
paddingBottom: '8px',
},
'& .MuiSvgIcon-root': {
top: 'calc(50% - 12px)',
},
...sx
}}
>
{organizations.map((org) => (
<MenuItem key={org.id} value={org.id}>
{org.name}
</MenuItem>
))}
</Select>
)
}
function MenuBar({activeOrganizationId, changeOrganizationCallback, showOrganizationSwitcher, drawerWidth}) {
const [menuOpen, setMenuOpen] = useState(false)
const [organizations, setOrganizations] = useState([])
const [deliverContentsJobStatus, setDeliverContentsJobStatus] = useState(null)
const [chip, setChip] = useState(null)
const { localeMessages, isPlatformAdmin, isOrganizationAdmin, direction, apiBaseUrl, platformBaseUrl, sidebarCustomComponent } = useAppContext();
const theme = useTheme();
const isMdUpScreen = useMediaQuery(theme.breakpoints.up('md'));
const drawerVariant = isMdUpScreen ? "permanent" : "temporary";
const logoHorizontalUrl = theme.palette.mode === 'light' ? logoHorizontalLightUrl : logoHorizontalDarkUrl;
const logoVerticalUrl = theme.palette.mode === 'light' ? logoVerticalLightUrl : logoVerticalDarkUrl;
const jobsStatusMap = {
"healthy": {
icon: <DoneIcon fontSize="small" color='black' />,
backgroundColor: "successChipBg",
},
"warning": {
icon: <WarningIcon fontSize="small" color='black' />,
backgroundColor: "warningChipBg",
},
"critical": {
icon: <ErrorIcon fontSize="small" color="black" />,
backgroundColor: "criticalChipBg"
},
};
useEffect(() => {
fetch(apiBaseUrl + '/status/jobs/', {
method: 'GET',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': getCookie('csrftoken'),
},
})
.then(response => response.json())
.then(data => {
const executionTime = data.jobs.deliver_contents.last_execution_started_at ? new Date(data.jobs.deliver_contents.last_execution_started_at).toLocaleString() : null;
setDeliverContentsJobStatus(data.jobs.deliver_contents);
setChip(
<Tooltip title={localeMessages["content_delivery_tooltip"]}><Chip
sx={(theme) => ({backgroundColor: theme.palette[jobsStatusMap[data.jobs.deliver_contents.job_health_status].backgroundColor], px: 1})}
size="small"
icon={jobsStatusMap[data.jobs.deliver_contents.job_health_status].icon}
label={localeMessages["content_delivery_job"]}
/> { executionTime ? <Typography variant="caption" pt="2px" mx={1}>{localeMessages["last_run"]} {executionTime}</Typography> : '' }</Tooltip>
);
})
.catch(error => {
console.error('Error fetching job status:', error);
});
if (!showOrganizationSwitcher) {
return;
}
fetch(apiBaseUrl + '/organizations/', {
method: 'GET',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': getCookie('csrftoken'),
},
})
.then(response => response.json())
.then(data => {
setOrganizations(data.organizations);
})
.catch(error => {
console.error('Error fetching organizations:', error);
});
}, []);
let pages = []
if (isOrganizationAdmin) {
pages.push(
{ name: localeMessages["organizations"], icon: <Diversity3Icon fontSize="small" />, href: platformBaseUrl + '/organizations/'},
);
}
pages.push({ name: localeMessages["course_management"], icon: <SchoolIcon fontSize="small" />, href: platformBaseUrl + '/courses/' });
if (isOrganizationAdmin || isPlatformAdmin) {
pages.push({ name: localeMessages["learners"], icon: <PeopleIcon fontSize="small" />, href: platformBaseUrl + '/learners/' });
}
// pages.push({ name: 'Analytics', icon: <BarChartIcon fontSize="small" />, href: platformBaseUrl + '/analytics/' });
if (isPlatformAdmin) {
pages.push({ name: localeMessages["api_keys"], icon: <VpnKeyIcon fontSize="small" />, href: platformBaseUrl + '/settings/api_keys' });
}
const toggleMenuDrawer = (newOpen) => () => {
setMenuOpen(newOpen);
};
return (
<Box component="nav"sx={{ width: { sm: drawerWidth }, flexShrink: { sm: 0 } }}>
<AppBar sx={{boxShadow: 0, backgroundColor: 'background.nav', borderBottom: {xs: '1px solid', md: 'none'}, borderColor: {xs: 'primary.main', md: 'none'} }}>
<Box my={1} ml={5} sx={{ height: {xs: "57px", md: "30px"}}}>
<img src={logoHorizontalUrl} alt="Logo" style={{maxHeight: "57px", height: "100%"}} />
</Box>
<Box sx={{ position: "absolute", left: direction === 'rtl' ? 'auto' : '270px', right: direction === 'rtl' ? '270px' : 'auto', top: '10px', display: {xs: 'none', md: 'flex' }}}>
{ chip }
</Box>
<Box sx={{display: { xs: 'flex'}, right: direction === 'rtl' ? 'auto' : '0', left: direction === 'rtl' ? '0' : 'auto', position: "absolute" }}>
<ThemeSwitcher />
<Box m={1} paddingTop="7px">
<IconButton aria-controls="menu-appbar" onClick={toggleMenuDrawer(true)} sx={{ display: { xs: 'inline-block', md: 'none' }}}>
<MenuIcon />
</IconButton>
</Box>
</Box>
</AppBar>
<Drawer anchor={direction === 'rtl' ? 'right' : 'left'} variant={drawerVariant} onClose={toggleMenuDrawer(false)} display={{md: "none" }} open={menuOpen} sx={{ '& .MuiDrawer-paper': { boxSizing: 'border-box', width: drawerWidth } }}
slotProps={{ backdrop: { sx: { backgroundColor: 'rgba(251, 251, 255, 0.57)', backdropFilter: 'blur(5px)' }}, paper: { sx: { borderRadius: 0, boxShadow: '2px 0px 8px rgba(0, 0, 0, 0.1)'}}}}>
<Box my={2} textAlign="center">
<img src={logoVerticalUrl} alt="Logo" style={{ width: "50%" }} />
</Box>
{
showOrganizationSwitcher && <OrganizationsSelect organizations={organizations} activeOrganizationId={activeOrganizationId} changeOrganizationCallback={changeOrganizationCallback} sx={{ m: 2 }} />
}
<MenuList>
{ pages.map((page) => (
<MenuItem key={page.name} sx={{'&:hover .MuiListItemIcon-root': { color: 'secondary.dark' }}}>
<Link href={page.href} underline="none" color="inherit" sx={{ display: 'flex', alignItems: 'center', width: '100%' }}>
<ListItemIcon>
{page.icon}
</ListItemIcon>
<ListItemText>{page.name}</ListItemText>
</Link>
</MenuItem>
)) }
</MenuList>
{sidebarCustomComponent && <Box sx={{ height: "100px", width: "100%" }} dangerouslySetInnerHTML={{ __html: sidebarCustomComponent.componentTag }} />}
</Drawer>
</Box>)
}
export default MenuBar