2018-05-28 02:10:37 +09:00
|
|
|
import { debounce } from 'lodash';
|
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';
|
2017-04-22 03:05:35 +09:00
|
|
|
import PropTypes from 'prop-types';
|
2017-12-04 16:26:40 +09:00
|
|
|
import StatusContainer from 'flavours/glitch/containers/status_container';
|
2017-05-03 09:04:16 +09:00
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
2018-05-28 03:23:56 +09:00
|
|
|
import LoadGap from './load_gap';
|
2017-08-29 05:23:44 +09:00
|
|
|
import ScrollableList from './scrollable_list';
|
2019-10-07 05:11:17 +09:00
|
|
|
import RegenerationIndicator from 'flavours/glitch/components/regeneration_indicator';
|
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,
|
2018-03-17 04:29:42 +09:00
|
|
|
featuredStatusIds: ImmutablePropTypes.list,
|
2018-05-28 00:46:48 +09:00
|
|
|
onLoadMore: PropTypes.func,
|
2017-05-12 21:44:10 +09:00
|
|
|
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
|
|
|
isLoading: PropTypes.bool,
|
2018-01-19 01:25:37 +09:00
|
|
|
isPartial: PropTypes.bool,
|
2017-05-12 21:44:10 +09:00
|
|
|
hasMore: PropTypes.bool,
|
|
|
|
prepend: PropTypes.node,
|
2017-05-21 00:31:47 +09:00
|
|
|
emptyMessage: PropTypes.node,
|
2022-02-25 08:34:33 +09:00
|
|
|
alwaysPrepend: PropTypes.bool,
|
|
|
|
withCounters: PropTypes.bool,
|
2018-05-29 23:40:04 +09:00
|
|
|
timelineId: PropTypes.string.isRequired,
|
2022-09-02 18:57:06 +09:00
|
|
|
regex: PropTypes.string,
|
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
|
|
|
};
|
|
|
|
|
2018-04-23 05:08:30 +09: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;
|
2019-04-16 03:40:05 +09:00
|
|
|
this._selectChild(elementIndex, true);
|
2017-10-06 08:07:59 +09:00
|
|
|
}
|
|
|
|
|
2018-04-23 05:08:30 +09:00
|
|
|
handleMoveDown = (id, featured) => {
|
|
|
|
const elementIndex = this.getCurrentStatusIndex(id, featured) + 1;
|
2019-04-16 03:40:05 +09:00
|
|
|
this._selectChild(elementIndex, false);
|
2017-10-06 08:07:59 +09:00
|
|
|
}
|
|
|
|
|
2018-05-28 02:10:37 +09:00
|
|
|
handleLoadOlder = debounce(() => {
|
2018-12-13 01:00:31 +09:00
|
|
|
this.props.onLoadMore(this.props.statusIds.size > 0 ? this.props.statusIds.last() : undefined);
|
2018-05-28 02:10:37 +09:00
|
|
|
}, 300, { leading: true })
|
|
|
|
|
2019-04-16 03:40:05 +09:00
|
|
|
_selectChild (index, align_top) {
|
|
|
|
const container = this.node.node;
|
|
|
|
const element = container.querySelector(`article:nth-of-type(${index + 1}) .focusable`);
|
2017-10-06 08:07:59 +09:00
|
|
|
|
|
|
|
if (element) {
|
2019-04-16 03:40:05 +09:00
|
|
|
if (align_top && container.scrollTop > element.offsetTop) {
|
|
|
|
element.scrollIntoView(true);
|
|
|
|
} else if (!align_top && container.scrollTop + container.clientHeight < element.offsetTop + element.offsetHeight) {
|
|
|
|
element.scrollIntoView(false);
|
|
|
|
}
|
2017-10-06 08:07:59 +09:00
|
|
|
element.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setRef = c => {
|
|
|
|
this.node = c;
|
|
|
|
}
|
|
|
|
|
2016-08-31 23:15:12 +09:00
|
|
|
render () {
|
2018-07-09 03:04:53 +09:00
|
|
|
const { statusIds, featuredStatusIds, onLoadMore, timelineId, ...other } = this.props;
|
2018-01-19 01:25:37 +09:00
|
|
|
const { isLoading, isPartial } = other;
|
|
|
|
|
|
|
|
if (isPartial) {
|
2019-10-07 05:11:17 +09:00
|
|
|
return <RegenerationIndicator />;
|
2018-01-19 01:25:37 +09:00
|
|
|
}
|
2017-08-29 05:23:44 +09:00
|
|
|
|
2018-03-17 04:29:42 +09:00
|
|
|
let scrollableContent = (isLoading || statusIds.size > 0) ? (
|
2018-05-28 02:10:37 +09: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 08:07:59 +09:00
|
|
|
<StatusContainer
|
|
|
|
key={statusId}
|
|
|
|
id={statusId}
|
|
|
|
onMoveUp={this.handleMoveUp}
|
|
|
|
onMoveDown={this.handleMoveDown}
|
2018-07-09 03:04:53 +09:00
|
|
|
contextType={timelineId}
|
2020-07-09 22:09:19 +09:00
|
|
|
scrollKey={this.props.scrollKey}
|
2022-02-25 08:34:33 +09:00
|
|
|
withCounters={this.props.withCounters}
|
2017-10-06 08:07:59 +09:00
|
|
|
/>
|
2017-08-29 05:23:44 +09:00
|
|
|
))
|
|
|
|
) : null;
|
|
|
|
|
2018-03-17 04:29:42 +09:00
|
|
|
if (scrollableContent && featuredStatusIds) {
|
|
|
|
scrollableContent = featuredStatusIds.map(statusId => (
|
|
|
|
<StatusContainer
|
|
|
|
key={`f-${statusId}`}
|
|
|
|
id={statusId}
|
|
|
|
featured
|
|
|
|
onMoveUp={this.handleMoveUp}
|
|
|
|
onMoveDown={this.handleMoveDown}
|
2018-07-09 03:04:53 +09:00
|
|
|
contextType={timelineId}
|
2020-07-09 22:09:19 +09:00
|
|
|
scrollKey={this.props.scrollKey}
|
2022-02-25 08:34:33 +09:00
|
|
|
withCounters={this.props.withCounters}
|
2018-03-17 04:29:42 +09:00
|
|
|
/>
|
|
|
|
)).concat(scrollableContent);
|
|
|
|
}
|
|
|
|
|
2017-08-29 05:23:44 +09:00
|
|
|
return (
|
2018-11-10 00:47:50 +09:00
|
|
|
<ScrollableList {...other} showLoading={isLoading && statusIds.size === 0} onLoadMore={onLoadMore && this.handleLoadOlder} ref={this.setRef}>
|
2017-08-29 05:23:44 +09:00
|
|
|
{scrollableContent}
|
|
|
|
</ScrollableList>
|
|
|
|
);
|
2016-08-25 00:56:44 +09:00
|
|
|
}
|
2016-08-31 23:15:12 +09:00
|
|
|
|
2017-04-22 03:05:35 +09:00
|
|
|
}
|