The code powering m.abunchtell.com https://m.abunchtell.com
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

129 linhas
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. 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} shouldUpdateScroll={shouldUpdateScroll} ref={this.setRef}>
  107. {scrollableContent}
  108. </ScrollableList>
  109. );
  110. }
  111. }