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.

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