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

567 lines
17 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-05-01 06:50:27 +01:00
import { me, displayMedia, compactMode } 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 ColumnIndicator from './column_indicator'
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'
// 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'])
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'
}
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,
2020-05-04 19:44:37 +01:00
ancestorStatus: ImmutablePropTypes.map,
2020-04-22 06:00:11 +01:00
isChild: PropTypes.bool,
isPromoted: PropTypes.bool,
isFeatured: PropTypes.bool,
isMuted: PropTypes.bool,
isHidden: PropTypes.bool,
isIntersecting: PropTypes.bool,
2020-05-05 06:16:01 +01:00
isComment: 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,
2020-05-05 06:16:01 +01:00
onFetchContext: 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-05-02 07:25:55 +01:00
commentsLimited: PropTypes.bool,
2020-05-03 06:22:49 +01:00
onOpenLikes: PropTypes.func.isRequired,
onOpenReposts: PropTypes.func.isRequired,
2020-05-07 00:40:54 +01:00
isComposeModalOpen: PropTypes.bool,
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',
2020-05-05 06:16:01 +01:00
'isComment',
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
}
// 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-05-05 06:16:01 +01:00
if (nextProps.isChild) return null
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, snapshot) {
2020-05-02 07:25:55 +01:00
// timeline lazy loading comments
2020-05-05 06:16:01 +01:00
if (!prevState.loadedComments && this.state.loadedComments && this.props.status && !this.props.isChild) {
2020-04-23 07:13:29 +01:00
const commentCount = this.props.status.get('replies_count')
2020-05-06 05:33:54 +01:00
if (this.props.isComment) {
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._measureHeight(prevState.height !== this.state.height)
this.props.onFetchComments(this.props.status.get('id'))
}
2020-04-22 06:00:11 +01:00
}
}
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-05-06 05:33:54 +01:00
console.log("updateScrollBottom")
2020-04-22 06:00:11 +01:00
this.props.updateScrollBottom(snapshot.height - snapshot.top)
}
}
}
}
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
}
}
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-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
}
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
isFeatured,
isPromoted,
2020-04-08 02:06:59 +01:00
isChild,
2020-04-22 06:00:11 +01:00
isHidden,
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,
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) {
return <ColumnIndicator type='loading' />
}
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
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-05-06 05:33:54 +01:00
const parentClasses = cx({
pb15: !isChild && !compactMode,
})
2020-02-28 15:20:47 +00:00
const containerClasses = cx({
2020-05-01 06:50:27 +01:00
radiusSmall: !isChild && !compactMode,
2020-04-29 23:32:49 +01:00
bgPrimary: !isChild,
2020-05-01 06:50:27 +01:00
boxShadowBlock: !isChild && !compactMode,
borderRight1PX: !isChild && compactMode,
borderLeft1PX: !isChild && compactMode,
borderBottom1PX: !isChild && compactMode,
borderColorSecondary: !isChild && compactMode,
2020-02-28 15:20:47 +00:00
})
2020-05-06 05:33:54 +01:00
const containerClassesXS = cx({
2020-05-07 00:40:54 +01:00
default: 1,
2020-05-06 05:33:54 +01:00
bgPrimary: !isChild,
boxShadowBlock: !isChild && !compactMode,
2020-05-07 00:40:54 +01:00
borderTop1PX: !isChild,
2020-05-06 05:33:54 +01:00
borderBottom1PX: !isChild && compactMode,
2020-05-07 00:40:54 +01:00
borderColorSecondary: !isChild,
2020-05-06 05:33:54 +01: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-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}>
2020-05-06 05:33:54 +01:00
<div className={parentClasses}>
<ResponsiveClassesComponent
classNames={containerClasses}
classNamesXS={containerClassesXS}
>
<div
className={[_s.default, _s.outlineNone].join(' ')}
tabIndex={this.props.isMuted ? null : 0}
data-featured={isFeatured ? 'true' : null}
aria-label={textForScreenReader(intl, status, rebloggedByText)}
ref={this.handleRef}
onClick={isChild ? this.handleClick : undefined}
>
<div className={innerContainerClasses}>
<div data-id={status.get('id')}>
<StatusPrepend
status={this.props.status}
isPromoted={isPromoted}
isFeatured={isFeatured}
isComment={isComment && !isChild}
/>
2020-05-07 00:40:54 +01:00
<StatusHeader
status={status}
reduced={isChild}
/>
2020-05-06 05:33:54 +01:00
<div className={_s.default}>
<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') && !isChild &&
<div className={[_s.default, _s.mt10, _s.px10].join(' ')}>
<Status status={status.get('quoted_status')} isChild intl={intl} />
</div>
}
{
!isChild &&
<StatusActionBar
status={status}
onFavorite={this.props.onFavorite}
onReply={this.props.onReply}
onRepost={this.props.onRepost}
onShare={this.props.onShare}
onOpenLikes={this.props.onOpenLikes}
onOpenReposts={this.props.onOpenReposts}
/>
}
{
!isChild && !compactMode && !!me &&
<div className={[_s.default, _s.borderTop1PX, _s.borderColorSecondary, _s.pt10, _s.px15, _s.mb10].join(' ')}>
<ComposeFormContainer replyToId={status.get('id')} shouldCondense />
</div>
}
{
descendantsIds && !compactMode && !isChild && descendantsIds.size > 0 &&
<div className={[_s.default, _s.mr10, _s.ml10, _s.mb10, _s.borderColorSecondary, _s.borderBottom1PX].join(' ')} />
}
{
descendantsIds && !compactMode && !isChild && descendantsIds.size > 0 &&
<CommentList
ancestorAccountId={status.getIn(['account', 'id'])}
commentsLimited={commentsLimited}
descendants={descendantsIds}
onViewComments={this.handleClick}
/>
}
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
)
}
}