2020-01-20 17:47:03 +01:00
|
|
|
// @flow
|
|
|
|
import React from 'react';
|
|
|
|
import { createNormalizedClaimSearchKey, MATURE_TAGS } from 'lbry-redux';
|
|
|
|
import ClaimPreviewTile from 'component/claimPreviewTile';
|
2020-05-21 17:38:28 +02:00
|
|
|
import { useHistory } from 'react-router';
|
2020-01-20 17:47:03 +01:00
|
|
|
|
|
|
|
type Props = {
|
2020-04-17 18:21:00 +02:00
|
|
|
prefixUris?: Array<string>,
|
2020-01-20 17:47:03 +01:00
|
|
|
uris: Array<string>,
|
|
|
|
doClaimSearch: ({}) => void,
|
|
|
|
showNsfw: boolean,
|
2020-04-15 18:43:22 +02:00
|
|
|
hideReposts: boolean,
|
2020-01-20 17:47:03 +01:00
|
|
|
history: { action: string, push: string => void, replace: string => void },
|
|
|
|
claimSearchByQuery: {
|
|
|
|
[string]: Array<string>,
|
|
|
|
},
|
|
|
|
// claim search options are below
|
|
|
|
tags: Array<string>,
|
|
|
|
hiddenUris: Array<string>,
|
|
|
|
channelIds?: Array<string>,
|
2020-02-18 18:00:47 +01:00
|
|
|
notChannelIds?: Array<string>,
|
2020-01-20 17:47:03 +01:00
|
|
|
pageSize: number,
|
|
|
|
orderBy?: Array<string>,
|
|
|
|
releaseTime?: string,
|
2020-02-26 22:46:46 +01:00
|
|
|
claimType?: Array<string>,
|
2020-01-20 17:47:03 +01:00
|
|
|
timestamp?: string,
|
2020-05-21 17:38:28 +02:00
|
|
|
feeAmount?: string,
|
2020-01-20 17:47:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
function ClaimTilesDiscover(props: Props) {
|
|
|
|
const {
|
|
|
|
doClaimSearch,
|
|
|
|
claimSearchByQuery,
|
|
|
|
showNsfw,
|
2020-04-15 18:43:22 +02:00
|
|
|
hideReposts,
|
2020-01-20 17:47:03 +01:00
|
|
|
hiddenUris,
|
|
|
|
// Below are options to pass that are forwarded to claim_search
|
|
|
|
tags,
|
|
|
|
channelIds,
|
2020-02-18 18:00:47 +01:00
|
|
|
notChannelIds,
|
2020-01-20 17:47:03 +01:00
|
|
|
orderBy,
|
|
|
|
pageSize = 8,
|
|
|
|
releaseTime,
|
|
|
|
claimType,
|
2020-04-17 18:21:00 +02:00
|
|
|
prefixUris,
|
2020-01-20 17:47:03 +01:00
|
|
|
timestamp,
|
2020-05-21 17:38:28 +02:00
|
|
|
feeAmount,
|
2020-01-20 17:47:03 +01:00
|
|
|
} = props;
|
2020-05-21 17:38:28 +02:00
|
|
|
const { location } = useHistory();
|
|
|
|
const urlParams = new URLSearchParams(location.search);
|
|
|
|
const feeAmountInUrl = urlParams.get('fee_amount');
|
|
|
|
const feeAmountParam = feeAmountInUrl || feeAmount;
|
2020-01-22 17:12:48 +01:00
|
|
|
const [hasSearched, setHasSearched] = React.useState(false);
|
2020-01-20 17:47:03 +01:00
|
|
|
const options: {
|
|
|
|
page_size: number,
|
|
|
|
no_totals: boolean,
|
|
|
|
any_tags: Array<string>,
|
|
|
|
channel_ids: Array<string>,
|
|
|
|
channel_ids: Array<string>,
|
|
|
|
not_channel_ids: Array<string>,
|
|
|
|
not_tags: Array<string>,
|
|
|
|
order_by: Array<string>,
|
|
|
|
release_time?: string,
|
2020-02-28 18:12:19 +01:00
|
|
|
claim_type?: Array<string>,
|
2020-01-20 17:47:03 +01:00
|
|
|
timestamp?: string,
|
2020-05-21 17:38:28 +02:00
|
|
|
fee_amount?: string,
|
2020-01-20 17:47:03 +01:00
|
|
|
} = {
|
|
|
|
page_size: pageSize,
|
2020-02-28 18:12:19 +01:00
|
|
|
claim_type: claimType || undefined,
|
2020-01-20 17:47:03 +01:00
|
|
|
// 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,
|
|
|
|
any_tags: tags || [],
|
|
|
|
not_tags: !showNsfw ? MATURE_TAGS : [],
|
|
|
|
channel_ids: channelIds || [],
|
2020-01-24 21:43:07 +01:00
|
|
|
not_channel_ids:
|
2020-02-18 18:00:47 +01:00
|
|
|
notChannelIds ||
|
2020-01-24 21:43:07 +01:00
|
|
|
// If channelIds were passed in, we don't need not_channel_ids
|
2020-02-18 18:00:47 +01:00
|
|
|
(!channelIds && hiddenUris && hiddenUris.length ? hiddenUris.map(hiddenUri => hiddenUri.split('#')[1]) : []),
|
2020-01-20 17:47:03 +01:00
|
|
|
order_by: orderBy || ['trending_group', 'trending_mixed'],
|
|
|
|
};
|
|
|
|
|
|
|
|
if (releaseTime) {
|
|
|
|
options.release_time = releaseTime;
|
|
|
|
}
|
|
|
|
|
2020-05-21 17:38:28 +02:00
|
|
|
if (feeAmountParam) {
|
|
|
|
options.fee_amount = feeAmountParam;
|
|
|
|
}
|
|
|
|
|
2020-02-28 23:36:22 +01:00
|
|
|
// https://github.com/lbryio/lbry-desktop/issues/3774
|
2020-04-15 18:43:22 +02:00
|
|
|
if (hideReposts) {
|
|
|
|
if (Array.isArray(options.claim_type)) {
|
|
|
|
options.claim_type = options.claim_type.filter(claimType => claimType !== 'repost');
|
|
|
|
} else {
|
|
|
|
options.claim_type = ['stream', 'channel'];
|
|
|
|
}
|
|
|
|
}
|
2020-02-20 13:30:27 +01:00
|
|
|
|
2020-01-20 17:47:03 +01:00
|
|
|
if (claimType) {
|
|
|
|
options.claim_type = claimType;
|
|
|
|
}
|
2020-02-20 13:30:27 +01:00
|
|
|
|
2020-01-20 17:47:03 +01:00
|
|
|
if (timestamp) {
|
|
|
|
options.timestamp = timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
const claimSearchCacheQuery = createNormalizedClaimSearchKey(options);
|
2020-04-17 18:21:00 +02:00
|
|
|
const uris = (prefixUris || []).concat(claimSearchByQuery[claimSearchCacheQuery] || []);
|
2020-03-12 02:43:52 +01:00
|
|
|
const shouldPerformSearch = !hasSearched || uris.length === 0;
|
2020-01-20 17:47:03 +01:00
|
|
|
// Don't use the query from createNormalizedClaimSearchKey for the effect since that doesn't include page & release_time
|
|
|
|
const optionsStringForEffect = JSON.stringify(options);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (shouldPerformSearch) {
|
|
|
|
const searchOptions = JSON.parse(optionsStringForEffect);
|
|
|
|
doClaimSearch(searchOptions);
|
2020-01-22 17:12:48 +01:00
|
|
|
setHasSearched(true);
|
2020-01-20 17:47:03 +01:00
|
|
|
}
|
2020-01-22 17:12:48 +01:00
|
|
|
}, [doClaimSearch, shouldPerformSearch, optionsStringForEffect, hasSearched]);
|
2020-01-20 17:47:03 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<ul className="claim-grid">
|
|
|
|
{uris && uris.length
|
|
|
|
? uris.map(uri => <ClaimPreviewTile key={uri} uri={uri} />)
|
|
|
|
: new Array(pageSize).fill(1).map((x, i) => <ClaimPreviewTile key={i} placeholder />)}
|
|
|
|
</ul>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
export default ClaimTilesDiscover;
|