-
-
Notifications
You must be signed in to change notification settings - Fork 793
/
UserMenu.js
141 lines (131 loc) 路 3.38 KB
/
UserMenu.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import * as React from 'react'
import {
Children,
cloneElement,
isValidElement,
useEffect,
useState,
} from 'react'
import PropTypes from 'prop-types'
import { useTranslate, useGetIdentity } from 'react-admin'
import {
Tooltip,
IconButton,
Popover,
MenuList,
Avatar,
Card,
CardContent,
Divider,
Typography,
} from '@material-ui/core'
import { makeStyles } from '@material-ui/core/styles'
import AccountCircle from '@material-ui/icons/AccountCircle'
import config from '../config'
import authProvider from '../authProvider'
import { startEventStream } from '../eventStream'
import { useDispatch } from 'react-redux'
const useStyles = makeStyles((theme) => ({
user: {},
avatar: {
width: theme.spacing(4),
height: theme.spacing(4),
},
username: {
maxWidth: '11em',
marginTop: '-0.7em',
marginBottom: '-1em',
},
usernameWrap: {
whiteSpace: 'nowrap',
overflow: 'hidden',
textOverflow: 'ellipsis',
},
}))
const UserMenu = (props) => {
const [anchorEl, setAnchorEl] = useState(null)
const translate = useTranslate()
const { loaded, identity } = useGetIdentity()
const classes = useStyles(props)
const dispatch = useDispatch()
const { children, label, icon, logout } = props
useEffect(() => {
if (config.devActivityPanel) {
authProvider
.checkAuth()
.then(() => startEventStream(dispatch))
.catch(() => {})
}
}, [dispatch])
if (!logout && !children) return null
const open = Boolean(anchorEl)
const handleMenu = (event) => setAnchorEl(event.currentTarget)
const handleClose = () => setAnchorEl(null)
return (
<div className={classes.user}>
<Tooltip title={label && translate(label, { _: label })}>
<IconButton
aria-label={label && translate(label, { _: label })}
aria-owns={open ? 'menu-appbar' : null}
aria-haspopup={true}
color="inherit"
onClick={handleMenu}
size={'small'}
>
{loaded && identity.avatar ? (
<Avatar
className={classes.avatar}
src={identity.avatar}
alt={identity.fullName}
/>
) : (
icon
)}
</IconButton>
</Tooltip>
<Popover
id="menu-appbar"
anchorEl={anchorEl}
anchorOrigin={{
vertical: 'bottom',
horizontal: 'right',
}}
transformOrigin={{
vertical: 'top',
horizontal: 'right',
}}
open={open}
onClose={handleClose}
>
<MenuList>
{loaded && (
<Card elevation={0} className={classes.username}>
<CardContent className={classes.usernameWrap}>
<Typography variant={'button'}>{identity.fullName}</Typography>
</CardContent>
</Card>
)}
<Divider />
{Children.map(children, (menuItem) =>
isValidElement(menuItem)
? cloneElement(menuItem, {
onClick: handleClose,
})
: null
)}
{!config.auth && logout}
</MenuList>
</Popover>
</div>
)
}
UserMenu.propTypes = {
children: PropTypes.node,
label: PropTypes.string.isRequired,
logout: PropTypes.element,
}
UserMenu.defaultProps = {
label: 'menu.settings',
icon: <AccountCircle />,
}
export default UserMenu