index.js 7.0 KB

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