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.

140 lines
4.6 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, { columnId }) => {
  16. const uuid = columnId;
  17. const columns = state.getIn(['settings', 'columns']);
  18. const index = columns.findIndex(c => c.get('uuid') === uuid);
  19. const onlyMedia = (columnId && index >= 0) ? columns.get(index).getIn(['params', 'other', 'onlyMedia']) : state.getIn(['settings', 'public', 'other', 'onlyMedia']);
  20. const onlyRemote = (columnId && index >= 0) ? columns.get(index).getIn(['params', 'other', 'onlyRemote']) : state.getIn(['settings', 'public', 'other', 'onlyRemote']);
  21. const timelineState = state.getIn(['timelines', `public${onlyMedia ? ':media' : ''}`]);
  22. return {
  23. hasUnread: !!timelineState && timelineState.get('unread') > 0,
  24. onlyMedia,
  25. onlyRemote,
  26. };
  27. };
  28. export default @connect(mapStateToProps)
  29. @injectIntl
  30. class PublicTimeline extends React.PureComponent {
  31. static contextTypes = {
  32. router: PropTypes.object,
  33. };
  34. static defaultProps = {
  35. onlyMedia: false,
  36. };
  37. static propTypes = {
  38. dispatch: PropTypes.func.isRequired,
  39. shouldUpdateScroll: PropTypes.func,
  40. intl: PropTypes.object.isRequired,
  41. columnId: PropTypes.string,
  42. multiColumn: PropTypes.bool,
  43. hasUnread: PropTypes.bool,
  44. onlyMedia: PropTypes.bool,
  45. onlyRemote: PropTypes.bool,
  46. };
  47. handlePin = () => {
  48. const { columnId, dispatch, onlyMedia, onlyRemote } = this.props;
  49. if (columnId) {
  50. dispatch(removeColumn(columnId));
  51. } else {
  52. dispatch(addColumn(onlyRemote ? 'REMOTE' : 'PUBLIC', { other: { onlyMedia, onlyRemote } }));
  53. }
  54. }
  55. handleMove = (dir) => {
  56. const { columnId, dispatch } = this.props;
  57. dispatch(moveColumn(columnId, dir));
  58. }
  59. handleHeaderClick = () => {
  60. this.column.scrollTop();
  61. }
  62. componentDidMount () {
  63. const { dispatch, onlyMedia, onlyRemote } = this.props;
  64. dispatch(expandPublicTimeline({ onlyMedia, onlyRemote }));
  65. this.disconnect = dispatch(connectPublicStream({ onlyMedia, onlyRemote }));
  66. }
  67. componentDidUpdate (prevProps) {
  68. if (prevProps.onlyMedia !== this.props.onlyMedia || prevProps.onlyRemote !== this.props.onlyRemote) {
  69. const { dispatch, onlyMedia, onlyRemote } = this.props;
  70. this.disconnect();
  71. dispatch(expandPublicTimeline({ onlyMedia, onlyRemote }));
  72. this.disconnect = dispatch(connectPublicStream({ onlyMedia, onlyRemote }));
  73. }
  74. }
  75. componentWillUnmount () {
  76. if (this.disconnect) {
  77. this.disconnect();
  78. this.disconnect = null;
  79. }
  80. }
  81. setRef = c => {
  82. this.column = c;
  83. }
  84. handleLoadMore = maxId => {
  85. const { dispatch, onlyMedia, onlyRemote } = this.props;
  86. dispatch(expandPublicTimeline({ maxId, onlyMedia, onlyRemote }));
  87. }
  88. render () {
  89. const { intl, shouldUpdateScroll, columnId, hasUnread, multiColumn, onlyMedia, onlyRemote } = this.props;
  90. const pinned = !!columnId;
  91. return (
  92. <Column bindToDocument={!multiColumn} ref={this.setRef} label={intl.formatMessage(messages.title)}>
  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 columnId={columnId} />
  104. </ColumnHeader>
  105. <StatusListContainer
  106. timelineId={`public${onlyRemote ? ':remote' : ''}${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 servers to fill it up' />}
  111. shouldUpdateScroll={shouldUpdateScroll}
  112. bindToDocument={!multiColumn}
  113. />
  114. </Column>
  115. );
  116. }
  117. }