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.

84 lines
2.0 KiB

  1. import Status from './status';
  2. import ImmutablePropTypes from 'react-immutable-proptypes';
  3. import PureRenderMixin from 'react-addons-pure-render-mixin';
  4. import { ScrollContainer } from 'react-router-scroll';
  5. import StatusContainer from '../containers/status_container';
  6. import moment from 'moment';
  7. const StatusList = React.createClass({
  8. propTypes: {
  9. statusIds: ImmutablePropTypes.list.isRequired,
  10. onScrollToBottom: React.PropTypes.func,
  11. trackScroll: React.PropTypes.bool
  12. },
  13. getDefaultProps () {
  14. return {
  15. trackScroll: true
  16. };
  17. },
  18. getInitialState () {
  19. return {
  20. now: moment()
  21. };
  22. },
  23. mixins: [PureRenderMixin],
  24. componentDidMount () {
  25. this._interval = setInterval(() => this.setState({ now: moment() }), 60000);
  26. },
  27. componentWillUnmount () {
  28. clearInterval(this._interval);
  29. },
  30. handleScroll (e) {
  31. const { scrollTop, scrollHeight, clientHeight } = e.target;
  32. this._oldScrollPosition = scrollHeight - scrollTop;
  33. if (scrollTop === scrollHeight - clientHeight) {
  34. this.props.onScrollToBottom();
  35. }
  36. },
  37. componentDidUpdate (prevProps) {
  38. if (prevProps.statusIds.size < this.props.statusIds.size && prevProps.statusIds.first() !== this.props.statusIds.first() && this._oldScrollPosition) {
  39. const node = ReactDOM.findDOMNode(this);
  40. if (node.scrollTop > 0) {
  41. node.scrollTop = node.scrollHeight - this._oldScrollPosition;
  42. }
  43. }
  44. },
  45. render () {
  46. const { statusIds, onScrollToBottom, trackScroll } = this.props;
  47. const scrollableArea = (
  48. <div className='scrollable' onScroll={this.handleScroll}>
  49. <div>
  50. {statusIds.map((statusId) => {
  51. return <StatusContainer key={statusId} id={statusId} now={this.state.now} />;
  52. })}
  53. </div>
  54. </div>
  55. );
  56. if (trackScroll) {
  57. return (
  58. <ScrollContainer scrollKey='status-list'>
  59. {scrollableArea}
  60. </ScrollContainer>
  61. );
  62. } else {
  63. return scrollableArea;
  64. }
  65. }
  66. });
  67. export default StatusList;