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.

176 lines
5.8 KiB

7 years ago
7 years ago
7 years ago
  1. import React from 'react';
  2. import { connect } from 'react-redux';
  3. import { expandHomeTimeline } from '../../actions/timelines';
  4. import PropTypes from 'prop-types';
  5. import StatusListContainer from '../ui/containers/status_list_container';
  6. import Column from '../../components/column';
  7. import ColumnHeader from '../../components/column_header';
  8. import { addColumn, removeColumn, moveColumn } from '../../actions/columns';
  9. import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
  10. import ColumnSettingsContainer from './containers/column_settings_container';
  11. import { Link } from 'react-router-dom';
  12. import { fetchAnnouncements, toggleShowAnnouncements } from 'mastodon/actions/announcements';
  13. import AnnouncementsContainer from 'mastodon/features/getting_started/containers/announcements_container';
  14. import classNames from 'classnames';
  15. import IconWithBadge from 'mastodon/components/icon_with_badge';
  16. import NotSignedInIndicator from 'mastodon/components/not_signed_in_indicator';
  17. import { Helmet } from 'react-helmet';
  18. const messages = defineMessages({
  19. title: { id: 'column.home', defaultMessage: 'Home' },
  20. show_announcements: { id: 'home.show_announcements', defaultMessage: 'Show announcements' },
  21. hide_announcements: { id: 'home.hide_announcements', defaultMessage: 'Hide announcements' },
  22. });
  23. const mapStateToProps = state => ({
  24. hasUnread: state.getIn(['timelines', 'home', 'unread']) > 0,
  25. isPartial: state.getIn(['timelines', 'home', 'isPartial']),
  26. hasAnnouncements: !state.getIn(['announcements', 'items']).isEmpty(),
  27. unreadAnnouncements: state.getIn(['announcements', 'items']).count(item => !item.get('read')),
  28. showAnnouncements: state.getIn(['announcements', 'show']),
  29. });
  30. class HomeTimeline extends React.PureComponent {
  31. static contextTypes = {
  32. identity: PropTypes.object,
  33. };
  34. static propTypes = {
  35. dispatch: PropTypes.func.isRequired,
  36. intl: PropTypes.object.isRequired,
  37. hasUnread: PropTypes.bool,
  38. isPartial: PropTypes.bool,
  39. columnId: PropTypes.string,
  40. multiColumn: PropTypes.bool,
  41. hasAnnouncements: PropTypes.bool,
  42. unreadAnnouncements: PropTypes.number,
  43. showAnnouncements: PropTypes.bool,
  44. };
  45. handlePin = () => {
  46. const { columnId, dispatch } = this.props;
  47. if (columnId) {
  48. dispatch(removeColumn(columnId));
  49. } else {
  50. dispatch(addColumn('HOME', {}));
  51. }
  52. };
  53. handleMove = (dir) => {
  54. const { columnId, dispatch } = this.props;
  55. dispatch(moveColumn(columnId, dir));
  56. };
  57. handleHeaderClick = () => {
  58. this.column.scrollTop();
  59. };
  60. setRef = c => {
  61. this.column = c;
  62. };
  63. handleLoadMore = maxId => {
  64. this.props.dispatch(expandHomeTimeline({ maxId }));
  65. };
  66. componentDidMount () {
  67. setTimeout(() => this.props.dispatch(fetchAnnouncements()), 700);
  68. this._checkIfReloadNeeded(false, this.props.isPartial);
  69. }
  70. componentDidUpdate (prevProps) {
  71. this._checkIfReloadNeeded(prevProps.isPartial, this.props.isPartial);
  72. }
  73. componentWillUnmount () {
  74. this._stopPolling();
  75. }
  76. _checkIfReloadNeeded (wasPartial, isPartial) {
  77. const { dispatch } = this.props;
  78. if (wasPartial === isPartial) {
  79. return;
  80. } else if (!wasPartial && isPartial) {
  81. this.polling = setInterval(() => {
  82. dispatch(expandHomeTimeline());
  83. }, 3000);
  84. } else if (wasPartial && !isPartial) {
  85. this._stopPolling();
  86. }
  87. }
  88. _stopPolling () {
  89. if (this.polling) {
  90. clearInterval(this.polling);
  91. this.polling = null;
  92. }
  93. }
  94. handleToggleAnnouncementsClick = (e) => {
  95. e.stopPropagation();
  96. this.props.dispatch(toggleShowAnnouncements());
  97. };
  98. render () {
  99. const { intl, hasUnread, columnId, multiColumn, hasAnnouncements, unreadAnnouncements, showAnnouncements } = this.props;
  100. const pinned = !!columnId;
  101. const { signedIn } = this.context.identity;
  102. let announcementsButton = null;
  103. if (hasAnnouncements) {
  104. announcementsButton = (
  105. <button
  106. type='button'
  107. className={classNames('column-header__button', { 'active': showAnnouncements })}
  108. title={intl.formatMessage(showAnnouncements ? messages.hide_announcements : messages.show_announcements)}
  109. aria-label={intl.formatMessage(showAnnouncements ? messages.hide_announcements : messages.show_announcements)}
  110. onClick={this.handleToggleAnnouncementsClick}
  111. >
  112. <IconWithBadge id='bullhorn' count={unreadAnnouncements} />
  113. </button>
  114. );
  115. }
  116. return (
  117. <Column bindToDocument={!multiColumn} ref={this.setRef} label={intl.formatMessage(messages.title)}>
  118. <ColumnHeader
  119. icon='home'
  120. active={hasUnread}
  121. title={intl.formatMessage(messages.title)}
  122. onPin={this.handlePin}
  123. onMove={this.handleMove}
  124. onClick={this.handleHeaderClick}
  125. pinned={pinned}
  126. multiColumn={multiColumn}
  127. extraButton={announcementsButton}
  128. appendContent={hasAnnouncements && showAnnouncements && <AnnouncementsContainer />}
  129. >
  130. <ColumnSettingsContainer />
  131. </ColumnHeader>
  132. {signedIn ? (
  133. <StatusListContainer
  134. trackScroll={!pinned}
  135. scrollKey={`home_timeline-${columnId}`}
  136. onLoadMore={this.handleLoadMore}
  137. timelineId='home'
  138. emptyMessage={<FormattedMessage id='empty_column.home' defaultMessage='Your home timeline is empty! Follow more people to fill it up. {suggestions}' values={{ suggestions: <Link to='/start'><FormattedMessage id='empty_column.home.suggestions' defaultMessage='See some suggestions' /></Link> }} />}
  139. bindToDocument={!multiColumn}
  140. />
  141. ) : <NotSignedInIndicator />}
  142. <Helmet>
  143. <title>{intl.formatMessage(messages.title)}</title>
  144. <meta name='robots' content='noindex' />
  145. </Helmet>
  146. </Column>
  147. );
  148. }
  149. }
  150. export default connect(mapStateToProps)(injectIntl(HomeTimeline));