lbry-desktop/ui/component/claimListDiscover/view.jsx

274 lines
8 KiB
React
Raw Normal View History

2019-06-11 20:10:58 +02:00
// @flow
2019-06-17 22:32:38 +02:00
import type { Node } from 'react';
2020-01-02 17:30:27 +01:00
import classnames from 'classnames';
import React, { Fragment, useEffect, useState } from 'react';
2019-07-17 22:49:06 +02:00
import { withRouter } from 'react-router';
2019-07-31 21:07:26 +02:00
import { createNormalizedClaimSearchKey, MATURE_TAGS } from 'lbry-redux';
2019-06-11 20:10:58 +02:00
import { FormField } from 'component/common/form';
import Button from 'component/button';
2019-07-17 22:49:06 +02:00
import moment from 'moment';
2019-06-19 07:05:43 +02:00
import ClaimList from 'component/claimList';
2019-06-27 08:18:45 +02:00
import ClaimPreview from 'component/claimPreview';
2019-07-17 22:49:06 +02:00
import { toCapitalCase } from 'util/string';
import I18nMessage from 'component/i18nMessage';
2019-06-11 20:10:58 +02:00
2019-06-27 08:18:45 +02:00
const PAGE_SIZE = 20;
2019-06-11 20:10:58 +02:00
const TIME_DAY = 'day';
const TIME_WEEK = 'week';
const TIME_MONTH = 'month';
const TIME_YEAR = 'year';
const TIME_ALL = 'all';
2019-07-01 03:52:38 +02:00
2020-01-02 17:30:27 +01:00
export const TYPE_TRENDING = 'trending';
export const TYPE_TOP = 'top';
export const TYPE_NEW = 'new';
const SEARCH_TYPES = [TYPE_TRENDING, TYPE_NEW, TYPE_TOP];
2019-06-11 20:56:23 +02:00
const SEARCH_TIMES = [TIME_DAY, TIME_WEEK, TIME_MONTH, TIME_YEAR, TIME_ALL];
2019-06-11 20:10:58 +02:00
type Props = {
uris: Array<string>,
2019-07-01 03:52:38 +02:00
subscribedChannels: Array<Subscription>,
2019-07-11 20:06:25 +02:00
doClaimSearch: ({}) => void,
2019-06-11 20:10:58 +02:00
tags: Array<string>,
loading: boolean,
2019-07-17 22:49:06 +02:00
personalView: boolean,
2019-06-17 22:32:38 +02:00
doToggleTagFollow: string => void,
meta?: Node,
showNsfw: boolean,
2019-07-17 22:49:06 +02:00
history: { action: string, push: string => void, replace: string => void },
location: { search: string, pathname: string },
claimSearchByQuery: {
[string]: Array<string>,
},
hiddenUris: Array<string>,
hiddenNsfwMessage?: Node,
2020-01-02 17:30:27 +01:00
channelIds?: Array<string>,
defaultTypeSort?: string,
2020-01-02 21:36:03 +01:00
headerLabel?: string | Node,
2019-06-11 20:10:58 +02:00
};
2019-06-19 07:05:43 +02:00
function ClaimListDiscover(props: Props) {
2019-07-17 22:49:06 +02:00
const {
doClaimSearch,
claimSearchByQuery,
tags,
loading,
personalView,
meta,
2020-01-02 17:30:27 +01:00
channelIds,
2019-07-17 22:49:06 +02:00
showNsfw,
history,
location,
hiddenUris,
hiddenNsfwMessage,
2020-01-02 17:30:27 +01:00
defaultTypeSort,
2020-01-02 21:36:03 +01:00
headerLabel,
2019-07-17 22:49:06 +02:00
} = props;
const didNavigateForward = history.action === 'PUSH';
2019-09-26 18:07:11 +02:00
const [page, setPage] = useState(1);
const { search } = location;
const [forceRefresh, setForceRefresh] = useState();
2019-07-17 22:49:06 +02:00
const urlParams = new URLSearchParams(search);
2020-01-02 17:30:27 +01:00
const typeSort = urlParams.get('type') || defaultTypeSort || TYPE_TRENDING;
2019-07-17 22:49:06 +02:00
const timeSort = urlParams.get('time') || TIME_WEEK;
const tagsInUrl = urlParams.get('t') || '';
const options: {
page_size: number,
page: number,
no_totals: boolean,
any_tags: Array<string>,
channel_ids: Array<string>,
not_channel_ids: Array<string>,
2019-07-17 22:49:06 +02:00
not_tags: Array<string>,
order_by: Array<string>,
release_time?: string,
} = {
page_size: PAGE_SIZE,
page,
// no_totals makes it so the sdk doesn't have to calculate total number pages for pagination
// it's faster, but we will need to remove it if we start using total_pages
no_totals: true,
2020-01-02 17:30:27 +01:00
any_tags: tags || [],
channel_ids: channelIds || [],
not_channel_ids:
// If channelIds were passed in, we don't need not_channel_ids
!channelIds && hiddenUris && hiddenUris.length ? hiddenUris.map(hiddenUri => hiddenUri.split('#')[1]) : [],
2019-07-17 22:49:06 +02:00
not_tags: !showNsfw ? MATURE_TAGS : [],
order_by:
typeSort === TYPE_TRENDING
? ['trending_group', 'trending_mixed']
2019-07-17 22:49:06 +02:00
: typeSort === TYPE_NEW
? ['release_time']
: ['effective_amount'], // Sort by top
};
if (typeSort === TYPE_TOP && timeSort !== TIME_ALL) {
options.release_time = `>${Math.floor(
moment()
.subtract(1, timeSort)
.startOf('hour')
.unix()
)}`;
} else if (typeSort === TYPE_NEW || typeSort === TYPE_TRENDING) {
2020-01-24 17:32:13 +01:00
// Warning - hack below
// If users are following more than 10 channels or tags, limit results to stuff less than a year old
// For more than 20, drop it down to 6 months
2020-01-24 16:16:25 +01:00
// This helps with timeout issues for users that are following a ton of stuff
2020-01-24 17:32:13 +01:00
// https://github.com/lbryio/lbry-sdk/issues/2420
if (options.channel_ids.length > 10 || options.any_tags.length > 10) {
options.release_time = `>${Math.floor(
moment()
.subtract(1, TIME_YEAR)
.startOf('week')
.unix()
)}`;
} else if (options.channel_ids.length > 20 || options.any_tags.length > 20) {
options.release_time = `>${Math.floor(
moment()
.subtract(6, TIME_MONTH)
.startOf('week')
.unix()
)}`;
}
2019-07-17 22:49:06 +02:00
}
2020-01-02 17:30:27 +01:00
const hasMatureTags = tags && tags.some(t => MATURE_TAGS.includes(t));
2019-07-31 21:07:26 +02:00
const claimSearchCacheQuery = createNormalizedClaimSearchKey(options);
2020-01-02 17:30:27 +01:00
const uris = claimSearchByQuery[claimSearchCacheQuery] || [];
2019-07-23 10:05:51 +02:00
const shouldPerformSearch =
2020-01-02 17:30:27 +01:00
uris.length === 0 ||
didNavigateForward ||
(!loading && uris.length < PAGE_SIZE * page && uris.length % PAGE_SIZE === 0);
2019-07-31 21:07:26 +02:00
// Don't use the query from createNormalizedClaimSearchKey for the effect since that doesn't include page & release_time
2019-07-17 22:49:06 +02:00
const optionsStringForEffect = JSON.stringify(options);
const noResults = (
<div>
<p>
<I18nMessage
tokens={{
again: (
<Button
button="link"
label={__('Please try again in a few seconds.')}
onClick={() => setForceRefresh(Date.now())}
/>
),
}}
>
Sorry, your request timed out. %again%
</I18nMessage>
</p>
<p>
<I18nMessage
tokens={{
2019-12-05 19:38:11 +01:00
contact_support: <Button button="link" label={__('contact support')} href="https://lbry.com/faq/support" />,
}}
>
2019-12-05 19:38:11 +01:00
If you continue to have issues, please %contact_support%.
</I18nMessage>
</p>
</div>
);
2019-07-17 22:49:06 +02:00
function getSearch() {
let search = `?`;
if (!personalView) {
search += `t=${tagsInUrl}&`;
2019-06-11 20:10:58 +02:00
}
2019-07-17 22:49:06 +02:00
return search;
}
2019-07-01 03:52:38 +02:00
2019-07-17 22:49:06 +02:00
function handleTypeSort(newTypeSort) {
2020-01-02 17:30:27 +01:00
let url = `${getSearch()}type=${newTypeSort}`;
2019-07-17 22:49:06 +02:00
if (newTypeSort === TYPE_TOP) {
url += `&time=${timeSort}`;
2019-07-01 03:52:38 +02:00
}
setPage(1);
2019-07-17 22:49:06 +02:00
history.push(url);
}
2019-07-01 03:52:38 +02:00
2019-07-17 22:49:06 +02:00
function handleTimeSort(newTimeSort) {
setPage(1);
2020-01-02 17:30:27 +01:00
history.push(`${getSearch()}type=${typeSort}&time=${newTimeSort}`);
}
2019-07-17 22:49:06 +02:00
function handleScrollBottom() {
if (!loading) {
setPage(page + 1);
2019-07-17 22:49:06 +02:00
}
}
useEffect(() => {
if (shouldPerformSearch) {
const searchOptions = JSON.parse(optionsStringForEffect);
doClaimSearch(searchOptions);
}
}, [doClaimSearch, shouldPerformSearch, optionsStringForEffect, forceRefresh]);
2019-07-17 22:49:06 +02:00
2019-06-11 20:10:58 +02:00
const header = (
2019-07-21 23:31:22 +02:00
<Fragment>
2020-01-02 17:30:27 +01:00
{SEARCH_TYPES.map(type => (
<Button
key={type}
button="alt"
onClick={() => handleTypeSort(type)}
className={classnames(`button-toggle button-toggle--${type}`, {
'button-toggle--active': typeSort === type,
})}
icon={toCapitalCase(type)}
label={__(toCapitalCase(type))}
/>
))}
2019-06-11 20:10:58 +02:00
{typeSort === 'top' && (
<FormField
className="claim-list__dropdown"
2019-06-11 20:10:58 +02:00
type="select"
name="trending_time"
value={timeSort}
2019-07-17 22:49:06 +02:00
onChange={e => handleTimeSort(e.target.value)}
2019-06-11 20:10:58 +02:00
>
2019-06-11 20:56:23 +02:00
{SEARCH_TIMES.map(time => (
2019-06-11 20:10:58 +02:00
<option key={time} value={time}>
2019-07-01 03:52:38 +02:00
{/* i18fixme */}
2019-07-02 23:21:46 +02:00
{time === TIME_DAY && __('Today')}
2020-01-09 20:03:47 +01:00
{time !== TIME_ALL &&
time !== TIME_DAY &&
__('This ' + toCapitalCase(time)) /* yes, concat before i18n, since it is read from const */}
2019-07-02 23:21:46 +02:00
{time === TIME_ALL && __('All time')}
2019-06-11 20:10:58 +02:00
</option>
))}
</FormField>
)}
{hasMatureTags && hiddenNsfwMessage}
2019-07-21 23:31:22 +02:00
</Fragment>
2019-06-11 20:10:58 +02:00
);
return (
2020-01-02 21:36:03 +01:00
<React.Fragment>
2019-06-19 07:05:43 +02:00
<ClaimList
2019-07-23 10:05:51 +02:00
id={claimSearchCacheQuery}
2019-06-17 22:32:38 +02:00
loading={loading}
uris={uris}
header={header}
2020-01-02 21:36:03 +01:00
headerLabel={headerLabel}
2019-07-01 03:52:38 +02:00
headerAltControls={meta}
2019-07-17 22:49:06 +02:00
onScrollBottom={handleScrollBottom}
2019-07-01 06:35:36 +02:00
page={page}
2019-07-02 04:54:11 +02:00
pageSize={PAGE_SIZE}
2020-01-02 17:30:27 +01:00
empty={noResults}
2019-06-17 22:32:38 +02:00
/>
2019-06-27 08:18:45 +02:00
2020-01-02 21:36:03 +01:00
<div className="card">
{loading && new Array(PAGE_SIZE).fill(1).map((x, i) => <ClaimPreview key={i} placeholder="loading" />)}
</div>
</React.Fragment>
2019-06-11 20:10:58 +02:00
);
}
2019-07-17 22:49:06 +02:00
export default withRouter(ClaimListDiscover);