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.

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