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
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.public', defaultMessage: 'Whole Known Network' }
  17. });
  18. const mapStateToProps = state => ({
  19. hasUnread: state.getIn(['timelines', 'public', 'unread']) > 0,
  20. accessToken: state.getIn(['meta', 'access_token'])
  21. });
  22. let subscription;
  23. const PublicTimeline = 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('public'));
  34. if (typeof subscription !== 'undefined') {
  35. return;
  36. }
  37. subscription = createStream(accessToken, 'public', {
  38. connected () {
  39. dispatch(connectTimeline('public'));
  40. },
  41. reconnected () {
  42. dispatch(connectTimeline('public'));
  43. },
  44. disconnected () {
  45. dispatch(disconnectTimeline('public'));
  46. },
  47. received (data) {
  48. switch(data.event) {
  49. case 'update':
  50. dispatch(updateTimeline('public', 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='globe' active={hasUnread} heading={intl.formatMessage(messages.title)}>
  69. <ColumnBackButtonSlim />
  70. <StatusListContainer type='public' 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' />} />
  71. </Column>
  72. );
  73. },
  74. });
  75. export default connect(mapStateToProps)(injectIntl(PublicTimeline));