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.

139 lines
4.1 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. @connect(mapStateToProps)
  25. @injectIntl
  26. export default 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. intl: PropTypes.object.isRequired,
  36. columnId: PropTypes.string,
  37. multiColumn: PropTypes.bool,
  38. hasUnread: PropTypes.bool,
  39. onlyMedia: PropTypes.bool,
  40. };
  41. handlePin = () => {
  42. const { columnId, dispatch, onlyMedia } = this.props;
  43. if (columnId) {
  44. dispatch(removeColumn(columnId));
  45. } else {
  46. dispatch(addColumn('PUBLIC', { other: { onlyMedia } }));
  47. }
  48. }
  49. handleMove = (dir) => {
  50. const { columnId, dispatch } = this.props;
  51. dispatch(moveColumn(columnId, dir));
  52. }
  53. handleHeaderClick = () => {
  54. this.column.scrollTop();
  55. }
  56. componentDidMount () {
  57. const { dispatch, onlyMedia } = this.props;
  58. dispatch(expandPublicTimeline({ onlyMedia }));
  59. this.disconnect = dispatch(connectPublicStream({ onlyMedia }));
  60. }
  61. componentDidUpdate (prevProps) {
  62. if (prevProps.onlyMedia !== this.props.onlyMedia) {
  63. const { dispatch, onlyMedia } = this.props;
  64. this.disconnect();
  65. dispatch(expandPublicTimeline({ onlyMedia }));
  66. this.disconnect = dispatch(connectPublicStream({ onlyMedia }));
  67. }
  68. }
  69. componentWillUnmount () {
  70. if (this.disconnect) {
  71. this.disconnect();
  72. this.disconnect = null;
  73. }
  74. }
  75. setRef = c => {
  76. this.column = c;
  77. }
  78. handleLoadMore = maxId => {
  79. const { dispatch, onlyMedia } = this.props;
  80. dispatch(expandPublicTimeline({ maxId, onlyMedia }));
  81. }
  82. handleSettingChanged = (key, checked) => {
  83. const { columnId } = this.props;
  84. if (!columnId && key[0] === 'other' && key[1] === 'onlyMedia') {
  85. this.context.router.history.replace(`/timelines/public${checked ? '/media' : ''}`);
  86. }
  87. }
  88. render () {
  89. const { intl, columnId, hasUnread, multiColumn, onlyMedia } = this.props;
  90. const pinned = !!columnId;
  91. return (
  92. <Column ref={this.setRef}>
  93. <ColumnHeader
  94. icon='globe'
  95. active={hasUnread}
  96. title={intl.formatMessage(messages.title)}
  97. onPin={this.handlePin}
  98. onMove={this.handleMove}
  99. onClick={this.handleHeaderClick}
  100. pinned={pinned}
  101. multiColumn={multiColumn}
  102. >
  103. <ColumnSettingsContainer onChange={this.handleSettingChanged} columnId={columnId} />
  104. </ColumnHeader>
  105. <StatusListContainer
  106. timelineId={`public${onlyMedia ? ':media' : ''}`}
  107. onLoadMore={this.handleLoadMore}
  108. trackScroll={!pinned}
  109. scrollKey={`public_timeline-${columnId}`}
  110. 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' />}
  111. />
  112. </Column>
  113. );
  114. }
  115. }