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.

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