-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathUserLabel.js
More file actions
172 lines (157 loc) · 4.3 KB
/
UserLabel.js
File metadata and controls
172 lines (157 loc) · 4.3 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import React, { useState, useMemo, useRef } from 'react'
import { Link } from 'react-router-dom'
import { Overlay, OverlayTrigger, Popover as BootstrapPopover, Tooltip } from 'react-bootstrap'
import PropTypes from 'prop-types'
import moment from 'moment'
import { throttle } from 'lodash'
import css from './UserLabel.css'
import { Avatar } from './Avatar'
import { getUserDisplayName } from '../lib/common'
import { getConfig } from './config'
const USER_TAG = {
new: {
content: 'New',
tip: 'Registered within 3 months',
},
early: {
content: 'Early',
tip: 'Registered before 2 years ago',
},
vip: {
content: 'VIP',
},
}
function getUserTags(user) {
const tags = []
const createdAt = user.createdAt || user.created_at
if (createdAt) {
const now = moment()
if (now.diff(createdAt, 'month') <= 3) {
tags.push('new')
}
if (now.diff(createdAt, 'year') >= 2) {
tags.push('early')
}
}
return user.tags ? tags.concat(user.tags) : tags
}
function UserTag({ name }) {
if (!USER_TAG[name]) {
return <span className={css.tag}>{name}</span>
}
const { content, tip } = USER_TAG[name]
const element = <span className={`${css.tag} ${css[name]}`}>{content}</span>
if (tip) {
return (
<OverlayTrigger placement="bottom" overlay={<Tooltip id={`user-tag-${name}`}>{tip}</Tooltip>}>
{element}
</OverlayTrigger>
)
}
return element
}
UserTag.propTypes = {
name: PropTypes.string.isRequired,
}
export function UserTags({ user, className }) {
const tags = getUserTags(user)
if (tags.length === 0) {
return null
}
return (
<span className={className}>
{tags.map((tag) => (
<UserTag key={tag} name={tag} />
))}
</span>
)
}
UserTags.propTypes = {
user: PropTypes.object.isRequired,
className: PropTypes.string,
}
const Popover = ({ children, overlay, placement = 'bottom', ...props }) => {
const [show, setShow] = useState(false)
const target = useRef(null)
const throttledSetShow = useMemo(() => throttle(setShow, 250, { leading: false }), [])
const handleMouseOver = (event) => {
!show && (target.current = event.target)
throttledSetShow(true)
}
const handleMouseLeave = () => {
throttledSetShow(false)
}
const handleOverlayMouseEnter = () => {
throttledSetShow(true)
}
const handleOverlayMouseLeave = () => {
throttledSetShow(false)
}
return (
<>
<span onMouseOver={handleMouseOver} onMouseLeave={handleMouseLeave} ref={target} {...props}>
{children}
</span>
<Overlay
target={target.current}
show={show}
placement={placement}
flip={true}
transition={false}
>
{(props) =>
overlay && (
<BootstrapPopover
onMouseEnter={handleOverlayMouseEnter}
onMouseLeave={handleOverlayMouseLeave}
{...props}
>
<BootstrapPopover.Content>{overlay}</BootstrapPopover.Content>
</BootstrapPopover>
)
}
</Overlay>
</>
)
}
Popover.propTypes = {
placement: PropTypes.string,
overlay: PropTypes.node.isRequired,
children: PropTypes.node.isRequired,
}
const isGenaratedName = (name) => /^[0-9a-z]{25}$/.test(name)
export function UserLabel({ user, simple, displayTags, displayId }) {
const name = getUserDisplayName(user)
const { overlay: UserLabelOverlay } =
getConfig('ticket.metadata.customMetadata.userLabelOverlay') ?? {}
if (simple) {
return <span>{name}</span>
}
return (
<span>
<Popover
overlay={UserLabelOverlay ? <UserLabelOverlay user={user} /> : false}
placement="bottom"
id={`popover-${name}`}
>
<Link to={'/users/' + user.username} className="avatar">
<Avatar user={user} />
</Link>
<Link to={'/users/' + user.username} className="username">
{name}
</Link>
</Popover>
{displayId && name !== user.username && !isGenaratedName(user.username) && (
<span className="text-muted"> ({user.username})</span>
)}
{displayTags && <UserTags user={user} />}
</span>
)
}
UserLabel.displayName = 'UserLabel'
UserLabel.propTypes = {
user: PropTypes.object.isRequired,
simple: PropTypes.bool,
displayTags: PropTypes.bool,
displayId: PropTypes.bool,
}