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.
 
 
 
 

132 lines
3.9 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 AnnouncementsContainer from 'mastodon/features/getting_started/containers/announcements_container';
  13. const messages = defineMessages({
  14. title: { id: 'column.home', defaultMessage: 'Home' },
  15. });
  16. const mapStateToProps = state => ({
  17. hasUnread: state.getIn(['timelines', 'home', 'unread']) > 0,
  18. isPartial: state.getIn(['timelines', 'home', 'isPartial']),
  19. });
  20. export default @connect(mapStateToProps)
  21. @injectIntl
  22. class HomeTimeline extends React.PureComponent {
  23. static propTypes = {
  24. dispatch: PropTypes.func.isRequired,
  25. shouldUpdateScroll: PropTypes.func,
  26. intl: PropTypes.object.isRequired,
  27. hasUnread: PropTypes.bool,
  28. isPartial: PropTypes.bool,
  29. columnId: PropTypes.string,
  30. multiColumn: PropTypes.bool,
  31. };
  32. handlePin = () => {
  33. const { columnId, dispatch } = this.props;
  34. if (columnId) {
  35. dispatch(removeColumn(columnId));
  36. } else {
  37. dispatch(addColumn('HOME', {}));
  38. }
  39. }
  40. handleMove = (dir) => {
  41. const { columnId, dispatch } = this.props;
  42. dispatch(moveColumn(columnId, dir));
  43. }
  44. handleHeaderClick = () => {
  45. this.column.scrollTop();
  46. }
  47. setRef = c => {
  48. this.column = c;
  49. }
  50. handleLoadMore = maxId => {
  51. this.props.dispatch(expandHomeTimeline({ maxId }));
  52. }
  53. componentDidMount () {
  54. this._checkIfReloadNeeded(false, this.props.isPartial);
  55. }
  56. componentDidUpdate (prevProps) {
  57. this._checkIfReloadNeeded(prevProps.isPartial, this.props.isPartial);
  58. }
  59. componentWillUnmount () {
  60. this._stopPolling();
  61. }
  62. _checkIfReloadNeeded (wasPartial, isPartial) {
  63. const { dispatch } = this.props;
  64. if (wasPartial === isPartial) {
  65. return;
  66. } else if (!wasPartial && isPartial) {
  67. this.polling = setInterval(() => {
  68. dispatch(expandHomeTimeline());
  69. }, 3000);
  70. } else if (wasPartial && !isPartial) {
  71. this._stopPolling();
  72. }
  73. }
  74. _stopPolling () {
  75. if (this.polling) {
  76. clearInterval(this.polling);
  77. this.polling = null;
  78. }
  79. }
  80. render () {
  81. const { intl, shouldUpdateScroll, hasUnread, columnId, multiColumn } = this.props;
  82. const pinned = !!columnId;
  83. return (
  84. <Column bindToDocument={!multiColumn} ref={this.setRef} label={intl.formatMessage(messages.title)}>
  85. <ColumnHeader
  86. icon='home'
  87. active={hasUnread}
  88. title={intl.formatMessage(messages.title)}
  89. onPin={this.handlePin}
  90. onMove={this.handleMove}
  91. onClick={this.handleHeaderClick}
  92. pinned={pinned}
  93. multiColumn={multiColumn}
  94. >
  95. <ColumnSettingsContainer />
  96. </ColumnHeader>
  97. <StatusListContainer
  98. prepend={<AnnouncementsContainer />}
  99. alwaysPrepend
  100. trackScroll={!pinned}
  101. scrollKey={`home_timeline-${columnId}`}
  102. onLoadMore={this.handleLoadMore}
  103. timelineId='home'
  104. 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> }} />}
  105. shouldUpdateScroll={shouldUpdateScroll}
  106. bindToDocument={!multiColumn}
  107. />
  108. </Column>
  109. );
  110. }
  111. }