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