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

547 lines
16 KiB
JavaScript
Raw Normal View History

2020-04-22 06:00:11 +01:00
import ImmutablePropTypes from 'react-immutable-proptypes'
import { injectIntl } from 'react-intl'
import ImmutablePureComponent from 'react-immutable-pure-component'
import { HotKeys } from 'react-hotkeys'
2020-02-28 15:20:47 +00:00
import classNames from 'classnames/bind'
2020-04-22 06:00:11 +01:00
import { me, displayMedia } from '../initial_state'
2020-04-11 23:29:19 +01:00
import StatusCard from './status_card'
2020-04-22 06:00:11 +01:00
import { MediaGallery, Video } from '../features/ui/util/async_components'
2020-04-11 23:29:19 +01:00
import ComposeFormContainer from '../features/compose/containers/compose_form_container'
import StatusContent from './status_content'
import StatusPrepend from './status_prepend'
2020-04-22 06:00:11 +01:00
import StatusActionBar from './status_action_bar'
import Poll from './poll'
2020-04-11 23:29:19 +01:00
import StatusHeader from './status_header'
2020-04-22 06:00:11 +01:00
import CommentList from './comment_list'
// We use the component (and not the container) since we do not want
// to use the progress bar to show download progress
2020-04-22 06:00:11 +01:00
import Bundle from '../features/ui/util/bundle'
2020-02-28 15:20:47 +00:00
const cx = classNames.bind(_s)
export const textForScreenReader = (intl, status, rebloggedByText = false) => {
2020-04-22 06:00:11 +01:00
if (!intl || !status) return ''
const displayName = status.getIn(['account', 'display_name'])
const values = [
2020-04-22 06:00:11 +01:00
displayName.length === 0 ? status.getIn(['account', 'acct']).split('@')[0] : displayName,
status.get('spoiler_text') && status.get('hidden')
? status.get('spoiler_text')
: status.get('search_index').slice(status.get('spoiler_text').length),
intl.formatDate(status.get('created_at'), { hour: '2-digit', minute: '2-digit', month: 'short', day: 'numeric' }),
`@${status.getIn(['account', 'acct'])}`,
]
if (rebloggedByText) {
2020-04-22 06:00:11 +01:00
values.push(rebloggedByText)
}
2020-04-22 06:00:11 +01:00
return values.join(', ')
}
2020-04-22 06:00:11 +01:00
export const defaultMediaVisibility = (status) => {
2020-04-02 04:17:21 +01:00
if (!status) return undefined
2020-04-22 06:00:11 +01:00
// console.log("status:", status)
if (status.get('reblog', null) !== null && typeof status.get('reblog') === 'object') {
2020-04-02 04:17:21 +01:00
status = status.get('reblog')
}
2020-04-02 04:17:21 +01:00
return (displayMedia !== 'hide_all' && !status.get('sensitive')) || displayMedia === 'show_all'
}
2020-02-25 16:04:44 +00:00
export default
@injectIntl
class Status extends ImmutablePureComponent {
static contextTypes = {
router: PropTypes.object,
2020-04-22 06:00:11 +01:00
}
static propTypes = {
2020-04-22 06:00:11 +01:00
intl: PropTypes.object.isRequired,
status: ImmutablePropTypes.map,
2020-04-22 06:00:11 +01:00
descendantsIds: ImmutablePropTypes.list,
isChild: PropTypes.bool,
isPromoted: PropTypes.bool,
isFeatured: PropTypes.bool,
isMuted: PropTypes.bool,
isHidden: PropTypes.bool,
isIntersecting: PropTypes.bool,
onClick: PropTypes.func,
onReply: PropTypes.func,
2020-04-24 04:17:27 +01:00
onRepost: PropTypes.func,
2020-03-04 22:26:01 +00:00
onFavorite: PropTypes.func,
onMention: PropTypes.func,
onOpenMedia: PropTypes.func,
onOpenVideo: PropTypes.func,
onHeightChange: PropTypes.func,
onToggleHidden: PropTypes.func,
2020-04-22 06:00:11 +01:00
onShare: PropTypes.func,
onMoveUp: PropTypes.func,
onMoveDown: PropTypes.func,
2020-04-22 06:00:11 +01:00
onFetchComments: PropTypes.func,
getScrollPosition: PropTypes.func,
updateScrollBottom: PropTypes.func,
cacheMediaWidth: PropTypes.func,
cachedMediaWidth: PropTypes.number,
2020-04-22 06:00:11 +01:00
contextType: PropTypes.string,
2020-04-02 04:17:21 +01:00
}
// Avoid checking props that are functions (and whose equality will always
// evaluate to false. See react-immutable-pure-component for usage.
2020-04-22 06:00:11 +01:00
updateOnProps = [
'status',
'descendantsIds',
'isChild',
'isPromoted',
'isFeatured',
'isMuted',
'isHidden',
'isIntersecting',
]
state = {
2020-04-22 06:00:11 +01:00
loadedComments: false,
showMedia: defaultMediaVisibility(this.props.status),
statusId: undefined,
2020-04-22 06:00:11 +01:00
height: undefined,
2020-04-02 04:17:21 +01:00
}
// Track height changes we know about to compensate scrolling
componentDidMount() {
2020-04-22 06:00:11 +01:00
const { isMuted, isHidden, status } = this.props
this.didShowCard = !isMuted && !isHidden && status && status.get('card')
}
getSnapshotBeforeUpdate() {
if (this.props.getScrollPosition) {
2020-04-02 04:17:21 +01:00
return this.props.getScrollPosition()
}
2020-04-02 04:17:21 +01:00
return null
}
static getDerivedStateFromProps(nextProps, prevState) {
2020-04-22 06:00:11 +01:00
if (!nextProps.isHidden && nextProps.isIntersecting && !prevState.loadedComments) {
return {
loadedComments: true
}
}
if (nextProps.status && nextProps.status.get('id') !== prevState.statusId) {
return {
showMedia: defaultMediaVisibility(nextProps.status),
statusId: nextProps.status.get('id'),
2020-04-22 06:00:11 +01:00
}
}
2020-04-22 06:00:11 +01:00
return null
}
// Compensate height changes
componentDidUpdate(prevProps, prevState, snapshot) {
2020-04-22 06:00:11 +01:00
if (!prevState.loadedComments && this.state.loadedComments && this.props.status) {
2020-04-23 07:13:29 +01:00
const commentCount = this.props.status.get('replies_count')
2020-04-22 06:00:11 +01:00
if (commentCount > 0) {
this.props.onFetchComments(this.props.status.get('id'))
this._measureHeight(prevState.height !== this.state.height)
}
}
const doShowCard = !this.props.isMuted && !this.props.isHidden && this.props.status && this.props.status.get('card')
if (doShowCard && !this.didShowCard) {
2020-04-22 06:00:11 +01:00
this.didShowCard = true
if (snapshot !== null && this.props.updateScrollBottom) {
if (this.node && this.node.offsetTop < snapshot.top) {
2020-04-22 06:00:11 +01:00
this.props.updateScrollBottom(snapshot.height - snapshot.top)
}
}
}
}
2020-04-22 06:00:11 +01:00
handleMoveUp = id => {
2020-04-23 07:13:29 +01:00
const { status, ancestorsIds, descendantsIds } = this.props
2020-04-22 06:00:11 +01:00
if (id === status.get('id')) {
2020-04-23 07:13:29 +01:00
this._selectChild(ancestorsIds.size - 1, true)
2020-04-22 06:00:11 +01:00
} else {
2020-04-23 07:13:29 +01:00
let index = ancestorsIds.indexOf(id)
2020-04-22 06:00:11 +01:00
if (index === -1) {
2020-04-23 07:13:29 +01:00
index = descendantsIds.indexOf(id)
this._selectChild(ancestorsIds.size + index, true)
2020-04-22 06:00:11 +01:00
} else {
2020-04-23 07:13:29 +01:00
this._selectChild(index - 1, true)
2020-04-22 06:00:11 +01:00
}
}
}
handleMoveDown = id => {
2020-04-23 07:13:29 +01:00
const { status, ancestorsIds, descendantsIds } = this.props
2020-04-22 06:00:11 +01:00
if (id === status.get('id')) {
2020-04-23 07:13:29 +01:00
this._selectChild(ancestorsIds.size + 1, false)
2020-04-22 06:00:11 +01:00
} else {
2020-04-23 07:13:29 +01:00
let index = ancestorsIds.indexOf(id)
2020-04-22 06:00:11 +01:00
if (index === -1) {
2020-04-23 07:13:29 +01:00
index = descendantsIds.indexOf(id)
this._selectChild(ancestorsIds.size + index + 2, false)
2020-04-22 06:00:11 +01:00
} else {
2020-04-23 07:13:29 +01:00
this._selectChild(index + 1, false)
2020-04-22 06:00:11 +01:00
}
}
}
_selectChild(index, align_top) {
2020-04-23 07:13:29 +01:00
const container = this.node
const element = container.querySelectorAll('.focusable')[index]
2020-04-22 06:00:11 +01:00
if (element) {
if (align_top && container.scrollTop > element.offsetTop) {
2020-04-23 07:13:29 +01:00
element.scrollIntoView(true)
2020-04-22 06:00:11 +01:00
} else if (!align_top && container.scrollTop + container.clientHeight < element.offsetTop + element.offsetHeight) {
2020-04-23 07:13:29 +01:00
element.scrollIntoView(false)
2020-04-22 06:00:11 +01:00
}
2020-04-23 07:13:29 +01:00
element.focus()
2020-04-22 06:00:11 +01:00
}
}
componentWillUnmount() {
if (this.node && this.props.getScrollPosition) {
2020-04-22 06:00:11 +01:00
const position = this.props.getScrollPosition()
if (position !== null && this.node.offsetTop < position.top) {
requestAnimationFrame(() => {
2020-04-22 06:00:11 +01:00
this.props.updateScrollBottom(position.height - position.top)
})
}
}
}
handleToggleMediaVisibility = () => {
2020-03-07 04:53:28 +00:00
this.setState({ showMedia: !this.state.showMedia })
}
handleClick = () => {
if (this.props.onClick) {
2020-04-22 06:00:11 +01:00
this.props.onClick()
return
}
2020-04-22 06:00:11 +01:00
if (!this.context.router) return
this.context.router.history.push(
`/${this._properStatus().getIn(['account', 'acct'])}/posts/${this._properStatus().get('id')}`
2020-03-07 04:53:28 +00:00
)
}
handleExpandClick = e => {
if (e.button === 0) {
2020-04-22 06:00:11 +01:00
if (!this.context.router) return
this.context.router.history.push(
`/${this._properStatus().getIn(['account', 'acct'])}/posts/${this._properStatus().get('id')}`
2020-04-22 06:00:11 +01:00
)
}
2020-04-22 06:00:11 +01:00
}
handleExpandedToggle = () => {
2020-04-22 06:00:11 +01:00
this.props.onToggleHidden(this._properStatus())
}
2020-04-22 06:00:11 +01:00
renderLoadingMedia() {
return <div className={_s.backgroundColorPanel} style={{ height: '110px' }} />
}
handleOpenVideo = (media, startTime) => {
2020-04-22 06:00:11 +01:00
this.props.onOpenVideo(media, startTime)
}
handleHotkeyReply = e => {
2020-04-22 06:00:11 +01:00
e.preventDefault()
this.props.onReply(this._properStatus(), this.context.router.history)
}
2020-03-04 22:26:01 +00:00
handleHotkeyFavorite = () => {
2020-04-22 06:00:11 +01:00
this.props.onFavorite(this._properStatus())
}
2020-04-22 06:00:11 +01:00
handleHotkeyRepost = e => {
this.props.onQuote(this._properStatus(), e)
}
handleHotkeyMention = e => {
2020-04-22 06:00:11 +01:00
e.preventDefault()
this.props.onMention(this._properStatus().get('account'), this.context.router.history)
}
handleHotkeyOpen = () => {
this.context.router.history.push(
`/${this._properStatus().getIn(['account', 'acct'])}/posts/${this._properStatus().get('id')}`
2020-04-22 06:00:11 +01:00
)
}
handleHotkeyOpenProfile = () => {
2020-04-22 06:00:11 +01:00
this.context.router.history.push(`/${this._properStatus().getIn(['account', 'acct'])}`)
}
handleHotkeyMoveUp = e => {
2020-04-02 04:17:21 +01:00
this.props.onMoveUp(this.props.status.get('id'), e.target.getAttribute('data-featured'))
}
handleHotkeyMoveDown = e => {
2020-04-02 04:17:21 +01:00
this.props.onMoveDown(this.props.status.get('id'), e.target.getAttribute('data-featured'))
}
handleHotkeyToggleHidden = () => {
2020-04-02 04:17:21 +01:00
this.props.onToggleHidden(this._properStatus())
}
handleHotkeyToggleSensitive = () => {
2020-04-02 04:17:21 +01:00
this.handleToggleMediaVisibility()
}
_properStatus() {
2020-04-02 04:17:21 +01:00
const { status } = this.props
if (status.get('reblog', null) !== null && typeof status.get('reblog') === 'object') {
2020-04-02 04:17:21 +01:00
return status.get('reblog')
}
2020-04-02 04:17:21 +01:00
return status
}
2020-04-22 06:00:11 +01:00
_measureHeight (heightJustChanged) {
try {
scheduleIdleTask(() => this.node && this.setState({ height: Math.ceil(this.node.scrollHeight) + 1 }))
if (heightJustChanged) {
this.props.onHeightChange()
}
} catch (error) {
}
2020-04-02 04:17:21 +01:00
}
2020-04-22 06:00:11 +01:00
handleRef = c => {
this.node = c
this._measureHeight()
2020-02-22 23:26:23 +00:00
}
2020-02-22 23:26:23 +00:00
render() {
2020-03-07 04:53:28 +00:00
const {
intl,
2020-04-22 06:00:11 +01:00
status,
isFeatured,
isPromoted,
2020-04-08 02:06:59 +01:00
isChild,
2020-04-22 06:00:11 +01:00
isHidden,
descendantsIds,
commentsLimited,
2020-03-07 04:53:28 +00:00
} = this.props
2020-04-22 06:00:11 +01:00
if (!status) return null
2020-04-02 04:17:21 +01:00
2020-04-22 06:00:11 +01:00
let media, reblogContent, rebloggedByText = null
2020-04-02 04:17:21 +01:00
2020-04-22 06:00:11 +01:00
if (status.get('reblog', null) !== null && typeof status.get('reblog') === 'object') {
rebloggedByText = intl.formatMessage(
{ id: 'status.reposted_by', defaultMessage: '{name} reposted' },
{ name: status.getIn(['account', 'acct']) }
)
reblogContent = status.get('contentHtml')
}
2020-02-14 00:40:04 +00:00
2020-04-23 07:13:29 +01:00
const handlers = (this.props.isMuted || isChild) ? {} : {
2020-04-22 06:00:11 +01:00
reply: this.handleHotkeyReply,
favorite: this.handleHotkeyFavorite,
repost: this.handleHotkeyRepost,
mention: this.handleHotkeyMention,
open: this.handleHotkeyOpen,
openProfile: this.handleHotkeyOpenProfile,
moveUp: this.handleHotkeyMoveUp,
moveDown: this.handleHotkeyMoveDown,
toggleHidden: this.handleHotkeyToggleHidden,
toggleSensitive: this.handleHotkeyToggleSensitive,
}
2020-04-22 06:00:11 +01:00
if (isHidden) {
return (
2020-04-22 06:00:11 +01:00
<HotKeys handlers={handlers}>
<div ref={this.handleRef} className={classNames({ focusable: !this.props.muted })} tabIndex='0'>
{status.getIn(['account', 'display_name']) || status.getIn(['account', 'username'])}
{status.get('content')}
</div>
</HotKeys>
2020-04-23 07:13:29 +01:00
)
}
if (status.get('filtered') || status.getIn(['reblog', 'filtered'])) {
2020-04-22 06:00:11 +01:00
return null
}
if (status.get('poll')) {
2020-03-04 03:45:16 +00:00
media = <Poll pollId={status.get('poll')} />
} else if (status.get('media_attachments').size > 0) {
if (status.getIn(['media_attachments', 0, 'type']) === 'video') {
2020-04-02 04:17:21 +01:00
const video = status.getIn(['media_attachments', 0])
2020-03-06 15:38:22 +00:00
media = (
2020-04-22 06:00:11 +01:00
<Bundle fetchComponent={Video} loading={this.renderLoadingMedia}>
{Component => (
<Component
2020-03-04 03:45:16 +00:00
inline
preview={video.get('preview_url')}
blurhash={video.get('blurhash')}
src={video.get('url')}
alt={video.get('description')}
aspectRatio={video.getIn(['meta', 'small', 'aspect'])}
width={this.props.cachedMediaWidth}
height={110}
sensitive={status.get('sensitive')}
onOpenVideo={this.handleOpenVideo}
cacheWidth={this.props.cacheMediaWidth}
visible={this.state.showMedia}
onToggleVisibility={this.handleToggleMediaVisibility}
/>
)}
</Bundle>
2020-03-04 03:45:16 +00:00
)
} else {
media = (
2020-04-22 06:00:11 +01:00
<Bundle fetchComponent={MediaGallery} loading={this.renderLoadingMedia}>
{Component => (
<Component
2020-04-02 04:17:21 +01:00
reduced={isChild}
media={status.get('media_attachments')}
sensitive={status.get('sensitive')}
onOpenMedia={this.props.onOpenMedia}
cacheWidth={this.props.cacheMediaWidth}
defaultWidth={this.props.cachedMediaWidth}
visible={this.state.showMedia}
onToggleVisibility={this.handleToggleMediaVisibility}
/>
)}
</Bundle>
2020-03-04 03:45:16 +00:00
)
}
} else if (status.get('spoiler_text').length === 0 && status.get('card')) {
media = (
2020-03-27 22:57:03 +00:00
<StatusCard
onOpenMedia={this.props.onOpenMedia}
card={status.get('card')}
cacheWidth={this.props.cacheMediaWidth}
defaultWidth={this.props.cachedMediaWidth}
/>
2020-03-04 03:45:16 +00:00
)
}
2020-02-28 15:20:47 +00:00
const containerClasses = cx({
default: 1,
2020-04-17 06:35:46 +01:00
radiusSmall: !isChild,
2020-04-22 06:00:11 +01:00
pb15: isFeatured,
radiusSmall: !isChild,
backgroundColorPrimary: !isChild,
2020-04-24 04:17:27 +01:00
boxShadowBlock: !isChild,
outlineNone: 1,
2020-04-22 06:00:11 +01:00
mb15: !isChild,
2020-04-24 04:17:27 +01:00
// border1PX: !isChild,
// borderBottom1PX: isFeatured && !isChild,
// borderColorSecondary: !isChild,
2020-02-28 15:20:47 +00:00
})
2020-03-08 23:02:28 +00:00
const innerContainerClasses = cx({
default: 1,
overflowHidden: 1,
2020-04-17 06:35:46 +01:00
radiusSmall: isChild,
borderColorSecondary: isChild,
border1PX: isChild,
2020-04-02 04:17:21 +01:00
pb10: isChild && status.get('media_attachments').size === 0,
2020-04-08 02:06:59 +01:00
pb5: isChild && status.get('media_attachments').size > 1,
2020-04-02 04:17:21 +01:00
cursorPointer: isChild,
2020-04-25 18:00:51 +01:00
backgroundColorSubtle_onHover: isChild,
2020-03-08 23:02:28 +00:00
})
return (
<HotKeys handlers={handlers}>
<div
2020-04-22 06:00:11 +01:00
className={containerClasses}
tabIndex={this.props.isMuted ? null : 0}
data-featured={isFeatured ? 'true' : null}
aria-label={textForScreenReader(intl, status, rebloggedByText)}
ref={this.handleRef}
2020-04-02 04:17:21 +01:00
// onClick={this.handleClick}
>
2020-03-08 23:02:28 +00:00
<div className={innerContainerClasses}>
2020-02-22 23:26:23 +00:00
2020-03-07 04:53:28 +00:00
<div data-id={status.get('id')}>
2020-02-22 23:26:23 +00:00
2020-04-22 06:00:11 +01:00
<StatusPrepend status={status} isPromoted={isPromoted} isFeatured={isFeatured} />
2020-04-02 04:17:21 +01:00
<StatusHeader status={status} reduced={isChild} />
2020-02-22 23:26:23 +00:00
<div className={_s.default}>
<StatusContent
status={status}
reblogContent={reblogContent}
onClick={this.handleClick}
expanded={!status.get('hidden')}
onExpandedToggle={this.handleExpandedToggle}
collapsable
/>
</div>
{media}
2020-04-02 04:17:21 +01:00
{
!!status.get('quote') && !isChild &&
<div className={[_s.default, _s.mt10, _s.px10].join(' ')}>
2020-04-23 07:13:29 +01:00
<Status status={status.get('quoted_status')} isChild intl={intl} />
2020-04-02 04:17:21 +01:00
</div>
}
2020-04-08 02:06:59 +01:00
2020-04-02 04:17:21 +01:00
{
!isChild &&
2020-04-22 06:00:11 +01:00
<StatusActionBar
status={status}
onFavorite={this.props.onFavorite}
onReply={this.props.onReply}
2020-04-24 04:17:27 +01:00
onRepost={this.props.onRepost}
2020-04-22 06:00:11 +01:00
onShare={this.props.onShare}
/>
2020-04-02 04:17:21 +01:00
}
{
2020-04-11 23:29:19 +01:00
!isChild && !!me &&
2020-04-02 04:17:21 +01:00
<div className={[_s.default, _s.borderTop1PX, _s.borderColorSecondary, _s.pt10, _s.px15, _s.mb10].join(' ')}>
<ComposeFormContainer replyToId={status.get('id')} shouldCondense />
</div>
}
2020-04-22 06:00:11 +01:00
{
descendantsIds && !isChild && descendantsIds.size > 0 &&
<div className={[_s.default, _s.mr10, _s.ml10, _s.mb10, _s.borderColorSecondary, _s.borderBottom1PX].join(' ')}/>
}
{
descendantsIds && !isChild && descendantsIds.size > 0 &&
<CommentList
commentsLimited={commentsLimited}
descendants={descendantsIds}
/>
}
2020-02-22 23:26:23 +00:00
</div>
2020-03-08 23:02:28 +00:00
</div>
</div>
</HotKeys>
2020-04-22 06:00:11 +01:00
)
}
}