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

154 lines
4.0 KiB
JavaScript
Raw Normal View History

import React from 'react'
import PropTypes from 'prop-types'
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'
import { injectIntl, defineMessages } from 'react-intl'
import { me } from '../initial_state'
import getSortBy from '../utils/group_sort_by'
2020-03-04 22:26:01 +00:00
import { connectGroupStream } from '../actions/streaming'
import {
clearTimeline,
expandGroupTimeline,
} from '../actions/timelines'
import {
setGroupTimelineSort,
} from '../actions/groups'
import {
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'
import GroupSortBlock from '../components/group_sort_block'
2020-02-21 00:57:29 +00:00
const messages = defineMessages({
2020-02-29 15:42:47 +00: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) => ({
groupId: props.params.id,
2020-02-29 15:42:47 +00:00
group: state.getIn(['groups', props.params.id]),
sortByValue: state.getIn(['group_lists', 'sortByValue']),
sortByTopValue: state.getIn(['group_lists', 'sortByTopValue']),
2020-02-29 15:42:47 +00:00
})
2020-02-21 00:57:29 +00:00
const mapDispatchToProps = (dispatch) => ({
onConnectGroupStream(groupId) {
dispatch(connectGroupStream(groupId))
},
onClearTimeline(timelineId) {
dispatch(clearTimeline(timelineId))
},
onExpandGroupTimeline(groupId, options) {
dispatch(expandGroupTimeline(groupId, options))
},
setMemberNewest() {
dispatch(setGroupTimelineSort(GROUP_TIMELINE_SORTING_TYPE_NEWEST))
},
})
2020-02-25 16:04:44 +00:00
export default
@connect(mapStateToProps, mapDispatchToProps)
2020-02-21 00:57:29 +00:00
@injectIntl
2020-02-29 15:42:47 +00:00
class GroupTimeline extends ImmutablePureComponent {
2020-02-21 00:57:29 +00:00
static propTypes = {
params: PropTypes.object.isRequired,
2020-04-23 07:13:29 +01:00
group: PropTypes.oneOfType([
ImmutablePropTypes.map,
PropTypes.bool,
]),
groupId: PropTypes.string,
2020-02-21 00:57:29 +00:00
intl: PropTypes.object.isRequired,
onConnectGroupStream: PropTypes.func.isRequired,
onClearTimeline: PropTypes.func.isRequired,
onExpandGroupTimeline: PropTypes.func.isRequired,
setMemberNewest: PropTypes.func.isRequired,
sortByValue: PropTypes.string.isRequired,
sortByTopValue: PropTypes.string,
onlyMedia: PropTypes.bool,
2020-02-21 00:57:29 +00:00
}
state = {
//keep track of loads for if no user,
//only allow 2 loads before showing sign up msg
loadCount: 0,
}
2020-02-29 15:42:47 +00:00
componentDidMount() {
const {
groupId,
sortByValue,
sortByTopValue,
onlyMedia,
} = this.props
if (sortByValue !== GROUP_TIMELINE_SORTING_TYPE_NEWEST) {
this.props.setMemberNewest()
} else {
const sortBy = getSortBy(sortByValue, sortByTopValue, onlyMedia)
this.props.onExpandGroupTimeline(groupId, { sortBy, onlyMedia })
2020-02-29 15:42:47 +00:00
if (!!me) {
this.disconnect = this.props.onConnectGroupStream(groupId)
}
}
2020-02-21 00:57:29 +00:00
}
componentDidUpdate(prevProps) {
if ((prevProps.sortByValue !== this.props.sortByValue ||
prevProps.sortByTopValue !== this.props.sortByTopValue ||
prevProps.onlyMedia !== this.props.onlyMedia) &&
prevProps.groupId === this.props.groupId) {
this.handleLoadMore()
this.props.onClearTimeline(`group:${this.props.groupId}`)
}
}
2020-02-29 15:42:47 +00:00
componentWillUnmount() {
if (this.disconnect && !!me) {
2020-02-29 15:42:47 +00:00
this.disconnect()
this.disconnect = null
}
}
2020-02-21 00:57:29 +00:00
handleLoadMore = (maxId) => {
const {
groupId,
sortByValue,
sortByTopValue,
onlyMedia,
} = this.props
const sortBy = getSortBy(sortByValue, sortByTopValue)
this.props.onExpandGroupTimeline(groupId, { sortBy, maxId, onlyMedia })
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() {
const {
group,
groupId,
intl,
} = this.props
2020-02-29 15:42:47 +00: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
return (
<React.Fragment>
<GroupSortBlock />
<StatusList
scrollKey={`group-timeline-${groupId}`}
timelineId={`group:${groupId}`}
onLoadMore={this.handleLoadMore}
emptyMessage={intl.formatMessage(messages.empty)}
/>
</React.Fragment>
2020-02-29 15:42:47 +00:00
)
2020-02-21 00:57:29 +00:00
}
2020-02-29 15:42:47 +00:00
}