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.
 
 
 
 

141 lines
3.7 KiB

  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 '../../components/column';
  6. import ColumnHeader from '../../components/column_header';
  7. import {
  8. refreshHashtagTimeline,
  9. expandHashtagTimeline,
  10. updateTimeline,
  11. deleteFromTimelines,
  12. } from '../../actions/timelines';
  13. import { addColumn, removeColumn, moveColumn } from '../../actions/columns';
  14. import { FormattedMessage } from 'react-intl';
  15. import createStream from '../../stream';
  16. const mapStateToProps = state => ({
  17. hasUnread: state.getIn(['timelines', 'tag', 'unread']) > 0,
  18. streamingAPIBaseURL: state.getIn(['meta', 'streaming_api_base_url']),
  19. accessToken: state.getIn(['meta', 'access_token']),
  20. });
  21. class HashtagTimeline extends React.PureComponent {
  22. static propTypes = {
  23. params: PropTypes.object.isRequired,
  24. columnId: PropTypes.string,
  25. dispatch: PropTypes.func.isRequired,
  26. streamingAPIBaseURL: PropTypes.string.isRequired,
  27. accessToken: PropTypes.string.isRequired,
  28. hasUnread: PropTypes.bool,
  29. multiColumn: PropTypes.bool,
  30. };
  31. handlePin = () => {
  32. const { columnId, dispatch } = this.props;
  33. if (columnId) {
  34. dispatch(removeColumn(columnId));
  35. } else {
  36. dispatch(addColumn('HASHTAG', { id: this.props.params.id }));
  37. }
  38. }
  39. handleMove = (dir) => {
  40. const { columnId, dispatch } = this.props;
  41. dispatch(moveColumn(columnId, dir));
  42. }
  43. handleHeaderClick = () => {
  44. this.column.scrollTop();
  45. }
  46. _subscribe (dispatch, id) {
  47. const { streamingAPIBaseURL, accessToken } = this.props;
  48. this.subscription = createStream(streamingAPIBaseURL, accessToken, `hashtag&tag=${id}`, {
  49. received (data) {
  50. switch(data.event) {
  51. case 'update':
  52. dispatch(updateTimeline(`hashtag:${id}`, JSON.parse(data.payload)));
  53. break;
  54. case 'delete':
  55. dispatch(deleteFromTimelines(data.payload));
  56. break;
  57. }
  58. },
  59. });
  60. }
  61. _unsubscribe () {
  62. if (typeof this.subscription !== 'undefined') {
  63. this.subscription.close();
  64. this.subscription = null;
  65. }
  66. }
  67. componentDidMount () {
  68. const { dispatch } = this.props;
  69. const { id } = this.props.params;
  70. dispatch(refreshHashtagTimeline(id));
  71. this._subscribe(dispatch, id);
  72. }
  73. componentWillReceiveProps (nextProps) {
  74. if (nextProps.params.id !== this.props.params.id) {
  75. this.props.dispatch(refreshHashtagTimeline(nextProps.params.id));
  76. this._unsubscribe();
  77. this._subscribe(this.props.dispatch, nextProps.params.id);
  78. }
  79. }
  80. componentWillUnmount () {
  81. this._unsubscribe();
  82. }
  83. setRef = c => {
  84. this.column = c;
  85. }
  86. handleLoadMore = () => {
  87. this.props.dispatch(expandHashtagTimeline(this.props.params.id));
  88. }
  89. render () {
  90. const { hasUnread, columnId, multiColumn } = this.props;
  91. const { id } = this.props.params;
  92. const pinned = !!columnId;
  93. return (
  94. <Column ref={this.setRef}>
  95. <ColumnHeader
  96. icon='hashtag'
  97. active={hasUnread}
  98. title={id}
  99. onPin={this.handlePin}
  100. onMove={this.handleMove}
  101. onClick={this.handleHeaderClick}
  102. pinned={pinned}
  103. multiColumn={multiColumn}
  104. showBackButton
  105. />
  106. <StatusListContainer
  107. trackScroll={!pinned}
  108. scrollKey={`hashtag_timeline-${columnId}`}
  109. timelineId={`hashtag:${id}`}
  110. loadMore={this.handleLoadMore}
  111. emptyMessage={<FormattedMessage id='empty_column.hashtag' defaultMessage='There is nothing in this hashtag yet.' />}
  112. />
  113. </Column>
  114. );
  115. }
  116. }
  117. export default connect(mapStateToProps)(HashtagTimeline);