2019-07-02 08:10:25 +01:00
import React from 'react' ;
import { connect } from 'react-redux' ;
import { defineMessages , injectIntl , FormattedMessage } from 'react-intl' ;
import PropTypes from 'prop-types' ;
import StatusListContainer from '../ui/containers/status_list_container' ;
import Column from '../../components/column' ;
import ColumnSettingsContainer from './containers/column_settings_container' ;
2019-07-09 04:59:11 +01:00
import HomeColumnHeader from '../../components/home_column_header' ;
2019-07-12 21:26:06 +01:00
import {
expandCommunityTimeline ,
expandPublicTimeline ,
} from '../../actions/timelines' ;
import {
connectCommunityStream ,
connectPublicStream ,
} from '../../actions/streaming' ;
2019-07-02 08:10:25 +01:00
const messages = defineMessages ( {
2019-07-12 21:26:06 +01:00
title : { id : 'column.community' , defaultMessage : 'Community timeline' } ,
2019-07-02 08:10:25 +01:00
} ) ;
2019-07-12 21:26:06 +01:00
const mapStateToProps = state => {
const allFediverse = state . getIn ( [ 'settings' , 'community' , 'other' , 'allFediverse' ] ) ;
const onlyMedia = state . getIn ( [ 'settings' , 'community' , 'other' , 'onlyMedia' ] ) ;
const timelineId = allFediverse ? 'public' : 'community' ;
return {
timelineId ,
allFediverse ,
onlyMedia ,
hasUnread : state . getIn ( [ 'timelines' , ` ${ timelineId } ${ onlyMedia ? ':media' : '' } ` , 'unread' ] ) > 0 ,
} ;
} ;
2019-07-02 08:10:25 +01:00
export default @ connect ( mapStateToProps )
@ injectIntl
class CommunityTimeline extends React . PureComponent {
static contextTypes = {
router : PropTypes . object ,
} ;
static propTypes = {
dispatch : PropTypes . func . isRequired ,
intl : PropTypes . object . isRequired ,
hasUnread : PropTypes . bool ,
onlyMedia : PropTypes . bool ,
2019-07-12 21:26:06 +01:00
allFediverse : PropTypes . bool ,
timelineId : PropTypes . string ,
2019-07-02 08:10:25 +01:00
} ;
componentDidMount ( ) {
2019-07-12 21:26:06 +01:00
const { dispatch , onlyMedia , allFediverse } = this . props ;
2019-07-02 08:10:25 +01:00
2019-07-12 21:26:06 +01:00
if ( allFediverse ) {
dispatch ( expandPublicTimeline ( { onlyMedia } ) ) ;
this . disconnect = dispatch ( connectPublicStream ( { onlyMedia } ) ) ;
}
else {
dispatch ( expandCommunityTimeline ( { onlyMedia } ) ) ;
this . disconnect = dispatch ( connectCommunityStream ( { onlyMedia } ) ) ;
}
2019-07-02 08:10:25 +01:00
}
componentDidUpdate ( prevProps ) {
2019-07-12 21:26:06 +01:00
if ( prevProps . onlyMedia !== this . props . onlyMedia || prevProps . allFediverse !== this . props . allFediverse ) {
const { dispatch , onlyMedia , allFediverse } = this . props ;
2019-07-02 08:10:25 +01:00
this . disconnect ( ) ;
2019-07-12 21:26:06 +01:00
if ( allFediverse ) {
dispatch ( expandPublicTimeline ( { onlyMedia } ) ) ;
this . disconnect = dispatch ( connectPublicStream ( { onlyMedia } ) ) ;
}
else {
dispatch ( expandCommunityTimeline ( { onlyMedia } ) ) ;
this . disconnect = dispatch ( connectCommunityStream ( { onlyMedia } ) ) ;
}
2019-07-02 08:10:25 +01:00
}
}
componentWillUnmount ( ) {
if ( this . disconnect ) {
this . disconnect ( ) ;
this . disconnect = null ;
}
}
handleLoadMore = maxId => {
2019-07-12 21:26:06 +01:00
const { dispatch , onlyMedia , allFediverse } = this . props ;
2019-07-02 08:10:25 +01:00
2019-07-12 21:26:06 +01:00
if ( allFediverse ) {
dispatch ( expandPublicTimeline ( { maxId , onlyMedia } ) ) ;
}
else {
dispatch ( expandCommunityTimeline ( { maxId , onlyMedia } ) ) ;
}
2019-07-02 08:10:25 +01:00
}
render ( ) {
2019-07-12 21:26:06 +01:00
const { intl , hasUnread , onlyMedia , timelineId , allFediverse } = this . props ;
2019-07-02 08:10:25 +01:00
return (
< Column label = { intl . formatMessage ( messages . title ) } >
2019-07-12 21:26:06 +01:00
< HomeColumnHeader activeItem = 'all' active = { hasUnread } >
2019-07-09 04:59:11 +01:00
< ColumnSettingsContainer / >
< / H o m e C o l u m n H e a d e r >
2019-07-02 08:10:25 +01:00
< StatusListContainer
2019-07-12 21:26:06 +01:00
scrollKey = { ` ${ timelineId } _timeline ` }
timelineId = { ` ${ timelineId } ${ onlyMedia ? ':media' : '' } ` }
2019-07-02 08:10:25 +01:00
onLoadMore = { this . handleLoadMore }
2019-07-12 21:26:06 +01:00
emptyMessage = { < FormattedMessage id = 'empty_column.community' defaultMessage = 'The community timeline is empty. Write something publicly to get the ball rolling!' / > }
2019-07-02 08:10:25 +01:00
/ >
< / C o l u m n >
) ;
}
}