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.

129 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. shouldUpdateScroll: PropTypes.func,
  20. isLoading: PropTypes.bool,
  21. isPartial: PropTypes.bool,
  22. hasMore: PropTypes.bool,
  23. prepend: PropTypes.node,
  24. emptyMessage: PropTypes.node,
  25. alwaysPrepend: PropTypes.bool,
  26. timelineId: PropTypes.string,
  27. };
  28. static defaultProps = {
  29. trackScroll: true,
  30. };
  31. getFeaturedStatusCount = () => {
  32. return this.props.featuredStatusIds ? this.props.featuredStatusIds.size : 0;
  33. }
  34. getCurrentStatusIndex = (id, featured) => {
  35. if (featured) {
  36. return this.props.featuredStatusIds.indexOf(id);
  37. } else {
  38. return this.props.statusIds.indexOf(id) + this.getFeaturedStatusCount();
  39. }
  40. }
  41. handleMoveUp = (id, featured) => {
  42. const elementIndex = this.getCurrentStatusIndex(id, featured) - 1;
  43. this._selectChild(elementIndex, true);
  44. }
  45. handleMoveDown = (id, featured) => {
  46. const elementIndex = this.getCurrentStatusIndex(id, featured) + 1;
  47. this._selectChild(elementIndex, false);
  48. }
  49. handleLoadOlder = debounce(() => {
  50. this.props.onLoadMore(this.props.statusIds.size > 0 ? this.props.statusIds.last() : undefined);
  51. }, 300, { leading: true })
  52. _selectChild (index, align_top) {
  53. const container = this.node.node;
  54. const element = container.querySelector(`article:nth-of-type(${index + 1}) .focusable`);
  55. if (element) {
  56. if (align_top && container.scrollTop > element.offsetTop) {
  57. element.scrollIntoView(true);
  58. } else if (!align_top && container.scrollTop + container.clientHeight < element.offsetTop + element.offsetHeight) {
  59. element.scrollIntoView(false);
  60. }
  61. element.focus();
  62. }
  63. }
  64. setRef = c => {
  65. this.node = c;
  66. }
  67. render () {
  68. const { statusIds, featuredStatusIds, shouldUpdateScroll, onLoadMore, timelineId, ...other } = this.props;
  69. const { isLoading, isPartial } = other;
  70. if (isPartial) {
  71. return <RegenerationIndicator />;
  72. }
  73. let scrollableContent = (isLoading || statusIds.size > 0) ? (
  74. statusIds.map((statusId, index) => statusId === null ? (
  75. <LoadGap
  76. key={'gap:' + statusIds.get(index + 1)}
  77. disabled={isLoading}
  78. maxId={index > 0 ? statusIds.get(index - 1) : null}
  79. onClick={onLoadMore}
  80. />
  81. ) : (
  82. <StatusContainer
  83. key={statusId}
  84. id={statusId}
  85. onMoveUp={this.handleMoveUp}
  86. onMoveDown={this.handleMoveDown}
  87. contextType={timelineId}
  88. scrollKey={this.props.scrollKey}
  89. showThread
  90. />
  91. ))
  92. ) : null;
  93. if (scrollableContent && featuredStatusIds) {
  94. scrollableContent = featuredStatusIds.map(statusId => (
  95. <StatusContainer
  96. key={`f-${statusId}`}
  97. id={statusId}
  98. featured
  99. onMoveUp={this.handleMoveUp}
  100. onMoveDown={this.handleMoveDown}
  101. contextType={timelineId}
  102. showThread
  103. />
  104. )).concat(scrollableContent);
  105. }
  106. return (
  107. <ScrollableList {...other} showLoading={isLoading && statusIds.size === 0} onLoadMore={onLoadMore && this.handleLoadOlder} shouldUpdateScroll={shouldUpdateScroll} ref={this.setRef}>
  108. {scrollableContent}
  109. </ScrollableList>
  110. );
  111. }
  112. }