You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
4.1 KiB

  1. import React from 'react';
  2. import { connect } from 'react-redux';
  3. import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
  4. import PropTypes from 'prop-types';
  5. import StatusListContainer from '../ui/containers/status_list_container';
  6. import Column from '../../components/column';
  7. import ColumnHeader from '../../components/column_header';
  8. import { expandCommunityTimeline } from '../../actions/timelines';
  9. import { addColumn, removeColumn, moveColumn } from '../../actions/columns';
  10. import ColumnSettingsContainer from './containers/column_settings_container';
  11. import { connectCommunityStream } from '../../actions/streaming';
  12. const messages = defineMessages({
  13. title: { id: 'column.community', defaultMessage: 'Local timeline' },
  14. });
  15. const mapStateToProps = (state, { columnId }) => {
  16. const uuid = columnId;
  17. const columns = state.getIn(['settings', 'columns']);
  18. const index = columns.findIndex(c => c.get('uuid') === uuid);
  19. const onlyMedia = (columnId && index >= 0) ? columns.get(index).getIn(['params', 'other', 'onlyMedia']) : state.getIn(['settings', 'community', 'other', 'onlyMedia']);
  20. const timelineState = state.getIn(['timelines', `community${onlyMedia ? ':media' : ''}`]);
  21. return {
  22. hasUnread: !!timelineState && timelineState.get('unread') > 0,
  23. onlyMedia,
  24. };
  25. };
  26. export default @connect(mapStateToProps)
  27. @injectIntl
  28. class CommunityTimeline extends React.PureComponent {
  29. static contextTypes = {
  30. router: PropTypes.object,
  31. };
  32. static defaultProps = {
  33. onlyMedia: false,
  34. };
  35. static propTypes = {
  36. dispatch: PropTypes.func.isRequired,
  37. shouldUpdateScroll: PropTypes.func,
  38. columnId: PropTypes.string,
  39. intl: PropTypes.object.isRequired,
  40. hasUnread: PropTypes.bool,
  41. multiColumn: PropTypes.bool,
  42. onlyMedia: PropTypes.bool,
  43. };
  44. handlePin = () => {
  45. const { columnId, dispatch, onlyMedia } = this.props;
  46. if (columnId) {
  47. dispatch(removeColumn(columnId));
  48. } else {
  49. dispatch(addColumn('COMMUNITY', { other: { onlyMedia } }));
  50. }
  51. }
  52. handleMove = (dir) => {
  53. const { columnId, dispatch } = this.props;
  54. dispatch(moveColumn(columnId, dir));
  55. }
  56. handleHeaderClick = () => {
  57. this.column.scrollTop();
  58. }
  59. componentDidMount () {
  60. const { dispatch, onlyMedia } = this.props;
  61. dispatch(expandCommunityTimeline({ onlyMedia }));
  62. this.disconnect = dispatch(connectCommunityStream({ onlyMedia }));
  63. }
  64. componentDidUpdate (prevProps) {
  65. if (prevProps.onlyMedia !== this.props.onlyMedia) {
  66. const { dispatch, onlyMedia } = this.props;
  67. this.disconnect();
  68. dispatch(expandCommunityTimeline({ onlyMedia }));
  69. this.disconnect = dispatch(connectCommunityStream({ onlyMedia }));
  70. }
  71. }
  72. componentWillUnmount () {
  73. if (this.disconnect) {
  74. this.disconnect();
  75. this.disconnect = null;
  76. }
  77. }
  78. setRef = c => {
  79. this.column = c;
  80. }
  81. handleLoadMore = maxId => {
  82. const { dispatch, onlyMedia } = this.props;
  83. dispatch(expandCommunityTimeline({ maxId, onlyMedia }));
  84. }
  85. render () {
  86. const { intl, shouldUpdateScroll, hasUnread, columnId, multiColumn, onlyMedia } = this.props;
  87. const pinned = !!columnId;
  88. return (
  89. <Column bindToDocument={!multiColumn} ref={this.setRef} label={intl.formatMessage(messages.title)}>
  90. <ColumnHeader
  91. icon='users'
  92. active={hasUnread}
  93. title={intl.formatMessage(messages.title)}
  94. onPin={this.handlePin}
  95. onMove={this.handleMove}
  96. onClick={this.handleHeaderClick}
  97. pinned={pinned}
  98. multiColumn={multiColumn}
  99. >
  100. <ColumnSettingsContainer columnId={columnId} />
  101. </ColumnHeader>
  102. <StatusListContainer
  103. trackScroll={!pinned}
  104. scrollKey={`community_timeline-${columnId}`}
  105. timelineId={`community${onlyMedia ? ':media' : ''}`}
  106. onLoadMore={this.handleLoadMore}
  107. emptyMessage={<FormattedMessage id='empty_column.community' defaultMessage='The local timeline is empty. Write something publicly to get the ball rolling!' />}
  108. shouldUpdateScroll={shouldUpdateScroll}
  109. bindToDocument={!multiColumn}
  110. />
  111. </Column>
  112. );
  113. }
  114. }