2017-05-03 00:04:16 +00:00
|
|
|
import React from 'react';
|
2016-11-05 14:20:05 +00:00
|
|
|
import { connect } from 'react-redux';
|
2017-04-21 18:05:35 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2022-07-22 13:52:06 +00:00
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
2016-11-05 14:20:05 +00:00
|
|
|
import StatusListContainer from '../ui/containers/status_list_container';
|
2022-07-22 13:52:06 +00:00
|
|
|
import Column from 'mastodon/components/column';
|
|
|
|
import ColumnHeader from 'mastodon/components/column_header';
|
2018-11-05 17:53:25 +00:00
|
|
|
import ColumnSettingsContainer from './containers/column_settings_container';
|
2022-07-22 13:52:06 +00:00
|
|
|
import { expandHashtagTimeline, clearTimeline } from 'mastodon/actions/timelines';
|
|
|
|
import { addColumn, removeColumn, moveColumn } from 'mastodon/actions/columns';
|
|
|
|
import { injectIntl, FormattedMessage, defineMessages } from 'react-intl';
|
|
|
|
import { connectHashtagStream } from 'mastodon/actions/streaming';
|
2018-11-17 10:07:48 +00:00
|
|
|
import { isEqual } from 'lodash';
|
2022-07-22 13:52:06 +00:00
|
|
|
import { fetchHashtag, followHashtag, unfollowHashtag } from 'mastodon/actions/tags';
|
|
|
|
import Icon from 'mastodon/components/icon';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
|
|
|
|
const messages = defineMessages({
|
|
|
|
followHashtag: { id: 'hashtag.follow', defaultMessage: 'Follow hashtag' },
|
|
|
|
unfollowHashtag: { id: 'hashtag.unfollow', defaultMessage: 'Unfollow hashtag' },
|
|
|
|
});
|
2017-02-03 23:34:31 +00:00
|
|
|
|
2017-08-20 15:12:06 +00:00
|
|
|
const mapStateToProps = (state, props) => ({
|
2020-05-23 03:46:29 +00:00
|
|
|
hasUnread: state.getIn(['timelines', `hashtag:${props.params.id}${props.params.local ? ':local' : ''}`, 'unread']) > 0,
|
2022-07-22 13:52:06 +00:00
|
|
|
tag: state.getIn(['tags', props.params.id]),
|
2017-02-03 23:34:31 +00:00
|
|
|
});
|
2016-11-05 14:20:05 +00:00
|
|
|
|
2018-09-14 15:59:48 +00:00
|
|
|
export default @connect(mapStateToProps)
|
2022-07-22 13:52:06 +00:00
|
|
|
@injectIntl
|
2018-09-14 15:59:48 +00:00
|
|
|
class HashtagTimeline extends React.PureComponent {
|
2016-11-05 14:20:05 +00:00
|
|
|
|
2018-11-05 17:53:25 +00:00
|
|
|
disconnects = [];
|
|
|
|
|
2017-05-12 12:44:10 +00:00
|
|
|
static propTypes = {
|
|
|
|
params: PropTypes.object.isRequired,
|
2017-06-03 23:39:38 +00:00
|
|
|
columnId: PropTypes.string,
|
2017-05-12 12:44:10 +00:00
|
|
|
dispatch: PropTypes.func.isRequired,
|
2017-05-20 15:31:47 +00:00
|
|
|
hasUnread: PropTypes.bool,
|
2022-07-22 13:52:06 +00:00
|
|
|
tag: ImmutablePropTypes.map,
|
2017-06-03 23:39:38 +00:00
|
|
|
multiColumn: PropTypes.bool,
|
2022-07-22 13:52:06 +00:00
|
|
|
intl: PropTypes.object,
|
2017-05-12 12:44:10 +00:00
|
|
|
};
|
|
|
|
|
2017-06-03 23:39:38 +00:00
|
|
|
handlePin = () => {
|
|
|
|
const { columnId, dispatch } = this.props;
|
|
|
|
|
|
|
|
if (columnId) {
|
|
|
|
dispatch(removeColumn(columnId));
|
|
|
|
} else {
|
|
|
|
dispatch(addColumn('HASHTAG', { id: this.props.params.id }));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-05 17:53:25 +00:00
|
|
|
title = () => {
|
2022-07-22 13:52:06 +00:00
|
|
|
const { id } = this.props.params;
|
|
|
|
const title = [id];
|
2019-02-14 14:46:42 +00:00
|
|
|
|
2018-11-05 17:53:25 +00:00
|
|
|
if (this.additionalFor('any')) {
|
2019-02-14 14:46:42 +00:00
|
|
|
title.push(' ', <FormattedMessage key='any' id='hashtag.column_header.tag_mode.any' values={{ additional: this.additionalFor('any') }} defaultMessage='or {additional}' />);
|
2018-11-05 17:53:25 +00:00
|
|
|
}
|
2019-02-14 14:46:42 +00:00
|
|
|
|
2018-11-05 17:53:25 +00:00
|
|
|
if (this.additionalFor('all')) {
|
2019-02-14 14:46:42 +00:00
|
|
|
title.push(' ', <FormattedMessage key='all' id='hashtag.column_header.tag_mode.all' values={{ additional: this.additionalFor('all') }} defaultMessage='and {additional}' />);
|
2018-11-05 17:53:25 +00:00
|
|
|
}
|
2019-02-14 14:46:42 +00:00
|
|
|
|
2018-11-05 17:53:25 +00:00
|
|
|
if (this.additionalFor('none')) {
|
2019-02-14 14:46:42 +00:00
|
|
|
title.push(' ', <FormattedMessage key='none' id='hashtag.column_header.tag_mode.none' values={{ additional: this.additionalFor('none') }} defaultMessage='without {additional}' />);
|
2018-11-05 17:53:25 +00:00
|
|
|
}
|
2019-02-14 14:46:42 +00:00
|
|
|
|
2018-11-05 17:53:25 +00:00
|
|
|
return title;
|
|
|
|
}
|
|
|
|
|
|
|
|
additionalFor = (mode) => {
|
|
|
|
const { tags } = this.props.params;
|
|
|
|
|
|
|
|
if (tags && (tags[mode] || []).length > 0) {
|
|
|
|
return tags[mode].map(tag => tag.value).join('/');
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-03 23:39:38 +00:00
|
|
|
handleMove = (dir) => {
|
|
|
|
const { columnId, dispatch } = this.props;
|
|
|
|
dispatch(moveColumn(columnId, dir));
|
|
|
|
}
|
|
|
|
|
|
|
|
handleHeaderClick = () => {
|
|
|
|
this.column.scrollTop();
|
|
|
|
}
|
|
|
|
|
2020-05-23 03:46:29 +00:00
|
|
|
_subscribe (dispatch, id, tags = {}, local) {
|
2018-11-05 17:53:25 +00:00
|
|
|
let any = (tags.any || []).map(tag => tag.value);
|
|
|
|
let all = (tags.all || []).map(tag => tag.value);
|
|
|
|
let none = (tags.none || []).map(tag => tag.value);
|
|
|
|
|
2019-02-14 14:46:42 +00:00
|
|
|
[id, ...any].map(tag => {
|
2020-05-23 03:46:29 +00:00
|
|
|
this.disconnects.push(dispatch(connectHashtagStream(id, tag, local, status => {
|
2018-11-05 17:53:25 +00:00
|
|
|
let tags = status.tags.map(tag => tag.name);
|
2019-02-14 14:46:42 +00:00
|
|
|
|
2018-11-05 17:53:25 +00:00
|
|
|
return all.filter(tag => tags.includes(tag)).length === all.length &&
|
|
|
|
none.filter(tag => tags.includes(tag)).length === 0;
|
|
|
|
})));
|
|
|
|
});
|
2017-04-21 18:05:35 +00:00
|
|
|
}
|
2016-11-05 14:20:05 +00:00
|
|
|
|
|
|
|
_unsubscribe () {
|
2018-11-05 17:53:25 +00:00
|
|
|
this.disconnects.map(disconnect => disconnect());
|
|
|
|
this.disconnects = [];
|
2017-04-21 18:05:35 +00:00
|
|
|
}
|
2016-11-05 14:20:05 +00:00
|
|
|
|
2022-07-22 13:52:06 +00:00
|
|
|
_unload () {
|
|
|
|
const { dispatch } = this.props;
|
|
|
|
const { id, local } = this.props.params;
|
|
|
|
|
|
|
|
this._unsubscribe();
|
|
|
|
dispatch(clearTimeline(`hashtag:${id}${local ? ':local' : ''}`));
|
|
|
|
}
|
|
|
|
|
|
|
|
_load() {
|
2016-11-05 14:20:05 +00:00
|
|
|
const { dispatch } = this.props;
|
2020-04-18 19:52:39 +00:00
|
|
|
const { id, tags, local } = this.props.params;
|
2016-11-05 14:20:05 +00:00
|
|
|
|
2020-05-23 03:46:29 +00:00
|
|
|
this._subscribe(dispatch, id, tags, local);
|
2020-04-18 19:52:39 +00:00
|
|
|
dispatch(expandHashtagTimeline(id, { tags, local }));
|
2022-07-22 13:52:06 +00:00
|
|
|
dispatch(fetchHashtag(id));
|
2017-04-21 18:05:35 +00:00
|
|
|
}
|
2016-11-05 14:20:05 +00:00
|
|
|
|
2022-07-22 13:52:06 +00:00
|
|
|
componentDidMount () {
|
|
|
|
this._load();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate (prevProps) {
|
|
|
|
const { params } = this.props;
|
|
|
|
const { id, tags, local } = prevProps.params;
|
2019-02-14 14:46:42 +00:00
|
|
|
|
2020-04-18 19:52:39 +00:00
|
|
|
if (id !== params.id || !isEqual(tags, params.tags) || !isEqual(local, params.local)) {
|
2022-07-22 13:52:06 +00:00
|
|
|
this._unload();
|
|
|
|
this._load();
|
2016-11-05 14:20:05 +00:00
|
|
|
}
|
2017-04-21 18:05:35 +00:00
|
|
|
}
|
2016-11-05 14:20:05 +00:00
|
|
|
|
|
|
|
componentWillUnmount () {
|
|
|
|
this._unsubscribe();
|
2017-04-21 18:05:35 +00:00
|
|
|
}
|
2016-11-05 14:20:05 +00:00
|
|
|
|
2017-06-03 23:39:38 +00:00
|
|
|
setRef = c => {
|
|
|
|
this.column = c;
|
|
|
|
}
|
|
|
|
|
2018-03-24 14:25:15 +00:00
|
|
|
handleLoadMore = maxId => {
|
2022-07-22 13:52:06 +00:00
|
|
|
const { dispatch, params } = this.props;
|
|
|
|
const { id, tags, local } = params;
|
|
|
|
|
|
|
|
dispatch(expandHashtagTimeline(id, { maxId, tags, local }));
|
|
|
|
}
|
|
|
|
|
|
|
|
handleFollow = () => {
|
|
|
|
const { dispatch, params, tag } = this.props;
|
|
|
|
const { id } = params;
|
|
|
|
|
|
|
|
if (tag.get('following')) {
|
|
|
|
dispatch(unfollowHashtag(id));
|
|
|
|
} else {
|
|
|
|
dispatch(followHashtag(id));
|
|
|
|
}
|
2017-06-11 15:07:35 +00:00
|
|
|
}
|
|
|
|
|
2016-11-05 14:20:05 +00:00
|
|
|
render () {
|
2022-07-22 13:52:06 +00:00
|
|
|
const { hasUnread, columnId, multiColumn, tag, intl } = this.props;
|
2020-05-23 03:46:29 +00:00
|
|
|
const { id, local } = this.props.params;
|
2017-06-03 23:39:38 +00:00
|
|
|
const pinned = !!columnId;
|
2016-11-05 14:20:05 +00:00
|
|
|
|
2022-07-22 13:52:06 +00:00
|
|
|
let followButton;
|
|
|
|
|
|
|
|
if (tag) {
|
|
|
|
const following = tag.get('following');
|
|
|
|
|
|
|
|
followButton = (
|
|
|
|
<button className={classNames('column-header__button')} onClick={this.handleFollow} title={intl.formatMessage(following ? messages.unfollowHashtag : messages.followHashtag)} aria-label={intl.formatMessage(following ? messages.unfollowHashtag : messages.followHashtag)} aria-pressed={following ? 'true' : 'false'}>
|
|
|
|
<Icon id={following ? 'user-times' : 'user-plus'} fixedWidth className='column-header__icon' />
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-11-05 14:20:05 +00:00
|
|
|
return (
|
2019-08-01 17:17:17 +00:00
|
|
|
<Column bindToDocument={!multiColumn} ref={this.setRef} label={`#${id}`}>
|
2017-06-03 23:39:38 +00:00
|
|
|
<ColumnHeader
|
|
|
|
icon='hashtag'
|
|
|
|
active={hasUnread}
|
2018-11-05 17:53:25 +00:00
|
|
|
title={this.title()}
|
2017-06-03 23:39:38 +00:00
|
|
|
onPin={this.handlePin}
|
|
|
|
onMove={this.handleMove}
|
|
|
|
onClick={this.handleHeaderClick}
|
|
|
|
pinned={pinned}
|
|
|
|
multiColumn={multiColumn}
|
2022-07-22 13:52:06 +00:00
|
|
|
extraButton={followButton}
|
2017-06-06 11:20:07 +00:00
|
|
|
showBackButton
|
2018-11-05 17:53:25 +00:00
|
|
|
>
|
|
|
|
{columnId && <ColumnSettingsContainer columnId={columnId} />}
|
|
|
|
</ColumnHeader>
|
2017-06-03 23:39:38 +00:00
|
|
|
|
|
|
|
<StatusListContainer
|
2017-06-05 13:20:46 +00:00
|
|
|
trackScroll={!pinned}
|
2017-06-03 23:39:38 +00:00
|
|
|
scrollKey={`hashtag_timeline-${columnId}`}
|
2020-05-23 03:46:29 +00:00
|
|
|
timelineId={`hashtag:${id}${local ? ':local' : ''}`}
|
2018-03-24 14:25:15 +00:00
|
|
|
onLoadMore={this.handleLoadMore}
|
2017-06-03 23:39:38 +00:00
|
|
|
emptyMessage={<FormattedMessage id='empty_column.hashtag' defaultMessage='There is nothing in this hashtag yet.' />}
|
2019-07-19 07:25:22 +00:00
|
|
|
bindToDocument={!multiColumn}
|
2017-06-03 23:39:38 +00:00
|
|
|
/>
|
2016-11-05 14:20:05 +00:00
|
|
|
</Column>
|
|
|
|
);
|
2017-04-21 18:05:35 +00:00
|
|
|
}
|
2016-11-05 14:20:05 +00:00
|
|
|
|
2017-04-21 18:05:35 +00:00
|
|
|
}
|