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. shouldUpdateScroll: PropTypes.func,
  47. statusIds: ImmutablePropTypes.list,
  48. featuredStatusIds: ImmutablePropTypes.list,
  49. isLoading: PropTypes.bool,
  50. hasMore: PropTypes.bool,
  51. withReplies: PropTypes.bool,
  52. blockedBy: PropTypes.bool,
  53. isAccount: PropTypes.bool,
  54. suspended: PropTypes.bool,
  55. remote: PropTypes.bool,
  56. remoteUrl: PropTypes.string,
  57. multiColumn: PropTypes.bool,
  58. };
  59. componentWillMount () {
  60. const { params: { accountId }, withReplies, dispatch } = this.props;
  61. dispatch(fetchAccount(accountId));
  62. dispatch(fetchAccountIdentityProofs(accountId));
  63. if (!withReplies) {
  64. dispatch(expandAccountFeaturedTimeline(accountId));
  65. }
  66. dispatch(expandAccountTimeline(accountId, { withReplies }));
  67. if (accountId === me) {
  68. dispatch(connectTimeline(`account:${me}`));
  69. }
  70. }
  71. componentWillReceiveProps (nextProps) {
  72. const { dispatch } = this.props;
  73. if ((nextProps.params.accountId !== this.props.params.accountId && nextProps.params.accountId) || nextProps.withReplies !== this.props.withReplies) {
  74. dispatch(fetchAccount(nextProps.params.accountId));
  75. dispatch(fetchAccountIdentityProofs(nextProps.params.accountId));
  76. if (!nextProps.withReplies) {
  77. dispatch(expandAccountFeaturedTimeline(nextProps.params.accountId));
  78. }
  79. dispatch(expandAccountTimeline(nextProps.params.accountId, { withReplies: nextProps.params.withReplies }));
  80. }
  81. if (nextProps.params.accountId === me && this.props.params.accountId !== me) {
  82. dispatch(connectTimeline(`account:${me}`));
  83. } else if (this.props.params.accountId === me && nextProps.params.accountId !== me) {
  84. dispatch(disconnectTimeline(`account:${me}`));
  85. }
  86. }
  87. componentWillUnmount () {
  88. const { dispatch, params: { accountId } } = this.props;
  89. if (accountId === me) {
  90. dispatch(disconnectTimeline(`account:${me}`));
  91. }
  92. }
  93. handleLoadMore = maxId => {
  94. this.props.dispatch(expandAccountTimeline(this.props.params.accountId, { maxId, withReplies: this.props.withReplies }));
  95. }
  96. render () {
  97. const { shouldUpdateScroll, statusIds, featuredStatusIds, isLoading, hasMore, blockedBy, suspended, isAccount, multiColumn, remote, remoteUrl } = this.props;
  98. if (!isAccount) {
  99. return (
  100. <Column>
  101. <ColumnBackButton multiColumn={multiColumn} />
  102. <MissingIndicator />
  103. </Column>
  104. );
  105. }
  106. if (!statusIds && isLoading) {
  107. return (
  108. <Column>
  109. <LoadingIndicator />
  110. </Column>
  111. );
  112. }
  113. let emptyMessage;
  114. if (suspended || blockedBy) {
  115. emptyMessage = <FormattedMessage id='empty_column.account_unavailable' defaultMessage='Profile unavailable' />;
  116. } else if (remote && statusIds.isEmpty()) {
  117. emptyMessage = <RemoteHint url={remoteUrl} />;
  118. } else {
  119. emptyMessage = <FormattedMessage id='empty_column.account_timeline' defaultMessage='No toots here!' />;
  120. }
  121. const remoteMessage = remote ? <RemoteHint url={remoteUrl} /> : null;
  122. return (
  123. <Column>
  124. <ColumnBackButton multiColumn={multiColumn} />
  125. <StatusList
  126. prepend={<HeaderContainer accountId={this.props.params.accountId} />}
  127. alwaysPrepend
  128. append={remoteMessage}
  129. scrollKey='account_timeline'
  130. statusIds={(suspended || blockedBy) ? emptyList : statusIds}
  131. featuredStatusIds={featuredStatusIds}
  132. isLoading={isLoading}
  133. hasMore={hasMore}
  134. onLoadMore={this.handleLoadMore}
  135. shouldUpdateScroll={shouldUpdateScroll}
  136. emptyMessage={emptyMessage}
  137. bindToDocument={!multiColumn}
  138. timelineId='account'
  139. />
  140. </Column>
  141. );
  142. }
  143. }