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.

137 lines
4.2 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 { expandCommunityTimeline } 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 { connectCommunityStream } from 'flavours/glitch/actions/streaming';
  12. const messages = defineMessages({
  13. title: { id: 'column.community', defaultMessage: 'Local 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', `community${onlyMedia ? ':media' : ''}`, 'unread']) > 0,
  21. onlyMedia: (columnId && index >= 0) ? columns.get(index).getIn(['params', 'other', 'onlyMedia']) : state.getIn(['settings', 'community', 'other', 'onlyMedia']),
  22. };
  23. };
  24. @connect(mapStateToProps)
  25. @injectIntl
  26. export default class CommunityTimeline 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. columnId: PropTypes.string,
  36. intl: PropTypes.object.isRequired,
  37. hasUnread: PropTypes.bool,
  38. multiColumn: 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('COMMUNITY', { 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(expandCommunityTimeline({ onlyMedia }));
  59. this.disconnect = dispatch(connectCommunityStream({ onlyMedia }));
  60. }
  61. componentDidUpdate (prevProps) {
  62. if (prevProps.onlyMedia !== this.props.onlyMedia) {
  63. const { dispatch, onlyMedia } = this.props;
  64. this.disconnect();
  65. dispatch(expandCommunityTimeline({ onlyMedia }));
  66. this.disconnect = dispatch(connectCommunityStream({ 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(expandCommunityTimeline({ maxId, onlyMedia }));
  81. }
  82. shouldUpdateScroll = (prevRouterProps, { location }) => {
  83. return !(location.state && location.state.mastodonModalOpen)
  84. }
  85. render () {
  86. const { intl, hasUnread, columnId, multiColumn, onlyMedia } = this.props;
  87. const pinned = !!columnId;
  88. return (
  89. <Column ref={this.setRef} name='local' label={intl.formatMessage(messages.title)}>
  90. <ColumnHeader
  91. icon='users'
  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. trackScroll={!pinned}
  104. scrollKey={`community_timeline-${columnId}`}
  105. shouldUpdateScroll={this.shouldUpdateScroll}
  106. timelineId={`community${onlyMedia ? ':media' : ''}`}
  107. onLoadMore={this.handleLoadMore}
  108. emptyMessage={<FormattedMessage id='empty_column.community' defaultMessage='The local timeline is empty. Write something publicly to get the ball rolling!' />}
  109. />
  110. </Column>
  111. );
  112. }
  113. }