2017-05-03 02:04:16 +02:00
|
|
|
import React from 'react';
|
2016-12-03 21:04:57 +01:00
|
|
|
import Status from './status';
|
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
2016-10-18 23:06:28 +02:00
|
|
|
import { ScrollContainer } from 'react-router-scroll';
|
2017-04-21 20:05:35 +02:00
|
|
|
import PropTypes from 'prop-types';
|
2016-12-03 21:04:57 +01:00
|
|
|
import StatusContainer from '../containers/status_container';
|
2017-01-30 18:04:15 +01:00
|
|
|
import LoadMore from './load_more';
|
2017-05-03 02:04:16 +02:00
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
2016-08-24 17:56:44 +02:00
|
|
|
|
2017-05-03 02:04:16 +02:00
|
|
|
class StatusList extends ImmutablePureComponent {
|
2017-04-21 20:05:35 +02:00
|
|
|
|
|
|
|
constructor (props, context) {
|
|
|
|
super(props, context);
|
|
|
|
this.handleScroll = this.handleScroll.bind(this);
|
|
|
|
this.setRef = this.setRef.bind(this);
|
|
|
|
this.handleLoadMore = this.handleLoadMore.bind(this);
|
|
|
|
}
|
2016-08-31 16:15:12 +02:00
|
|
|
|
2016-09-22 01:08:35 +02:00
|
|
|
handleScroll (e) {
|
|
|
|
const { scrollTop, scrollHeight, clientHeight } = e.target;
|
2017-01-24 04:12:10 +01:00
|
|
|
const offset = scrollHeight - scrollTop - clientHeight;
|
2016-11-07 02:02:55 +01:00
|
|
|
this._oldScrollPosition = scrollHeight - scrollTop;
|
|
|
|
|
2017-01-24 04:12:10 +01:00
|
|
|
if (250 > offset && this.props.onScrollToBottom && !this.props.isLoading) {
|
2016-09-22 01:08:35 +02:00
|
|
|
this.props.onScrollToBottom();
|
2016-12-26 21:33:51 +01:00
|
|
|
} else if (scrollTop < 100 && this.props.onScrollToTop) {
|
2016-12-03 21:04:57 +01:00
|
|
|
this.props.onScrollToTop();
|
2016-12-26 21:33:51 +01:00
|
|
|
} else if (this.props.onScroll) {
|
2016-12-03 21:04:57 +01:00
|
|
|
this.props.onScroll();
|
2016-09-22 01:08:35 +02:00
|
|
|
}
|
2017-04-21 20:05:35 +02:00
|
|
|
}
|
2016-09-22 01:08:35 +02:00
|
|
|
|
2017-01-24 04:12:10 +01:00
|
|
|
componentDidMount () {
|
|
|
|
this.attachScrollListener();
|
2017-04-21 20:05:35 +02:00
|
|
|
}
|
2016-11-07 02:02:55 +01:00
|
|
|
|
2017-01-24 04:12:10 +01:00
|
|
|
componentDidUpdate (prevProps) {
|
|
|
|
if (this.node.scrollTop > 0 && (prevProps.statusIds.size < this.props.statusIds.size && prevProps.statusIds.first() !== this.props.statusIds.first() && !!this._oldScrollPosition)) {
|
|
|
|
this.node.scrollTop = this.node.scrollHeight - this._oldScrollPosition;
|
2016-11-07 02:02:55 +01:00
|
|
|
}
|
2017-04-21 20:05:35 +02:00
|
|
|
}
|
2016-11-07 02:02:55 +01:00
|
|
|
|
2017-01-24 04:12:10 +01:00
|
|
|
componentWillUnmount () {
|
|
|
|
this.detachScrollListener();
|
2017-04-21 20:05:35 +02:00
|
|
|
}
|
2017-01-24 04:12:10 +01:00
|
|
|
|
|
|
|
attachScrollListener () {
|
|
|
|
this.node.addEventListener('scroll', this.handleScroll);
|
2017-04-21 20:05:35 +02:00
|
|
|
}
|
2017-01-24 04:12:10 +01:00
|
|
|
|
|
|
|
detachScrollListener () {
|
|
|
|
this.node.removeEventListener('scroll', this.handleScroll);
|
2017-04-21 20:05:35 +02:00
|
|
|
}
|
2017-01-24 04:12:10 +01:00
|
|
|
|
|
|
|
setRef (c) {
|
|
|
|
this.node = c;
|
2017-04-21 20:05:35 +02:00
|
|
|
}
|
2017-01-24 04:12:10 +01:00
|
|
|
|
2017-01-30 18:04:15 +01:00
|
|
|
handleLoadMore (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.props.onScrollToBottom();
|
2017-04-21 20:05:35 +02:00
|
|
|
}
|
2017-01-30 18:04:15 +01:00
|
|
|
|
2016-08-31 16:15:12 +02:00
|
|
|
render () {
|
2017-04-24 04:49:08 +02:00
|
|
|
const { statusIds, onScrollToBottom, scrollKey, shouldUpdateScroll, isLoading, isUnread, hasMore, prepend, emptyMessage } = this.props;
|
2017-01-30 18:04:15 +01:00
|
|
|
|
2017-02-21 00:10:49 +01:00
|
|
|
let loadMore = '';
|
2017-02-18 02:37:59 +01:00
|
|
|
let scrollableArea = '';
|
2017-02-21 00:10:49 +01:00
|
|
|
let unread = '';
|
2017-01-30 18:04:15 +01:00
|
|
|
|
2017-02-22 16:30:09 +01:00
|
|
|
if (!isLoading && statusIds.size > 0 && hasMore) {
|
2017-01-30 18:04:15 +01:00
|
|
|
loadMore = <LoadMore onClick={this.handleLoadMore} />;
|
|
|
|
}
|
2016-09-30 00:00:45 +02:00
|
|
|
|
2017-02-21 00:10:49 +01:00
|
|
|
if (isUnread) {
|
|
|
|
unread = <div className='status-list__unread-indicator' />;
|
|
|
|
}
|
|
|
|
|
2017-02-18 02:37:59 +01:00
|
|
|
if (isLoading || statusIds.size > 0 || !emptyMessage) {
|
|
|
|
scrollableArea = (
|
|
|
|
<div className='scrollable' ref={this.setRef}>
|
2017-02-21 00:10:49 +01:00
|
|
|
{unread}
|
|
|
|
|
2017-04-23 04:26:55 +02:00
|
|
|
<div className='status-list'>
|
2017-02-18 02:37:59 +01:00
|
|
|
{prepend}
|
2017-01-30 21:40:55 +01:00
|
|
|
|
2017-02-18 02:37:59 +01:00
|
|
|
{statusIds.map((statusId) => {
|
|
|
|
return <StatusContainer key={statusId} id={statusId} />;
|
|
|
|
})}
|
2017-01-30 18:04:15 +01:00
|
|
|
|
2017-02-18 02:37:59 +01:00
|
|
|
{loadMore}
|
|
|
|
</div>
|
2016-08-24 17:56:44 +02:00
|
|
|
</div>
|
2017-02-18 02:37:59 +01:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
scrollableArea = (
|
|
|
|
<div className='empty-column-indicator' ref={this.setRef}>
|
|
|
|
{emptyMessage}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2016-10-18 23:06:28 +02:00
|
|
|
|
2017-04-24 04:49:08 +02:00
|
|
|
return (
|
|
|
|
<ScrollContainer scrollKey={scrollKey} shouldUpdateScroll={shouldUpdateScroll}>
|
|
|
|
{scrollableArea}
|
|
|
|
</ScrollContainer>
|
|
|
|
);
|
2016-08-24 17:56:44 +02:00
|
|
|
}
|
2016-08-31 16:15:12 +02:00
|
|
|
|
2017-04-21 20:05:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
StatusList.propTypes = {
|
2017-04-24 04:49:08 +02:00
|
|
|
scrollKey: PropTypes.string.isRequired,
|
2017-04-21 20:05:35 +02:00
|
|
|
statusIds: ImmutablePropTypes.list.isRequired,
|
|
|
|
onScrollToBottom: PropTypes.func,
|
|
|
|
onScrollToTop: PropTypes.func,
|
|
|
|
onScroll: PropTypes.func,
|
2017-04-24 04:49:08 +02:00
|
|
|
shouldUpdateScroll: PropTypes.func,
|
2017-04-21 20:05:35 +02:00
|
|
|
isLoading: PropTypes.bool,
|
|
|
|
isUnread: PropTypes.bool,
|
|
|
|
hasMore: PropTypes.bool,
|
|
|
|
prepend: PropTypes.node,
|
|
|
|
emptyMessage: PropTypes.node
|
|
|
|
};
|
|
|
|
|
|
|
|
StatusList.defaultProps = {
|
|
|
|
trackScroll: true
|
|
|
|
};
|
2016-08-24 17:56:44 +02:00
|
|
|
|
|
|
|
export default StatusList;
|