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.

141 lines
4.3 KiB

  1. import React from 'react';
  2. import { connect } from 'react-redux';
  3. import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
  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 { expandPublicTimeline } from '../../actions/timelines';
  9. import { addColumn, removeColumn, moveColumn } from '../../actions/columns';
  10. import ColumnSettingsContainer from './containers/column_settings_container';
  11. import { connectPublicStream } from '../../actions/streaming';
  12. const messages = defineMessages({
  13. title: { id: 'column.public', defaultMessage: 'Federated timeline' },
  14. });
  15. const mapStateToProps = (state, { onlyMedia, columnId }) => {
  16. const uuid = columnId;
  17. const columns = state.getIn(['settings', 'columns']);
  18. const index = columns.findIndex(c => c.get('uuid') === uuid);
  19. return {
  20. hasUnread: state.getIn(['timelines', `public${onlyMedia ? ':media' : ''}`, 'unread']) > 0,
  21. onlyMedia: (columnId && index >= 0) ? columns.get(index).getIn(['params', 'other', 'onlyMedia']) : state.getIn(['settings', 'public', 'other', 'onlyMedia']),
  22. };
  23. };
  24. export default @connect(mapStateToProps)
  25. @injectIntl
  26. class PublicTimeline extends React.PureComponent {
  27. static contextTypes = {
  28. router: PropTypes.object,
  29. };
  30. static defaultProps = {
  31. onlyMedia: false,
  32. };
  33. static propTypes = {
  34. dispatch: PropTypes.func.isRequired,
  35. shouldUpdateScroll: PropTypes.func,
  36. intl: PropTypes.object.isRequired,
  37. columnId: PropTypes.string,
  38. multiColumn: PropTypes.bool,
  39. hasUnread: PropTypes.bool,
  40. onlyMedia: PropTypes.bool,
  41. };
  42. handlePin = () => {
  43. const { columnId, dispatch, onlyMedia } = this.props;
  44. if (columnId) {
  45. dispatch(removeColumn(columnId));
  46. } else {
  47. dispatch(addColumn('PUBLIC', { other: { onlyMedia } }));
  48. }
  49. }
  50. handleMove = (dir) => {
  51. const { columnId, dispatch } = this.props;
  52. dispatch(moveColumn(columnId, dir));
  53. }
  54. handleHeaderClick = () => {
  55. this.column.scrollTop();
  56. }
  57. componentDidMount () {
  58. const { dispatch, onlyMedia } = this.props;
  59. dispatch(expandPublicTimeline({ onlyMedia }));
  60. this.disconnect = dispatch(connectPublicStream({ onlyMedia }));
  61. }
  62. componentDidUpdate (prevProps) {
  63. if (prevProps.onlyMedia !== this.props.onlyMedia) {
  64. const { dispatch, onlyMedia } = this.props;
  65. this.disconnect();
  66. dispatch(expandPublicTimeline({ onlyMedia }));
  67. this.disconnect = dispatch(connectPublicStream({ onlyMedia }));
  68. }
  69. }
  70. componentWillUnmount () {
  71. if (this.disconnect) {
  72. this.disconnect();
  73. this.disconnect = null;
  74. }
  75. }
  76. setRef = c => {
  77. this.column = c;
  78. }
  79. handleLoadMore = maxId => {
  80. const { dispatch, onlyMedia } = this.props;
  81. dispatch(expandPublicTimeline({ maxId, onlyMedia }));
  82. }
  83. handleSettingChanged = (key, checked) => {
  84. const { columnId } = this.props;
  85. if (!columnId && key[0] === 'other' && key[1] === 'onlyMedia') {
  86. this.context.router.history.replace(`/timelines/public${checked ? '/media' : ''}`);
  87. }
  88. }
  89. render () {
  90. const { intl, shouldUpdateScroll, columnId, hasUnread, multiColumn, onlyMedia } = this.props;
  91. const pinned = !!columnId;
  92. return (
  93. <Column ref={this.setRef} label={intl.formatMessage(messages.title)}>
  94. <ColumnHeader
  95. icon='globe'
  96. active={hasUnread}
  97. title={intl.formatMessage(messages.title)}
  98. onPin={this.handlePin}
  99. onMove={this.handleMove}
  100. onClick={this.handleHeaderClick}
  101. pinned={pinned}
  102. multiColumn={multiColumn}
  103. >
  104. <ColumnSettingsContainer onChange={this.handleSettingChanged} columnId={columnId} />
  105. </ColumnHeader>
  106. <StatusListContainer
  107. timelineId={`public${onlyMedia ? ':media' : ''}`}
  108. onLoadMore={this.handleLoadMore}
  109. trackScroll={!pinned}
  110. scrollKey={`public_timeline-${columnId}`}
  111. emptyMessage={<FormattedMessage id='empty_column.public' defaultMessage='There is nothing here! Write something publicly, or manually follow users from other instances to fill it up' />}
  112. shouldUpdateScroll={shouldUpdateScroll}
  113. />
  114. </Column>
  115. );
  116. }
  117. }