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
3.6 KiB

  1. import React from 'react';
  2. import { connect } from 'react-redux';
  3. import PropTypes from 'prop-types';
  4. import StatusListContainer from '../ui/containers/status_list_container';
  5. import Column from '../../components/column';
  6. import ColumnHeader from '../../components/column_header';
  7. import {
  8. refreshTimeline,
  9. updateTimeline,
  10. deleteFromTimelines,
  11. connectTimeline,
  12. disconnectTimeline,
  13. } from '../../actions/timelines';
  14. import { addColumn, removeColumn, moveColumn } from '../../actions/columns';
  15. import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
  16. import ColumnBackButtonSlim from '../../components/column_back_button_slim';
  17. import createStream from '../../stream';
  18. const messages = defineMessages({
  19. title: { id: 'column.community', defaultMessage: 'Local timeline' },
  20. });
  21. const mapStateToProps = state => ({
  22. hasUnread: state.getIn(['timelines', 'community', 'unread']) > 0,
  23. streamingAPIBaseURL: state.getIn(['meta', 'streaming_api_base_url']),
  24. accessToken: state.getIn(['meta', 'access_token']),
  25. });
  26. class CommunityTimeline extends React.PureComponent {
  27. static propTypes = {
  28. dispatch: PropTypes.func.isRequired,
  29. columnId: PropTypes.string,
  30. intl: PropTypes.object.isRequired,
  31. streamingAPIBaseURL: PropTypes.string.isRequired,
  32. accessToken: PropTypes.string.isRequired,
  33. hasUnread: PropTypes.bool,
  34. multiColumn: PropTypes.bool,
  35. };
  36. handlePin = () => {
  37. const { columnId, dispatch } = this.props;
  38. if (columnId) {
  39. dispatch(removeColumn(columnId));
  40. } else {
  41. dispatch(addColumn('COMMUNITY', {}));
  42. }
  43. }
  44. handleMove = (dir) => {
  45. const { columnId, dispatch } = this.props;
  46. dispatch(moveColumn(columnId, dir));
  47. }
  48. handleHeaderClick = () => {
  49. this.column.scrollTop();
  50. }
  51. componentDidMount () {
  52. const { dispatch, streamingAPIBaseURL, accessToken } = this.props;
  53. dispatch(refreshTimeline('community'));
  54. if (typeof this._subscription !== 'undefined') {
  55. return;
  56. }
  57. this._subscription = createStream(streamingAPIBaseURL, accessToken, 'public:local', {
  58. connected () {
  59. dispatch(connectTimeline('community'));
  60. },
  61. reconnected () {
  62. dispatch(connectTimeline('community'));
  63. },
  64. disconnected () {
  65. dispatch(disconnectTimeline('community'));
  66. },
  67. received (data) {
  68. switch(data.event) {
  69. case 'update':
  70. dispatch(updateTimeline('community', JSON.parse(data.payload)));
  71. break;
  72. case 'delete':
  73. dispatch(deleteFromTimelines(data.payload));
  74. break;
  75. }
  76. },
  77. });
  78. }
  79. componentWillUnmount () {
  80. if (typeof this._subscription !== 'undefined') {
  81. this._subscription.close();
  82. this._subscription = null;
  83. }
  84. }
  85. setRef = c => {
  86. this.column = c;
  87. }
  88. render () {
  89. const { intl, hasUnread, columnId, multiColumn } = this.props;
  90. const pinned = !!columnId;
  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. <StatusListContainer
  104. {...this.props}
  105. scrollKey={`community_timeline-${columnId}`}
  106. type='community'
  107. emptyMessage={<FormattedMessage id='empty_column.community' defaultMessage='The local timeline is empty. Write something publicly to get the ball rolling!' />}
  108. />
  109. </Column>
  110. );
  111. }
  112. }
  113. export default connect(mapStateToProps)(injectIntl(CommunityTimeline));