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.

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