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.

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