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

95 lines
2.7 KiB

7 years ago
7 years ago
  1. import { connect } from 'react-redux';
  2. import PropTypes from 'prop-types';
  3. import StatusListContainer from '../ui/containers/status_list_container';
  4. import Column from '../ui/components/column';
  5. import {
  6. refreshTimeline,
  7. updateTimeline,
  8. deleteFromTimelines,
  9. connectTimeline,
  10. disconnectTimeline
  11. } from '../../actions/timelines';
  12. import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
  13. import ColumnBackButtonSlim from '../../components/column_back_button_slim';
  14. import createStream from '../../stream';
  15. const messages = defineMessages({
  16. title: { id: 'column.community', defaultMessage: 'Local timeline' }
  17. });
  18. const mapStateToProps = state => ({
  19. hasUnread: state.getIn(['timelines', 'community', 'unread']) > 0,
  20. streamingAPIBaseURL: state.getIn(['meta', 'streaming_api_base_url']),
  21. accessToken: state.getIn(['meta', 'access_token'])
  22. });
  23. let subscription;
  24. class CommunityTimeline extends React.PureComponent {
  25. componentDidMount () {
  26. const { dispatch, streamingAPIBaseURL, accessToken } = this.props;
  27. dispatch(refreshTimeline('community'));
  28. if (typeof subscription !== 'undefined') {
  29. return;
  30. }
  31. subscription = createStream(streamingAPIBaseURL, accessToken, 'public:local', {
  32. connected () {
  33. dispatch(connectTimeline('community'));
  34. },
  35. reconnected () {
  36. dispatch(connectTimeline('community'));
  37. },
  38. disconnected () {
  39. dispatch(disconnectTimeline('community'));
  40. },
  41. received (data) {
  42. switch(data.event) {
  43. case 'update':
  44. dispatch(updateTimeline('community', JSON.parse(data.payload)));
  45. break;
  46. case 'delete':
  47. dispatch(deleteFromTimelines(data.payload));
  48. break;
  49. }
  50. }
  51. });
  52. }
  53. componentWillUnmount () {
  54. // if (typeof subscription !== 'undefined') {
  55. // subscription.close();
  56. // subscription = null;
  57. // }
  58. }
  59. render () {
  60. const { intl, hasUnread } = this.props;
  61. return (
  62. <Column icon='users' active={hasUnread} heading={intl.formatMessage(messages.title)}>
  63. <ColumnBackButtonSlim />
  64. <StatusListContainer {...this.props} scrollKey='community_timeline' type='community' emptyMessage={<FormattedMessage id='empty_column.community' defaultMessage='The local timeline is empty. Write something publicly to get the ball rolling!' />} />
  65. </Column>
  66. );
  67. }
  68. }
  69. CommunityTimeline.propTypes = {
  70. dispatch: PropTypes.func.isRequired,
  71. intl: PropTypes.object.isRequired,
  72. streamingAPIBaseURL: PropTypes.string.isRequired,
  73. accessToken: PropTypes.string.isRequired,
  74. hasUnread: PropTypes.bool
  75. };
  76. export default connect(mapStateToProps)(injectIntl(CommunityTimeline));