You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.9 KiB

  1. import { debounce } from 'lodash';
  2. import React from 'react';
  3. import ImmutablePropTypes from 'react-immutable-proptypes';
  4. import PropTypes from 'prop-types';
  5. import StatusContainer from '../containers/status_container';
  6. import ImmutablePureComponent from 'react-immutable-pure-component';
  7. import LoadGap from './load_gap';
  8. import ScrollableList from './scrollable_list';
  9. import RegenerationIndicator from 'mastodon/components/regeneration_indicator';
  10. export default class StatusList extends ImmutablePureComponent {
  11. static propTypes = {
  12. scrollKey: PropTypes.string.isRequired,
  13. statusIds: ImmutablePropTypes.list.isRequired,
  14. featuredStatusIds: ImmutablePropTypes.list,
  15. onLoadMore: PropTypes.func,
  16. onScrollToTop: PropTypes.func,
  17. onScroll: PropTypes.func,
  18. trackScroll: PropTypes.bool,
  19. isLoading: PropTypes.bool,
  20. isPartial: PropTypes.bool,
  21. hasMore: PropTypes.bool,
  22. prepend: PropTypes.node,
  23. emptyMessage: PropTypes.node,
  24. alwaysPrepend: PropTypes.bool,
  25. timelineId: PropTypes.string,
  26. };
  27. static defaultProps = {
  28. trackScroll: true,
  29. };
  30. getFeaturedStatusCount = () => {
  31. return this.props.featuredStatusIds ? this.props.featuredStatusIds.size : 0;
  32. }
  33. getCurrentStatusIndex = (id, featured) => {
  34. if (featured) {
  35. return this.props.featuredStatusIds.indexOf(id);
  36. } else {
  37. return this.props.statusIds.indexOf(id) + this.getFeaturedStatusCount();
  38. }
  39. }
  40. handleMoveUp = (id, featured) => {
  41. const elementIndex = this.getCurrentStatusIndex(id, featured) - 1;
  42. this._selectChild(elementIndex, true);
  43. }
  44. handleMoveDown = (id, featured) => {
  45. const elementIndex = this.getCurrentStatusIndex(id, featured) + 1;
  46. this._selectChild(elementIndex, false);
  47. }
  48. handleLoadOlder = debounce(() => {
  49. this.props.onLoadMore(this.props.statusIds.size > 0 ? this.props.statusIds.last() : undefined);
  50. }, 300, { leading: true })
  51. _selectChild (index, align_top) {
  52. const container = this.node.node;
  53. const element = container.querySelector(`article:nth-of-type(${index + 1}) .focusable`);
  54. if (element) {
  55. if (align_top && container.scrollTop > element.offsetTop) {
  56. element.scrollIntoView(true);
  57. } else if (!align_top && container.scrollTop + container.clientHeight < element.offsetTop + element.offsetHeight) {
  58. element.scrollIntoView(false);
  59. }
  60. element.focus();
  61. }
  62. }
  63. setRef = c => {
  64. this.node = c;
  65. }
  66. render () {
  67. const { statusIds, featuredStatusIds, onLoadMore, timelineId, ...other } = this.props;
  68. const { isLoading, isPartial } = other;
  69. if (isPartial) {
  70. return <RegenerationIndicator />;
  71. }
  72. let scrollableContent = (isLoading || statusIds.size > 0) ? (
  73. statusIds.map((statusId, index) => statusId === null ? (
  74. <LoadGap
  75. key={'gap:' + statusIds.get(index + 1)}
  76. disabled={isLoading}
  77. maxId={index > 0 ? statusIds.get(index - 1) : null}
  78. onClick={onLoadMore}
  79. />
  80. ) : (
  81. <StatusContainer
  82. key={statusId}
  83. id={statusId}
  84. onMoveUp={this.handleMoveUp}
  85. onMoveDown={this.handleMoveDown}
  86. contextType={timelineId}
  87. scrollKey={this.props.scrollKey}
  88. showThread
  89. />
  90. ))
  91. ) : null;
  92. if (scrollableContent && featuredStatusIds) {
  93. scrollableContent = featuredStatusIds.map(statusId => (
  94. <StatusContainer
  95. key={`f-${statusId}`}
  96. id={statusId}
  97. featured
  98. onMoveUp={this.handleMoveUp}
  99. onMoveDown={this.handleMoveDown}
  100. contextType={timelineId}
  101. showThread
  102. />
  103. )).concat(scrollableContent);
  104. }
  105. return (
  106. <ScrollableList {...other} showLoading={isLoading && statusIds.size === 0} onLoadMore={onLoadMore && this.handleLoadOlder} ref={this.setRef}>
  107. {scrollableContent}
  108. </ScrollableList>
  109. );
  110. }
  111. }