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.

172 lines
6.1 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 { 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 { fetchAccountIdentityProofs } from '../../actions/identity_proofs';
  16. import MissingIndicator from 'mastodon/components/missing_indicator';
  17. import TimelineHint from 'mastodon/components/timeline_hint';
  18. import { me } from 'mastodon/initial_state';
  19. import { connectTimeline, disconnectTimeline } from 'mastodon/actions/timelines';
  20. const emptyList = ImmutableList();
  21. const mapStateToProps = (state, { params: { accountId }, withReplies = false }) => {
  22. const path = withReplies ? `${accountId}:with_replies` : accountId;
  23. return {
  24. remote: !!(state.getIn(['accounts', accountId, 'acct']) !== state.getIn(['accounts', accountId, 'username'])),
  25. remoteUrl: state.getIn(['accounts', accountId, 'url']),
  26. isAccount: !!state.getIn(['accounts', accountId]),
  27. statusIds: state.getIn(['timelines', `account:${path}`, 'items'], emptyList),
  28. featuredStatusIds: withReplies ? ImmutableList() : state.getIn(['timelines', `account:${accountId}:pinned`, 'items'], emptyList),
  29. isLoading: state.getIn(['timelines', `account:${path}`, 'isLoading']),
  30. hasMore: state.getIn(['timelines', `account:${path}`, 'hasMore']),
  31. suspended: state.getIn(['accounts', accountId, 'suspended'], false),
  32. blockedBy: state.getIn(['relationships', accountId, 'blocked_by'], false),
  33. };
  34. };
  35. const RemoteHint = ({ url }) => (
  36. <TimelineHint url={url} resource={<FormattedMessage id='timeline_hint.resources.statuses' defaultMessage='Older toots' />} />
  37. );
  38. RemoteHint.propTypes = {
  39. url: PropTypes.string.isRequired,
  40. };
  41. export default @connect(mapStateToProps)
  42. class AccountTimeline extends ImmutablePureComponent {
  43. static propTypes = {
  44. params: PropTypes.object.isRequired,
  45. dispatch: PropTypes.func.isRequired,
  46. statusIds: ImmutablePropTypes.list,
  47. featuredStatusIds: ImmutablePropTypes.list,
  48. isLoading: PropTypes.bool,
  49. hasMore: PropTypes.bool,
  50. withReplies: PropTypes.bool,
  51. blockedBy: PropTypes.bool,
  52. isAccount: PropTypes.bool,
  53. suspended: PropTypes.bool,
  54. remote: PropTypes.bool,
  55. remoteUrl: PropTypes.string,
  56. multiColumn: PropTypes.bool,
  57. };
  58. componentWillMount () {
  59. const { params: { accountId }, withReplies, dispatch } = this.props;
  60. dispatch(fetchAccount(accountId));
  61. dispatch(fetchAccountIdentityProofs(accountId));
  62. if (!withReplies) {
  63. dispatch(expandAccountFeaturedTimeline(accountId));
  64. }
  65. dispatch(expandAccountTimeline(accountId, { withReplies }));
  66. if (accountId === me) {
  67. dispatch(connectTimeline(`account:${me}`));
  68. }
  69. }
  70. componentWillReceiveProps (nextProps) {
  71. const { dispatch } = this.props;
  72. if ((nextProps.params.accountId !== this.props.params.accountId && nextProps.params.accountId) || nextProps.withReplies !== this.props.withReplies) {
  73. dispatch(fetchAccount(nextProps.params.accountId));
  74. dispatch(fetchAccountIdentityProofs(nextProps.params.accountId));
  75. if (!nextProps.withReplies) {
  76. dispatch(expandAccountFeaturedTimeline(nextProps.params.accountId));
  77. }
  78. dispatch(expandAccountTimeline(nextProps.params.accountId, { withReplies: nextProps.params.withReplies }));
  79. }
  80. if (nextProps.params.accountId === me && this.props.params.accountId !== me) {
  81. dispatch(connectTimeline(`account:${me}`));
  82. } else if (this.props.params.accountId === me && nextProps.params.accountId !== me) {
  83. dispatch(disconnectTimeline(`account:${me}`));
  84. }
  85. }
  86. componentWillUnmount () {
  87. const { dispatch, params: { accountId } } = this.props;
  88. if (accountId === me) {
  89. dispatch(disconnectTimeline(`account:${me}`));
  90. }
  91. }
  92. handleLoadMore = maxId => {
  93. this.props.dispatch(expandAccountTimeline(this.props.params.accountId, { maxId, withReplies: this.props.withReplies }));
  94. }
  95. render () {
  96. const { statusIds, featuredStatusIds, isLoading, hasMore, blockedBy, suspended, isAccount, multiColumn, remote, remoteUrl } = this.props;
  97. if (!isAccount) {
  98. return (
  99. <Column>
  100. <ColumnBackButton multiColumn={multiColumn} />
  101. <MissingIndicator />
  102. </Column>
  103. );
  104. }
  105. if (!statusIds && isLoading) {
  106. return (
  107. <Column>
  108. <LoadingIndicator />
  109. </Column>
  110. );
  111. }
  112. let emptyMessage;
  113. if (suspended) {
  114. emptyMessage = <FormattedMessage id='empty_column.account_suspended' defaultMessage='Account suspended' />;
  115. } else if (blockedBy) {
  116. emptyMessage = <FormattedMessage id='empty_column.account_unavailable' defaultMessage='Profile unavailable' />;
  117. } else if (remote && statusIds.isEmpty()) {
  118. emptyMessage = <RemoteHint url={remoteUrl} />;
  119. } else {
  120. emptyMessage = <FormattedMessage id='empty_column.account_timeline' defaultMessage='No toots here!' />;
  121. }
  122. const remoteMessage = remote ? <RemoteHint url={remoteUrl} /> : null;
  123. return (
  124. <Column>
  125. <ColumnBackButton multiColumn={multiColumn} />
  126. <StatusList
  127. prepend={<HeaderContainer accountId={this.props.params.accountId} />}
  128. alwaysPrepend
  129. append={remoteMessage}
  130. scrollKey='account_timeline'
  131. statusIds={(suspended || blockedBy) ? emptyList : statusIds}
  132. featuredStatusIds={featuredStatusIds}
  133. isLoading={isLoading}
  134. hasMore={hasMore}
  135. onLoadMore={this.handleLoadMore}
  136. emptyMessage={emptyMessage}
  137. bindToDocument={!multiColumn}
  138. timelineId='account'
  139. />
  140. </Column>
  141. );
  142. }
  143. }