闭社主体 forked from https://github.com/tootsuite/mastodon
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.

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