The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

165 lines
5.5 KiB

  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. const messages = defineMessages({
  17. title: { id: 'column.home', defaultMessage: 'Home' },
  18. show_announcements: { id: 'home.show_announcements', defaultMessage: 'Show announcements' },
  19. hide_announcements: { id: 'home.hide_announcements', defaultMessage: 'Hide announcements' },
  20. });
  21. const mapStateToProps = state => ({
  22. hasUnread: state.getIn(['timelines', 'home', 'unread']) > 0,
  23. isPartial: state.getIn(['timelines', 'home', 'isPartial']),
  24. hasAnnouncements: !state.getIn(['announcements', 'items']).isEmpty(),
  25. unreadAnnouncements: state.getIn(['announcements', 'unread']).size,
  26. showAnnouncements: state.getIn(['announcements', 'show']),
  27. });
  28. export default @connect(mapStateToProps)
  29. @injectIntl
  30. class HomeTimeline extends React.PureComponent {
  31. static propTypes = {
  32. dispatch: PropTypes.func.isRequired,
  33. shouldUpdateScroll: PropTypes.func,
  34. intl: PropTypes.object.isRequired,
  35. hasUnread: PropTypes.bool,
  36. isPartial: PropTypes.bool,
  37. columnId: PropTypes.string,
  38. multiColumn: PropTypes.bool,
  39. hasAnnouncements: PropTypes.bool,
  40. unreadAnnouncements: PropTypes.number,
  41. showAnnouncements: PropTypes.bool,
  42. };
  43. handlePin = () => {
  44. const { columnId, dispatch } = this.props;
  45. if (columnId) {
  46. dispatch(removeColumn(columnId));
  47. } else {
  48. dispatch(addColumn('HOME', {}));
  49. }
  50. }
  51. handleMove = (dir) => {
  52. const { columnId, dispatch } = this.props;
  53. dispatch(moveColumn(columnId, dir));
  54. }
  55. handleHeaderClick = () => {
  56. this.column.scrollTop();
  57. }
  58. setRef = c => {
  59. this.column = c;
  60. }
  61. handleLoadMore = maxId => {
  62. this.props.dispatch(expandHomeTimeline({ maxId }));
  63. }
  64. componentDidMount () {
  65. this.props.dispatch(fetchAnnouncements());
  66. this._checkIfReloadNeeded(false, this.props.isPartial);
  67. }
  68. componentDidUpdate (prevProps) {
  69. this._checkIfReloadNeeded(prevProps.isPartial, this.props.isPartial);
  70. }
  71. componentWillUnmount () {
  72. this._stopPolling();
  73. }
  74. _checkIfReloadNeeded (wasPartial, isPartial) {
  75. const { dispatch } = this.props;
  76. if (wasPartial === isPartial) {
  77. return;
  78. } else if (!wasPartial && isPartial) {
  79. this.polling = setInterval(() => {
  80. dispatch(expandHomeTimeline());
  81. }, 3000);
  82. } else if (wasPartial && !isPartial) {
  83. this._stopPolling();
  84. }
  85. }
  86. _stopPolling () {
  87. if (this.polling) {
  88. clearInterval(this.polling);
  89. this.polling = null;
  90. }
  91. }
  92. handleToggleAnnouncementsClick = (e) => {
  93. e.stopPropagation();
  94. this.props.dispatch(toggleShowAnnouncements());
  95. }
  96. render () {
  97. const { intl, shouldUpdateScroll, hasUnread, columnId, multiColumn, hasAnnouncements, unreadAnnouncements, showAnnouncements } = this.props;
  98. const pinned = !!columnId;
  99. let announcementsButton = null;
  100. if (hasAnnouncements) {
  101. announcementsButton = (
  102. <button
  103. className={classNames('column-header__button', { 'active': showAnnouncements })}
  104. title={intl.formatMessage(showAnnouncements ? messages.hide_announcements : messages.show_announcements)}
  105. aria-label={intl.formatMessage(showAnnouncements ? messages.hide_announcements : messages.show_announcements)}
  106. aria-pressed={showAnnouncements ? 'true' : 'false'}
  107. onClick={this.handleToggleAnnouncementsClick}
  108. >
  109. <IconWithBadge id='bullhorn' count={unreadAnnouncements} />
  110. </button>
  111. );
  112. }
  113. return (
  114. <Column bindToDocument={!multiColumn} ref={this.setRef} label={intl.formatMessage(messages.title)}>
  115. <ColumnHeader
  116. icon='home'
  117. active={hasUnread}
  118. title={intl.formatMessage(messages.title)}
  119. onPin={this.handlePin}
  120. onMove={this.handleMove}
  121. onClick={this.handleHeaderClick}
  122. pinned={pinned}
  123. multiColumn={multiColumn}
  124. extraButton={announcementsButton}
  125. appendContent={hasAnnouncements && showAnnouncements && <AnnouncementsContainer />}
  126. >
  127. <ColumnSettingsContainer />
  128. </ColumnHeader>
  129. <StatusListContainer
  130. trackScroll={!pinned}
  131. scrollKey={`home_timeline-${columnId}`}
  132. onLoadMore={this.handleLoadMore}
  133. timelineId='home'
  134. emptyMessage={<FormattedMessage id='empty_column.home' defaultMessage='Your home timeline is empty! Visit {public} or use search to get started and meet other users.' values={{ public: <Link to='/timelines/public'><FormattedMessage id='empty_column.home.public_timeline' defaultMessage='the public timeline' /></Link> }} />}
  135. shouldUpdateScroll={shouldUpdateScroll}
  136. bindToDocument={!multiColumn}
  137. />
  138. </Column>
  139. );
  140. }
  141. }