2020-08-17 21:07:16 +01:00
import React from 'react'
2020-08-17 21:59:29 +01:00
import PropTypes from 'prop-types'
2020-08-17 21:39:25 +01:00
import { connect } from 'react-redux'
2020-02-29 15:42:47 +00:00
import ImmutablePureComponent from 'react-immutable-pure-component'
import ImmutablePropTypes from 'react-immutable-proptypes'
2020-09-10 21:07:01 +01:00
import { List as ImmutableList } from 'immutable'
2020-02-29 15:42:47 +00:00
import { injectIntl , defineMessages } from 'react-intl'
2020-07-22 05:16:39 +01:00
import { me } from '../initial_state'
2020-08-07 23:59:39 +01:00
import getSortBy from '../utils/group_sort_by'
import {
clearTimeline ,
expandGroupTimeline ,
2020-09-10 21:07:01 +01:00
expandGroupFeaturedTimeline ,
2020-08-07 23:59:39 +01:00
} from '../actions/timelines'
import {
setGroupTimelineSort ,
} from '../actions/groups'
import {
2021-01-14 03:49:00 +00:00
MIN _UNAUTHENTICATED _PAGES ,
2020-08-07 23:59:39 +01:00
GROUP _TIMELINE _SORTING _TYPE _NEWEST ,
} from '../constants'
2020-04-11 23:29:19 +01:00
import StatusList from '../components/status_list'
2020-03-04 22:26:01 +00:00
import ColumnIndicator from '../components/column_indicator'
2020-08-07 05:19:18 +01:00
import GroupSortBlock from '../components/group_sort_block'
2020-02-21 00:57:29 +00:00
2020-02-29 15:42:47 +00:00
class GroupTimeline extends ImmutablePureComponent {
2020-08-07 23:59:39 +01:00
state = {
2020-12-24 18:34:14 +00:00
//keep track of page loads for if no user,
2021-01-14 03:49:00 +00:00
//only allow MIN_UNAUTHENTICATED_PAGES page loads before showing sign up msg
2020-12-24 18:34:14 +00:00
page : 1 ,
2020-08-07 23:59:39 +01:00
}
2020-02-29 15:42:47 +00:00
componentDidMount ( ) {
2020-08-07 05:19:18 +01:00
const {
2020-08-07 23:59:39 +01:00
groupId ,
2020-08-07 05:19:18 +01:00
sortByValue ,
sortByTopValue ,
2020-08-07 23:59:39 +01:00
onlyMedia ,
2020-12-23 04:48:01 +00:00
isDeckConnected ,
2020-08-07 05:19:18 +01:00
} = this . props
2020-08-07 23:59:39 +01:00
if ( sortByValue !== GROUP _TIMELINE _SORTING _TYPE _NEWEST ) {
this . props . setMemberNewest ( )
} else {
const sortBy = getSortBy ( sortByValue , sortByTopValue , onlyMedia )
2020-09-10 21:07:01 +01:00
2020-12-23 04:48:01 +00:00
if ( ! isDeckConnected ) {
this . props . onExpandGroupFeaturedTimeline ( groupId )
}
2020-08-07 23:59:39 +01:00
this . props . onExpandGroupTimeline ( groupId , { sortBy , onlyMedia } )
2020-07-22 05:16:39 +01:00
}
2020-02-21 00:57:29 +00:00
}
2020-08-07 05:19:18 +01:00
componentDidUpdate ( prevProps ) {
2020-08-07 23:59:39 +01:00
if ( ( prevProps . sortByValue !== this . props . sortByValue ||
prevProps . sortByTopValue !== this . props . sortByTopValue ||
prevProps . onlyMedia !== this . props . onlyMedia ) &&
prevProps . groupId === this . props . groupId ) {
2020-08-07 05:19:18 +01:00
this . handleLoadMore ( )
2020-08-07 23:59:39 +01:00
this . props . onClearTimeline ( ` group: ${ this . props . groupId } ` )
2020-08-07 05:19:18 +01:00
}
2020-09-10 21:07:01 +01:00
2020-12-23 04:48:01 +00:00
if ( prevProps . groupId !== this . props . groupId && ! this . props . isDeckConnected ) {
2020-09-10 21:07:01 +01:00
this . props . onExpandGroupFeaturedTimeline ( this . props . groupId )
}
2020-08-07 05:19:18 +01:00
}
handleLoadMore = ( maxId ) => {
const {
2020-08-07 23:59:39 +01:00
groupId ,
2020-08-07 05:19:18 +01:00
sortByValue ,
sortByTopValue ,
2020-08-07 23:59:39 +01:00
onlyMedia ,
2020-08-07 05:19:18 +01:00
} = this . props
2020-12-24 18:34:14 +00:00
const { page } = this . state
const newPage = ! ! maxId ? this . state . page + 1 : 1
2021-01-14 03:49:00 +00:00
if ( ! ! maxId && ! me && page >= MIN _UNAUTHENTICATED _PAGES ) return false
2020-12-24 18:34:14 +00:00
this . setState ( { page : newPage } )
2020-09-14 23:19:24 +01:00
2020-08-07 23:59:39 +01:00
const sortBy = getSortBy ( sortByValue , sortByTopValue )
2020-12-24 18:34:14 +00:00
this . props . onExpandGroupTimeline ( groupId , {
sortBy ,
maxId ,
onlyMedia ,
page : newPage ,
} )
2020-02-29 15:42:47 +00:00
}
2020-02-21 00:57:29 +00:00
2020-02-29 15:42:47 +00:00
render ( ) {
2020-08-07 23:59:39 +01:00
const {
group ,
groupId ,
intl ,
2020-09-10 21:07:01 +01:00
groupPinnedStatusIds ,
2020-08-07 23:59:39 +01:00
} = this . props
2020-12-24 18:34:14 +00:00
const { page } = this . state
2020-02-29 15:42:47 +00:00
2020-07-22 05:05:54 +01:00
if ( typeof group === 'undefined' ) {
2020-02-29 15:42:47 +00:00
return < ColumnIndicator type = 'loading' / >
} else if ( group === false ) {
return < ColumnIndicator type = 'missing' / >
}
2020-02-21 00:57:29 +00:00
2021-01-14 03:49:00 +00:00
const canLoadMore = page < MIN _UNAUTHENTICATED _PAGES && ! me || ! ! me
2020-09-14 23:19:24 +01:00
2020-02-21 00:57:29 +00:00
return (
2020-08-17 21:07:16 +01:00
< React . Fragment >
2020-08-07 23:59:39 +01:00
< GroupSortBlock / >
2020-08-07 05:19:18 +01:00
< StatusList
2020-08-07 23:59:39 +01:00
scrollKey = { ` group-timeline- ${ groupId } ` }
timelineId = { ` group: ${ groupId } ` }
2020-09-10 21:07:01 +01:00
groupPinnedStatusIds = { groupPinnedStatusIds }
2020-09-14 23:19:24 +01:00
onLoadMore = { canLoadMore ? this . handleLoadMore : undefined }
2020-08-07 05:19:18 +01:00
emptyMessage = { intl . formatMessage ( messages . empty ) }
/ >
2020-08-17 21:07:16 +01:00
< / R e a c t . F r a g m e n t >
2020-02-29 15:42:47 +00:00
)
2020-02-21 00:57:29 +00:00
}
2020-02-29 15:42:47 +00:00
}
2020-08-19 01:22:15 +01:00
const messages = defineMessages ( {
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.' } ,
} )
2020-09-10 21:07:01 +01:00
const emptyList = ImmutableList ( )
2020-08-19 01:22:15 +01:00
const mapStateToProps = ( state , props ) => ( {
groupId : props . params . id ,
group : state . getIn ( [ 'groups' , props . params . id ] ) ,
2020-09-10 21:07:01 +01:00
groupPinnedStatusIds : state . getIn ( [ 'timelines' , ` group: ${ props . params . id } :pinned ` , 'items' ] , emptyList ) ,
2020-08-19 01:22:15 +01:00
sortByValue : state . getIn ( [ 'group_lists' , 'sortByValue' ] ) ,
sortByTopValue : state . getIn ( [ 'group_lists' , 'sortByTopValue' ] ) ,
2020-12-23 04:48:01 +00:00
isDeckConnected : state . getIn ( [ 'deck' , 'connected' ] , false ) ,
2020-08-19 01:22:15 +01:00
} )
const mapDispatchToProps = ( dispatch ) => ( {
onClearTimeline ( timelineId ) {
dispatch ( clearTimeline ( timelineId ) )
} ,
onExpandGroupTimeline ( groupId , options ) {
dispatch ( expandGroupTimeline ( groupId , options ) )
} ,
setMemberNewest ( ) {
dispatch ( setGroupTimelineSort ( GROUP _TIMELINE _SORTING _TYPE _NEWEST ) )
} ,
2020-09-10 21:07:01 +01:00
onExpandGroupFeaturedTimeline ( groupId ) {
dispatch ( expandGroupFeaturedTimeline ( groupId ) )
} ,
2020-08-19 01:22:15 +01:00
} )
GroupTimeline . propTypes = {
params : PropTypes . object . isRequired ,
group : PropTypes . oneOfType ( [
ImmutablePropTypes . map ,
PropTypes . bool ,
] ) ,
groupId : PropTypes . string ,
intl : PropTypes . object . isRequired ,
onClearTimeline : PropTypes . func . isRequired ,
onExpandGroupTimeline : PropTypes . func . isRequired ,
2020-09-10 21:07:01 +01:00
onExpandGroupFeaturedTimeline : PropTypes . func . isRequired ,
2020-08-19 01:22:15 +01:00
setMemberNewest : PropTypes . func . isRequired ,
sortByValue : PropTypes . string . isRequired ,
sortByTopValue : PropTypes . string ,
onlyMedia : PropTypes . bool ,
}
export default injectIntl ( connect ( mapStateToProps , mapDispatchToProps ) ( GroupTimeline ) )