闭社主体 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

  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. refreshCommunityTimeline,
  9. expandCommunityTimeline,
  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.community', defaultMessage: 'Local timeline' },
  22. });
  23. const mapStateToProps = state => ({
  24. hasUnread: state.getIn(['timelines', 'community', 'unread']) > 0,
  25. streamingAPIBaseURL: state.getIn(['meta', 'streaming_api_base_url']),
  26. accessToken: state.getIn(['meta', 'access_token']),
  27. });
  28. class CommunityTimeline extends React.PureComponent {
  29. static propTypes = {
  30. dispatch: PropTypes.func.isRequired,
  31. columnId: PropTypes.string,
  32. intl: PropTypes.object.isRequired,
  33. streamingAPIBaseURL: PropTypes.string.isRequired,
  34. accessToken: PropTypes.string.isRequired,
  35. hasUnread: PropTypes.bool,
  36. multiColumn: PropTypes.bool,
  37. };
  38. handlePin = () => {
  39. const { columnId, dispatch } = this.props;
  40. if (columnId) {
  41. dispatch(removeColumn(columnId));
  42. } else {
  43. dispatch(addColumn('COMMUNITY', {}));
  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(refreshCommunityTimeline());
  56. if (typeof this._subscription !== 'undefined') {
  57. return;
  58. }
  59. this._subscription = createStream(streamingAPIBaseURL, accessToken, 'public:local', {
  60. connected () {
  61. dispatch(connectTimeline('community'));
  62. },
  63. reconnected () {
  64. dispatch(connectTimeline('community'));
  65. },
  66. disconnected () {
  67. dispatch(disconnectTimeline('community'));
  68. },
  69. received (data) {
  70. switch(data.event) {
  71. case 'update':
  72. dispatch(updateTimeline('community', 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(expandCommunityTimeline());
  92. }
  93. render () {
  94. const { intl, hasUnread, columnId, multiColumn } = this.props;
  95. const pinned = !!columnId;
  96. return (
  97. <Column ref={this.setRef}>
  98. <ColumnHeader
  99. icon='users'
  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. trackScroll={!pinned}
  112. scrollKey={`community_timeline-${columnId}`}
  113. timelineId='community'
  114. loadMore={this.handleLoadMore}
  115. emptyMessage={<FormattedMessage id='empty_column.community' defaultMessage='The local timeline is empty. Write something publicly to get the ball rolling!' />}
  116. />
  117. </Column>
  118. );
  119. }
  120. }
  121. export default connect(mapStateToProps)(injectIntl(CommunityTimeline));