闭社主体 forked from https://github.com/tootsuite/mastodon
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.

114 lines
3.7 KiB

  1. import React from 'react';
  2. import { connect } from 'react-redux';
  3. import ImmutablePropTypes from 'react-immutable-proptypes';
  4. import PropTypes from 'prop-types';
  5. import { fetchAccount } from '../../actions/accounts';
  6. import { refreshAccountMediaTimeline, expandAccountMediaTimeline } from '../../actions/timelines';
  7. import LoadingIndicator from '../../components/loading_indicator';
  8. import Column from '../ui/components/column';
  9. import ColumnBackButton from '../../components/column_back_button';
  10. import ImmutablePureComponent from 'react-immutable-pure-component';
  11. import { getAccountGallery } from '../../selectors';
  12. import MediaItem from './components/media_item';
  13. import HeaderContainer from '../account_timeline/containers/header_container';
  14. import { FormattedMessage } from 'react-intl';
  15. import { ScrollContainer } from 'react-router-scroll';
  16. import LoadMore from '../../components/load_more';
  17. const mapStateToProps = (state, props) => ({
  18. medias: getAccountGallery(state, Number(props.params.accountId)),
  19. isLoading: state.getIn(['timelines', `account:${Number(props.params.accountId)}:media`, 'isLoading']),
  20. hasMore: !!state.getIn(['timelines', `account:${Number(props.params.accountId)}:media`, 'next']),
  21. autoPlayGif: state.getIn(['meta', 'auto_play_gif']),
  22. });
  23. @connect(mapStateToProps)
  24. export default class AccountGallery extends ImmutablePureComponent {
  25. static propTypes = {
  26. params: PropTypes.object.isRequired,
  27. dispatch: PropTypes.func.isRequired,
  28. medias: ImmutablePropTypes.list.isRequired,
  29. isLoading: PropTypes.bool,
  30. hasMore: PropTypes.bool,
  31. autoPlayGif: PropTypes.bool,
  32. };
  33. componentDidMount () {
  34. this.props.dispatch(fetchAccount(Number(this.props.params.accountId)));
  35. this.props.dispatch(refreshAccountMediaTimeline(Number(this.props.params.accountId)));
  36. }
  37. componentWillReceiveProps (nextProps) {
  38. if (nextProps.params.accountId !== this.props.params.accountId && nextProps.params.accountId) {
  39. this.props.dispatch(fetchAccount(Number(nextProps.params.accountId)));
  40. this.props.dispatch(refreshAccountMediaTimeline(Number(this.props.params.accountId)));
  41. }
  42. }
  43. handleScrollToBottom = () => {
  44. if (this.props.hasMore) {
  45. this.props.dispatch(expandAccountMediaTimeline(Number(this.props.params.accountId)));
  46. }
  47. }
  48. handleScroll = (e) => {
  49. const { scrollTop, scrollHeight, clientHeight } = e.target;
  50. const offset = scrollHeight - scrollTop - clientHeight;
  51. if (150 > offset && !this.props.isLoading) {
  52. this.handleScrollToBottom();
  53. }
  54. }
  55. handleLoadMore = (e) => {
  56. e.preventDefault();
  57. this.handleScrollToBottom();
  58. }
  59. render () {
  60. const { medias, autoPlayGif, isLoading, hasMore } = this.props;
  61. let loadMore = null;
  62. if (!medias && isLoading) {
  63. return (
  64. <Column>
  65. <LoadingIndicator />
  66. </Column>
  67. );
  68. }
  69. if (!isLoading && medias.size > 0 && hasMore) {
  70. loadMore = <LoadMore onClick={this.handleLoadMore} />;
  71. }
  72. return (
  73. <Column>
  74. <ColumnBackButton />
  75. <ScrollContainer scrollKey='account_gallery'>
  76. <div className='scrollable' onScroll={this.handleScroll}>
  77. <HeaderContainer accountId={this.props.params.accountId} />
  78. <div className='account-section-headline'>
  79. <FormattedMessage id='account.media' defaultMessage='Media' />
  80. </div>
  81. <div className='account-gallery__container'>
  82. {medias.map(media =>
  83. <MediaItem
  84. key={media.get('id')}
  85. media={media}
  86. autoPlayGif={autoPlayGif}
  87. />
  88. )}
  89. {loadMore}
  90. </div>
  91. </div>
  92. </ScrollContainer>
  93. </Column>
  94. );
  95. }
  96. }