frontend/containers/GenericMenu/index.js (view raw)
1import Menu from '@material-ui/core/Menu';
2import {useTranslation} from 'react-i18next';
3import useAuthStore from '../../stores/useAuthStore';
4import useProfile from '../../hooks/useProfile';
5import useSettings from '../../hooks/useSettings';
6import Languages from '../Languages';
7import Action from './Action';
8
9const GenericMenu = ({anchorEl, setAnchorEl, actions = []}) => {
10 const {t} = useTranslation();
11 const settings = useSettings();
12 const logout = useAuthStore(s => s.logout);
13 const {user} = useProfile();
14
15 const logoutMenuItem = user && {
16 label: t('menu.logout'),
17 onClick: () => {
18 logout();
19 window.location.href = settings['about_link'];
20 setAnchorEl(null);
21 },
22 id: 'LogoutTabs',
23 };
24 const aboutMenuItem = {
25 label: t('menu.about'),
26 onClick: () => {
27 window.location.href = settings['about_link'];
28 setAnchorEl(null);
29 },
30 id: 'AboutTabs',
31 };
32 const languageMenuItem = {
33 label: <Languages />,
34 isComponentLabel: true,
35 id: 'LanguageSelection',
36 };
37
38 const validActions = [
39 ...actions,
40 aboutMenuItem,
41 languageMenuItem,
42 logoutMenuItem,
43 ].filter(Boolean);
44
45 return (
46 <Menu
47 anchorEl={anchorEl}
48 anchorOrigin={{
49 vertical: 'top',
50 horizontal: 'right',
51 }}
52 keepMounted
53 transformOrigin={{
54 vertical: 'top',
55 horizontal: 'right',
56 }}
57 open={!!anchorEl}
58 onClose={() => setAnchorEl(null)}
59 >
60 {validActions?.map((action, index) => (
61 <Action action={action} key={index} setAnchorEl={setAnchorEl} />
62 ))}
63 </Menu>
64 );
65};
66
67export default GenericMenu;