gab-social/app/javascript/gabsocial/features/account_timeline.js

125 lines
4.0 KiB
JavaScript
Raw Normal View History

import React from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
2020-03-04 22:26:01 +00:00
import ImmutablePropTypes from 'react-immutable-proptypes'
import ImmutablePureComponent from 'react-immutable-pure-component'
import { List as ImmutableList } from 'immutable'
import { injectIntl, defineMessages } from 'react-intl'
import { MODAL_COMPOSE } from '../constants'
2020-03-27 15:29:52 +00:00
import { expandAccountFeaturedTimeline, expandAccountTimeline } from '../actions/timelines'
import { openModal } from '../actions/modal'
2020-03-27 15:29:52 +00:00
import StatusList from '../components/status_list'
import Text from '../components/text'
import Button from '../components/button'
2019-08-13 16:54:29 +01:00
class AccountTimeline extends ImmutablePureComponent {
2020-02-24 23:25:55 +00:00
componentWillMount() {
const { accountId, commentsOnly, isDeckConnected } = this.props
2019-08-13 16:54:29 +01:00
if (accountId && accountId !== -1) {
if (!commentsOnly && !isDeckConnected) {
2020-03-04 22:26:01 +00:00
this.props.dispatch(expandAccountFeaturedTimeline(accountId))
2019-08-13 16:54:29 +01:00
}
2020-04-17 06:35:46 +01:00
this.props.dispatch(expandAccountTimeline(accountId, { commentsOnly }))
2019-08-13 16:54:29 +01:00
}
}
2020-02-24 23:25:55 +00:00
componentWillReceiveProps(nextProps) {
2020-04-17 06:35:46 +01:00
if (nextProps.accountId && nextProps.accountId !== -1 && (nextProps.accountId !== this.props.accountId && nextProps.accountId) || nextProps.commentsOnly !== this.props.commentsOnly) {
if (!nextProps.commentsOnly && !nextProps.isDeckConnected) {
2020-03-04 22:26:01 +00:00
this.props.dispatch(expandAccountFeaturedTimeline(nextProps.accountId))
2019-08-13 16:54:29 +01:00
}
2020-04-17 06:35:46 +01:00
this.props.dispatch(expandAccountTimeline(nextProps.accountId, { commentsOnly: nextProps.commentsOnly }))
2019-08-13 16:54:29 +01:00
}
}
handleLoadMore = (maxId) => {
2019-08-13 16:54:29 +01:00
if (this.props.accountId && this.props.accountId !== -1) {
2020-03-04 22:26:01 +00:00
this.props.dispatch(expandAccountTimeline(this.props.accountId, {
maxId,
2020-04-17 06:35:46 +01:00
commentsOnly: this.props.commentsOnly
2020-03-04 22:26:01 +00:00
}))
2019-08-13 16:54:29 +01:00
}
}
handleOnOpenComposeModal = () => {
this.props.dispatch(openModal(MODAL_COMPOSE))
}
2020-02-24 23:25:55 +00:00
render() {
2020-03-04 22:26:01 +00:00
const {
statusIds,
featuredStatusIds,
isLoading,
hasMore,
intl,
isMe,
2020-03-04 22:26:01 +00:00
} = this.props
const emptyMessage = (
<div className={[_s.d, _s.w100PC, _s.aiCenter, _s.jcCenter].join(' ')}>
<Text>{intl.formatMessage(messages.empty)}</Text>
{
isMe &&
<Button
className={[_s.d, _s.mt15].join(' ')}
onClick={this.handleOnOpenComposeModal}
>
Create a gab
</Button>
}
</div>
)
2019-08-13 16:54:29 +01:00
return (
2020-02-24 23:25:55 +00:00
<StatusList
scrollKey='account_timeline'
statusIds={statusIds}
featuredStatusIds={featuredStatusIds}
isLoading={isLoading}
hasMore={hasMore}
onLoadMore={this.handleLoadMore}
emptyMessage={emptyMessage}
2020-02-24 23:25:55 +00:00
/>
2020-03-04 22:26:01 +00:00
)
2019-08-13 16:54:29 +01:00
}
}
const messages = defineMessages({
empty: { id: 'empty_column.account_timeline', defaultMessage: 'No gabs here!' },
})
const emptyList = ImmutableList()
2020-12-16 22:29:06 +00:00
const mapStateToProps = (state, { id, account, commentsOnly = false }) => {
const accountId = !!id ? id : !!account ? account.getIn(['id'], null) : -1
const path = commentsOnly ? `${accountId}:comments_only` : accountId
return {
accountId,
isDeckConnected: state.getIn(['deck', 'connected'], false),
statusIds: state.getIn(['timelines', `account:${path}`, 'items'], emptyList),
featuredStatusIds: commentsOnly ? ImmutableList() : state.getIn(['timelines', `account:${accountId}:pinned`, 'items'], emptyList),
isLoading: state.getIn(['timelines', `account:${path}`, 'isLoading'], true),
hasMore: state.getIn(['timelines', `account:${path}`, 'hasMore']),
}
}
AccountTimeline.propTypes = {
params: PropTypes.object.isRequired,
dispatch: PropTypes.func.isRequired,
statusIds: ImmutablePropTypes.list,
featuredStatusIds: ImmutablePropTypes.list,
isLoading: PropTypes.bool,
hasMore: PropTypes.bool,
commentsOnly: PropTypes.bool,
isDeckConnected: PropTypes.bool,
intl: PropTypes.object.isRequired,
isMe: PropTypes.bool.isRequired,
}
export default injectIntl(connect(mapStateToProps)(AccountTimeline))