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.

145 lines
3.8 KiB

7 years ago
  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. refreshPublicTimeline,
  9. expandPublicTimeline,
  10. updateTimeline,
  11. deleteFromTimelines,
  12. connectTimeline,
  13. disconnectTimeline,
  14. } from '../../actions/timelines';
  15. import { addColumn, removeColumn, moveColumn } from '../../actions/columns';
  16. import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
  17. import ColumnSettingsContainer from './containers/column_settings_container';
  18. import createStream from '../../stream';
  19. const messages = defineMessages({
  20. title: { id: 'column.public', defaultMessage: 'Federated timeline' },
  21. });
  22. const mapStateToProps = state => ({
  23. hasUnread: state.getIn(['timelines', 'public', 'unread']) > 0,
  24. streamingAPIBaseURL: state.getIn(['meta', 'streaming_api_base_url']),
  25. accessToken: state.getIn(['meta', 'access_token']),
  26. });
  27. @connect(mapStateToProps)
  28. @injectIntl
  29. export default class PublicTimeline extends React.PureComponent {
  30. static propTypes = {
  31. dispatch: PropTypes.func.isRequired,
  32. intl: PropTypes.object.isRequired,
  33. columnId: PropTypes.string,
  34. multiColumn: PropTypes.bool,
  35. streamingAPIBaseURL: PropTypes.string.isRequired,
  36. accessToken: PropTypes.string.isRequired,
  37. hasUnread: PropTypes.bool,
  38. };
  39. handlePin = () => {
  40. const { columnId, dispatch } = this.props;
  41. if (columnId) {
  42. dispatch(removeColumn(columnId));
  43. } else {
  44. dispatch(addColumn('PUBLIC', {}));
  45. }
  46. }
  47. handleMove = (dir) => {
  48. const { columnId, dispatch } = this.props;
  49. dispatch(moveColumn(columnId, dir));
  50. }
  51. handleHeaderClick = () => {
  52. this.column.scrollTop();
  53. }
  54. componentDidMount () {
  55. const { dispatch, streamingAPIBaseURL, accessToken } = this.props;
  56. dispatch(refreshPublicTimeline());
  57. if (typeof this._subscription !== 'undefined') {
  58. return;
  59. }
  60. this._subscription = createStream(streamingAPIBaseURL, accessToken, 'public', {
  61. connected () {
  62. dispatch(connectTimeline('public'));
  63. },
  64. reconnected () {
  65. dispatch(connectTimeline('public'));
  66. },
  67. disconnected () {
  68. dispatch(disconnectTimeline('public'));
  69. },
  70. received (data) {
  71. switch(data.event) {
  72. case 'update':
  73. dispatch(updateTimeline('public', JSON.parse(data.payload)));
  74. break;
  75. case 'delete':
  76. dispatch(deleteFromTimelines(data.payload));
  77. break;
  78. }
  79. },
  80. });
  81. }
  82. componentWillUnmount () {
  83. if (typeof this._subscription !== 'undefined') {
  84. this._subscription.close();
  85. this._subscription = null;
  86. }
  87. }
  88. setRef = c => {
  89. this.column = c;
  90. }
  91. handleLoadMore = () => {
  92. this.props.dispatch(expandPublicTimeline());
  93. }
  94. render () {
  95. const { intl, columnId, hasUnread, multiColumn } = this.props;
  96. const pinned = !!columnId;
  97. return (
  98. <Column ref={this.setRef}>
  99. <ColumnHeader
  100. icon='globe'
  101. active={hasUnread}
  102. title={intl.formatMessage(messages.title)}
  103. onPin={this.handlePin}
  104. onMove={this.handleMove}
  105. onClick={this.handleHeaderClick}
  106. pinned={pinned}
  107. multiColumn={multiColumn}
  108. >
  109. <ColumnSettingsContainer />
  110. </ColumnHeader>
  111. <StatusListContainer
  112. timelineId='public'
  113. loadMore={this.handleLoadMore}
  114. trackScroll={!pinned}
  115. scrollKey={`public_timeline-${columnId}`}
  116. emptyMessage={<FormattedMessage id='empty_column.public' defaultMessage='There is nothing here! Write something publicly, or manually follow users from other instances to fill it up' />}
  117. />
  118. </Column>
  119. );
  120. }
  121. }