2022-02-25 01:34:33 +02:00
|
|
|
import PropTypes from 'prop-types';
|
2023-05-28 17:38:10 +03:00
|
|
|
import { PureComponent } from 'react';
|
|
|
|
|
2022-09-29 05:39:33 +03:00
|
|
|
import { injectIntl, defineMessages, FormattedMessage } from 'react-intl';
|
2023-05-28 17:38:10 +03:00
|
|
|
|
|
|
|
import { Helmet } from 'react-helmet';
|
|
|
|
|
|
|
|
import { List as ImmutableList } from 'immutable';
|
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
2022-02-25 01:34:33 +02:00
|
|
|
import { connect } from 'react-redux';
|
2023-05-28 17:38:10 +03:00
|
|
|
|
2022-02-25 01:34:33 +02:00
|
|
|
import { expandSearch } from 'flavours/glitch/actions/search';
|
|
|
|
import { ImmutableHashtag as Hashtag } from 'flavours/glitch/components/hashtag';
|
2023-06-06 12:28:50 +03:00
|
|
|
import { LoadMore } from 'flavours/glitch/components/load_more';
|
2023-06-13 20:26:25 +03:00
|
|
|
import { LoadingIndicator } from 'flavours/glitch/components/loading_indicator';
|
2023-05-28 17:38:10 +03:00
|
|
|
import Account from 'flavours/glitch/containers/account_container';
|
|
|
|
import Status from 'flavours/glitch/containers/status_container';
|
|
|
|
|
|
|
|
|
2022-02-25 01:34:33 +02:00
|
|
|
|
2022-09-29 05:39:33 +03:00
|
|
|
const messages = defineMessages({
|
|
|
|
title: { id: 'search_results.title', defaultMessage: 'Search for {q}' },
|
|
|
|
});
|
|
|
|
|
2022-02-25 01:34:33 +02:00
|
|
|
const mapStateToProps = state => ({
|
|
|
|
isLoading: state.getIn(['search', 'isLoading']),
|
|
|
|
results: state.getIn(['search', 'results']),
|
2022-09-29 05:39:33 +03:00
|
|
|
q: state.getIn(['search', 'searchTerm']),
|
2022-02-25 01:34:33 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const appendLoadMore = (id, list, onLoadMore) => {
|
|
|
|
if (list.size >= 5) {
|
|
|
|
return list.push(<LoadMore key={`${id}-load-more`} visible onClick={onLoadMore} />);
|
|
|
|
} else {
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-22 19:20:25 +02:00
|
|
|
const renderAccounts = (results, onLoadMore) => appendLoadMore('accounts', results.get('accounts', ImmutableList()).map(item => (
|
2022-02-25 01:34:33 +02:00
|
|
|
<Account key={`account-${item}`} id={item} />
|
|
|
|
)), onLoadMore);
|
|
|
|
|
2022-03-22 19:20:25 +02:00
|
|
|
const renderHashtags = (results, onLoadMore) => appendLoadMore('hashtags', results.get('hashtags', ImmutableList()).map(item => (
|
2022-02-25 01:34:33 +02:00
|
|
|
<Hashtag key={`tag-${item.get('name')}`} hashtag={item} />
|
|
|
|
)), onLoadMore);
|
|
|
|
|
2022-03-22 19:20:25 +02:00
|
|
|
const renderStatuses = (results, onLoadMore) => appendLoadMore('statuses', results.get('statuses', ImmutableList()).map(item => (
|
2022-02-25 01:34:33 +02:00
|
|
|
<Status key={`status-${item}`} id={item} />
|
|
|
|
)), onLoadMore);
|
|
|
|
|
2023-05-28 15:18:23 +03:00
|
|
|
class Results extends PureComponent {
|
2022-02-25 01:34:33 +02:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
results: ImmutablePropTypes.map,
|
|
|
|
isLoading: PropTypes.bool,
|
|
|
|
multiColumn: PropTypes.bool,
|
|
|
|
dispatch: PropTypes.func.isRequired,
|
2022-09-29 05:39:33 +03:00
|
|
|
q: PropTypes.string,
|
|
|
|
intl: PropTypes.object,
|
2022-02-25 01:34:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
state = {
|
|
|
|
type: 'all',
|
|
|
|
};
|
|
|
|
|
|
|
|
handleSelectAll = () => this.setState({ type: 'all' });
|
|
|
|
handleSelectAccounts = () => this.setState({ type: 'accounts' });
|
|
|
|
handleSelectHashtags = () => this.setState({ type: 'hashtags' });
|
|
|
|
handleSelectStatuses = () => this.setState({ type: 'statuses' });
|
|
|
|
handleLoadMoreAccounts = () => this.loadMore('accounts');
|
|
|
|
handleLoadMoreStatuses = () => this.loadMore('statuses');
|
|
|
|
handleLoadMoreHashtags = () => this.loadMore('hashtags');
|
|
|
|
|
|
|
|
loadMore (type) {
|
|
|
|
const { dispatch } = this.props;
|
|
|
|
dispatch(expandSearch(type));
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
2022-09-29 05:39:33 +03:00
|
|
|
const { intl, isLoading, q, results } = this.props;
|
2022-02-25 01:34:33 +02:00
|
|
|
const { type } = this.state;
|
|
|
|
|
|
|
|
let filteredResults = ImmutableList();
|
|
|
|
|
|
|
|
if (!isLoading) {
|
|
|
|
switch(type) {
|
|
|
|
case 'all':
|
|
|
|
filteredResults = filteredResults.concat(renderAccounts(results, this.handleLoadMoreAccounts), renderHashtags(results, this.handleLoadMoreHashtags), renderStatuses(results, this.handleLoadMoreStatuses));
|
|
|
|
break;
|
|
|
|
case 'accounts':
|
|
|
|
filteredResults = filteredResults.concat(renderAccounts(results, this.handleLoadMoreAccounts));
|
|
|
|
break;
|
|
|
|
case 'hashtags':
|
|
|
|
filteredResults = filteredResults.concat(renderHashtags(results, this.handleLoadMoreHashtags));
|
|
|
|
break;
|
|
|
|
case 'statuses':
|
|
|
|
filteredResults = filteredResults.concat(renderStatuses(results, this.handleLoadMoreStatuses));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filteredResults.size === 0) {
|
|
|
|
filteredResults = (
|
|
|
|
<div className='empty-column-indicator'>
|
|
|
|
<FormattedMessage id='search_results.nothing_found' defaultMessage='Could not find anything for these search terms' />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2023-05-28 15:56:24 +03:00
|
|
|
<>
|
2022-02-25 01:34:33 +02:00
|
|
|
<div className='account__section-headline'>
|
|
|
|
<button onClick={this.handleSelectAll} className={type === 'all' && 'active'}><FormattedMessage id='search_results.all' defaultMessage='All' /></button>
|
2023-07-30 19:42:35 +03:00
|
|
|
<button onClick={this.handleSelectAccounts} className={type === 'accounts' && 'active'}><FormattedMessage id='search_results.accounts' defaultMessage='Profiles' /></button>
|
2022-02-25 01:34:33 +02:00
|
|
|
<button onClick={this.handleSelectHashtags} className={type === 'hashtags' && 'active'}><FormattedMessage id='search_results.hashtags' defaultMessage='Hashtags' /></button>
|
2023-06-10 20:21:44 +03:00
|
|
|
<button onClick={this.handleSelectStatuses} className={type === 'statuses' && 'active'}><FormattedMessage id='search_results.statuses' defaultMessage='Posts' /></button>
|
2022-02-25 01:34:33 +02:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className='explore__search-results'>
|
2022-02-27 08:37:07 +02:00
|
|
|
{isLoading ? <LoadingIndicator /> : filteredResults}
|
2022-02-25 01:34:33 +02:00
|
|
|
</div>
|
2022-09-29 05:39:33 +03:00
|
|
|
|
|
|
|
<Helmet>
|
2022-10-09 07:08:37 +03:00
|
|
|
<title>{intl.formatMessage(messages.title, { q })}</title>
|
2022-09-29 05:39:33 +03:00
|
|
|
</Helmet>
|
2023-05-28 15:56:24 +03:00
|
|
|
</>
|
2022-02-25 01:34:33 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2023-03-25 00:15:25 +02:00
|
|
|
|
|
|
|
export default connect(mapStateToProps)(injectIntl(Results));
|