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.

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