The code powering m.abunchtell.com https://m.abunchtell.com
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 rivejä
2.5 KiB

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