blob: 303fc674a85d1b27484ee76d1238364736a4f069 [file] [log] [blame]
simon26e79f72022-10-05 22:16:08 -04001/*
2 * Copyright (C) 2022 Savoir-faire Linux Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation; either version 3 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
13 *
14 * You should have received a copy of the GNU Affero General Public
15 * License along with this program. If not, see
16 * <https://www.gnu.org/licenses/>.
17 */
simond47ef9e2022-09-28 22:24:28 -040018import { Box, Button, Menu, MenuItem } from '@mui/material';
simon6b9ddfb2022-10-03 00:04:50 -040019import { MouseEvent, useState } from 'react';
simond47ef9e2022-09-28 22:24:28 -040020import { useNavigate, useParams } from 'react-router-dom';
simon07b4eb02022-09-29 17:50:26 -040021
simond47ef9e2022-09-28 22:24:28 -040022import authManager from '../AuthManager';
Larbi Gharibe9af9732021-03-31 15:08:01 +010023
24export default function Header() {
simond47ef9e2022-09-28 22:24:28 -040025 const navigate = useNavigate();
simon6b9ddfb2022-10-03 00:04:50 -040026 const [anchorEl, setAnchorEl] = useState<HTMLButtonElement | null>(null);
27 const handleClick = (event: MouseEvent<HTMLButtonElement>) => setAnchorEl(event.currentTarget);
simond47ef9e2022-09-28 22:24:28 -040028 const handleClose = () => setAnchorEl(null);
29 const params = useParams();
Larbi Gharibe9af9732021-03-31 15:08:01 +010030
simond47ef9e2022-09-28 22:24:28 -040031 const goToAccountSelection = () => navigate(`/account`);
32 const goToContacts = () => navigate(`/Contacts`);
33 const goToAccountSettings = () => navigate(`/account/${params.accountId}/settings`);
Larbi Gharibe9af9732021-03-31 15:08:01 +010034
Adrien Béraud150b4782021-04-21 19:40:59 -040035 return (
36 <Box>
37 <Button aria-controls="simple-menu" aria-haspopup="true" onClick={handleClick}>
38 Menu
39 </Button>
simond47ef9e2022-09-28 22:24:28 -040040 <Menu id="simple-menu" anchorEl={anchorEl} open={Boolean(anchorEl)} onClose={handleClose}>
Adrien Béraud150b4782021-04-21 19:40:59 -040041 <MenuItem onClick={goToAccountSelection}>Change account</MenuItem>
idillon531b6f22022-09-16 14:02:00 -040042 <MenuItem onClick={goToContacts}>Contacts</MenuItem>
Adrien Béraud150b4782021-04-21 19:40:59 -040043 {params.accountId && <MenuItem onClick={goToAccountSettings}>Account settings</MenuItem>}
44 <MenuItem onClick={() => authManager.disconnect()}>Log out</MenuItem>
45 </Menu>
simond47ef9e2022-09-28 22:24:28 -040046 </Box>
47 );
48}