2017-05-03 09:04:16 +09:00
|
|
|
import React from 'react';
|
2016-12-04 05:04:57 +09:00
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
2016-10-19 06:06:28 +09:00
|
|
|
import { ScrollContainer } from 'react-router-scroll';
|
2017-04-22 03:05:35 +09:00
|
|
|
import PropTypes from 'prop-types';
|
2016-12-04 05:04:57 +09:00
|
|
|
import StatusContainer from '../containers/status_container';
|
2017-01-31 02:04:15 +09:00
|
|
|
import LoadMore from './load_more';
|
2017-05-03 09:04:16 +09:00
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
2017-05-30 01:17:51 +09:00
|
|
|
import IntersectionObserverWrapper from '../features/ui/util/intersection_observer_wrapper';
|
2017-06-18 09:59:29 +09:00
|
|
|
import { debounce } from 'lodash';
|
2016-08-25 00:56:44 +09:00
|
|
|
|
2017-06-24 02:36:54 +09:00
|
|
|
export default class StatusList extends ImmutablePureComponent {
|
2017-04-22 03:05:35 +09:00
|
|
|
|
2017-05-12 21:44:10 +09:00
|
|
|
static propTypes = {
|
|
|
|
scrollKey: PropTypes.string.isRequired,
|
|
|
|
statusIds: ImmutablePropTypes.list.isRequired,
|
|
|
|
onScrollToBottom: PropTypes.func,
|
|
|
|
onScrollToTop: PropTypes.func,
|
|
|
|
onScroll: PropTypes.func,
|
2017-06-05 22:20:46 +09:00
|
|
|
trackScroll: PropTypes.bool,
|
2017-05-12 21:44:10 +09:00
|
|
|
shouldUpdateScroll: PropTypes.func,
|
|
|
|
isLoading: PropTypes.bool,
|
|
|
|
hasMore: PropTypes.bool,
|
|
|
|
prepend: PropTypes.node,
|
2017-05-21 00:31:47 +09:00
|
|
|
emptyMessage: PropTypes.node,
|
2017-05-12 21:44:10 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
static defaultProps = {
|
2017-05-21 00:31:47 +09:00
|
|
|
trackScroll: true,
|
2017-05-12 21:44:10 +09:00
|
|
|
};
|
|
|
|
|
2017-05-30 01:17:51 +09:00
|
|
|
intersectionObserverWrapper = new IntersectionObserverWrapper();
|
2017-05-25 00:55:00 +09:00
|
|
|
|
2017-06-18 09:59:29 +09:00
|
|
|
handleScroll = debounce((e) => {
|
2016-09-22 08:08:35 +09:00
|
|
|
const { scrollTop, scrollHeight, clientHeight } = e.target;
|
2017-01-24 12:12:10 +09:00
|
|
|
const offset = scrollHeight - scrollTop - clientHeight;
|
2016-11-07 10:02:55 +09:00
|
|
|
this._oldScrollPosition = scrollHeight - scrollTop;
|
|
|
|
|
2017-01-24 12:12:10 +09:00
|
|
|
if (250 > offset && this.props.onScrollToBottom && !this.props.isLoading) {
|
2016-09-22 08:08:35 +09:00
|
|
|
this.props.onScrollToBottom();
|
2016-12-27 05:33:51 +09:00
|
|
|
} else if (scrollTop < 100 && this.props.onScrollToTop) {
|
2016-12-04 05:04:57 +09:00
|
|
|
this.props.onScrollToTop();
|
2016-12-27 05:33:51 +09:00
|
|
|
} else if (this.props.onScroll) {
|
2016-12-04 05:04:57 +09:00
|
|
|
this.props.onScroll();
|
2016-09-22 08:08:35 +09:00
|
|
|
}
|
2017-06-18 09:59:29 +09:00
|
|
|
}, 200, {
|
|
|
|
trailing: true,
|
|
|
|
});
|
2016-09-22 08:08:35 +09:00
|
|
|
|
2017-01-24 12:12:10 +09:00
|
|
|
componentDidMount () {
|
|
|
|
this.attachScrollListener();
|
2017-05-25 00:55:00 +09:00
|
|
|
this.attachIntersectionObserver();
|
2017-04-22 03:05:35 +09:00
|
|
|
}
|
2016-11-07 10:02:55 +09:00
|
|
|
|
2017-01-24 12:12:10 +09:00
|
|
|
componentDidUpdate (prevProps) {
|
2017-06-18 09:59:29 +09:00
|
|
|
// Reset the scroll position when a new toot comes in in order not to
|
|
|
|
// jerk the scrollbar around if you're already scrolled down the page.
|
|
|
|
if (prevProps.statusIds.size < this.props.statusIds.size &&
|
|
|
|
prevProps.statusIds.first() !== this.props.statusIds.first() &&
|
|
|
|
this._oldScrollPosition &&
|
|
|
|
this.node.scrollTop > 0) {
|
|
|
|
let newScrollTop = this.node.scrollHeight - this._oldScrollPosition;
|
|
|
|
if (this.node.scrollTop !== newScrollTop) {
|
|
|
|
this.node.scrollTop = newScrollTop;
|
|
|
|
}
|
2016-11-07 10:02:55 +09:00
|
|
|
}
|
2017-04-22 03:05:35 +09:00
|
|
|
}
|
2016-11-07 10:02:55 +09:00
|
|
|
|
2017-01-24 12:12:10 +09:00
|
|
|
componentWillUnmount () {
|
|
|
|
this.detachScrollListener();
|
2017-05-25 00:55:00 +09:00
|
|
|
this.detachIntersectionObserver();
|
|
|
|
}
|
|
|
|
|
|
|
|
attachIntersectionObserver () {
|
2017-05-30 01:17:51 +09:00
|
|
|
this.intersectionObserverWrapper.connect({
|
2017-05-25 00:55:00 +09:00
|
|
|
root: this.node,
|
|
|
|
rootMargin: '300% 0px',
|
2017-05-30 01:17:51 +09:00
|
|
|
});
|
2017-05-25 00:55:00 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
detachIntersectionObserver () {
|
2017-05-30 01:17:51 +09:00
|
|
|
this.intersectionObserverWrapper.disconnect();
|
2017-04-22 03:05:35 +09:00
|
|
|
}
|
2017-01-24 12:12:10 +09:00
|
|
|
|
|
|
|
attachScrollListener () {
|
|
|
|
this.node.addEventListener('scroll', this.handleScroll);
|
2017-04-22 03:05:35 +09:00
|
|
|
}
|
2017-01-24 12:12:10 +09:00
|
|
|
|
|
|
|
detachScrollListener () {
|
|
|
|
this.node.removeEventListener('scroll', this.handleScroll);
|
2017-04-22 03:05:35 +09:00
|
|
|
}
|
2017-01-24 12:12:10 +09:00
|
|
|
|
2017-05-12 21:44:10 +09:00
|
|
|
setRef = (c) => {
|
2017-01-24 12:12:10 +09:00
|
|
|
this.node = c;
|
2017-04-22 03:05:35 +09:00
|
|
|
}
|
2017-01-24 12:12:10 +09:00
|
|
|
|
2017-05-12 21:44:10 +09:00
|
|
|
handleLoadMore = (e) => {
|
2017-01-31 02:04:15 +09:00
|
|
|
e.preventDefault();
|
|
|
|
this.props.onScrollToBottom();
|
2017-04-22 03:05:35 +09:00
|
|
|
}
|
2017-01-31 02:04:15 +09:00
|
|
|
|
2016-08-31 23:15:12 +09:00
|
|
|
render () {
|
2017-06-23 23:05:04 +09:00
|
|
|
const { statusIds, scrollKey, trackScroll, shouldUpdateScroll, isLoading, hasMore, prepend, emptyMessage } = this.props;
|
2017-01-31 02:04:15 +09:00
|
|
|
|
2017-05-25 00:55:00 +09:00
|
|
|
let loadMore = null;
|
|
|
|
let scrollableArea = null;
|
2017-01-31 02:04:15 +09:00
|
|
|
|
2017-02-23 00:30:09 +09:00
|
|
|
if (!isLoading && statusIds.size > 0 && hasMore) {
|
2017-01-31 02:04:15 +09:00
|
|
|
loadMore = <LoadMore onClick={this.handleLoadMore} />;
|
|
|
|
}
|
2016-09-30 07:00:45 +09:00
|
|
|
|
2017-02-18 10:37:59 +09:00
|
|
|
if (isLoading || statusIds.size > 0 || !emptyMessage) {
|
|
|
|
scrollableArea = (
|
|
|
|
<div className='scrollable' ref={this.setRef}>
|
2017-04-23 11:26:55 +09:00
|
|
|
<div className='status-list'>
|
2017-02-18 10:37:59 +09:00
|
|
|
{prepend}
|
2017-01-31 05:40:55 +09:00
|
|
|
|
2017-02-18 10:37:59 +09:00
|
|
|
{statusIds.map((statusId) => {
|
2017-05-30 01:17:51 +09:00
|
|
|
return <StatusContainer key={statusId} id={statusId} intersectionObserverWrapper={this.intersectionObserverWrapper} />;
|
2017-02-18 10:37:59 +09:00
|
|
|
})}
|
2017-01-31 02:04:15 +09:00
|
|
|
|
2017-02-18 10:37:59 +09:00
|
|
|
{loadMore}
|
|
|
|
</div>
|
2016-08-25 00:56:44 +09:00
|
|
|
</div>
|
2017-02-18 10:37:59 +09:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
scrollableArea = (
|
|
|
|
<div className='empty-column-indicator' ref={this.setRef}>
|
|
|
|
{emptyMessage}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2016-10-19 06:06:28 +09:00
|
|
|
|
2017-06-05 22:20:46 +09:00
|
|
|
if (trackScroll) {
|
|
|
|
return (
|
|
|
|
<ScrollContainer scrollKey={scrollKey} shouldUpdateScroll={shouldUpdateScroll}>
|
|
|
|
{scrollableArea}
|
|
|
|
</ScrollContainer>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return scrollableArea;
|
|
|
|
}
|
2016-08-25 00:56:44 +09:00
|
|
|
}
|
2016-08-31 23:15:12 +09:00
|
|
|
|
2017-04-22 03:05:35 +09:00
|
|
|
}
|