profileBar.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // import { Avatar, Button, AppBar } from "@mui/material"
  2. import { connect } from "react-redux"
  3. import { actionAuthLogout } from "../actions"
  4. import { useState } from "react";
  5. import Box from '@mui/material/Box';
  6. import Avatar from '@mui/material/Avatar';
  7. import Menu from '@mui/material/Menu';
  8. import MenuItem from '@mui/material/MenuItem';
  9. import ListItemIcon from '@mui/material/ListItemIcon';
  10. import Divider from '@mui/material/Divider';
  11. import IconButton from '@mui/material/IconButton';
  12. import Tooltip from '@mui/material/Tooltip';
  13. import Logout from '@mui/icons-material/Logout';
  14. import { Link } from "react-router-dom";
  15. const Profile = ({ user, onLogout }) => {
  16. const [anchorEl, setAnchorEl] = useState(null);
  17. const open = Boolean(anchorEl);
  18. const handleClick = (event) => {
  19. setAnchorEl(event.currentTarget);
  20. }
  21. const handleClose = () => {
  22. setAnchorEl(null);
  23. }
  24. return (
  25. <>
  26. <Box sx={{ display: 'flex', alignItems: 'center', textAlign: 'center' }}>
  27. {user && <Tooltip title="Account settings">
  28. <IconButton onClick={handleClick} size="small" sx={{ ml: 2 }}>
  29. {user.avatar ?
  30. <Avatar src={user.avatar.url} alt={user.login} />
  31. :
  32. <Avatar>{(user.nick && user.nick[0]) || user.login[0]}</Avatar>}
  33. </IconButton>
  34. </Tooltip>}
  35. </Box>
  36. <Menu
  37. anchorEl={anchorEl}
  38. open={open}
  39. onClose={handleClose}
  40. onClick={handleClose}
  41. PaperProps={{
  42. elevation: 0,
  43. sx: {
  44. overflow: 'visible',
  45. filter: 'drop-shadow(0px 2px 8px rgba(0,0,0,0.32))',
  46. mt: 1.5,
  47. '& .MuiAvatar-root': {
  48. width: 32,
  49. height: 32,
  50. ml: -0.5,
  51. mr: 1,
  52. },
  53. '&:before': {
  54. content: '""',
  55. display: 'block',
  56. position: 'absolute',
  57. top: 0,
  58. right: 14,
  59. width: 10,
  60. height: 10,
  61. bgcolor: 'background.paper',
  62. transform: 'translateY(-50%) rotate(45deg)',
  63. zIndex: 0,
  64. },
  65. },
  66. }}
  67. transformOrigin={{ horizontal: 'right', vertical: 'top' }}
  68. anchorOrigin={{ horizontal: 'right', vertical: 'bottom' }}
  69. >
  70. <MenuItem component={Link} to={`/profile`}>
  71. Profile </MenuItem>
  72. <Divider />
  73. <MenuItem onClick={onLogout}>
  74. <ListItemIcon>
  75. <Logout fontSize="small" />
  76. </ListItemIcon>
  77. Logout
  78. </MenuItem>
  79. </Menu>
  80. </>
  81. )
  82. }
  83. export const ConnectProfile = connect(state => ({ user: state.promise.userInfo?.payload?.data?.UserFindOne }),
  84. {
  85. onLogout: actionAuthLogout
  86. })(Profile)