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.

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