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.

136 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 '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 { expandPublicTimeline } from 'flavours/glitch/actions/timelines';
  9. import { addColumn, removeColumn, moveColumn } from 'flavours/glitch/actions/columns';
  10. import ColumnSettingsContainer from './containers/column_settings_container';
  11. import { connectPublicStream } from 'flavours/glitch/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 defaultProps = {
  28. onlyMedia: false,
  29. };
  30. static contextTypes = {
  31. router: PropTypes.object,
  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. shouldUpdateScroll = (prevRouterProps, { location }) => {
  83. return !(location.state && location.state.mastodonModalOpen)
  84. }
  85. render () {
  86. const { intl, columnId, hasUnread, multiColumn, onlyMedia } = this.props;
  87. const pinned = !!columnId;
  88. return (
  89. <Column ref={this.setRef} name='federated' label={intl.formatMessage(messages.title)}>
  90. <ColumnHeader
  91. icon='globe'
  92. active={hasUnread}
  93. title={intl.formatMessage(messages.title)}
  94. onPin={this.handlePin}
  95. onMove={this.handleMove}
  96. onClick={this.handleHeaderClick}
  97. pinned={pinned}
  98. multiColumn={multiColumn}
  99. >
  100. <ColumnSettingsContainer columnId={columnId} />
  101. </ColumnHeader>
  102. <StatusListContainer
  103. timelineId={`public${onlyMedia ? ':media' : ''}`}
  104. onLoadMore={this.handleLoadMore}
  105. trackScroll={!pinned}
  106. scrollKey={`public_timeline-${columnId}`}
  107. 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' />}
  108. />
  109. </Column>
  110. );
  111. }
  112. }