blob: 496330946442a78963a7eb16468bd0ad76897721 [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 */
Ziwei Wang822718d2023-02-17 14:15:13 -050018
simond47ef9e2022-09-28 22:24:28 -040019import { Box, Button, Menu, MenuItem } from '@mui/material';
simon6b9ddfb2022-10-03 00:04:50 -040020import { MouseEvent, useState } from 'react';
simon4e7445c2022-11-16 21:18:46 -050021import { useTranslation } from 'react-i18next';
simon5da8ca62022-11-09 15:21:25 -050022import { useNavigate } from 'react-router-dom';
simon07b4eb02022-09-29 17:50:26 -040023
simon416d0792022-11-03 02:46:18 -040024import { useAuthContext } from '../contexts/AuthProvider';
Larbi Gharibe9af9732021-03-31 15:08:01 +010025
Ziwei Wang822718d2023-02-17 14:15:13 -050026type NavigateURL = '/' | '/contacts' | '/settings-account' | '/settings-general';
27
Larbi Gharibe9af9732021-03-31 15:08:01 +010028export default function Header() {
simon4e7445c2022-11-16 21:18:46 -050029 const { t } = useTranslation();
simon5da8ca62022-11-09 15:21:25 -050030 const { logout } = useAuthContext();
simon416d0792022-11-03 02:46:18 -040031
simond47ef9e2022-09-28 22:24:28 -040032 const navigate = useNavigate();
Larbi Gharibe9af9732021-03-31 15:08:01 +010033
Ziwei Wang822718d2023-02-17 14:15:13 -050034 const [anchorEl, setAnchorEl] = useState<HTMLButtonElement | null>(null);
35
36 const handleClick = (event: MouseEvent<HTMLButtonElement>) => setAnchorEl(event.currentTarget);
37 const closeMenuAndNavigate = (navigateUrl?: NavigateURL) => {
38 setAnchorEl(null);
39 if (navigateUrl) {
40 navigate(navigateUrl);
41 }
42 };
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040043
Adrien Béraud150b4782021-04-21 19:40:59 -040044 return (
45 <Box>
46 <Button aria-controls="simple-menu" aria-haspopup="true" onClick={handleClick}>
idillon3470d072022-11-22 15:22:34 -050047 {t('Menu')}
Adrien Béraud150b4782021-04-21 19:40:59 -040048 </Button>
Ziwei Wang822718d2023-02-17 14:15:13 -050049 <Menu id="simple-menu" anchorEl={anchorEl} open={Boolean(anchorEl)} onClose={() => closeMenuAndNavigate()}>
50 <MenuItem onClick={() => closeMenuAndNavigate(`/`)}>{t('conversations')}</MenuItem>
51 <MenuItem onClick={() => closeMenuAndNavigate(`/contacts`)}>{t('settings_menu_item_contacts')}</MenuItem>
52 <MenuItem onClick={() => closeMenuAndNavigate('/settings-account')}>{t('settings_menu_item_account')}</MenuItem>
53 <MenuItem onClick={() => closeMenuAndNavigate('/settings-general')}>{t('settings_menu_item_general')}</MenuItem>
simon4e7445c2022-11-16 21:18:46 -050054 <MenuItem onClick={logout}>{t('logout')}</MenuItem>
Adrien Béraud150b4782021-04-21 19:40:59 -040055 </Menu>
simond47ef9e2022-09-28 22:24:28 -040056 </Box>
57 );
58}