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.

90 lines
2.5 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
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. } from '../../actions/timelines';
  11. import ColumnBackButtonSlim from '../../components/column_back_button_slim';
  12. import { FormattedMessage } from 'react-intl';
  13. import createStream from '../../stream';
  14. const mapStateToProps = state => ({
  15. hasUnread: state.getIn(['timelines', 'tag', 'unread']) > 0,
  16. streamingAPIBaseURL: state.getIn(['meta', 'streaming_api_base_url']),
  17. accessToken: state.getIn(['meta', 'access_token'])
  18. });
  19. class HashtagTimeline extends React.PureComponent {
  20. static propTypes = {
  21. params: PropTypes.object.isRequired,
  22. dispatch: PropTypes.func.isRequired,
  23. streamingAPIBaseURL: PropTypes.string.isRequired,
  24. accessToken: PropTypes.string.isRequired,
  25. hasUnread: PropTypes.bool
  26. };
  27. _subscribe (dispatch, id) {
  28. const { streamingAPIBaseURL, accessToken } = this.props;
  29. this.subscription = createStream(streamingAPIBaseURL, accessToken, `hashtag&tag=${id}`, {
  30. received (data) {
  31. switch(data.event) {
  32. case 'update':
  33. dispatch(updateTimeline('tag', JSON.parse(data.payload)));
  34. break;
  35. case 'delete':
  36. dispatch(deleteFromTimelines(data.payload));
  37. break;
  38. }
  39. }
  40. });
  41. }
  42. _unsubscribe () {
  43. if (typeof this.subscription !== 'undefined') {
  44. this.subscription.close();
  45. this.subscription = null;
  46. }
  47. }
  48. componentDidMount () {
  49. const { dispatch } = this.props;
  50. const { id } = this.props.params;
  51. dispatch(refreshTimeline('tag', id));
  52. this._subscribe(dispatch, id);
  53. }
  54. componentWillReceiveProps (nextProps) {
  55. if (nextProps.params.id !== this.props.params.id) {
  56. this.props.dispatch(refreshTimeline('tag', nextProps.params.id));
  57. this._unsubscribe();
  58. this._subscribe(this.props.dispatch, nextProps.params.id);
  59. }
  60. }
  61. componentWillUnmount () {
  62. this._unsubscribe();
  63. }
  64. render () {
  65. const { id, hasUnread } = this.props.params;
  66. return (
  67. <Column icon='hashtag' active={hasUnread} heading={id}>
  68. <ColumnBackButtonSlim />
  69. <StatusListContainer scrollKey='hashtag_timeline' type='tag' id={id} emptyMessage={<FormattedMessage id='empty_column.hashtag' defaultMessage='There is nothing in this hashtag yet.' />} />
  70. </Column>
  71. );
  72. }
  73. }
  74. export default connect(mapStateToProps)(HashtagTimeline);