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.

130 lines
3.8 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 'flavours/glitch/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 { FormattedMessage } from 'react-intl';
  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. timelineId: PropTypes.string.isRequired,
  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);
  43. }
  44. handleMoveDown = (id, featured) => {
  45. const elementIndex = this.getCurrentStatusIndex(id, featured) + 1;
  46. this._selectChild(elementIndex);
  47. }
  48. handleLoadOlder = debounce(() => {
  49. this.props.onLoadMore(this.props.statusIds.last());
  50. }, 300, { leading: true })
  51. _selectChild (index) {
  52. const element = this.node.node.querySelector(`article:nth-of-type(${index + 1}) .focusable`);
  53. if (element) {
  54. element.focus();
  55. }
  56. }
  57. setRef = c => {
  58. this.node = c;
  59. }
  60. render () {
  61. const { statusIds, featuredStatusIds, onLoadMore, timelineId, ...other } = this.props;
  62. const { isLoading, isPartial } = other;
  63. if (isPartial) {
  64. return (
  65. <div className='regeneration-indicator'>
  66. <div>
  67. <div className='regeneration-indicator__figure' />
  68. <div className='regeneration-indicator__label'>
  69. <FormattedMessage id='regeneration_indicator.label' tagName='strong' defaultMessage='Loading&hellip;' />
  70. <FormattedMessage id='regeneration_indicator.sublabel' defaultMessage='Your home feed is being prepared!' />
  71. </div>
  72. </div>
  73. </div>
  74. );
  75. }
  76. let scrollableContent = (isLoading || statusIds.size > 0) ? (
  77. statusIds.map((statusId, index) => statusId === null ? (
  78. <LoadGap
  79. key={'gap:' + statusIds.get(index + 1)}
  80. disabled={isLoading}
  81. maxId={index > 0 ? statusIds.get(index - 1) : null}
  82. onClick={onLoadMore}
  83. />
  84. ) : (
  85. <StatusContainer
  86. key={statusId}
  87. id={statusId}
  88. onMoveUp={this.handleMoveUp}
  89. onMoveDown={this.handleMoveDown}
  90. contextType={timelineId}
  91. />
  92. ))
  93. ) : null;
  94. if (scrollableContent && featuredStatusIds) {
  95. scrollableContent = featuredStatusIds.map(statusId => (
  96. <StatusContainer
  97. key={`f-${statusId}`}
  98. id={statusId}
  99. featured
  100. onMoveUp={this.handleMoveUp}
  101. onMoveDown={this.handleMoveDown}
  102. contextType={timelineId}
  103. />
  104. )).concat(scrollableContent);
  105. }
  106. return (
  107. <ScrollableList {...other} onLoadMore={onLoadMore && this.handleLoadOlder} ref={this.setRef}>
  108. {scrollableContent}
  109. </ScrollableList>
  110. );
  111. }
  112. }