index.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 '../../actions/accounts';
  6. import { expandAccountFeaturedTimeline, expandAccountTimeline } from '../../actions/timelines';
  7. import StatusList from '../../components/status_list';
  8. import LoadingIndicator from '../../components/loading_indicator';
  9. import Column from '../ui/components/column';
  10. import HeaderContainer from './containers/header_container';
  11. import ColumnBackButton from '../../components/column_back_button';
  12. import { List as ImmutableList } from 'immutable';
  13. import ImmutablePureComponent from 'react-immutable-pure-component';
  14. import { FormattedMessage } from 'react-intl';
  15. import MissingIndicator from 'mastodon/components/missing_indicator';
  16. import TimelineHint from 'mastodon/components/timeline_hint';
  17. import { me } from 'mastodon/initial_state';
  18. import { connectTimeline, disconnectTimeline } from 'mastodon/actions/timelines';
  19. import LimitedAccountHint from './components/limited_account_hint';
  20. import { getAccountHidden } from 'mastodon/selectors';
  21. import { fetchFeaturedTags } from '../../actions/featured_tags';
  22. const emptyList = ImmutableList();
  23. const mapStateToProps = (state, { params: { acct, id, tagged }, withReplies = false }) => {
  24. const accountId = id || state.getIn(['accounts_map', acct]);
  25. if (!accountId) {
  26. return {
  27. isLoading: true,
  28. };
  29. }
  30. const path = withReplies ? `${accountId}:with_replies` : `${accountId}${tagged ? `:${tagged}` : ''}`;
  31. return {
  32. accountId,
  33. remote: !!(state.getIn(['accounts', accountId, 'acct']) !== state.getIn(['accounts', accountId, 'username'])),
  34. remoteUrl: state.getIn(['accounts', accountId, 'url']),
  35. isAccount: !!state.getIn(['accounts', accountId]),
  36. statusIds: state.getIn(['timelines', `account:${path}`, 'items'], emptyList),
  37. featuredStatusIds: withReplies ? ImmutableList() : state.getIn(['timelines', `account:${accountId}:pinned${tagged ? `:${tagged}` : ''}`, 'items'], emptyList),
  38. isLoading: state.getIn(['timelines', `account:${path}`, 'isLoading']),
  39. hasMore: state.getIn(['timelines', `account:${path}`, 'hasMore']),
  40. suspended: state.getIn(['accounts', accountId, 'suspended'], false),
  41. hidden: getAccountHidden(state, accountId),
  42. blockedBy: state.getIn(['relationships', accountId, 'blocked_by'], false),
  43. };
  44. };
  45. const RemoteHint = ({ url }) => (
  46. <TimelineHint url={url} resource={<FormattedMessage id='timeline_hint.resources.statuses' defaultMessage='Older posts' />} />
  47. );
  48. RemoteHint.propTypes = {
  49. url: PropTypes.string.isRequired,
  50. };
  51. export default @connect(mapStateToProps)
  52. class AccountTimeline extends ImmutablePureComponent {
  53. static propTypes = {
  54. params: PropTypes.shape({
  55. acct: PropTypes.string,
  56. id: PropTypes.string,
  57. tagged: PropTypes.string,
  58. }).isRequired,
  59. accountId: PropTypes.string,
  60. dispatch: PropTypes.func.isRequired,
  61. statusIds: ImmutablePropTypes.list,
  62. featuredStatusIds: ImmutablePropTypes.list,
  63. isLoading: PropTypes.bool,
  64. hasMore: PropTypes.bool,
  65. withReplies: PropTypes.bool,
  66. blockedBy: PropTypes.bool,
  67. isAccount: PropTypes.bool,
  68. suspended: PropTypes.bool,
  69. hidden: PropTypes.bool,
  70. remote: PropTypes.bool,
  71. remoteUrl: PropTypes.string,
  72. multiColumn: PropTypes.bool,
  73. };
  74. _load () {
  75. const { accountId, withReplies, params: { tagged }, dispatch } = this.props;
  76. dispatch(fetchAccount(accountId));
  77. if (!withReplies) {
  78. dispatch(expandAccountFeaturedTimeline(accountId, { tagged }));
  79. }
  80. dispatch(fetchFeaturedTags(accountId));
  81. dispatch(expandAccountTimeline(accountId, { withReplies, tagged }));
  82. if (accountId === me) {
  83. dispatch(connectTimeline(`account:${me}`));
  84. }
  85. }
  86. componentDidMount () {
  87. const { params: { acct }, accountId, dispatch } = this.props;
  88. if (accountId) {
  89. this._load();
  90. } else {
  91. dispatch(lookupAccount(acct));
  92. }
  93. }
  94. componentDidUpdate (prevProps) {
  95. const { params: { acct, tagged }, accountId, withReplies, dispatch } = this.props;
  96. if (prevProps.accountId !== accountId && accountId) {
  97. this._load();
  98. } else if (prevProps.params.acct !== acct) {
  99. dispatch(lookupAccount(acct));
  100. } else if (prevProps.params.tagged !== tagged) {
  101. if (!withReplies) {
  102. dispatch(expandAccountFeaturedTimeline(accountId, { tagged }));
  103. }
  104. dispatch(expandAccountTimeline(accountId, { withReplies, tagged }));
  105. }
  106. if (prevProps.accountId === me && accountId !== me) {
  107. dispatch(disconnectTimeline(`account:${me}`));
  108. }
  109. }
  110. componentWillUnmount () {
  111. const { dispatch, accountId } = this.props;
  112. if (accountId === me) {
  113. dispatch(disconnectTimeline(`account:${me}`));
  114. }
  115. }
  116. handleLoadMore = maxId => {
  117. this.props.dispatch(expandAccountTimeline(this.props.accountId, { maxId, withReplies: this.props.withReplies, tagged: this.props.params.tagged }));
  118. }
  119. render () {
  120. const { accountId, statusIds, featuredStatusIds, isLoading, hasMore, blockedBy, suspended, isAccount, hidden, multiColumn, remote, remoteUrl } = this.props;
  121. if (isLoading && statusIds.isEmpty()) {
  122. return (
  123. <Column>
  124. <LoadingIndicator />
  125. </Column>
  126. );
  127. } else if (!isLoading && !isAccount) {
  128. return (
  129. <Column>
  130. <ColumnBackButton multiColumn={multiColumn} />
  131. <MissingIndicator />
  132. </Column>
  133. );
  134. }
  135. let emptyMessage;
  136. const forceEmptyState = suspended || blockedBy || hidden;
  137. if (suspended) {
  138. emptyMessage = <FormattedMessage id='empty_column.account_suspended' defaultMessage='Account suspended' />;
  139. } else if (hidden) {
  140. emptyMessage = <LimitedAccountHint accountId={accountId} />;
  141. } else if (blockedBy) {
  142. emptyMessage = <FormattedMessage id='empty_column.account_unavailable' defaultMessage='Profile unavailable' />;
  143. } else if (remote && statusIds.isEmpty()) {
  144. emptyMessage = <RemoteHint url={remoteUrl} />;
  145. } else {
  146. emptyMessage = <FormattedMessage id='empty_column.account_timeline' defaultMessage='No posts found' />;
  147. }
  148. const remoteMessage = remote ? <RemoteHint url={remoteUrl} /> : null;
  149. return (
  150. <Column>
  151. <ColumnBackButton multiColumn={multiColumn} />
  152. <StatusList
  153. prepend={<HeaderContainer accountId={this.props.accountId} hideTabs={forceEmptyState} tagged={this.props.params.tagged} />}
  154. alwaysPrepend
  155. append={remoteMessage}
  156. scrollKey='account_timeline'
  157. statusIds={forceEmptyState ? emptyList : statusIds}
  158. featuredStatusIds={featuredStatusIds}
  159. isLoading={isLoading}
  160. hasMore={!forceEmptyState && hasMore}
  161. onLoadMore={this.handleLoadMore}
  162. emptyMessage={emptyMessage}
  163. bindToDocument={!multiColumn}
  164. timelineId='account'
  165. />
  166. </Column>
  167. );
  168. }
  169. }