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.

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