The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

146 lines
3.8 KiB

  1. import React from 'react';
  2. import { connect } from 'react-redux';
  3. import PropTypes from 'prop-types';
  4. import StatusListContainer from '../ui/containers/status_list_container';
  5. import Column from '../../components/column';
  6. import ColumnHeader from '../../components/column_header';
  7. import {
  8. refreshPublicTimeline,
  9. expandPublicTimeline,
  10. updateTimeline,
  11. deleteFromTimelines,
  12. connectTimeline,
  13. disconnectTimeline,
  14. } from '../../actions/timelines';
  15. import { addColumn, removeColumn, moveColumn } from '../../actions/columns';
  16. import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
  17. import ColumnSettingsContainer from './containers/column_settings_container';
  18. import createStream from '../../stream';
  19. const messages = defineMessages({
  20. title: { id: 'column.public', defaultMessage: 'Federated timeline' },
  21. });
  22. const mapStateToProps = state => ({
  23. hasUnread: state.getIn(['timelines', 'public', 'unread']) > 0,
  24. streamingAPIBaseURL: state.getIn(['meta', 'streaming_api_base_url']),
  25. accessToken: state.getIn(['meta', 'access_token']),
  26. });
  27. class PublicTimeline extends React.PureComponent {
  28. static propTypes = {
  29. dispatch: PropTypes.func.isRequired,
  30. intl: PropTypes.object.isRequired,
  31. columnId: PropTypes.string,
  32. multiColumn: PropTypes.bool,
  33. streamingAPIBaseURL: PropTypes.string.isRequired,
  34. accessToken: PropTypes.string.isRequired,
  35. hasUnread: PropTypes.bool,
  36. };
  37. handlePin = () => {
  38. const { columnId, dispatch } = this.props;
  39. if (columnId) {
  40. dispatch(removeColumn(columnId));
  41. } else {
  42. dispatch(addColumn('PUBLIC', {}));
  43. }
  44. }
  45. handleMove = (dir) => {
  46. const { columnId, dispatch } = this.props;
  47. dispatch(moveColumn(columnId, dir));
  48. }
  49. handleHeaderClick = () => {
  50. this.column.scrollTop();
  51. }
  52. componentDidMount () {
  53. const { dispatch, streamingAPIBaseURL, accessToken } = this.props;
  54. dispatch(refreshPublicTimeline());
  55. if (typeof this._subscription !== 'undefined') {
  56. return;
  57. }
  58. this._subscription = createStream(streamingAPIBaseURL, accessToken, 'public', {
  59. connected () {
  60. dispatch(connectTimeline('public'));
  61. },
  62. reconnected () {
  63. dispatch(connectTimeline('public'));
  64. },
  65. disconnected () {
  66. dispatch(disconnectTimeline('public'));
  67. },
  68. received (data) {
  69. switch(data.event) {
  70. case 'update':
  71. dispatch(updateTimeline('public', JSON.parse(data.payload)));
  72. break;
  73. case 'delete':
  74. dispatch(deleteFromTimelines(data.payload));
  75. break;
  76. }
  77. },
  78. });
  79. }
  80. componentWillUnmount () {
  81. if (typeof this._subscription !== 'undefined') {
  82. this._subscription.close();
  83. this._subscription = null;
  84. }
  85. }
  86. setRef = c => {
  87. this.column = c;
  88. }
  89. handleLoadMore = () => {
  90. this.props.dispatch(expandPublicTimeline());
  91. }
  92. render () {
  93. const { intl, columnId, hasUnread, multiColumn } = this.props;
  94. const pinned = !!columnId;
  95. return (
  96. <Column ref={this.setRef}>
  97. <ColumnHeader
  98. icon='globe'
  99. active={hasUnread}
  100. title={intl.formatMessage(messages.title)}
  101. onPin={this.handlePin}
  102. onMove={this.handleMove}
  103. onClick={this.handleHeaderClick}
  104. pinned={pinned}
  105. multiColumn={multiColumn}
  106. >
  107. <ColumnSettingsContainer />
  108. </ColumnHeader>
  109. <StatusListContainer
  110. timelineId='public'
  111. loadMore={this.handleLoadMore}
  112. trackScroll={!pinned}
  113. scrollKey={`public_timeline-${columnId}`}
  114. 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' />}
  115. />
  116. </Column>
  117. );
  118. }
  119. }
  120. export default connect(mapStateToProps)(injectIntl(PublicTimeline));