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
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.public', defaultMessage: 'Federated timeline' }
  17. });
  18. const mapStateToProps = state => ({
  19. hasUnread: state.getIn(['timelines', 'public', 'unread']) > 0,
  20. streamingAPIBaseURL: state.getIn(['meta', 'streaming_api_base_url']),
  21. accessToken: state.getIn(['meta', 'access_token'])
  22. });
  23. let subscription;
  24. class PublicTimeline extends React.PureComponent {
  25. componentDidMount () {
  26. const { dispatch, streamingAPIBaseURL, accessToken } = this.props;
  27. dispatch(refreshTimeline('public'));
  28. if (typeof subscription !== 'undefined') {
  29. return;
  30. }
  31. subscription = createStream(streamingAPIBaseURL, accessToken, 'public', {
  32. connected () {
  33. dispatch(connectTimeline('public'));
  34. },
  35. reconnected () {
  36. dispatch(connectTimeline('public'));
  37. },
  38. disconnected () {
  39. dispatch(disconnectTimeline('public'));
  40. },
  41. received (data) {
  42. switch(data.event) {
  43. case 'update':
  44. dispatch(updateTimeline('public', 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='globe' active={hasUnread} heading={intl.formatMessage(messages.title)}>
  63. <ColumnBackButtonSlim />
  64. <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' />} />
  65. </Column>
  66. );
  67. }
  68. }
  69. PublicTimeline.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(PublicTimeline));