simon | 26e79f7 | 2022-10-05 22:16:08 -0400 | [diff] [blame] | 1 | /* |
| 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 Wang | 822718d | 2023-02-17 14:15:13 -0500 | [diff] [blame] | 18 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 19 | import { Box, Button, Menu, MenuItem } from '@mui/material'; |
simon | 6b9ddfb | 2022-10-03 00:04:50 -0400 | [diff] [blame] | 20 | import { MouseEvent, useState } from 'react'; |
simon | 4e7445c | 2022-11-16 21:18:46 -0500 | [diff] [blame] | 21 | import { useTranslation } from 'react-i18next'; |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 22 | import { useNavigate } from 'react-router-dom'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 23 | |
simon | 416d079 | 2022-11-03 02:46:18 -0400 | [diff] [blame] | 24 | import { useAuthContext } from '../contexts/AuthProvider'; |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 25 | |
Ziwei Wang | 822718d | 2023-02-17 14:15:13 -0500 | [diff] [blame] | 26 | type NavigateURL = '/' | '/contacts' | '/settings-account' | '/settings-general'; |
| 27 | |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 28 | export default function Header() { |
simon | 4e7445c | 2022-11-16 21:18:46 -0500 | [diff] [blame] | 29 | const { t } = useTranslation(); |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 30 | const { logout } = useAuthContext(); |
simon | 416d079 | 2022-11-03 02:46:18 -0400 | [diff] [blame] | 31 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 32 | const navigate = useNavigate(); |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 33 | |
Ziwei Wang | 822718d | 2023-02-17 14:15:13 -0500 | [diff] [blame] | 34 | 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 Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 43 | |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 44 | return ( |
| 45 | <Box> |
| 46 | <Button aria-controls="simple-menu" aria-haspopup="true" onClick={handleClick}> |
idillon | 3470d07 | 2022-11-22 15:22:34 -0500 | [diff] [blame] | 47 | {t('Menu')} |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 48 | </Button> |
Ziwei Wang | 822718d | 2023-02-17 14:15:13 -0500 | [diff] [blame] | 49 | <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> |
simon | 4e7445c | 2022-11-16 21:18:46 -0500 | [diff] [blame] | 54 | <MenuItem onClick={logout}>{t('logout')}</MenuItem> |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 55 | </Menu> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 56 | </Box> |
| 57 | ); |
| 58 | } |