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

588 lines
18 KiB
JavaScript
Raw Normal View History

import React from 'react'
import PropTypes from 'prop-types'
2020-04-22 06:00:11 +01:00
import ImmutablePropTypes from 'react-immutable-proptypes'
import { defineMessages, injectIntl } from 'react-intl'
2020-04-22 06:00:11 +01:00
import ImmutablePureComponent from 'react-immutable-pure-component'
import { HotKeys } from 'react-hotkeys'
import {
CX,
COMMENT_SORTING_TYPE_NEWEST,
COMMENT_SORTING_TYPE_TOP,
} from '../constants'
import { me, displayMedia } from '../initial_state'
2020-05-06 05:33:54 +01:00
import scheduleIdleTask from '../utils/schedule_idle_task'
2020-04-11 23:29:19 +01:00
import ComposeFormContainer from '../features/compose/containers/compose_form_container'
2020-05-06 05:33:54 +01:00
import ResponsiveClassesComponent from '../features/ui/util/responsive_classes_component'
import CommentPlaceholder from './placeholder/comment_placeholder'
2020-04-11 23:29:19 +01:00
import StatusContent from './status_content'
import StatusPrepend from './status_prepend'
2020-04-22 06:00:11 +01:00
import StatusActionBar from './status_action_bar'
2020-05-02 07:25:55 +01:00
import StatusMedia from './status_media'
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'
import Button from './button'
import Text from './text'
import SortBlock from './sort_block'
import ColumnIndicator from './column_indicator'
// 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'
const messages = defineMessages({
sortBy: { id: 'comment_sort.title', defaultMessage: 'Sort by' },
oldest: { id: 'comment_sort.oldest', defaultMessage: 'Oldest' },
newest: { id: 'comment_sort.newest', defaultMessage: 'Recent' },
top: { id: 'comment_sort.top', defaultMessage: 'Most Liked' },
})
2020-02-28 15:20:47 +00:00
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'])
2020-05-04 19:44:37 +01:00
// : todo :
const values = [
2020-05-04 19:44:37 +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'])}`,
2020-04-22 06:00:11 +01:00
]
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
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'
}
class Status extends ImmutablePureComponent {
static contextTypes = {
router: PropTypes.object,
2020-04-22 06:00:11 +01:00
}
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
}
static getDerivedStateFromProps(nextProps, prevState) {
2020-05-05 06:16:01 +01:00
if (nextProps.isChild) return null
2020-05-15 03:31:24 +01:00
2020-05-02 07:25:55 +01:00
if (!nextProps.isHidden && (nextProps.isIntersecting || !nextProps.commentsLimited) && !prevState.loadedComments) {
2020-04-22 06:00:11 +01:00
return {
loadedComments: true
}
}
if (nextProps.status && nextProps.status.get('id') !== prevState.statusId) {
return {
2020-05-04 19:44:37 +01:00
loadedComments: false, //reset
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) {
2020-05-02 07:25:55 +01:00
// timeline lazy loading comments
if (!prevState.loadedComments && this.state.loadedComments && this.props.status && !this.props.isChild && this.props.contextType !== 'feature') {
2020-04-23 07:13:29 +01:00
const commentCount = this.props.status.get('replies_count')
2020-05-15 03:31:24 +01:00
if (this.props.isComment && !this.props.ancestorStatus) {
2020-05-05 06:16:01 +01:00
this.props.onFetchContext(this.props.status.get('id'))
2020-05-06 05:33:54 +01:00
this._measureHeight(prevState.height !== this.state.height)
} else {
if (commentCount > 0 && !this.props.commentsLimited) {
2020-05-06 05:33:54 +01:00
this._measureHeight(prevState.height !== this.state.height)
this.props.onFetchComments(this.props.status.get('id'))
}
2020-04-22 06:00:11 +01:00
}
}
}
2020-05-02 07:25:55 +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
}
}
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)
}
2020-05-15 04:17:31 +01:00
handleHotkeyReply = (e) => {
2020-04-22 06:00:11 +01:00
e.preventDefault()
2020-05-15 04:17:31 +01:00
this.props.onReply(this._properStatus(), this.context.router)
}
handleOnReply = (status) => {
this.props.onReply(status || this._properStatus(), this.context.router, true)
2020-04-22 06:00:11 +01:00
}
handleOnQuote = (status) => {
this.props.onQuote(status || this._properStatus(), this.context.router)
}
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()
2020-05-15 04:17:31 +01:00
this.props.onMention(this._properStatus().get('account'), this.context.router)
2020-04-22 06:00:11 +01:00
}
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-05-05 06:16:01 +01:00
const { status, ancestorStatus } = this.props
if (ancestorStatus) {
return ancestorStatus
}
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-05-06 05:33:54 +01:00
_measureHeight(heightJustChanged) {
2020-04-22 06:00:11 +01:00
try {
scheduleIdleTask(() => this.node && this.setState({ height: Math.ceil(this.node.scrollHeight) + 1 }))
2020-05-06 05:33:54 +01:00
2020-04-22 06:00:11 +01:00
if (heightJustChanged) {
this.props.onHeightChange()
2020-05-06 05:33:54 +01:00
}
2020-04-22 06:00:11 +01:00
} catch (error) {
2020-05-06 05:33:54 +01:00
//
2020-04-22 06:00:11 +01:00
}
2020-04-02 04:17:21 +01:00
}
handleOnCommentSortOpen = (btn) => {
this.props.onCommentSortOpen(btn)
}
handleRef = (c) => {
2020-04-22 06:00:11 +01:00
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
isFeatured,
isPinnedInGroup,
2020-04-22 06:00:11 +01:00
isPromoted,
2020-04-08 02:06:59 +01:00
isChild,
2020-04-22 06:00:11 +01:00
isHidden,
2020-05-14 07:03:22 +01:00
isNotification,
2020-04-22 06:00:11 +01:00
descendantsIds,
commentsLimited,
2020-05-05 06:16:01 +01:00
ancestorStatus,
isComment,
2020-05-06 05:33:54 +01:00
contextType,
2020-05-07 00:40:54 +01:00
isComposeModalOpen,
commentSortingType,
onOpenProModal,
2020-03-07 04:53:28 +00:00
} = this.props
2020-05-06 05:33:54 +01:00
// const { height } = this.state
2020-05-01 06:50:27 +01:00
let { status } = this.props
2020-03-07 04:53:28 +00:00
2020-04-22 06:00:11 +01:00
if (!status) return null
2020-04-02 04:17:21 +01:00
2020-05-06 05:33:54 +01:00
if (isComment && !ancestorStatus && !isChild) {
2020-05-15 03:31:24 +01:00
// Wait to load...
// return <StatusPlaceholder />
if (contextType === 'feature') {
return <ColumnIndicator type='loading' />
}
2020-05-15 03:31:24 +01:00
return null
2020-05-06 05:33:54 +01:00
}
2020-05-02 07:25:55 +01:00
let reblogContent, rebloggedByText = null
2020-04-02 04:17:21 +01:00
2020-05-05 06:16:01 +01:00
if (ancestorStatus) {
status = ancestorStatus
} else {
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')
status = status.get('reblog')
}
2020-04-22 06:00:11 +01:00
}
2020-02-14 00:40:04 +00:00
let sortByTitle = intl.formatMessage(messages.oldest)
if (commentSortingType === COMMENT_SORTING_TYPE_NEWEST) {
sortByTitle = intl.formatMessage(messages.newest)
} else if (commentSortingType === COMMENT_SORTING_TYPE_TOP) {
sortByTitle = intl.formatMessage(messages.top)
}
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,
}
const parentClasses = CX({
pb15: !isChild,
2020-05-06 05:33:54 +01:00
})
const containerClasses = CX({
d: 1,
radiusSmall: !isChild,
2020-04-29 23:32:49 +01:00
bgPrimary: !isChild,
boxShadowBlock: !isChild,
2020-02-28 15:20:47 +00:00
})
const containerClassesXS = CX({
d: 1,
2020-05-06 05:33:54 +01:00
bgPrimary: !isChild,
boxShadowBlock: !isChild,
2020-05-07 00:40:54 +01:00
borderTop1PX: !isChild,
borderColorSecondary: !isChild,
2020-05-06 05:33:54 +01:00
})
const innerContainerClasses = CX({
d: 1,
2020-03-08 23:02:28 +00:00
overflowHidden: 1,
2020-04-17 06:35:46 +01:00
radiusSmall: isChild,
borderColorSecondary: isChild,
border1PX: isChild,
pb10: isChild && (status.get('media_attachments').size === 0 && !isNotification) || status.get('sensitive'),
2020-05-14 07:03:22 +01:00
pb5: isChild && status.get('media_attachments').size > 1 && !isNotification,
2020-04-02 04:17:21 +01:00
cursorPointer: isChild,
2020-04-29 23:32:49 +01:00
bgSubtle_onHover: isChild,
2020-03-08 23:02:28 +00:00
})
2020-05-06 05:33:54 +01:00
if (status.get('filtered') || status.getIn(['reblog', 'filtered'])) {
return null
}
if (isHidden) {
return (
<HotKeys handlers={handlers}>
<div ref={this.handleRef} className={parentClasses} tabIndex='0'>
{status.getIn(['account', 'display_name']) || status.getIn(['account', 'username'])}
{status.get('content')}
</div>
</HotKeys>
)
}
return (
<HotKeys handlers={handlers} className={_s.outlineNone}>
2020-05-06 05:33:54 +01:00
<div className={parentClasses}>
<ResponsiveClassesComponent
classNames={containerClasses}
classNamesXS={containerClassesXS}
>
<div
className={[_s.d, _s.outlineNone].join(' ')}
2020-05-06 05:33:54 +01:00
tabIndex={this.props.isMuted ? null : 0}
data-featured={(isFeatured || isPinnedInGroup) ? 'true' : null}
2020-05-06 05:33:54 +01:00
aria-label={textForScreenReader(intl, status, rebloggedByText)}
ref={this.handleRef}
onClick={isChild && !isNotification ? this.handleClick : undefined}
2020-05-06 05:33:54 +01:00
>
<div className={innerContainerClasses}>
<div data-id={status.get('id')}>
<StatusPrepend
status={this.props.status}
isPromoted={isPromoted}
isFeatured={isFeatured}
isPinnedInGroup={isPinnedInGroup}
2020-05-06 05:33:54 +01:00
isComment={isComment && !isChild}
onOpenProModal={onOpenProModal}
2020-05-06 05:33:54 +01:00
/>
2020-05-07 00:40:54 +01:00
<StatusHeader
status={status}
reduced={isChild}
/>
2020-05-06 05:33:54 +01:00
<div className={_s.d}>
2020-05-06 05:33:54 +01:00
<StatusContent
status={status}
reblogContent={reblogContent}
onClick={this.handleClick}
expanded={!status.get('hidden')}
onExpandedToggle={this.handleExpandedToggle}
collapsable={contextType !== 'feature'}
/>
</div>
<StatusMedia
isChild={isChild}
2020-05-07 00:40:54 +01:00
isComposeModalOpen={isComposeModalOpen}
2020-05-06 05:33:54 +01:00
status={status}
onOpenMedia={this.props.onOpenMedia}
cacheWidth={this.props.cacheMediaWidth}
defaultWidth={this.props.cachedMediaWidth}
visible={this.state.showMedia}
onToggleVisibility={this.handleToggleMediaVisibility}
width={this.props.cachedMediaWidth}
onOpenVideo={this.handleOpenVideo}
/>
{
(!!status.get('quote') || status.get('has_quote')) && !isChild &&
<div className={[_s.d, _s.mt10, _s.px10].join(' ')}>
{
!!status.get('quoted_status') &&
<Status status={status.get('quoted_status')} isChild intl={intl} />
}
{
!status.get('quoted_status') &&
<div className={[_s.d, _s.border1PX, _s.bgSubtle, _s.radiusSmall, _s.py15, _s.px15, _s.borderColorSecondary].join(' ')}>
<Text color='tertiary' size='medium'>The quoted gab is unavailable.</Text>
</div>
}
2020-05-06 05:33:54 +01:00
</div>
}
{
2020-05-14 07:03:22 +01:00
(!isChild || isNotification) &&
2020-05-06 05:33:54 +01:00
<StatusActionBar
status={status}
onFavorite={this.props.onFavorite}
2020-05-15 04:17:31 +01:00
onReply={this.handleOnReply}
2020-05-06 05:33:54 +01:00
onRepost={this.props.onRepost}
onShare={this.props.onShare}
onOpenLikes={this.props.onOpenLikes}
onOpenReposts={this.props.onOpenReposts}
onQuote={this.handleOnQuote}
2020-05-06 05:33:54 +01:00
/>
}
{
!isChild && !!me &&
2020-05-09 03:17:19 +01:00
<ResponsiveClassesComponent
classNames={[_s.d, _s.borderTop1PX, _s.borderColorSecondary, _s.pt10, _s.px15, _s.mb10].join(' ')}
classNamesXS={[_s.d, _s.borderTop1PX, _s.borderColorSecondary, _s.pt10, _s.px10, _s.mb10].join(' ')}
2020-05-09 03:17:19 +01:00
>
2020-05-06 05:33:54 +01:00
<ComposeFormContainer replyToId={status.get('id')} shouldCondense />
2020-05-09 03:17:19 +01:00
</ResponsiveClassesComponent>
2020-05-06 05:33:54 +01:00
}
{
status.get('replies_count') > 0 && !isChild && !isNotification && !commentsLimited &&
<React.Fragment>
<div className={[_s.d, _s.mr10, _s.ml10, _s.mb10, _s.borderColorSecondary, _s.borderBottom1PX].join(' ')} />
<SortBlock
value={sortByTitle}
onClickValue={this.handleOnCommentSortOpen}
/>
{
descendantsIds.size === 0 &&
<React.Fragment>
<CommentPlaceholder />
<CommentPlaceholder />
<CommentPlaceholder />
</React.Fragment>
}
{
descendantsIds.size > 0 &&
<CommentList
ancestorAccountId={status.getIn(['account', 'id'])}
commentsLimited={commentsLimited}
descendants={descendantsIds}
onViewComments={this.handleClick}
/>
}
</React.Fragment>
2020-05-06 05:33:54 +01:00
}
2020-04-02 04:17:21 +01:00
</div>
2020-05-06 05:33:54 +01:00
</div>
2020-02-22 23:26:23 +00:00
</div>
2020-05-06 05:33:54 +01:00
</ResponsiveClassesComponent>
</div>
</HotKeys>
2020-04-22 06:00:11 +01:00
)
}
}
Status.propTypes = {
intl: PropTypes.object.isRequired,
status: ImmutablePropTypes.map,
descendantsIds: ImmutablePropTypes.list,
ancestorStatus: ImmutablePropTypes.map,
isNotification: PropTypes.bool,
isChild: PropTypes.bool,
isPromoted: PropTypes.bool,
isFeatured: PropTypes.bool,
isPinnedInGroup: PropTypes.bool,
isMuted: PropTypes.bool,
isHidden: PropTypes.bool,
isIntersecting: PropTypes.bool,
isComment: PropTypes.bool,
onClick: PropTypes.func,
onReply: PropTypes.func,
onRepost: PropTypes.func,
onQuote: PropTypes.func,
onFavorite: PropTypes.func,
onMention: PropTypes.func,
onOpenMedia: PropTypes.func,
onOpenProModal: PropTypes.func,
onOpenVideo: PropTypes.func,
onHeightChange: PropTypes.func,
onToggleHidden: PropTypes.func,
onShare: PropTypes.func,
onMoveUp: PropTypes.func,
onMoveDown: PropTypes.func,
onFetchComments: PropTypes.func,
onFetchContext: PropTypes.func,
cacheMediaWidth: PropTypes.func,
cachedMediaWidth: PropTypes.number,
contextType: PropTypes.string,
commentsLimited: PropTypes.bool,
onOpenLikes: PropTypes.func.isRequired,
onOpenReposts: PropTypes.func.isRequired,
onCommentSortOpen: PropTypes.func.isRequired,
isComposeModalOpen: PropTypes.bool,
commentSortingType: PropTypes.string,
}
export default injectIntl(Status)