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

143 lines
3.9 KiB
JavaScript
Raw Normal View History

2020-02-19 23:57:07 +00:00
import ImmutablePropTypes from 'react-immutable-proptypes'
import ImmutablePureComponent from 'react-immutable-pure-component'
import debounce from 'lodash.debounce'
2020-02-19 23:57:07 +00:00
import { autoPlayGif } from '../initial_state'
2020-04-24 04:17:27 +01:00
import { openPopover, closePopover } from '../actions/popover'
2020-02-19 23:57:07 +00:00
import Image from './image'
2020-04-24 04:17:27 +01:00
const mapDispatchToProps = (dispatch) => ({
openUserInfoPopover(props) {
dispatch(openPopover('USER_INFO', props))
},
closeUserInfoPopover() {
dispatch(closePopover('USER_INFO'))
}
})
2020-04-23 07:13:29 +01:00
/**
* Renders an avatar component
* @param {map} [props.account] - the account for image
* @param {number} [props.size=40] - the size of the avatar
*/
2020-04-24 04:17:27 +01:00
export default
@connect(null, mapDispatchToProps)
class Avatar extends ImmutablePureComponent {
2020-02-19 23:57:07 +00:00
static propTypes = {
account: ImmutablePropTypes.map,
2020-04-24 04:17:27 +01:00
noHover: PropTypes.bool,
openUserInfoPopover: PropTypes.func.isRequired,
2020-04-23 07:13:29 +01:00
size: PropTypes.number,
2020-02-19 23:57:07 +00:00
}
static defaultProps = {
size: 40,
}
state = {
hovering: false,
2020-03-14 17:31:29 +00:00
sameImg: !this.props.account ? false : this.props.account.get('avatar') === this.props.account.get('avatar_static'),
}
2020-04-24 04:17:27 +01:00
updateOnProps = [
'account',
'noHover',
'size',
]
mouseOverTimeout = null
2020-03-14 17:31:29 +00:00
componentDidUpdate (prevProps) {
if (prevProps.account !== this.props.account) {
this.setState({
sameImg: !this.props.account ? false : this.props.account.get('avatar') === this.props.account.get('avatar_static'),
})
}
2020-02-19 23:57:07 +00:00
}
componentWillUnmount () {
document.removeEventListener('mousemove', this.handleMouseMove, true)
clearTimeout(this.mouseOverTimeout)
}
2020-02-19 23:57:07 +00:00
handleMouseEnter = () => {
this.setState({ hovering: true })
2020-04-24 04:17:27 +01:00
if (this.mouseOverTimeout || this.props.noHover) return null
2020-04-24 04:17:27 +01:00
this.mouseOverTimeout = setTimeout(() => {
this.props.openUserInfoPopover({
targetRef: this.node,
position: 'top',
accountId: this.props.account.get('id'),
2020-04-24 04:17:27 +01:00
})
document.addEventListener('mousemove', this.handleMouseMove, true)
}, 1250)
2020-02-19 23:57:07 +00:00
}
handleMouseLeave = debounce((e) => {
2020-02-19 23:57:07 +00:00
this.setState({ hovering: false })
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')
2020-04-24 04:17:27 +01:00
if (this.mouseOverTimeout &&
!this.props.noHover &&
(
!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-24 04:17:27 +01:00
clearTimeout(this.mouseOverTimeout)
this.mouseOverTimeout = null
this.props.closeUserInfoPopover()
}
}
setRef = (n) => {
this.node = n
2020-02-19 23:57:07 +00:00
}
2020-02-25 16:04:44 +00:00
render() {
2020-04-23 07:13:29 +01:00
const { account, size } = this.props
2020-02-19 23:57:07 +00:00
const { hovering, sameImg } = this.state
2020-04-24 04:17:27 +01:00
const isPro = !!account ? account.get('is_pro') : false
const alt = !account ? '' : `${account.get('display_name')} ${isPro ? '(PRO)' : ''}`.trim()
const classes = [_s.default, _s.circle, _s.overflowHidden]
if (isPro) {
classes.push(_s.boxShadowAvatarPro)
}
2020-02-19 23:57:07 +00:00
const options = {
onMouseEnter: this.handleMouseEnter,
onMouseLeave: this.handleMouseLeave,
src: !account ? undefined : account.get(((hovering || autoPlayGif) && !sameImg) ? 'avatar' : 'avatar_static'),
2020-03-14 17:31:29 +00:00
alt: !account ? undefined : account.get('display_name'),
2020-02-19 23:57:07 +00:00
style: {
width: `${size}px`,
height: `${size}px`,
},
}
return (
2020-04-24 04:17:27 +01:00
<Image
alt={alt}
2020-05-01 06:50:27 +01:00
imageRef={this.setRef}
2020-04-24 04:17:27 +01:00
className={classes.join(' ')}
{...options}
/>
2020-02-19 23:57:07 +00:00
)
}
}