index.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 { lookupAccount, fetchAccount } from 'mastodon/actions/accounts';
  6. import { expandAccountMediaTimeline } from '../../actions/timelines';
  7. import LoadingIndicator from 'mastodon/components/loading_indicator';
  8. import Column from '../ui/components/column';
  9. import ColumnBackButton from 'mastodon/components/column_back_button';
  10. import ImmutablePureComponent from 'react-immutable-pure-component';
  11. import { getAccountGallery } from 'mastodon/selectors';
  12. import MediaItem from './components/media_item';
  13. import HeaderContainer from '../account_timeline/containers/header_container';
  14. import ScrollContainer from 'mastodon/containers/scroll_container';
  15. import LoadMore from 'mastodon/components/load_more';
  16. import MissingIndicator from 'mastodon/components/missing_indicator';
  17. import { openModal } from 'mastodon/actions/modal';
  18. import { FormattedMessage } from 'react-intl';
  19. import { normalizeForLookup } from 'mastodon/reducers/accounts_map';
  20. const mapStateToProps = (state, { params: { acct, id } }) => {
  21. const accountId = id || state.getIn(['accounts_map', normalizeForLookup(acct)]);
  22. if (!accountId) {
  23. return {
  24. isLoading: true,
  25. };
  26. }
  27. return {
  28. accountId,
  29. isAccount: !!state.getIn(['accounts', accountId]),
  30. attachments: getAccountGallery(state, accountId),
  31. isLoading: state.getIn(['timelines', `account:${accountId}:media`, 'isLoading']),
  32. hasMore: state.getIn(['timelines', `account:${accountId}:media`, 'hasMore']),
  33. suspended: state.getIn(['accounts', accountId, 'suspended'], false),
  34. blockedBy: state.getIn(['relationships', accountId, 'blocked_by'], false),
  35. };
  36. };
  37. class LoadMoreMedia extends ImmutablePureComponent {
  38. static propTypes = {
  39. maxId: PropTypes.string,
  40. onLoadMore: PropTypes.func.isRequired,
  41. };
  42. handleLoadMore = () => {
  43. this.props.onLoadMore(this.props.maxId);
  44. }
  45. render () {
  46. return (
  47. <LoadMore
  48. disabled={this.props.disabled}
  49. onClick={this.handleLoadMore}
  50. />
  51. );
  52. }
  53. }
  54. export default @connect(mapStateToProps)
  55. class AccountGallery extends ImmutablePureComponent {
  56. static propTypes = {
  57. params: PropTypes.shape({
  58. acct: PropTypes.string,
  59. id: PropTypes.string,
  60. }).isRequired,
  61. accountId: PropTypes.string,
  62. dispatch: PropTypes.func.isRequired,
  63. attachments: ImmutablePropTypes.list.isRequired,
  64. isLoading: PropTypes.bool,
  65. hasMore: PropTypes.bool,
  66. isAccount: PropTypes.bool,
  67. blockedBy: PropTypes.bool,
  68. suspended: PropTypes.bool,
  69. multiColumn: PropTypes.bool,
  70. };
  71. state = {
  72. width: 323,
  73. };
  74. _load () {
  75. const { accountId, isAccount, dispatch } = this.props;
  76. if (!isAccount) dispatch(fetchAccount(accountId));
  77. dispatch(expandAccountMediaTimeline(accountId));
  78. }
  79. componentDidMount () {
  80. const { params: { acct }, accountId, dispatch } = this.props;
  81. if (accountId) {
  82. this._load();
  83. } else {
  84. dispatch(lookupAccount(acct));
  85. }
  86. }
  87. componentDidUpdate (prevProps) {
  88. const { params: { acct }, accountId, dispatch } = this.props;
  89. if (prevProps.accountId !== accountId && accountId) {
  90. this._load();
  91. } else if (prevProps.params.acct !== acct) {
  92. dispatch(lookupAccount(acct));
  93. }
  94. }
  95. handleScrollToBottom = () => {
  96. if (this.props.hasMore) {
  97. this.handleLoadMore(this.props.attachments.size > 0 ? this.props.attachments.last().getIn(['status', 'id']) : undefined);
  98. }
  99. }
  100. handleScroll = e => {
  101. const { scrollTop, scrollHeight, clientHeight } = e.target;
  102. const offset = scrollHeight - scrollTop - clientHeight;
  103. if (150 > offset && !this.props.isLoading) {
  104. this.handleScrollToBottom();
  105. }
  106. }
  107. handleLoadMore = maxId => {
  108. this.props.dispatch(expandAccountMediaTimeline(this.props.accountId, { maxId }));
  109. };
  110. handleLoadOlder = e => {
  111. e.preventDefault();
  112. this.handleScrollToBottom();
  113. }
  114. handleOpenMedia = attachment => {
  115. const { dispatch } = this.props;
  116. const statusId = attachment.getIn(['status', 'id']);
  117. if (attachment.get('type') === 'video') {
  118. dispatch(openModal('VIDEO', { media: attachment, statusId, options: { autoPlay: true } }));
  119. } else if (attachment.get('type') === 'audio') {
  120. dispatch(openModal('AUDIO', { media: attachment, statusId, options: { autoPlay: true } }));
  121. } else {
  122. const media = attachment.getIn(['status', 'media_attachments']);
  123. const index = media.findIndex(x => x.get('id') === attachment.get('id'));
  124. dispatch(openModal('MEDIA', { media, index, statusId }));
  125. }
  126. }
  127. handleRef = c => {
  128. if (c) {
  129. this.setState({ width: c.offsetWidth });
  130. }
  131. }
  132. render () {
  133. const { attachments, isLoading, hasMore, isAccount, multiColumn, blockedBy, suspended } = this.props;
  134. const { width } = this.state;
  135. if (!isAccount) {
  136. return (
  137. <Column>
  138. <MissingIndicator />
  139. </Column>
  140. );
  141. }
  142. if (!attachments && isLoading) {
  143. return (
  144. <Column>
  145. <LoadingIndicator />
  146. </Column>
  147. );
  148. }
  149. let loadOlder = null;
  150. if (hasMore && !(isLoading && attachments.size === 0)) {
  151. loadOlder = <LoadMore visible={!isLoading} onClick={this.handleLoadOlder} />;
  152. }
  153. let emptyMessage;
  154. if (suspended) {
  155. emptyMessage = <FormattedMessage id='empty_column.account_suspended' defaultMessage='Account suspended' />;
  156. } else if (blockedBy) {
  157. emptyMessage = <FormattedMessage id='empty_column.account_unavailable' defaultMessage='Profile unavailable' />;
  158. }
  159. return (
  160. <Column>
  161. <ColumnBackButton multiColumn={multiColumn} />
  162. <ScrollContainer scrollKey='account_gallery'>
  163. <div className='scrollable scrollable--flex' onScroll={this.handleScroll}>
  164. <HeaderContainer accountId={this.props.accountId} />
  165. {(suspended || blockedBy) ? (
  166. <div className='empty-column-indicator'>
  167. {emptyMessage}
  168. </div>
  169. ) : (
  170. <div role='feed' className='account-gallery__container' ref={this.handleRef}>
  171. {attachments.map((attachment, index) => attachment === null ? (
  172. <LoadMoreMedia key={'more:' + attachments.getIn(index + 1, 'id')} maxId={index > 0 ? attachments.getIn(index - 1, 'id') : null} onLoadMore={this.handleLoadMore} />
  173. ) : (
  174. <MediaItem key={attachment.get('id')} attachment={attachment} displayWidth={width} onOpenMedia={this.handleOpenMedia} />
  175. ))}
  176. {loadOlder}
  177. </div>
  178. )}
  179. {isLoading && attachments.size === 0 && (
  180. <div className='scrollable__append'>
  181. <LoadingIndicator />
  182. </div>
  183. )}
  184. </div>
  185. </ScrollContainer>
  186. </Column>
  187. );
  188. }
  189. }