2017-05-03 03:04:16 +03:00
|
|
|
import React from 'react';
|
2016-12-03 22:04:57 +02:00
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
2016-10-19 00:06:28 +03:00
|
|
|
import { ScrollContainer } from 'react-router-scroll';
|
2017-04-21 21:05:35 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2016-12-03 22:04:57 +02:00
|
|
|
import StatusContainer from '../containers/status_container';
|
2017-01-30 19:04:15 +02:00
|
|
|
import LoadMore from './load_more';
|
2017-05-03 03:04:16 +03:00
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
2016-08-24 18:56:44 +03:00
|
|
|
|
2017-05-03 03:04:16 +03:00
|
|
|
class StatusList extends ImmutablePureComponent {
|
2017-04-21 21:05:35 +03:00
|
|
|
|
2017-05-12 15:44:10 +03:00
|
|
|
static propTypes = {
|
|
|
|
scrollKey: PropTypes.string.isRequired,
|
|
|
|
statusIds: ImmutablePropTypes.list.isRequired,
|
|
|
|
onScrollToBottom: PropTypes.func,
|
|
|
|
onScrollToTop: PropTypes.func,
|
|
|
|
onScroll: PropTypes.func,
|
|
|
|
shouldUpdateScroll: PropTypes.func,
|
|
|
|
isLoading: PropTypes.bool,
|
|
|
|
isUnread: PropTypes.bool,
|
|
|
|
hasMore: PropTypes.bool,
|
|
|
|
prepend: PropTypes.node,
|
2017-05-20 18:31:47 +03:00
|
|
|
emptyMessage: PropTypes.node,
|
2017-05-12 15:44:10 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static defaultProps = {
|
2017-05-20 18:31:47 +03:00
|
|
|
trackScroll: true,
|
2017-05-12 15:44:10 +03:00
|
|
|
};
|
|
|
|
|
2017-05-24 18:55:00 +03:00
|
|
|
state = {
|
|
|
|
isIntersecting: [{ }],
|
|
|
|
}
|
|
|
|
|
|
|
|
statusRefQueue = []
|
|
|
|
|
2017-05-12 15:44:10 +03:00
|
|
|
handleScroll = (e) => {
|
2016-09-22 02:08:35 +03:00
|
|
|
const { scrollTop, scrollHeight, clientHeight } = e.target;
|
2017-01-24 05:12:10 +02:00
|
|
|
const offset = scrollHeight - scrollTop - clientHeight;
|
2016-11-07 03:02:55 +02:00
|
|
|
this._oldScrollPosition = scrollHeight - scrollTop;
|
|
|
|
|
2017-01-24 05:12:10 +02:00
|
|
|
if (250 > offset && this.props.onScrollToBottom && !this.props.isLoading) {
|
2016-09-22 02:08:35 +03:00
|
|
|
this.props.onScrollToBottom();
|
2016-12-26 22:33:51 +02:00
|
|
|
} else if (scrollTop < 100 && this.props.onScrollToTop) {
|
2016-12-03 22:04:57 +02:00
|
|
|
this.props.onScrollToTop();
|
2016-12-26 22:33:51 +02:00
|
|
|
} else if (this.props.onScroll) {
|
2016-12-03 22:04:57 +02:00
|
|
|
this.props.onScroll();
|
2016-09-22 02:08:35 +03:00
|
|
|
}
|
2017-04-21 21:05:35 +03:00
|
|
|
}
|
2016-09-22 02:08:35 +03:00
|
|
|
|
2017-01-24 05:12:10 +02:00
|
|
|
componentDidMount () {
|
|
|
|
this.attachScrollListener();
|
2017-05-24 18:55:00 +03:00
|
|
|
this.attachIntersectionObserver();
|
2017-04-21 21:05:35 +03:00
|
|
|
}
|
2016-11-07 03:02:55 +02:00
|
|
|
|
2017-01-24 05:12:10 +02:00
|
|
|
componentDidUpdate (prevProps) {
|
2017-05-20 15:58:13 +03:00
|
|
|
if ((prevProps.statusIds.size < this.props.statusIds.size && prevProps.statusIds.first() !== this.props.statusIds.first() && !!this._oldScrollPosition) && this.node.scrollTop > 0) {
|
2017-01-24 05:12:10 +02:00
|
|
|
this.node.scrollTop = this.node.scrollHeight - this._oldScrollPosition;
|
2016-11-07 03:02:55 +02:00
|
|
|
}
|
2017-04-21 21:05:35 +03:00
|
|
|
}
|
2016-11-07 03:02:55 +02:00
|
|
|
|
2017-01-24 05:12:10 +02:00
|
|
|
componentWillUnmount () {
|
|
|
|
this.detachScrollListener();
|
2017-05-24 18:55:00 +03:00
|
|
|
this.detachIntersectionObserver();
|
|
|
|
}
|
|
|
|
|
|
|
|
attachIntersectionObserver () {
|
|
|
|
const onIntersection = (entries) => {
|
|
|
|
this.setState(state => {
|
|
|
|
const isIntersecting = { };
|
|
|
|
|
|
|
|
entries.forEach(entry => {
|
|
|
|
const statusId = entry.target.getAttribute('data-id');
|
|
|
|
|
|
|
|
state.isIntersecting[0][statusId] = entry.isIntersecting;
|
|
|
|
});
|
|
|
|
|
|
|
|
return { isIntersecting: [state.isIntersecting[0]] };
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
root: this.node,
|
|
|
|
rootMargin: '300% 0px',
|
|
|
|
};
|
|
|
|
|
|
|
|
this.intersectionObserver = new IntersectionObserver(onIntersection, options);
|
|
|
|
|
|
|
|
if (this.statusRefQueue.length) {
|
|
|
|
this.statusRefQueue.forEach(node => this.intersectionObserver.observe(node));
|
|
|
|
this.statusRefQueue = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
detachIntersectionObserver () {
|
|
|
|
this.intersectionObserver.disconnect();
|
2017-04-21 21:05:35 +03:00
|
|
|
}
|
2017-01-24 05:12:10 +02:00
|
|
|
|
|
|
|
attachScrollListener () {
|
|
|
|
this.node.addEventListener('scroll', this.handleScroll);
|
2017-04-21 21:05:35 +03:00
|
|
|
}
|
2017-01-24 05:12:10 +02:00
|
|
|
|
|
|
|
detachScrollListener () {
|
|
|
|
this.node.removeEventListener('scroll', this.handleScroll);
|
2017-04-21 21:05:35 +03:00
|
|
|
}
|
2017-01-24 05:12:10 +02:00
|
|
|
|
2017-05-12 15:44:10 +03:00
|
|
|
setRef = (c) => {
|
2017-01-24 05:12:10 +02:00
|
|
|
this.node = c;
|
2017-04-21 21:05:35 +03:00
|
|
|
}
|
2017-01-24 05:12:10 +02:00
|
|
|
|
2017-05-24 18:55:00 +03:00
|
|
|
handleStatusRef = (node) => {
|
|
|
|
if (node && this.intersectionObserver) {
|
|
|
|
const statusId = node.getAttribute('data-id');
|
|
|
|
this.intersectionObserver.observe(node);
|
|
|
|
} else {
|
|
|
|
this.statusRefQueue.push(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-12 15:44:10 +03:00
|
|
|
handleLoadMore = (e) => {
|
2017-01-30 19:04:15 +02:00
|
|
|
e.preventDefault();
|
|
|
|
this.props.onScrollToBottom();
|
2017-04-21 21:05:35 +03:00
|
|
|
}
|
2017-01-30 19:04:15 +02:00
|
|
|
|
2016-08-31 17:15:12 +03:00
|
|
|
render () {
|
2017-04-24 05:49:08 +03:00
|
|
|
const { statusIds, onScrollToBottom, scrollKey, shouldUpdateScroll, isLoading, isUnread, hasMore, prepend, emptyMessage } = this.props;
|
2017-05-24 18:55:00 +03:00
|
|
|
const isIntersecting = this.state.isIntersecting[0];
|
2017-01-30 19:04:15 +02:00
|
|
|
|
2017-05-24 18:55:00 +03:00
|
|
|
let loadMore = null;
|
|
|
|
let scrollableArea = null;
|
|
|
|
let unread = null;
|
2017-01-30 19:04:15 +02:00
|
|
|
|
2017-02-22 17:30:09 +02:00
|
|
|
if (!isLoading && statusIds.size > 0 && hasMore) {
|
2017-01-30 19:04:15 +02:00
|
|
|
loadMore = <LoadMore onClick={this.handleLoadMore} />;
|
|
|
|
}
|
2016-09-30 01:00:45 +03:00
|
|
|
|
2017-02-21 01:10:49 +02:00
|
|
|
if (isUnread) {
|
|
|
|
unread = <div className='status-list__unread-indicator' />;
|
|
|
|
}
|
|
|
|
|
2017-02-18 03:37:59 +02:00
|
|
|
if (isLoading || statusIds.size > 0 || !emptyMessage) {
|
|
|
|
scrollableArea = (
|
|
|
|
<div className='scrollable' ref={this.setRef}>
|
2017-02-21 01:10:49 +02:00
|
|
|
{unread}
|
|
|
|
|
2017-04-23 05:26:55 +03:00
|
|
|
<div className='status-list'>
|
2017-02-18 03:37:59 +02:00
|
|
|
{prepend}
|
2017-01-30 22:40:55 +02:00
|
|
|
|
2017-02-18 03:37:59 +02:00
|
|
|
{statusIds.map((statusId) => {
|
2017-05-24 18:55:00 +03:00
|
|
|
return <StatusContainer key={statusId} id={statusId} isIntersecting={isIntersecting[statusId]} onRef={this.handleStatusRef} />;
|
2017-02-18 03:37:59 +02:00
|
|
|
})}
|
2017-01-30 19:04:15 +02:00
|
|
|
|
2017-02-18 03:37:59 +02:00
|
|
|
{loadMore}
|
|
|
|
</div>
|
2016-08-24 18:56:44 +03:00
|
|
|
</div>
|
2017-02-18 03:37:59 +02:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
scrollableArea = (
|
|
|
|
<div className='empty-column-indicator' ref={this.setRef}>
|
|
|
|
{emptyMessage}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2016-10-19 00:06:28 +03:00
|
|
|
|
2017-04-24 05:49:08 +03:00
|
|
|
return (
|
|
|
|
<ScrollContainer scrollKey={scrollKey} shouldUpdateScroll={shouldUpdateScroll}>
|
|
|
|
{scrollableArea}
|
|
|
|
</ScrollContainer>
|
|
|
|
);
|
2016-08-24 18:56:44 +03:00
|
|
|
}
|
2016-08-31 17:15:12 +03:00
|
|
|
|
2017-04-21 21:05:35 +03:00
|
|
|
}
|
|
|
|
|
2016-08-24 18:56:44 +03:00
|
|
|
export default StatusList;
|