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.

160 lines
4.9 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 { expandCommunityTimeline } from '../../actions/timelines';
  9. import { addColumn, removeColumn, moveColumn } from '../../actions/columns';
  10. import ColumnSettingsContainer from './containers/column_settings_container';
  11. import { connectCommunityStream } from '../../actions/streaming';
  12. import { Helmet } from 'react-helmet';
  13. import { domain } from 'mastodon/initial_state';
  14. import DismissableBanner from 'mastodon/components/dismissable_banner';
  15. const messages = defineMessages({
  16. title: { id: 'column.community', defaultMessage: 'Local timeline' },
  17. });
  18. const mapStateToProps = (state, { columnId }) => {
  19. const uuid = columnId;
  20. const columns = state.getIn(['settings', 'columns']);
  21. const index = columns.findIndex(c => c.get('uuid') === uuid);
  22. const onlyMedia = (columnId && index >= 0) ? columns.get(index).getIn(['params', 'other', 'onlyMedia']) : state.getIn(['settings', 'community', 'other', 'onlyMedia']);
  23. const timelineState = state.getIn(['timelines', `community${onlyMedia ? ':media' : ''}`]);
  24. return {
  25. hasUnread: !!timelineState && timelineState.get('unread') > 0,
  26. onlyMedia,
  27. };
  28. };
  29. class CommunityTimeline extends React.PureComponent {
  30. static contextTypes = {
  31. router: PropTypes.object,
  32. identity: PropTypes.object,
  33. };
  34. static defaultProps = {
  35. onlyMedia: false,
  36. };
  37. static propTypes = {
  38. dispatch: PropTypes.func.isRequired,
  39. columnId: PropTypes.string,
  40. intl: PropTypes.object.isRequired,
  41. hasUnread: PropTypes.bool,
  42. multiColumn: PropTypes.bool,
  43. onlyMedia: PropTypes.bool,
  44. };
  45. handlePin = () => {
  46. const { columnId, dispatch, onlyMedia } = this.props;
  47. if (columnId) {
  48. dispatch(removeColumn(columnId));
  49. } else {
  50. dispatch(addColumn('COMMUNITY', { other: { onlyMedia } }));
  51. }
  52. };
  53. handleMove = (dir) => {
  54. const { columnId, dispatch } = this.props;
  55. dispatch(moveColumn(columnId, dir));
  56. };
  57. handleHeaderClick = () => {
  58. this.column.scrollTop();
  59. };
  60. componentDidMount () {
  61. const { dispatch, onlyMedia } = this.props;
  62. const { signedIn } = this.context.identity;
  63. dispatch(expandCommunityTimeline({ onlyMedia }));
  64. if (signedIn) {
  65. this.disconnect = dispatch(connectCommunityStream({ onlyMedia }));
  66. }
  67. }
  68. componentDidUpdate (prevProps) {
  69. const { signedIn } = this.context.identity;
  70. if (prevProps.onlyMedia !== this.props.onlyMedia) {
  71. const { dispatch, onlyMedia } = this.props;
  72. if (this.disconnect) {
  73. this.disconnect();
  74. }
  75. dispatch(expandCommunityTimeline({ onlyMedia }));
  76. if (signedIn) {
  77. this.disconnect = dispatch(connectCommunityStream({ onlyMedia }));
  78. }
  79. }
  80. }
  81. componentWillUnmount () {
  82. if (this.disconnect) {
  83. this.disconnect();
  84. this.disconnect = null;
  85. }
  86. }
  87. setRef = c => {
  88. this.column = c;
  89. };
  90. handleLoadMore = maxId => {
  91. const { dispatch, onlyMedia } = this.props;
  92. dispatch(expandCommunityTimeline({ maxId, onlyMedia }));
  93. };
  94. render () {
  95. const { intl, hasUnread, columnId, multiColumn, onlyMedia } = this.props;
  96. const pinned = !!columnId;
  97. return (
  98. <Column bindToDocument={!multiColumn} ref={this.setRef} label={intl.formatMessage(messages.title)}>
  99. <ColumnHeader
  100. icon='users'
  101. active={hasUnread}
  102. title={intl.formatMessage(messages.title)}
  103. onPin={this.handlePin}
  104. onMove={this.handleMove}
  105. onClick={this.handleHeaderClick}
  106. pinned={pinned}
  107. multiColumn={multiColumn}
  108. >
  109. <ColumnSettingsContainer columnId={columnId} />
  110. </ColumnHeader>
  111. <DismissableBanner id='community_timeline'>
  112. <FormattedMessage id='dismissable_banner.community_timeline' defaultMessage='These are the most recent public posts from people whose accounts are hosted by {domain}.' values={{ domain }} />
  113. </DismissableBanner>
  114. <StatusListContainer
  115. trackScroll={!pinned}
  116. scrollKey={`community_timeline-${columnId}`}
  117. timelineId={`community${onlyMedia ? ':media' : ''}`}
  118. onLoadMore={this.handleLoadMore}
  119. emptyMessage={<FormattedMessage id='empty_column.community' defaultMessage='The local timeline is empty. Write something publicly to get the ball rolling!' />}
  120. bindToDocument={!multiColumn}
  121. />
  122. <Helmet>
  123. <title>{intl.formatMessage(messages.title)}</title>
  124. <meta name='robots' content='noindex' />
  125. </Helmet>
  126. </Column>
  127. );
  128. }
  129. }
  130. export default connect(mapStateToProps)(injectIntl(CommunityTimeline));