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.

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