2017-05-03 10:04:16 +10:00
|
|
|
import React from 'react';
|
2016-12-04 07:04:57 +11:00
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
2016-10-19 08:06:28 +11:00
|
|
|
import { ScrollContainer } from 'react-router-scroll';
|
2017-04-22 04:05:35 +10:00
|
|
|
import PropTypes from 'prop-types';
|
2016-12-04 07:04:57 +11:00
|
|
|
import StatusContainer from '../containers/status_container';
|
2017-01-31 04:04:15 +11:00
|
|
|
import LoadMore from './load_more';
|
2017-05-03 10:04:16 +10:00
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
2017-05-30 02:17:51 +10:00
|
|
|
import IntersectionObserverWrapper from '../features/ui/util/intersection_observer_wrapper';
|
2017-07-27 07:35:03 +10:00
|
|
|
import { throttle } from 'lodash';
|
2016-08-25 01:56:44 +10:00
|
|
|
|
2017-06-24 03:36:54 +10:00
|
|
|
export default class StatusList extends ImmutablePureComponent {
|
2017-04-22 04:05:35 +10:00
|
|
|
|
2017-05-12 22:44:10 +10:00
|
|
|
static propTypes = {
|
|
|
|
scrollKey: PropTypes.string.isRequired,
|
|
|
|
statusIds: ImmutablePropTypes.list.isRequired,
|
|
|
|
onScrollToBottom: PropTypes.func,
|
|
|
|
onScrollToTop: PropTypes.func,
|
|
|
|
onScroll: PropTypes.func,
|
2017-06-05 23:20:46 +10:00
|
|
|
trackScroll: PropTypes.bool,
|
2017-05-12 22:44:10 +10:00
|
|
|
shouldUpdateScroll: PropTypes.func,
|
|
|
|
isLoading: PropTypes.bool,
|
|
|
|
hasMore: PropTypes.bool,
|
|
|
|
prepend: PropTypes.node,
|
2017-05-21 01:31:47 +10:00
|
|
|
emptyMessage: PropTypes.node,
|
2017-05-12 22:44:10 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
static defaultProps = {
|
2017-05-21 01:31:47 +10:00
|
|
|
trackScroll: true,
|
2017-05-12 22:44:10 +10:00
|
|
|
};
|
|
|
|
|
2017-05-30 02:17:51 +10:00
|
|
|
intersectionObserverWrapper = new IntersectionObserverWrapper();
|
2017-05-25 01:55:00 +10:00
|
|
|
|
2017-07-27 07:35:03 +10:00
|
|
|
handleScroll = throttle(() => {
|
2017-07-25 10:13:05 +10:00
|
|
|
if (this.node) {
|
|
|
|
const { scrollTop, scrollHeight, clientHeight } = this.node;
|
|
|
|
const offset = scrollHeight - scrollTop - clientHeight;
|
|
|
|
this._oldScrollPosition = scrollHeight - scrollTop;
|
|
|
|
|
2017-07-27 07:35:03 +10:00
|
|
|
if (400 > offset && this.props.onScrollToBottom && !this.props.isLoading) {
|
2017-07-25 10:13:05 +10:00
|
|
|
this.props.onScrollToBottom();
|
|
|
|
} else if (scrollTop < 100 && this.props.onScrollToTop) {
|
|
|
|
this.props.onScrollToTop();
|
|
|
|
} else if (this.props.onScroll) {
|
|
|
|
this.props.onScroll();
|
|
|
|
}
|
2016-09-22 09:08:35 +10:00
|
|
|
}
|
2017-07-27 07:35:03 +10:00
|
|
|
}, 150, {
|
2017-06-18 10:59:29 +10:00
|
|
|
trailing: true,
|
|
|
|
});
|
2016-09-22 09:08:35 +10:00
|
|
|
|
2017-01-24 14:12:10 +11:00
|
|
|
componentDidMount () {
|
|
|
|
this.attachScrollListener();
|
2017-05-25 01:55:00 +10:00
|
|
|
this.attachIntersectionObserver();
|
2017-07-20 04:26:51 +10:00
|
|
|
|
|
|
|
// Handle initial scroll posiiton
|
|
|
|
this.handleScroll();
|
2017-04-22 04:05:35 +10:00
|
|
|
}
|
2016-11-07 12:02:55 +11:00
|
|
|
|
2017-01-24 14:12:10 +11:00
|
|
|
componentDidUpdate (prevProps) {
|
2017-06-18 10:59:29 +10: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.
|
2017-07-20 06:38:26 +10:00
|
|
|
if (prevProps.statusIds.size < this.props.statusIds.size && this._oldScrollPosition && this.node.scrollTop > 0) {
|
|
|
|
if (prevProps.statusIds.first() !== this.props.statusIds.first()) {
|
|
|
|
let newScrollTop = this.node.scrollHeight - this._oldScrollPosition;
|
|
|
|
if (this.node.scrollTop !== newScrollTop) {
|
|
|
|
this.node.scrollTop = newScrollTop;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this._oldScrollPosition = this.node.scrollHeight - this.node.scrollTop;
|
2017-06-18 10:59:29 +10:00
|
|
|
}
|
2016-11-07 12:02:55 +11:00
|
|
|
}
|
2017-04-22 04:05:35 +10:00
|
|
|
}
|
2016-11-07 12:02:55 +11:00
|
|
|
|
2017-01-24 14:12:10 +11:00
|
|
|
componentWillUnmount () {
|
|
|
|
this.detachScrollListener();
|
2017-05-25 01:55:00 +10:00
|
|
|
this.detachIntersectionObserver();
|
|
|
|
}
|
|
|
|
|
|
|
|
attachIntersectionObserver () {
|
2017-05-30 02:17:51 +10:00
|
|
|
this.intersectionObserverWrapper.connect({
|
2017-05-25 01:55:00 +10:00
|
|
|
root: this.node,
|
|
|
|
rootMargin: '300% 0px',
|
2017-05-30 02:17:51 +10:00
|
|
|
});
|
2017-05-25 01:55:00 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
detachIntersectionObserver () {
|
2017-05-30 02:17:51 +10:00
|
|
|
this.intersectionObserverWrapper.disconnect();
|
2017-04-22 04:05:35 +10:00
|
|
|
}
|
2017-01-24 14:12:10 +11:00
|
|
|
|
|
|
|
attachScrollListener () {
|
|
|
|
this.node.addEventListener('scroll', this.handleScroll);
|
2017-04-22 04:05:35 +10:00
|
|
|
}
|
2017-01-24 14:12:10 +11:00
|
|
|
|
|
|
|
detachScrollListener () {
|
|
|
|
this.node.removeEventListener('scroll', this.handleScroll);
|
2017-04-22 04:05:35 +10:00
|
|
|
}
|
2017-01-24 14:12:10 +11:00
|
|
|
|
2017-05-12 22:44:10 +10:00
|
|
|
setRef = (c) => {
|
2017-01-24 14:12:10 +11:00
|
|
|
this.node = c;
|
2017-04-22 04:05:35 +10:00
|
|
|
}
|
2017-01-24 14:12:10 +11:00
|
|
|
|
2017-05-12 22:44:10 +10:00
|
|
|
handleLoadMore = (e) => {
|
2017-01-31 04:04:15 +11:00
|
|
|
e.preventDefault();
|
|
|
|
this.props.onScrollToBottom();
|
2017-04-22 04:05:35 +10:00
|
|
|
}
|
2017-01-31 04:04:15 +11:00
|
|
|
|
2017-07-28 21:33:40 +10:00
|
|
|
handleKeyDown = (e) => {
|
2017-08-02 21:09:09 +10:00
|
|
|
if (['PageDown', 'PageUp'].includes(e.key) || (e.ctrlKey && ['End', 'Home'].includes(e.key))) {
|
2017-07-28 21:33:40 +10:00
|
|
|
const article = (() => {
|
|
|
|
switch (e.key) {
|
|
|
|
case 'PageDown':
|
2017-07-29 06:55:19 +10:00
|
|
|
return e.target.nodeName === 'ARTICLE' && e.target.nextElementSibling;
|
2017-07-28 21:33:40 +10:00
|
|
|
case 'PageUp':
|
2017-07-29 06:55:19 +10:00
|
|
|
return e.target.nodeName === 'ARTICLE' && e.target.previousElementSibling;
|
2017-07-28 21:33:40 +10:00
|
|
|
case 'End':
|
|
|
|
return this.node.querySelector('[role="feed"] > article:last-of-type');
|
|
|
|
case 'Home':
|
|
|
|
return this.node.querySelector('[role="feed"] > article:first-of-type');
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
|
|
if (article) {
|
|
|
|
e.preventDefault();
|
|
|
|
article.focus();
|
|
|
|
article.scrollIntoView();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-01 00:15:12 +10:00
|
|
|
render () {
|
2017-06-24 00:05:04 +10:00
|
|
|
const { statusIds, scrollKey, trackScroll, shouldUpdateScroll, isLoading, hasMore, prepend, emptyMessage } = this.props;
|
2017-01-31 04:04:15 +11:00
|
|
|
|
2017-07-15 06:31:25 +10:00
|
|
|
const loadMore = <LoadMore visible={!isLoading && statusIds.size > 0 && hasMore} onClick={this.handleLoadMore} />;
|
2017-05-25 01:55:00 +10:00
|
|
|
let scrollableArea = null;
|
2017-01-31 04:04:15 +11:00
|
|
|
|
2017-02-18 12:37:59 +11:00
|
|
|
if (isLoading || statusIds.size > 0 || !emptyMessage) {
|
|
|
|
scrollableArea = (
|
|
|
|
<div className='scrollable' ref={this.setRef}>
|
2017-07-28 21:33:40 +10:00
|
|
|
<div role='feed' className='status-list' onKeyDown={this.handleKeyDown}>
|
2017-02-18 12:37:59 +11:00
|
|
|
{prepend}
|
2017-01-31 07:40:55 +11:00
|
|
|
|
2017-07-28 12:37:30 +10:00
|
|
|
{statusIds.map((statusId, index) => {
|
|
|
|
return <StatusContainer key={statusId} id={statusId} index={index} listLength={statusIds.size} intersectionObserverWrapper={this.intersectionObserverWrapper} />;
|
2017-02-18 12:37:59 +11:00
|
|
|
})}
|
2017-01-31 04:04:15 +11:00
|
|
|
|
2017-02-18 12:37:59 +11:00
|
|
|
{loadMore}
|
|
|
|
</div>
|
2016-08-25 01:56:44 +10:00
|
|
|
</div>
|
2017-02-18 12:37:59 +11:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
scrollableArea = (
|
|
|
|
<div className='empty-column-indicator' ref={this.setRef}>
|
|
|
|
{emptyMessage}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2016-10-19 08:06:28 +11:00
|
|
|
|
2017-06-05 23:20:46 +10:00
|
|
|
if (trackScroll) {
|
|
|
|
return (
|
|
|
|
<ScrollContainer scrollKey={scrollKey} shouldUpdateScroll={shouldUpdateScroll}>
|
|
|
|
{scrollableArea}
|
|
|
|
</ScrollContainer>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return scrollableArea;
|
|
|
|
}
|
2016-08-25 01:56:44 +10:00
|
|
|
}
|
2016-09-01 00:15:12 +10:00
|
|
|
|
2017-04-22 04:05:35 +10:00
|
|
|
}
|