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

97 lines
2.7 KiB

7 years ago
7 years ago
7 years ago
  1. import { connect } from 'react-redux';
  2. import PureRenderMixin from 'react-addons-pure-render-mixin';
  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. const CommunityTimeline = React.createClass({
  25. propTypes: {
  26. dispatch: React.PropTypes.func.isRequired,
  27. intl: React.PropTypes.object.isRequired,
  28. streamingAPIBaseURL: React.PropTypes.string.isRequired,
  29. accessToken: React.PropTypes.string.isRequired,
  30. hasUnread: React.PropTypes.bool
  31. },
  32. mixins: [PureRenderMixin],
  33. componentDidMount () {
  34. const { dispatch, streamingAPIBaseURL, accessToken } = this.props;
  35. dispatch(refreshTimeline('community'));
  36. if (typeof subscription !== 'undefined') {
  37. return;
  38. }
  39. subscription = createStream(streamingAPIBaseURL, accessToken, 'public:local', {
  40. connected () {
  41. dispatch(connectTimeline('community'));
  42. },
  43. reconnected () {
  44. dispatch(connectTimeline('community'));
  45. },
  46. disconnected () {
  47. dispatch(disconnectTimeline('community'));
  48. },
  49. received (data) {
  50. switch(data.event) {
  51. case 'update':
  52. dispatch(updateTimeline('community', JSON.parse(data.payload)));
  53. break;
  54. case 'delete':
  55. dispatch(deleteFromTimelines(data.payload));
  56. break;
  57. }
  58. }
  59. });
  60. },
  61. componentWillUnmount () {
  62. // if (typeof subscription !== 'undefined') {
  63. // subscription.close();
  64. // subscription = null;
  65. // }
  66. },
  67. render () {
  68. const { intl, hasUnread } = this.props;
  69. return (
  70. <Column icon='users' active={hasUnread} heading={intl.formatMessage(messages.title)}>
  71. <ColumnBackButtonSlim />
  72. <StatusListContainer type='community' emptyMessage={<FormattedMessage id='empty_column.community' defaultMessage='The local timeline is empty. Write something publicly to get the ball rolling!' />} />
  73. </Column>
  74. );
  75. },
  76. });
  77. export default connect(mapStateToProps)(injectIntl(CommunityTimeline));