2018-03-24 15:25:15 +01:00
|
|
|
import { debounce } from 'lodash';
|
2017-05-03 02:04:16 +02:00
|
|
|
import React from 'react';
|
2018-07-29 16:52:06 +02:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
2016-12-03 21:04:57 +01:00
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
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-05-03 02:04:16 +02:00
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
2018-04-10 17:12:10 +02:00
|
|
|
import LoadGap from './load_gap';
|
2017-08-28 22:23:44 +02:00
|
|
|
import ScrollableList from './scrollable_list';
|
2016-08-24 17:56:44 +02:00
|
|
|
|
2017-06-23 19:36:54 +02:00
|
|
|
export default class StatusList extends ImmutablePureComponent {
|
2017-04-21 20:05:35 +02:00
|
|
|
|
2017-05-12 14:44:10 +02:00
|
|
|
static propTypes = {
|
|
|
|
scrollKey: PropTypes.string.isRequired,
|
|
|
|
statusIds: ImmutablePropTypes.list.isRequired,
|
2018-03-04 09:19:11 +01:00
|
|
|
featuredStatusIds: ImmutablePropTypes.list,
|
2018-03-05 19:31:40 +01:00
|
|
|
onLoadMore: PropTypes.func,
|
2017-05-12 14:44:10 +02:00
|
|
|
onScrollToTop: PropTypes.func,
|
|
|
|
onScroll: PropTypes.func,
|
2017-06-05 15:20:46 +02:00
|
|
|
trackScroll: PropTypes.bool,
|
2017-05-12 14:44:10 +02:00
|
|
|
shouldUpdateScroll: PropTypes.func,
|
|
|
|
isLoading: PropTypes.bool,
|
2018-01-17 23:56:03 +01:00
|
|
|
isPartial: PropTypes.bool,
|
2017-05-12 14:44:10 +02:00
|
|
|
hasMore: PropTypes.bool,
|
|
|
|
prepend: PropTypes.node,
|
2017-05-20 17:31:47 +02:00
|
|
|
emptyMessage: PropTypes.node,
|
2018-05-29 02:01:04 +02:00
|
|
|
alwaysPrepend: PropTypes.bool,
|
2018-06-29 15:34:36 +02:00
|
|
|
timelineId: PropTypes.string.isRequired,
|
2017-05-12 14:44:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static defaultProps = {
|
2017-05-20 17:31:47 +02:00
|
|
|
trackScroll: true,
|
2017-05-12 14:44:10 +02:00
|
|
|
};
|
|
|
|
|
2018-04-20 18:14:21 +02:00
|
|
|
getFeaturedStatusCount = () => {
|
|
|
|
return this.props.featuredStatusIds ? this.props.featuredStatusIds.size : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
getCurrentStatusIndex = (id, featured) => {
|
|
|
|
if (featured) {
|
|
|
|
return this.props.featuredStatusIds.indexOf(id);
|
|
|
|
} else {
|
|
|
|
return this.props.statusIds.indexOf(id) + this.getFeaturedStatusCount();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleMoveUp = (id, featured) => {
|
|
|
|
const elementIndex = this.getCurrentStatusIndex(id, featured) - 1;
|
2017-10-06 01:07:59 +02:00
|
|
|
this._selectChild(elementIndex);
|
|
|
|
}
|
|
|
|
|
2018-04-20 18:14:21 +02:00
|
|
|
handleMoveDown = (id, featured) => {
|
|
|
|
const elementIndex = this.getCurrentStatusIndex(id, featured) + 1;
|
2017-10-06 01:07:59 +02:00
|
|
|
this._selectChild(elementIndex);
|
|
|
|
}
|
|
|
|
|
2018-03-24 15:25:15 +01:00
|
|
|
handleLoadOlder = debounce(() => {
|
|
|
|
this.props.onLoadMore(this.props.statusIds.last());
|
|
|
|
}, 300, { leading: true })
|
|
|
|
|
2017-10-06 01:07:59 +02:00
|
|
|
_selectChild (index) {
|
|
|
|
const element = this.node.node.querySelector(`article:nth-of-type(${index + 1}) .focusable`);
|
|
|
|
|
|
|
|
if (element) {
|
|
|
|
element.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setRef = c => {
|
|
|
|
this.node = c;
|
|
|
|
}
|
|
|
|
|
2016-08-31 16:15:12 +02:00
|
|
|
render () {
|
2018-07-29 16:52:06 +02:00
|
|
|
const { statusIds, featuredStatusIds, shouldUpdateScroll, onLoadMore, timelineId, ...other } = this.props;
|
2018-01-17 23:56:03 +01:00
|
|
|
const { isLoading, isPartial } = other;
|
|
|
|
|
|
|
|
if (isPartial) {
|
|
|
|
return (
|
|
|
|
<div className='regeneration-indicator'>
|
|
|
|
<div>
|
|
|
|
<div className='regeneration-indicator__figure' />
|
|
|
|
|
|
|
|
<div className='regeneration-indicator__label'>
|
|
|
|
<FormattedMessage id='regeneration_indicator.label' tagName='strong' defaultMessage='Loading…' />
|
|
|
|
<FormattedMessage id='regeneration_indicator.sublabel' defaultMessage='Your home feed is being prepared!' />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2017-08-28 22:23:44 +02:00
|
|
|
|
2018-03-04 09:19:11 +01:00
|
|
|
let scrollableContent = (isLoading || statusIds.size > 0) ? (
|
2018-03-24 15:25:15 +01:00
|
|
|
statusIds.map((statusId, index) => statusId === null ? (
|
|
|
|
<LoadGap
|
|
|
|
key={'gap:' + statusIds.get(index + 1)}
|
|
|
|
disabled={isLoading}
|
|
|
|
maxId={index > 0 ? statusIds.get(index - 1) : null}
|
|
|
|
onClick={onLoadMore}
|
|
|
|
/>
|
|
|
|
) : (
|
2017-10-06 01:07:59 +02:00
|
|
|
<StatusContainer
|
|
|
|
key={statusId}
|
|
|
|
id={statusId}
|
|
|
|
onMoveUp={this.handleMoveUp}
|
|
|
|
onMoveDown={this.handleMoveDown}
|
2018-06-29 15:34:36 +02:00
|
|
|
contextType={timelineId}
|
2018-11-08 21:08:57 +01:00
|
|
|
showThread
|
2017-10-06 01:07:59 +02:00
|
|
|
/>
|
2017-08-28 22:23:44 +02:00
|
|
|
))
|
|
|
|
) : null;
|
|
|
|
|
2018-03-04 09:19:11 +01:00
|
|
|
if (scrollableContent && featuredStatusIds) {
|
|
|
|
scrollableContent = featuredStatusIds.map(statusId => (
|
|
|
|
<StatusContainer
|
|
|
|
key={`f-${statusId}`}
|
|
|
|
id={statusId}
|
|
|
|
featured
|
|
|
|
onMoveUp={this.handleMoveUp}
|
|
|
|
onMoveDown={this.handleMoveDown}
|
2018-06-29 15:34:36 +02:00
|
|
|
contextType={timelineId}
|
2018-11-08 21:08:57 +01:00
|
|
|
showThread
|
2018-03-04 09:19:11 +01:00
|
|
|
/>
|
|
|
|
)).concat(scrollableContent);
|
|
|
|
}
|
|
|
|
|
2017-08-28 22:23:44 +02:00
|
|
|
return (
|
2018-07-29 16:52:06 +02:00
|
|
|
<ScrollableList {...other} onLoadMore={onLoadMore && this.handleLoadOlder} shouldUpdateScroll={shouldUpdateScroll} ref={this.setRef}>
|
2017-08-28 22:23:44 +02:00
|
|
|
{scrollableContent}
|
|
|
|
</ScrollableList>
|
|
|
|
);
|
2016-08-24 17:56:44 +02:00
|
|
|
}
|
2016-08-31 16:15:12 +02:00
|
|
|
|
2017-04-21 20:05:35 +02:00
|
|
|
}
|