index.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import React from 'react';
  2. import { connect } from 'react-redux';
  3. import PropTypes from 'prop-types';
  4. import ImmutablePropTypes from 'react-immutable-proptypes';
  5. import StatusListContainer from '../ui/containers/status_list_container';
  6. import Column from 'mastodon/components/column';
  7. import ColumnHeader from 'mastodon/components/column_header';
  8. import ColumnSettingsContainer from './containers/column_settings_container';
  9. import { expandHashtagTimeline, clearTimeline } from 'mastodon/actions/timelines';
  10. import { addColumn, removeColumn, moveColumn } from 'mastodon/actions/columns';
  11. import { injectIntl, FormattedMessage, defineMessages } from 'react-intl';
  12. import { connectHashtagStream } from 'mastodon/actions/streaming';
  13. import { isEqual } from 'lodash';
  14. import { fetchHashtag, followHashtag, unfollowHashtag } from 'mastodon/actions/tags';
  15. import Icon from 'mastodon/components/icon';
  16. import classNames from 'classnames';
  17. import { Helmet } from 'react-helmet';
  18. const messages = defineMessages({
  19. followHashtag: { id: 'hashtag.follow', defaultMessage: 'Follow hashtag' },
  20. unfollowHashtag: { id: 'hashtag.unfollow', defaultMessage: 'Unfollow hashtag' },
  21. });
  22. const mapStateToProps = (state, props) => ({
  23. hasUnread: state.getIn(['timelines', `hashtag:${props.params.id}${props.params.local ? ':local' : ''}`, 'unread']) > 0,
  24. tag: state.getIn(['tags', props.params.id]),
  25. });
  26. export default @connect(mapStateToProps)
  27. @injectIntl
  28. class HashtagTimeline extends React.PureComponent {
  29. disconnects = [];
  30. static contextTypes = {
  31. identity: PropTypes.object,
  32. };
  33. static propTypes = {
  34. params: PropTypes.object.isRequired,
  35. columnId: PropTypes.string,
  36. dispatch: PropTypes.func.isRequired,
  37. hasUnread: PropTypes.bool,
  38. tag: ImmutablePropTypes.map,
  39. multiColumn: PropTypes.bool,
  40. intl: PropTypes.object,
  41. };
  42. handlePin = () => {
  43. const { columnId, dispatch } = this.props;
  44. if (columnId) {
  45. dispatch(removeColumn(columnId));
  46. } else {
  47. dispatch(addColumn('HASHTAG', { id: this.props.params.id }));
  48. }
  49. }
  50. title = () => {
  51. const { id } = this.props.params;
  52. const title = [id];
  53. if (this.additionalFor('any')) {
  54. title.push(' ', <FormattedMessage key='any' id='hashtag.column_header.tag_mode.any' values={{ additional: this.additionalFor('any') }} defaultMessage='or {additional}' />);
  55. }
  56. if (this.additionalFor('all')) {
  57. title.push(' ', <FormattedMessage key='all' id='hashtag.column_header.tag_mode.all' values={{ additional: this.additionalFor('all') }} defaultMessage='and {additional}' />);
  58. }
  59. if (this.additionalFor('none')) {
  60. title.push(' ', <FormattedMessage key='none' id='hashtag.column_header.tag_mode.none' values={{ additional: this.additionalFor('none') }} defaultMessage='without {additional}' />);
  61. }
  62. return title;
  63. }
  64. additionalFor = (mode) => {
  65. const { tags } = this.props.params;
  66. if (tags && (tags[mode] || []).length > 0) {
  67. return tags[mode].map(tag => tag.value).join('/');
  68. } else {
  69. return '';
  70. }
  71. }
  72. handleMove = (dir) => {
  73. const { columnId, dispatch } = this.props;
  74. dispatch(moveColumn(columnId, dir));
  75. }
  76. handleHeaderClick = () => {
  77. this.column.scrollTop();
  78. }
  79. _subscribe (dispatch, id, tags = {}, local) {
  80. const { signedIn } = this.context.identity;
  81. if (!signedIn) {
  82. return;
  83. }
  84. let any = (tags.any || []).map(tag => tag.value);
  85. let all = (tags.all || []).map(tag => tag.value);
  86. let none = (tags.none || []).map(tag => tag.value);
  87. [id, ...any].map(tag => {
  88. this.disconnects.push(dispatch(connectHashtagStream(id, tag, local, status => {
  89. let tags = status.tags.map(tag => tag.name);
  90. return all.filter(tag => tags.includes(tag)).length === all.length &&
  91. none.filter(tag => tags.includes(tag)).length === 0;
  92. })));
  93. });
  94. }
  95. _unsubscribe () {
  96. this.disconnects.map(disconnect => disconnect());
  97. this.disconnects = [];
  98. }
  99. _unload () {
  100. const { dispatch } = this.props;
  101. const { id, local } = this.props.params;
  102. this._unsubscribe();
  103. dispatch(clearTimeline(`hashtag:${id}${local ? ':local' : ''}`));
  104. }
  105. _load() {
  106. const { dispatch } = this.props;
  107. const { id, tags, local } = this.props.params;
  108. this._subscribe(dispatch, id, tags, local);
  109. dispatch(expandHashtagTimeline(id, { tags, local }));
  110. dispatch(fetchHashtag(id));
  111. }
  112. componentDidMount () {
  113. this._load();
  114. }
  115. componentDidUpdate (prevProps) {
  116. const { params } = this.props;
  117. const { id, tags, local } = prevProps.params;
  118. if (id !== params.id || !isEqual(tags, params.tags) || !isEqual(local, params.local)) {
  119. this._unload();
  120. this._load();
  121. }
  122. }
  123. componentWillUnmount () {
  124. this._unsubscribe();
  125. }
  126. setRef = c => {
  127. this.column = c;
  128. }
  129. handleLoadMore = maxId => {
  130. const { dispatch, params } = this.props;
  131. const { id, tags, local } = params;
  132. dispatch(expandHashtagTimeline(id, { maxId, tags, local }));
  133. }
  134. handleFollow = () => {
  135. const { dispatch, params, tag } = this.props;
  136. const { id } = params;
  137. const { signedIn } = this.context.identity;
  138. if (!signedIn) {
  139. return;
  140. }
  141. if (tag.get('following')) {
  142. dispatch(unfollowHashtag(id));
  143. } else {
  144. dispatch(followHashtag(id));
  145. }
  146. }
  147. render () {
  148. const { hasUnread, columnId, multiColumn, tag, intl } = this.props;
  149. const { id, local } = this.props.params;
  150. const pinned = !!columnId;
  151. const { signedIn } = this.context.identity;
  152. let followButton;
  153. if (tag) {
  154. const following = tag.get('following');
  155. followButton = (
  156. <button className={classNames('column-header__button')} onClick={this.handleFollow} disabled={!signedIn} title={intl.formatMessage(following ? messages.unfollowHashtag : messages.followHashtag)} aria-label={intl.formatMessage(following ? messages.unfollowHashtag : messages.followHashtag)} aria-pressed={following ? 'true' : 'false'}>
  157. <Icon id={following ? 'user-times' : 'user-plus'} fixedWidth className='column-header__icon' />
  158. </button>
  159. );
  160. }
  161. return (
  162. <Column bindToDocument={!multiColumn} ref={this.setRef} label={`#${id}`}>
  163. <ColumnHeader
  164. icon='hashtag'
  165. active={hasUnread}
  166. title={this.title()}
  167. onPin={this.handlePin}
  168. onMove={this.handleMove}
  169. onClick={this.handleHeaderClick}
  170. pinned={pinned}
  171. multiColumn={multiColumn}
  172. extraButton={followButton}
  173. showBackButton
  174. >
  175. {columnId && <ColumnSettingsContainer columnId={columnId} />}
  176. </ColumnHeader>
  177. <StatusListContainer
  178. trackScroll={!pinned}
  179. scrollKey={`hashtag_timeline-${columnId}`}
  180. timelineId={`hashtag:${id}${local ? ':local' : ''}`}
  181. onLoadMore={this.handleLoadMore}
  182. emptyMessage={<FormattedMessage id='empty_column.hashtag' defaultMessage='There is nothing in this hashtag yet.' />}
  183. bindToDocument={!multiColumn}
  184. />
  185. <Helmet>
  186. <title>#{id}</title>
  187. <meta name='robots' content='noindex' />
  188. </Helmet>
  189. </Column>
  190. );
  191. }
  192. }