2020-08-06 23:19:18 -05:00
import { Fragment } from 'react'
2020-02-29 10:42:47 -05:00
import ImmutablePureComponent from 'react-immutable-pure-component'
import ImmutablePropTypes from 'react-immutable-proptypes'
import { injectIntl , defineMessages } from 'react-intl'
2020-07-21 23:16:39 -05:00
import { me } from '../initial_state'
2020-08-06 23:19:18 -05:00
import { GROUP _TIMELINE _SORTING _TYPE _TOP } from '../constants'
2020-03-04 17:26:01 -05:00
import { connectGroupStream } from '../actions/streaming'
import { expandGroupTimeline } from '../actions/timelines'
2020-04-11 18:29:19 -04:00
import StatusList from '../components/status_list'
2020-03-04 17:26:01 -05:00
import ColumnIndicator from '../components/column_indicator'
2020-08-06 23:19:18 -05:00
import GroupSortBlock from '../components/group_sort_block'
2020-02-20 19:57:29 -05:00
const messages = defineMessages ( {
2020-02-29 10:42:47 -05:00
empty : { id : 'empty_column.group' , defaultMessage : 'There is nothing in this group yet.\nWhen members of this group post new statuses, they will appear here.' } ,
} )
const mapStateToProps = ( state , props ) => ( {
group : state . getIn ( [ 'groups' , props . params . id ] ) ,
2020-08-06 23:19:18 -05:00
sortByValue : state . getIn ( [ 'group_lists' , 'sortByValue' ] ) ,
sortByTopValue : state . getIn ( [ 'group_lists' , 'sortByTopValue' ] ) ,
2020-02-29 10:42:47 -05:00
} )
2020-02-20 19:57:29 -05:00
2020-02-25 11:04:44 -05:00
export default
@ connect ( mapStateToProps )
2020-02-20 19:57:29 -05:00
@ injectIntl
2020-02-29 10:42:47 -05:00
class GroupTimeline extends ImmutablePureComponent {
2020-02-20 19:57:29 -05:00
static propTypes = {
params : PropTypes . object . isRequired ,
dispatch : PropTypes . func . isRequired ,
2020-04-23 02:13:29 -04:00
group : PropTypes . oneOfType ( [
ImmutablePropTypes . map ,
PropTypes . bool ,
] ) ,
2020-02-20 19:57:29 -05:00
intl : PropTypes . object . isRequired ,
2020-08-06 23:19:18 -05:00
sortByValue : PropTypes . string . isRequired ,
sortByTopValue : PropTypes . string ,
2020-02-20 19:57:29 -05:00
}
2020-02-29 10:42:47 -05:00
componentDidMount ( ) {
2020-08-06 23:19:18 -05:00
const {
dispatch ,
sortByValue ,
sortByTopValue ,
} = this . props
2020-02-29 10:42:47 -05:00
const { id } = this . props . params
2020-08-06 23:19:18 -05:00
const sortBy = sortByValue === GROUP _TIMELINE _SORTING _TYPE _TOP ? ` ${ sortByValue } _ ${ sortByTopValue } ` : sortByValue
const options = { sortBy }
dispatch ( expandGroupTimeline ( id , options ) )
2020-02-29 10:42:47 -05:00
2020-07-21 23:16:39 -05:00
if ( ! ! me ) {
this . disconnect = dispatch ( connectGroupStream ( id ) )
}
2020-02-20 19:57:29 -05:00
}
2020-08-06 23:19:18 -05:00
componentDidUpdate ( prevProps ) {
if ( prevProps . sortByValue !== this . props . sortByValue || prevProps . sortByTopValue !== this . props . sortByTopValue ) {
this . handleLoadMore ( )
}
}
2020-02-29 10:42:47 -05:00
componentWillUnmount ( ) {
2020-07-21 23:16:39 -05:00
if ( this . disconnect && ! ! me ) {
2020-02-29 10:42:47 -05:00
this . disconnect ( )
this . disconnect = null
}
}
2020-02-20 19:57:29 -05:00
2020-08-06 23:19:18 -05:00
handleLoadMore = ( maxId ) => {
const {
dispatch ,
sortByValue ,
sortByTopValue ,
} = this . props
2020-02-29 10:42:47 -05:00
const { id } = this . props . params
2020-08-06 23:19:18 -05:00
const sortBy = sortByValue === GROUP _TIMELINE _SORTING _TYPE _TOP ? ` ${ sortByValue } _ ${ sortByTopValue } ` : sortByValue
const options = { sortBy , maxId }
dispatch ( expandGroupTimeline ( id , options ) )
2020-02-29 10:42:47 -05:00
}
2020-02-20 19:57:29 -05:00
2020-02-29 10:42:47 -05:00
render ( ) {
2020-08-05 23:58:59 -05:00
const { group , intl } = this . props
2020-02-29 10:42:47 -05:00
const { id } = this . props . params
2020-07-21 23:05:54 -05:00
if ( typeof group === 'undefined' ) {
2020-02-29 10:42:47 -05:00
return < ColumnIndicator type = 'loading' / >
} else if ( group === false ) {
return < ColumnIndicator type = 'missing' / >
}
2020-02-20 19:57:29 -05:00
return (
2020-08-06 23:19:18 -05:00
< Fragment >
< GroupSortBlock groupId = { id } / >
< StatusList
scrollKey = { ` group-timeline- ${ id } ` }
timelineId = { ` group: ${ id } ` }
onLoadMore = { this . handleLoadMore }
emptyMessage = { intl . formatMessage ( messages . empty ) }
/ >
< / F r a g m e n t >
2020-02-29 10:42:47 -05:00
)
2020-02-20 19:57:29 -05:00
}
2020-02-29 10:42:47 -05:00
}