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

96 lines
2.7 KiB

7 years ago
7 years ago
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 '../ui/components/column';
  6. import {
  7. refreshTimeline,
  8. updateTimeline,
  9. deleteFromTimelines,
  10. connectTimeline,
  11. disconnectTimeline,
  12. } from '../../actions/timelines';
  13. import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
  14. import ColumnBackButtonSlim from '../../components/column_back_button_slim';
  15. import createStream from '../../stream';
  16. const messages = defineMessages({
  17. title: { id: 'column.public', defaultMessage: 'Federated timeline' },
  18. });
  19. const mapStateToProps = state => ({
  20. hasUnread: state.getIn(['timelines', 'public', 'unread']) > 0,
  21. streamingAPIBaseURL: state.getIn(['meta', 'streaming_api_base_url']),
  22. accessToken: state.getIn(['meta', 'access_token']),
  23. });
  24. let subscription;
  25. class PublicTimeline extends React.PureComponent {
  26. static propTypes = {
  27. dispatch: PropTypes.func.isRequired,
  28. intl: PropTypes.object.isRequired,
  29. streamingAPIBaseURL: PropTypes.string.isRequired,
  30. accessToken: PropTypes.string.isRequired,
  31. hasUnread: PropTypes.bool,
  32. };
  33. componentDidMount () {
  34. const { dispatch, streamingAPIBaseURL, accessToken } = this.props;
  35. dispatch(refreshTimeline('public'));
  36. if (typeof subscription !== 'undefined') {
  37. return;
  38. }
  39. subscription = createStream(streamingAPIBaseURL, accessToken, 'public', {
  40. connected () {
  41. dispatch(connectTimeline('public'));
  42. },
  43. reconnected () {
  44. dispatch(connectTimeline('public'));
  45. },
  46. disconnected () {
  47. dispatch(disconnectTimeline('public'));
  48. },
  49. received (data) {
  50. switch(data.event) {
  51. case 'update':
  52. dispatch(updateTimeline('public', 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='globe' active={hasUnread} heading={intl.formatMessage(messages.title)}>
  71. <ColumnBackButtonSlim />
  72. <StatusListContainer {...this.props} type='public' scrollKey='public_timeline' 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' />} />
  73. </Column>
  74. );
  75. }
  76. }
  77. export default connect(mapStateToProps)(injectIntl(PublicTimeline));