|
11 | 11 | * limitations under the License. |
12 | 12 | * */ |
13 | 13 |
|
14 | | -import React, { useState } from "react"; |
15 | | -import T from "i18n-react"; |
16 | | -import {Button, Box} from '@mui/material'; |
17 | | -import styles from "./styles.module.scss" |
| 14 | +import React, {useRef, useState} from "react"; |
| 15 | +import T from "i18n-react/dist/i18n-react"; |
| 16 | +import {useTheme} from "@mui/material/styles"; |
| 17 | +import Box from "@mui/material/Box"; |
| 18 | +import Button from "@mui/material/Button"; |
| 19 | +import Divider from "@mui/material/Divider"; |
| 20 | +import IconButton from "@mui/material/IconButton"; |
| 21 | +import ListItemIcon from "@mui/material/ListItemIcon"; |
| 22 | +import ListItemText from "@mui/material/ListItemText"; |
| 23 | +import Menu from "@mui/material/Menu"; |
| 24 | +import MenuItem from "@mui/material/MenuItem"; |
| 25 | +import Typography from "@mui/material/Typography"; |
| 26 | +import AccountCircleIcon from "@mui/icons-material/AccountCircle"; |
| 27 | +import LogoutIcon from "@mui/icons-material/Logout"; |
| 28 | +import PersonIcon from "@mui/icons-material/Person"; |
| 29 | +import SettingsIcon from "@mui/icons-material/Settings"; |
| 30 | +import styles from "./styles.module.scss"; |
18 | 31 |
|
19 | | -const AuthButton = ({ isLoggedUser, doLogin, initLogOut, picture }) => { |
20 | | - const [showLogOut, setShowLogOut] = useState(false); |
| 32 | +const AuthButton = ({ |
| 33 | + isLoggedUser, |
| 34 | + doLogin, |
| 35 | + initLogOut, |
| 36 | + profileEmail, |
| 37 | + profileName |
| 38 | + }) => { |
| 39 | + const theme = useTheme(); |
| 40 | + const anchorRef = useRef(null); |
| 41 | + const [menuOpen, setMenuOpen] = useState(false); |
21 | 42 |
|
22 | | - const toggleLogOut = () => { |
23 | | - setShowLogOut(!showLogOut); |
| 43 | + const menuFontSize = theme?.custom?.menu?.fontSize || "1.4rem"; |
| 44 | + const menuLineHeight = theme?.custom?.menu?.lineHeight || "2rem"; |
| 45 | + const menuIconColor = theme?.custom?.menu?.icon || theme.palette.text.secondary; |
| 46 | + |
| 47 | + const openMenu = () => { |
| 48 | + setMenuOpen(true); |
| 49 | + }; |
| 50 | + |
| 51 | + const closeMenu = () => { |
| 52 | + setMenuOpen(false); |
24 | 53 | }; |
25 | 54 |
|
26 | 55 | if (isLoggedUser) { |
27 | 56 | return ( |
28 | | - <div className={styles.userMenu} onClick={toggleLogOut}> |
29 | | - <div |
30 | | - className={styles.profilePic} |
31 | | - style={{ backgroundImage: `url(${picture})` }} |
32 | | - /> |
33 | | - {showLogOut && ( |
34 | | - <Button |
35 | | - className={styles.logout} |
36 | | - variant="contained" |
37 | | - size="small" |
38 | | - color="secondary" |
| 57 | + <Box className={styles.userMenu}> |
| 58 | + <IconButton |
| 59 | + aria-controls={menuOpen ? "user-menu" : undefined} |
| 60 | + aria-expanded={menuOpen ? "true" : undefined} |
| 61 | + aria-haspopup="true" |
| 62 | + onClick={openMenu} |
| 63 | + size="medium" |
| 64 | + data-testid="user-menu-button" |
| 65 | + sx={{ |
| 66 | + color: theme.palette.text.primary, |
| 67 | + p: 0, |
| 68 | + width: 28, |
| 69 | + height: 28 |
| 70 | + }} |
| 71 | + ref={anchorRef} |
| 72 | + > |
| 73 | + <AccountCircleIcon sx={{fontSize: 28}}/> |
| 74 | + </IconButton> |
| 75 | + <Menu |
| 76 | + id="user-menu" |
| 77 | + anchorEl={anchorRef.current} |
| 78 | + open={menuOpen} |
| 79 | + onClose={closeMenu} |
| 80 | + anchorOrigin={{ |
| 81 | + vertical: "bottom", |
| 82 | + horizontal: "right" |
| 83 | + }} |
| 84 | + transformOrigin={{ |
| 85 | + vertical: "top", |
| 86 | + horizontal: "right" |
| 87 | + }} |
| 88 | + slotProps={{ |
| 89 | + paper: { |
| 90 | + sx: { |
| 91 | + mt: 1.5, |
| 92 | + minWidth: 260, |
| 93 | + borderRadius: 2, |
| 94 | + overflow: "hidden", |
| 95 | + boxShadow: "0 16px 40px rgba(0, 0, 0, 0.18)" |
| 96 | + } |
| 97 | + }, |
| 98 | + list: { |
| 99 | + disablePadding: true, |
| 100 | + sx: {py: 0} |
| 101 | + } |
| 102 | + }} |
| 103 | + > |
| 104 | + <Box sx={{px: 3, py: 2.5}}> |
| 105 | + <Typography |
| 106 | + variant="body1" |
| 107 | + sx={{ |
| 108 | + fontSize: menuFontSize, |
| 109 | + lineHeight: menuLineHeight, |
| 110 | + color: theme.palette.text.primary, |
| 111 | + mb: 0.5 |
| 112 | + }} |
| 113 | + > |
| 114 | + {profileName || "User"} |
| 115 | + </Typography> |
| 116 | + <Typography |
| 117 | + variant="body2" |
| 118 | + sx={{ |
| 119 | + fontSize: menuFontSize, |
| 120 | + lineHeight: menuLineHeight, |
| 121 | + color: theme.palette.text.secondary |
| 122 | + }} |
| 123 | + > |
| 124 | + {profileEmail || ""} |
| 125 | + </Typography> |
| 126 | + </Box> |
| 127 | + <Divider/> |
| 128 | + <MenuItem onClick={closeMenu} sx={{py: 1.5, px: 3, gap: 1.5}}> |
| 129 | + <ListItemIcon sx={{minWidth: 32, color: menuIconColor}}> |
| 130 | + <SettingsIcon sx={{fontSize: 24}}/> |
| 131 | + </ListItemIcon> |
| 132 | + <ListItemText |
| 133 | + primary={T.translate("general.settings")} |
| 134 | + primaryTypographyProps={{ |
| 135 | + sx: { |
| 136 | + fontSize: menuFontSize, |
| 137 | + color: theme.palette.text.primary |
| 138 | + } |
| 139 | + }} |
| 140 | + /> |
| 141 | + </MenuItem> |
| 142 | + <MenuItem onClick={closeMenu} sx={{py: 1.5, px: 3, gap: 1.5}}> |
| 143 | + <ListItemIcon sx={{minWidth: 32, color: menuIconColor}}> |
| 144 | + <PersonIcon sx={{fontSize: 24}}/> |
| 145 | + </ListItemIcon> |
| 146 | + <ListItemText |
| 147 | + primary={T.translate("general.profile")} |
| 148 | + primaryTypographyProps={{ |
| 149 | + sx: { |
| 150 | + fontSize: menuFontSize, |
| 151 | + color: theme.palette.text.primary |
| 152 | + } |
| 153 | + }} |
| 154 | + /> |
| 155 | + </MenuItem> |
| 156 | + <Divider/> |
| 157 | + <MenuItem |
39 | 158 | onClick={() => { |
| 159 | + closeMenu(); |
40 | 160 | initLogOut(); |
41 | 161 | }} |
| 162 | + sx={{py: 1.5, px: 3, gap: 1.5}} |
42 | 163 | > |
43 | | - {T.translate("buttons.sign_out")} |
44 | | - </Button> |
45 | | - )} |
46 | | - </div> |
47 | | - ); |
48 | | - } |
49 | | - return ( |
50 | | - <Box className={styles.login}> |
51 | | - <Button |
52 | | - variant="contained" |
53 | | - size="small" |
54 | | - color="secondary" |
55 | | - onClick={() => { |
56 | | - doLogin(); |
57 | | - }} |
58 | | - > |
59 | | - {T.translate("buttons.log_in")} |
60 | | - </Button> |
| 164 | + <ListItemIcon sx={{minWidth: 32, color: menuIconColor}}> |
| 165 | + <LogoutIcon sx={{fontSize: 24}}/> |
| 166 | + </ListItemIcon> |
| 167 | + <ListItemText |
| 168 | + primary={T.translate("buttons.sign_out")} |
| 169 | + primaryTypographyProps={{ |
| 170 | + sx: { |
| 171 | + fontSize: menuFontSize, |
| 172 | + color: theme.palette.text.primary |
| 173 | + } |
| 174 | + }} |
| 175 | + /> |
| 176 | + </MenuItem> |
| 177 | + </Menu> |
61 | 178 | </Box> |
62 | 179 | ); |
| 180 | + } |
63 | 181 |
|
| 182 | + return ( |
| 183 | + <Box className={styles.login}> |
| 184 | + <Button |
| 185 | + variant="contained" |
| 186 | + size="small" |
| 187 | + color="secondary" |
| 188 | + onClick={() => { |
| 189 | + doLogin(); |
| 190 | + }} |
| 191 | + > |
| 192 | + {T.translate("buttons.log_in")} |
| 193 | + </Button> |
| 194 | + </Box> |
| 195 | + ); |
64 | 196 | }; |
65 | 197 |
|
66 | 198 | export default AuthButton; |
0 commit comments