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.

81 lines
2.2 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. } from '../../actions/timelines';
  10. import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
  11. import ColumnBackButtonSlim from '../../components/column_back_button_slim';
  12. import createStream from '../../stream';
  13. const messages = defineMessages({
  14. title: { id: 'column.community', defaultMessage: 'Local' }
  15. });
  16. const mapStateToProps = state => ({
  17. hasUnread: state.getIn(['timelines', 'community', 'unread']) > 0,
  18. accessToken: state.getIn(['meta', 'access_token'])
  19. });
  20. let subscription;
  21. const CommunityTimeline = React.createClass({
  22. propTypes: {
  23. dispatch: React.PropTypes.func.isRequired,
  24. intl: React.PropTypes.object.isRequired,
  25. accessToken: React.PropTypes.string.isRequired,
  26. hasUnread: React.PropTypes.bool
  27. },
  28. mixins: [PureRenderMixin],
  29. componentDidMount () {
  30. const { dispatch, accessToken } = this.props;
  31. dispatch(refreshTimeline('community'));
  32. if (typeof subscription !== 'undefined') {
  33. return;
  34. }
  35. subscription = createStream(accessToken, 'public:local', {
  36. received (data) {
  37. switch(data.event) {
  38. case 'update':
  39. dispatch(updateTimeline('community', JSON.parse(data.payload)));
  40. break;
  41. case 'delete':
  42. dispatch(deleteFromTimelines(data.payload));
  43. break;
  44. }
  45. }
  46. });
  47. },
  48. componentWillUnmount () {
  49. // if (typeof subscription !== 'undefined') {
  50. // subscription.close();
  51. // subscription = null;
  52. // }
  53. },
  54. render () {
  55. const { intl, hasUnread } = this.props;
  56. return (
  57. <Column icon='users' active={hasUnread} heading={intl.formatMessage(messages.title)}>
  58. <ColumnBackButtonSlim />
  59. <StatusListContainer type='community' emptyMessage={<FormattedMessage id='empty_column.community' defaultMessage='The local timeline is empty. Write something publicly to get the ball rolling!' />} />
  60. </Column>
  61. );
  62. },
  63. });
  64. export default connect(mapStateToProps)(injectIntl(CommunityTimeline));