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

162 lines
4.2 KiB
JavaScript
Raw Normal View History

2020-03-04 22:26:01 +00:00
import ImmutablePropTypes from 'react-immutable-proptypes'
import ImmutablePureComponent from 'react-immutable-pure-component'
import { NavLink } from 'react-router-dom'
import { decode } from 'blurhash'
import { autoPlayGif, displayMedia } from '../initial_state'
2020-03-07 04:53:28 +00:00
import classNames from 'classnames/bind'
2020-03-04 22:26:01 +00:00
import Icon from './icon'
import Image from './image'
import Text from './text'
2020-03-07 04:53:28 +00:00
const cx = classNames.bind(_s)
2020-03-04 22:26:01 +00:00
export default class MediaItem extends ImmutablePureComponent {
static propTypes = {
2020-04-28 06:33:58 +01:00
account: ImmutablePropTypes.map.isRequired,
2020-03-04 22:26:01 +00:00
attachment: ImmutablePropTypes.map.isRequired,
2020-04-28 06:33:58 +01:00
isSmall: PropTypes.bool,
2020-03-04 22:26:01 +00:00
}
state = {
loaded: false,
2020-04-28 06:33:58 +01:00
visible: displayMedia !== 'hide_all' && !this.props.attachment.getIn(['status', 'sensitive']) || displayMedia === 'show_all',
2020-03-04 22:26:01 +00:00
}
componentDidMount() {
if (this.props.attachment.get('blurhash')) {
this._decode()
}
}
componentDidUpdate(prevProps) {
if (prevProps.attachment.get('blurhash') !== this.props.attachment.get('blurhash') && this.props.attachment.get('blurhash')) {
this._decode()
}
}
_decode() {
const hash = this.props.attachment.get('blurhash')
const pixels = decode(hash, 160, 160)
if (pixels && this.canvas) {
const ctx = this.canvas.getContext('2d')
const imageData = new ImageData(pixels, 160, 160)
ctx.putImageData(imageData, 0, 0)
}
}
setCanvasRef = c => {
this.canvas = c
}
handleImageLoad = () => {
this.setState({ loaded: true })
}
hoverToPlay() {
return !autoPlayGif && ['gifv', 'video'].indexOf(this.props.attachment.get('type')) !== -1
}
render() {
2020-04-28 06:33:58 +01:00
const {
account,
attachment,
isSmall,
} = this.props
2020-03-04 22:26:01 +00:00
const { visible, loaded } = this.state
const status = attachment.get('status')
const title = status.get('spoiler_text') || attachment.get('description')
const attachmentType = attachment.get('type')
let badge = null
if (attachmentType === 'video') {
const duration = attachment.getIn(['meta', 'duration'])
badge = (duration / 60).toFixed(2)
} else if (attachmentType === 'gifv') {
badge = 'GIF'
}
2020-03-07 04:53:28 +00:00
const containerClasses = cx({
default: 1,
2020-04-23 07:13:29 +01:00
posAbs: 1,
2020-03-07 04:53:28 +00:00
top0: 1,
height100PC: 1,
width100PC: 1,
2020-04-28 06:33:58 +01:00
py2: !isSmall,
px2: !isSmall,
2020-03-07 04:53:28 +00:00
})
const linkClasses = cx({
default: 1,
width100PC: 1,
height100PC: 1,
overflowHidden: 1,
border1PX: 1,
2020-04-28 06:33:58 +01:00
borderColorPrimary: 1,
2020-03-07 04:53:28 +00:00
})
2020-04-28 06:33:58 +01:00
const statusUrl = `/${account.getIn(['acct'])}/posts/${status.get('id')}`;
2020-03-04 22:26:01 +00:00
return (
2020-04-23 07:13:29 +01:00
<div className={[_s.default, _s.width25PC, _s.pt25PC].join(' ')}>
2020-03-07 04:53:28 +00:00
<div className={containerClasses}>
2020-03-04 22:26:01 +00:00
<NavLink
2020-04-28 06:33:58 +01:00
to={statusUrl}
2020-03-04 22:26:01 +00:00
title={title}
2020-03-07 04:53:28 +00:00
className={linkClasses}
2020-03-04 22:26:01 +00:00
>
{
(!loaded || !visible) &&
<canvas
height='100%'
width='100%'
ref={this.setCanvasRef}
className={[_s.default, _s.width100PC, _s.height100PC, _s.z2].join(' ')}
/>
}
{
visible &&
<Image
height='100%'
src={attachment.get('preview_url')}
alt={attachment.get('description')}
title={attachment.get('description')}
onLoad={this.handleImageLoad}
className={_s.z1}
/>
}
2020-04-23 07:13:29 +01:00
<div className={[_s.default, _s.alignItemsCenter, _s.justifyContentCenter, _s.height100PC, _s.width100PC, _s.z3, _s.posAbs].join(' ')}>
2020-03-04 22:26:01 +00:00
{
!visible &&
<Icon
id='hidden'
2020-04-23 07:13:29 +01:00
size='22px'
2020-04-29 23:32:49 +01:00
className={[_s.fillWhite].join('')}
2020-03-04 22:26:01 +00:00
/>
}
{
!!badge &&
2020-04-29 23:32:49 +01:00
<div className={[_s.default, _s.posAbs, _s.radiusSmall, _s.bgBlackOpaque, _s.px5, _s.py5, _s.mr5, _s.mt5, _s.mb5, _s.bottom0, _s.right0].join(' ')}>
2020-03-04 22:26:01 +00:00
<Text size='extraSmall' color='white'>
{badge}
</Text>
</div>
}
</div>
</NavLink>
</div>
</div>
)
}
}