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.

178 lines
6.0 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 'flavours/glitch/actions/timelines';
  4. import PropTypes from 'prop-types';
  5. import StatusListContainer from 'flavours/glitch/features/ui/containers/status_list_container';
  6. import Column from 'flavours/glitch/components/column';
  7. import ColumnHeader from 'flavours/glitch/components/column_header';
  8. import { addColumn, removeColumn, moveColumn } from 'flavours/glitch/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 'flavours/glitch/actions/announcements';
  13. import AnnouncementsContainer from 'flavours/glitch/features/getting_started/containers/announcements_container';
  14. import classNames from 'classnames';
  15. import IconWithBadge from 'flavours/glitch/components/icon_with_badge';
  16. import NotSignedInIndicator from 'flavours/glitch/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. regex: state.getIn(['settings', 'home', 'regex', 'body']),
  30. });
  31. class HomeTimeline extends React.PureComponent {
  32. static contextTypes = {
  33. identity: PropTypes.object,
  34. };
  35. static propTypes = {
  36. dispatch: PropTypes.func.isRequired,
  37. intl: PropTypes.object.isRequired,
  38. hasUnread: PropTypes.bool,
  39. isPartial: PropTypes.bool,
  40. columnId: PropTypes.string,
  41. multiColumn: PropTypes.bool,
  42. hasAnnouncements: PropTypes.bool,
  43. unreadAnnouncements: PropTypes.number,
  44. showAnnouncements: PropTypes.bool,
  45. regex: PropTypes.string,
  46. };
  47. handlePin = () => {
  48. const { columnId, dispatch } = this.props;
  49. if (columnId) {
  50. dispatch(removeColumn(columnId));
  51. } else {
  52. dispatch(addColumn('HOME', {}));
  53. }
  54. };
  55. handleMove = (dir) => {
  56. const { columnId, dispatch } = this.props;
  57. dispatch(moveColumn(columnId, dir));
  58. };
  59. handleHeaderClick = () => {
  60. this.column.scrollTop();
  61. };
  62. setRef = c => {
  63. this.column = c;
  64. };
  65. handleLoadMore = maxId => {
  66. this.props.dispatch(expandHomeTimeline({ maxId }));
  67. };
  68. componentDidMount () {
  69. setTimeout(() => this.props.dispatch(fetchAnnouncements()), 700);
  70. this._checkIfReloadNeeded(false, this.props.isPartial);
  71. }
  72. componentDidUpdate (prevProps) {
  73. this._checkIfReloadNeeded(prevProps.isPartial, this.props.isPartial);
  74. }
  75. componentWillUnmount () {
  76. this._stopPolling();
  77. }
  78. _checkIfReloadNeeded (wasPartial, isPartial) {
  79. const { dispatch } = this.props;
  80. if (wasPartial === isPartial) {
  81. return;
  82. } else if (!wasPartial && isPartial) {
  83. this.polling = setInterval(() => {
  84. dispatch(expandHomeTimeline());
  85. }, 3000);
  86. } else if (wasPartial && !isPartial) {
  87. this._stopPolling();
  88. }
  89. }
  90. _stopPolling () {
  91. if (this.polling) {
  92. clearInterval(this.polling);
  93. this.polling = null;
  94. }
  95. }
  96. handleToggleAnnouncementsClick = (e) => {
  97. e.stopPropagation();
  98. this.props.dispatch(toggleShowAnnouncements());
  99. };
  100. render () {
  101. const { intl, hasUnread, columnId, multiColumn, hasAnnouncements, unreadAnnouncements, showAnnouncements } = this.props;
  102. const pinned = !!columnId;
  103. const { signedIn } = this.context.identity;
  104. let announcementsButton = null;
  105. if (hasAnnouncements) {
  106. announcementsButton = (
  107. <button
  108. className={classNames('column-header__button', { 'active': showAnnouncements })}
  109. title={intl.formatMessage(showAnnouncements ? messages.hide_announcements : messages.show_announcements)}
  110. aria-label={intl.formatMessage(showAnnouncements ? messages.hide_announcements : messages.show_announcements)}
  111. onClick={this.handleToggleAnnouncementsClick}
  112. >
  113. <IconWithBadge id='bullhorn' count={unreadAnnouncements} />
  114. </button>
  115. );
  116. }
  117. return (
  118. <Column bindToDocument={!multiColumn} ref={this.setRef} name='home' label={intl.formatMessage(messages.title)}>
  119. <ColumnHeader
  120. icon='home'
  121. active={hasUnread}
  122. title={intl.formatMessage(messages.title)}
  123. onPin={this.handlePin}
  124. onMove={this.handleMove}
  125. onClick={this.handleHeaderClick}
  126. pinned={pinned}
  127. multiColumn={multiColumn}
  128. extraButton={announcementsButton}
  129. appendContent={hasAnnouncements && showAnnouncements && <AnnouncementsContainer />}
  130. >
  131. <ColumnSettingsContainer />
  132. </ColumnHeader>
  133. {signedIn ? (
  134. <StatusListContainer
  135. trackScroll={!pinned}
  136. scrollKey={`home_timeline-${columnId}`}
  137. onLoadMore={this.handleLoadMore}
  138. timelineId='home'
  139. 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> }} />}
  140. bindToDocument={!multiColumn}
  141. regex={this.props.regex}
  142. />
  143. ) : <NotSignedInIndicator />}
  144. <Helmet>
  145. <title>{intl.formatMessage(messages.title)}</title>
  146. <meta name='robots' content='noindex' />
  147. </Helmet>
  148. </Column>
  149. );
  150. }
  151. }
  152. export default connect(mapStateToProps)(injectIntl(HomeTimeline));