gab-social/app/javascript/gabsocial/components/display_name.js

214 lines
5.8 KiB
JavaScript
Raw Normal View History

import React from 'react'
import PropTypes from 'prop-types'
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'
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
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',
accountId: this.props.account.get('id'),
2020-04-23 07:13:29 +01:00
})
document.addEventListener('mousemove', this.handleMouseMove, true)
}, 1250)
2020-04-23 07:13:29 +01:00
}
2020-03-11 23:56:18 +00:00
handleMouseLeave = debounce((e) => {
this.attemptToHidePopover(e)
}, 250)
handleMouseMove = debounce((e) => {
this.attemptToHidePopover(e)
}, 100)
attemptToHidePopover = (e) => {
const lastTarget = e.toElement || e.relatedTarget
const isElement = (lastTarget instanceof Element || lastTarget instanceof HTMLDocument)
const userInfoPopoverEl = document.getElementById('user-info-popover')
if (this.mouseOverTimeout &&
(
!isElement && !userInfoPopoverEl ||
(userInfoPopoverEl && isElement && lastTarget && !userInfoPopoverEl.contains(lastTarget)) ||
(!userInfoPopoverEl && isElement && lastTarget && this.node && !this.node.contains(lastTarget))
)) {
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-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({
d: 1,
maxW100PC: 1,
aiCenter: !isMultiline,
2020-04-24 04:17:27 +01:00
flexRow: !isMultiline,
cursorPointer: !noHover,
aiCenter: isCentered,
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,
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,
flexNormal: 1,
flexShrink1: 1,
overflowWrapBreakWord: 1,
textOverflowEllipsis: 1,
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) {
relationshipLabel = 'Follows you'//intl.formatMessage(messages.accountFollowsYou)
}
}
// {
// /* : todo : audio-mute, bot
2020-04-24 04:17:27 +01:00
// account.getIn(['relationship', 'muting'])
// */
// }
// bot: { id: 'account.badges.bot', defaultMessage: 'Bot' },
2020-04-23 07:13:29 +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}
>
<span className={[_s.d, _s.flexRow, _s.aiCenter, _s.maxW100PC].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') &&
<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') &&
<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 &&
<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
}
}
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,
}
export default (connect(null, mapDispatchToProps)(DisplayName))