The code powering m.abunchtell.com https://m.abunchtell.com
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

137 行
3.6 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. refreshTimeline,
  9. updateTimeline,
  10. deleteFromTimelines,
  11. } from '../../actions/timelines';
  12. import { addColumn, removeColumn, moveColumn } from '../../actions/columns';
  13. import ColumnBackButtonSlim from '../../components/column_back_button_slim';
  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('tag', 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(refreshTimeline('tag', id));
  71. this._subscribe(dispatch, id);
  72. }
  73. componentWillReceiveProps (nextProps) {
  74. if (nextProps.params.id !== this.props.params.id) {
  75. this.props.dispatch(refreshTimeline('tag', 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. render () {
  87. const { hasUnread, columnId, multiColumn } = this.props;
  88. const { id } = this.props.params;
  89. const pinned = !!columnId;
  90. return (
  91. <Column ref={this.setRef}>
  92. <ColumnHeader
  93. icon='hashtag'
  94. active={hasUnread}
  95. title={id}
  96. onPin={this.handlePin}
  97. onMove={this.handleMove}
  98. onClick={this.handleHeaderClick}
  99. pinned={pinned}
  100. multiColumn={multiColumn}
  101. showBackButton
  102. />
  103. <StatusListContainer
  104. trackScroll={!pinned}
  105. scrollKey={`hashtag_timeline-${columnId}`}
  106. type='tag'
  107. id={id}
  108. emptyMessage={<FormattedMessage id='empty_column.hashtag' defaultMessage='There is nothing in this hashtag yet.' />}
  109. />
  110. </Column>
  111. );
  112. }
  113. }
  114. export default connect(mapStateToProps)(HashtagTimeline);