2020-08-17 21:07:16 +01:00
|
|
|
import React from 'react'
|
2020-08-17 21:59:29 +01:00
|
|
|
import PropTypes from 'prop-types'
|
2020-08-17 21:39:25 +01:00
|
|
|
import { connect } from 'react-redux'
|
2020-02-21 00:57:29 +00:00
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes'
|
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component'
|
2020-06-13 02:51:39 +01:00
|
|
|
import debounce from 'lodash.debounce'
|
2020-04-24 04:17:27 +01:00
|
|
|
import { me } from '../initial_state'
|
|
|
|
import {
|
|
|
|
CX,
|
|
|
|
POPOVER_USER_INFO,
|
|
|
|
} from '../constants'
|
2020-02-28 15:20:47 +00:00
|
|
|
import { openPopover, closePopover } from '../actions/popover'
|
2020-02-21 00:57:29 +00:00
|
|
|
import Icon from './icon'
|
2020-04-24 04:17:27 +01:00
|
|
|
import Text from './text'
|
2020-02-28 15:20:47 +00:00
|
|
|
|
|
|
|
class DisplayName extends ImmutablePureComponent {
|
2020-02-08 06:12:01 +00:00
|
|
|
|
2020-04-23 07:13:29 +01:00
|
|
|
mouseOverTimeout = null
|
|
|
|
|
2020-06-13 02:51:39 +01:00
|
|
|
componentWillUnmount () {
|
|
|
|
document.removeEventListener('mousemove', this.handleMouseMove, true)
|
|
|
|
clearTimeout(this.mouseOverTimeout)
|
|
|
|
}
|
|
|
|
|
2020-04-23 07:13:29 +01:00
|
|
|
handleMouseEnter = () => {
|
|
|
|
if (this.mouseOverTimeout) return null
|
|
|
|
this.mouseOverTimeout = setTimeout(() => {
|
|
|
|
this.props.openUserInfoPopover({
|
|
|
|
targetRef: this.node,
|
|
|
|
position: 'top',
|
2020-06-13 02:51:39 +01:00
|
|
|
accountId: this.props.account.get('id'),
|
2020-04-23 07:13:29 +01:00
|
|
|
})
|
2020-06-13 02:51:39 +01:00
|
|
|
document.addEventListener('mousemove', this.handleMouseMove, true)
|
2020-06-15 22:55:29 +01:00
|
|
|
}, 1250)
|
2020-04-23 07:13:29 +01:00
|
|
|
}
|
2020-03-11 23:56:18 +00:00
|
|
|
|
2020-06-13 02:51:39 +01:00
|
|
|
handleMouseLeave = debounce((e) => {
|
|
|
|
this.attemptToHidePopover(e)
|
|
|
|
}, 250)
|
|
|
|
|
|
|
|
handleMouseMove = debounce((e) => {
|
|
|
|
this.attemptToHidePopover(e)
|
2020-06-15 22:55:29 +01:00
|
|
|
}, 100)
|
2020-06-13 02:51:39 +01:00
|
|
|
|
|
|
|
attemptToHidePopover = (e) => {
|
|
|
|
const lastTarget = e.toElement || e.relatedTarget
|
2020-06-15 22:55:29 +01:00
|
|
|
const isElement = (lastTarget instanceof Element || lastTarget instanceof HTMLDocument)
|
2020-06-13 02:51:39 +01:00
|
|
|
const userInfoPopoverEl = document.getElementById('user-info-popover')
|
|
|
|
|
|
|
|
if (this.mouseOverTimeout &&
|
|
|
|
(
|
2020-06-15 22:55:29 +01:00
|
|
|
!isElement && !userInfoPopoverEl ||
|
|
|
|
(userInfoPopoverEl && isElement && lastTarget && !userInfoPopoverEl.contains(lastTarget)) ||
|
|
|
|
(!userInfoPopoverEl && isElement && lastTarget && this.node && !this.node.contains(lastTarget))
|
2020-06-13 02:51:39 +01:00
|
|
|
)) {
|
|
|
|
document.removeEventListener('mousemove', this.handleMouseMove, true)
|
2020-04-23 07:13:29 +01:00
|
|
|
clearTimeout(this.mouseOverTimeout)
|
|
|
|
this.mouseOverTimeout = null
|
|
|
|
this.props.closeUserInfoPopover()
|
|
|
|
}
|
|
|
|
}
|
2020-02-28 15:20:47 +00:00
|
|
|
|
2020-03-11 23:56:18 +00:00
|
|
|
setRef = (n) => {
|
2020-04-23 07:13:29 +01:00
|
|
|
this.node = n
|
2020-02-21 00:57:29 +00:00
|
|
|
}
|
2020-02-08 06:12:01 +00:00
|
|
|
|
2020-02-28 15:20:47 +00:00
|
|
|
render() {
|
2020-03-04 03:45:16 +00:00
|
|
|
const {
|
|
|
|
account,
|
2020-04-24 04:17:27 +01:00
|
|
|
isMultiline,
|
|
|
|
isLarge,
|
2020-03-04 03:45:16 +00:00
|
|
|
noHover,
|
2020-03-08 23:02:28 +00:00
|
|
|
noUsername,
|
2020-04-24 04:17:27 +01:00
|
|
|
noRelationship,
|
2020-05-07 00:40:54 +01:00
|
|
|
isSmall,
|
|
|
|
isComment,
|
2020-05-10 04:26:58 +01:00
|
|
|
isCentered,
|
2020-12-06 04:47:48 +00:00
|
|
|
isInline,
|
2020-03-04 03:45:16 +00:00
|
|
|
} = this.props
|
2020-02-28 15:20:47 +00:00
|
|
|
|
|
|
|
if (!account) return null
|
|
|
|
|
2020-04-24 04:17:27 +01:00
|
|
|
const containerClassName = CX({
|
2020-08-18 21:49:11 +01:00
|
|
|
d: 1,
|
2020-08-18 21:43:06 +01:00
|
|
|
maxW100PC: 1,
|
|
|
|
aiCenter: !isMultiline,
|
2020-04-24 04:17:27 +01:00
|
|
|
flexRow: !isMultiline,
|
|
|
|
cursorPointer: !noHover,
|
2020-08-18 21:43:06 +01:00
|
|
|
aiCenter: isCentered,
|
2020-12-06 04:47:48 +00:00
|
|
|
displayInlineBlock: isInline,
|
2020-04-24 04:17:27 +01:00
|
|
|
})
|
2020-02-28 15:20:47 +00:00
|
|
|
|
2020-04-24 04:17:27 +01:00
|
|
|
const displayNameClasses = CX({
|
2020-02-28 15:20:47 +00:00
|
|
|
text: 1,
|
|
|
|
overflowWrapBreakWord: 1,
|
|
|
|
whiteSpaceNoWrap: 1,
|
2020-08-18 21:43:06 +01:00
|
|
|
fw600: 1,
|
|
|
|
cPrimary: 1,
|
2020-03-11 23:56:18 +00:00
|
|
|
mr2: 1,
|
2020-04-24 04:17:27 +01:00
|
|
|
lineHeight125: !isSmall,
|
2020-04-29 23:32:49 +01:00
|
|
|
fs14PX: isSmall,
|
|
|
|
fs15PX: !isLarge,
|
|
|
|
fs24PX: isLarge && !isSmall,
|
2020-02-28 15:20:47 +00:00
|
|
|
})
|
|
|
|
|
2020-04-24 04:17:27 +01:00
|
|
|
const usernameClasses = CX({
|
2020-02-28 15:20:47 +00:00
|
|
|
text: 1,
|
|
|
|
displayFlex: 1,
|
2020-12-20 17:27:24 +00:00
|
|
|
flexShrink0: 1,
|
2020-02-28 15:20:47 +00:00
|
|
|
overflowWrapBreakWord: 1,
|
|
|
|
textOverflowEllipsis: 1,
|
2020-08-18 21:43:06 +01:00
|
|
|
cSecondary: 1,
|
|
|
|
fw400: 1,
|
2020-04-24 04:17:27 +01:00
|
|
|
lineHeight15: isMultiline,
|
|
|
|
lineHeight125: !isMultiline,
|
|
|
|
ml5: !isMultiline,
|
2020-04-29 23:32:49 +01:00
|
|
|
fs14PX: isSmall,
|
|
|
|
fs15PX: !isLarge,
|
|
|
|
fs16PX: isLarge && !isSmall,
|
2020-02-28 15:20:47 +00:00
|
|
|
})
|
|
|
|
|
2020-03-08 23:02:28 +00:00
|
|
|
const iconSize =
|
2020-05-10 04:26:58 +01:00
|
|
|
!!isLarge ? 19 :
|
|
|
|
!!isComment ? 12 :
|
|
|
|
!!isSmall ? 14 : 15
|
2020-02-08 06:12:01 +00:00
|
|
|
|
2020-04-24 04:17:27 +01:00
|
|
|
let relationshipLabel
|
|
|
|
if (me && account) {
|
|
|
|
const accountId = account.get('id')
|
|
|
|
const isFollowedBy = (me !== accountId && account.getIn(['relationship', 'followed_by']))
|
|
|
|
|
|
|
|
if (isFollowedBy) {
|
2020-12-20 17:27:24 +00:00
|
|
|
relationshipLabel = 'Follows you'
|
2020-04-24 04:17:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-08 06:12:01 +00:00
|
|
|
return (
|
2020-04-24 04:17:27 +01:00
|
|
|
<div
|
|
|
|
className={containerClassName}
|
|
|
|
onMouseEnter={noHover ? undefined : this.handleMouseEnter}
|
|
|
|
onMouseLeave={noHover ? undefined : this.handleMouseLeave}
|
|
|
|
ref={this.setRef}
|
|
|
|
>
|
2020-12-31 20:24:44 +00:00
|
|
|
<span className={[_s.d, _s.flexRow, _s.aiCenter, _s.maxW100PC, _s.flexShrink1, _s.overflowHidden, _s.cPrimary].join(' ')}>
|
2020-02-28 15:20:47 +00:00
|
|
|
<bdi className={[_s.text, _s.whiteSpaceNoWrap, _s.textOverflowEllipsis].join(' ')}>
|
|
|
|
<strong
|
|
|
|
className={displayNameClasses}
|
|
|
|
dangerouslySetInnerHTML={{ __html: account.get('display_name_html') }}
|
|
|
|
/>
|
2020-04-24 04:17:27 +01:00
|
|
|
{
|
2020-05-10 04:26:58 +01:00
|
|
|
account.get('locked') &&
|
2020-08-18 21:43:06 +01:00
|
|
|
<Icon id='lock-filled' size={`${iconSize - 3}px`} className={[_s.cPrimary, _s.ml5].join(' ')} />
|
2020-04-24 04:17:27 +01:00
|
|
|
}
|
2020-02-28 15:20:47 +00:00
|
|
|
</bdi>
|
|
|
|
{
|
|
|
|
account.get('is_verified') &&
|
2020-08-18 21:49:11 +01:00
|
|
|
<Icon id='verified' size={`${iconSize}px`} className={[_s.ml5, _s.d].join(' ')} />
|
2020-02-28 15:20:47 +00:00
|
|
|
}
|
2020-04-24 04:17:27 +01:00
|
|
|
</span>
|
2020-03-04 03:45:16 +00:00
|
|
|
{
|
|
|
|
!noUsername &&
|
|
|
|
<span className={usernameClasses}>
|
|
|
|
@{account.get('acct')}
|
2020-04-24 04:17:27 +01:00
|
|
|
{
|
|
|
|
!noRelationship && !!relationshipLabel &&
|
2020-08-18 21:49:11 +01:00
|
|
|
<span className={[_s.d, _s.ml5, _s.jcCenter].join(' ')}>
|
2020-04-24 04:17:27 +01:00
|
|
|
<Text
|
|
|
|
size='extraSmall'
|
|
|
|
isBadge
|
|
|
|
color='tertiary'
|
2020-04-29 23:32:49 +01:00
|
|
|
className={[_s.bgSecondary, _s.py2].join(' ')}
|
2020-04-24 04:17:27 +01:00
|
|
|
>
|
|
|
|
{relationshipLabel}
|
|
|
|
</Text>
|
|
|
|
</span>
|
|
|
|
}
|
2020-03-04 03:45:16 +00:00
|
|
|
</span>
|
|
|
|
}
|
2020-04-24 04:17:27 +01:00
|
|
|
</div>
|
2020-02-21 00:57:29 +00:00
|
|
|
)
|
2020-02-08 06:12:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-08-18 01:57:35 +01:00
|
|
|
|
|
|
|
const mapDispatchToProps = (dispatch) => ({
|
|
|
|
openUserInfoPopover(props) {
|
|
|
|
dispatch(openPopover(POPOVER_USER_INFO, props))
|
|
|
|
},
|
|
|
|
closeUserInfoPopover() {
|
|
|
|
dispatch(closePopover(POPOVER_USER_INFO))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
DisplayName.propTypes = {
|
|
|
|
account: ImmutablePropTypes.map,
|
|
|
|
openUserInfoPopover: PropTypes.func.isRequired,
|
|
|
|
closeUserInfoPopover: PropTypes.func.isRequired,
|
|
|
|
isLarge: PropTypes.bool,
|
|
|
|
isMultiline: PropTypes.bool,
|
|
|
|
isSmall: PropTypes.bool,
|
|
|
|
noHover: PropTypes.bool,
|
|
|
|
noRelationship: PropTypes.bool,
|
|
|
|
noUsername: PropTypes.bool,
|
|
|
|
isComment: PropTypes.bool,
|
|
|
|
isCentered: PropTypes.bool,
|
2020-12-06 04:47:48 +00:00
|
|
|
isInline: PropTypes.bool,
|
2020-08-18 01:57:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export default (connect(null, mapDispatchToProps)(DisplayName))
|