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.
 
 
 
 

94 lines
2.8 KiB

  1. import React from 'react';
  2. import { connect } from 'react-redux';
  3. import PropTypes from 'prop-types';
  4. import ImmutablePropTypes from 'react-immutable-proptypes';
  5. import { expandHashtagTimeline } from '../../../actions/timelines';
  6. import { connectHashtagStream } from '../../../actions/streaming';
  7. import Masonry from 'react-masonry-infinite';
  8. import { List as ImmutableList } from 'immutable';
  9. import DetailedStatusContainer from '../../status/containers/detailed_status_container';
  10. import { debounce } from 'lodash';
  11. import LoadingIndicator from '../../../components/loading_indicator';
  12. const mapStateToProps = (state, { hashtag }) => ({
  13. statusIds: state.getIn(['timelines', `hashtag:${hashtag}`, 'items'], ImmutableList()),
  14. isLoading: state.getIn(['timelines', `hashtag:${hashtag}`, 'isLoading'], false),
  15. hasMore: state.getIn(['timelines', `hashtag:${hashtag}`, 'hasMore'], false),
  16. });
  17. export default @connect(mapStateToProps)
  18. class HashtagTimeline extends React.PureComponent {
  19. static propTypes = {
  20. dispatch: PropTypes.func.isRequired,
  21. statusIds: ImmutablePropTypes.list.isRequired,
  22. isLoading: PropTypes.bool.isRequired,
  23. hasMore: PropTypes.bool.isRequired,
  24. hashtag: PropTypes.string.isRequired,
  25. };
  26. componentDidMount () {
  27. const { dispatch, hashtag } = this.props;
  28. dispatch(expandHashtagTimeline(hashtag));
  29. this.disconnect = dispatch(connectHashtagStream(hashtag, hashtag));
  30. }
  31. componentWillUnmount () {
  32. if (this.disconnect) {
  33. this.disconnect();
  34. this.disconnect = null;
  35. }
  36. }
  37. handleLoadMore = () => {
  38. const maxId = this.props.statusIds.last();
  39. if (maxId) {
  40. this.props.dispatch(expandHashtagTimeline(this.props.hashtag, { maxId }));
  41. }
  42. }
  43. setRef = c => {
  44. this.masonry = c;
  45. }
  46. handleHeightChange = debounce(() => {
  47. if (!this.masonry) {
  48. return;
  49. }
  50. this.masonry.forcePack();
  51. }, 50)
  52. render () {
  53. const { statusIds, hasMore, isLoading } = this.props;
  54. const sizes = [
  55. { columns: 1, gutter: 0 },
  56. { mq: '415px', columns: 1, gutter: 10 },
  57. { mq: '640px', columns: 2, gutter: 10 },
  58. { mq: '960px', columns: 3, gutter: 10 },
  59. { mq: '1255px', columns: 3, gutter: 10 },
  60. ];
  61. const loader = (isLoading && statusIds.isEmpty()) ? <LoadingIndicator key={0} /> : undefined;
  62. return (
  63. <Masonry ref={this.setRef} className='statuses-grid' hasMore={hasMore} loadMore={this.handleLoadMore} sizes={sizes} loader={loader}>
  64. {statusIds.map(statusId => (
  65. <div className='statuses-grid__item' key={statusId}>
  66. <DetailedStatusContainer
  67. id={statusId}
  68. showThread
  69. measureHeight
  70. onHeightChange={this.handleHeightChange}
  71. />
  72. </div>
  73. )).toArray()}
  74. </Masonry>
  75. );
  76. }
  77. }