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.

229 lines
6.9 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 { 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. class AccountGallery extends ImmutablePureComponent {
  55. static propTypes = {
  56. params: PropTypes.shape({
  57. acct: PropTypes.string,
  58. id: PropTypes.string,
  59. }).isRequired,
  60. accountId: PropTypes.string,
  61. dispatch: PropTypes.func.isRequired,
  62. attachments: ImmutablePropTypes.list.isRequired,
  63. isLoading: PropTypes.bool,
  64. hasMore: PropTypes.bool,
  65. isAccount: PropTypes.bool,
  66. blockedBy: PropTypes.bool,
  67. suspended: PropTypes.bool,
  68. multiColumn: PropTypes.bool,
  69. };
  70. state = {
  71. width: 323,
  72. };
  73. _load () {
  74. const { accountId, isAccount, dispatch } = this.props;
  75. if (!isAccount) dispatch(fetchAccount(accountId));
  76. dispatch(expandAccountMediaTimeline(accountId));
  77. }
  78. componentDidMount () {
  79. const { params: { acct }, accountId, dispatch } = this.props;
  80. if (accountId) {
  81. this._load();
  82. } else {
  83. dispatch(lookupAccount(acct));
  84. }
  85. }
  86. componentDidUpdate (prevProps) {
  87. const { params: { acct }, accountId, dispatch } = this.props;
  88. if (prevProps.accountId !== accountId && accountId) {
  89. this._load();
  90. } else if (prevProps.params.acct !== acct) {
  91. dispatch(lookupAccount(acct));
  92. }
  93. }
  94. handleScrollToBottom = () => {
  95. if (this.props.hasMore) {
  96. this.handleLoadMore(this.props.attachments.size > 0 ? this.props.attachments.last().getIn(['status', 'id']) : undefined);
  97. }
  98. };
  99. handleScroll = e => {
  100. const { scrollTop, scrollHeight, clientHeight } = e.target;
  101. const offset = scrollHeight - scrollTop - clientHeight;
  102. if (150 > offset && !this.props.isLoading) {
  103. this.handleScrollToBottom();
  104. }
  105. };
  106. handleLoadMore = maxId => {
  107. this.props.dispatch(expandAccountMediaTimeline(this.props.accountId, { maxId }));
  108. };
  109. handleLoadOlder = e => {
  110. e.preventDefault();
  111. this.handleScrollToBottom();
  112. };
  113. handleOpenMedia = attachment => {
  114. const { dispatch } = this.props;
  115. const statusId = attachment.getIn(['status', 'id']);
  116. if (attachment.get('type') === 'video') {
  117. dispatch(openModal('VIDEO', { media: attachment, statusId, options: { autoPlay: true } }));
  118. } else if (attachment.get('type') === 'audio') {
  119. dispatch(openModal('AUDIO', { media: attachment, statusId, options: { autoPlay: true } }));
  120. } else {
  121. const media = attachment.getIn(['status', 'media_attachments']);
  122. const index = media.findIndex(x => x.get('id') === attachment.get('id'));
  123. dispatch(openModal('MEDIA', { media, index, statusId }));
  124. }
  125. };
  126. handleRef = c => {
  127. if (c) {
  128. this.setState({ width: c.offsetWidth });
  129. }
  130. };
  131. render () {
  132. const { attachments, isLoading, hasMore, isAccount, multiColumn, blockedBy, suspended } = this.props;
  133. const { width } = this.state;
  134. if (!isAccount) {
  135. return (
  136. <Column>
  137. <MissingIndicator />
  138. </Column>
  139. );
  140. }
  141. if (!attachments && isLoading) {
  142. return (
  143. <Column>
  144. <LoadingIndicator />
  145. </Column>
  146. );
  147. }
  148. let loadOlder = null;
  149. if (hasMore && !(isLoading && attachments.size === 0)) {
  150. loadOlder = <LoadMore visible={!isLoading} onClick={this.handleLoadOlder} />;
  151. }
  152. let emptyMessage;
  153. if (suspended) {
  154. emptyMessage = <FormattedMessage id='empty_column.account_suspended' defaultMessage='Account suspended' />;
  155. } else if (blockedBy) {
  156. emptyMessage = <FormattedMessage id='empty_column.account_unavailable' defaultMessage='Profile unavailable' />;
  157. }
  158. return (
  159. <Column>
  160. <ColumnBackButton multiColumn={multiColumn} />
  161. <ScrollContainer scrollKey='account_gallery'>
  162. <div className='scrollable scrollable--flex' onScroll={this.handleScroll}>
  163. <HeaderContainer accountId={this.props.accountId} />
  164. {(suspended || blockedBy) ? (
  165. <div className='empty-column-indicator'>
  166. {emptyMessage}
  167. </div>
  168. ) : (
  169. <div role='feed' className='account-gallery__container' ref={this.handleRef}>
  170. {attachments.map((attachment, index) => attachment === null ? (
  171. <LoadMoreMedia key={'more:' + attachments.getIn(index + 1, 'id')} maxId={index > 0 ? attachments.getIn(index - 1, 'id') : null} onLoadMore={this.handleLoadMore} />
  172. ) : (
  173. <MediaItem key={attachment.get('id')} attachment={attachment} displayWidth={width} onOpenMedia={this.handleOpenMedia} />
  174. ))}
  175. {loadOlder}
  176. </div>
  177. )}
  178. {isLoading && attachments.size === 0 && (
  179. <div className='scrollable__append'>
  180. <LoadingIndicator />
  181. </div>
  182. )}
  183. </div>
  184. </ScrollContainer>
  185. </Column>
  186. );
  187. }
  188. }
  189. export default connect(mapStateToProps)(AccountGallery);