2020-01-20 17:47:03 +01:00
|
|
|
// @flow
|
2021-04-16 04:52:41 +02:00
|
|
|
import { ENABLE_NO_SOURCE_CLAIMS, SIMPLE_SITE } from 'config';
|
|
|
|
import * as CS from 'constants/claim_search';
|
2021-04-23 21:59:48 +02:00
|
|
|
import type { Node } from 'react';
|
2020-01-20 17:47:03 +01:00
|
|
|
import React from 'react';
|
2021-07-15 22:22:44 +02:00
|
|
|
import { createNormalizedClaimSearchKey, MATURE_TAGS, splitBySeparator } from 'lbry-redux';
|
2020-01-20 17:47:03 +01:00
|
|
|
import ClaimPreviewTile from 'component/claimPreviewTile';
|
2020-05-21 17:38:28 +02:00
|
|
|
import { useHistory } from 'react-router';
|
2021-05-15 07:52:24 +02:00
|
|
|
import { getLivestreamOnlyOptions } from 'util/search';
|
2021-05-05 04:36:14 +02:00
|
|
|
|
2021-04-29 07:20:20 +02:00
|
|
|
/**
|
|
|
|
* Updates 'uris' by adding and/or moving active livestreams to the front of
|
|
|
|
* list.
|
|
|
|
* 'liveUris' is also updated with any entries that were moved to the
|
|
|
|
* front, for convenience.
|
|
|
|
*
|
|
|
|
* @param uris [Ref]
|
|
|
|
* @param liveUris [Ref]
|
|
|
|
* @param livestreamMap
|
|
|
|
* @param claimsByUri
|
|
|
|
* @param claimSearchByQuery
|
|
|
|
* @param options
|
|
|
|
*/
|
|
|
|
export function prioritizeActiveLivestreams(
|
|
|
|
uris: Array<string>,
|
|
|
|
liveUris: Array<string>,
|
|
|
|
livestreamMap: { [string]: any },
|
|
|
|
claimsByUri: { [string]: any },
|
|
|
|
claimSearchByQuery: { [string]: Array<string> },
|
|
|
|
options: any
|
|
|
|
) {
|
|
|
|
if (!livestreamMap || !uris) return;
|
|
|
|
|
|
|
|
const claimIsLive = (claim, liveChannelIds) => {
|
|
|
|
// This function relies on:
|
|
|
|
// 1. Only 1 actual livestream per channel (i.e. all other livestream-claims
|
|
|
|
// for that channel actually point to the same source).
|
|
|
|
// 2. 'liveChannelIds' needs to be pruned after being accounted for,
|
|
|
|
// otherwise all livestream-claims will be "live" (we'll only take the
|
2021-07-07 20:21:11 +02:00
|
|
|
// latest one as "live" ).
|
2021-04-29 07:20:20 +02:00
|
|
|
return (
|
|
|
|
claim &&
|
|
|
|
claim.value_type === 'stream' &&
|
|
|
|
claim.value.source === undefined &&
|
|
|
|
claim.signing_channel &&
|
|
|
|
liveChannelIds.includes(claim.signing_channel.claim_id)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
let liveChannelIds = Object.keys(livestreamMap);
|
|
|
|
|
|
|
|
// 1. Collect active livestreams from the primary search to put in front.
|
2021-05-05 04:36:14 +02:00
|
|
|
uris.forEach((uri) => {
|
2021-04-29 07:20:20 +02:00
|
|
|
const claim = claimsByUri[uri];
|
|
|
|
if (claimIsLive(claim, liveChannelIds)) {
|
|
|
|
liveUris.push(uri);
|
|
|
|
// This live channel has been accounted for, so remove it.
|
|
|
|
liveChannelIds.splice(liveChannelIds.indexOf(claim.signing_channel.claim_id), 1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// 2. Now, repeat on the secondary search.
|
|
|
|
if (options) {
|
2021-05-15 07:52:24 +02:00
|
|
|
const livestreamsOnlySearchCacheQuery = createNormalizedClaimSearchKey(getLivestreamOnlyOptions(options));
|
2021-04-29 07:20:20 +02:00
|
|
|
const livestreamsOnlyUris = claimSearchByQuery[livestreamsOnlySearchCacheQuery];
|
|
|
|
if (livestreamsOnlyUris) {
|
2021-05-05 04:36:14 +02:00
|
|
|
livestreamsOnlyUris.forEach((uri) => {
|
2021-04-29 07:20:20 +02:00
|
|
|
const claim = claimsByUri[uri];
|
|
|
|
if (!uris.includes(uri) && claimIsLive(claim, liveChannelIds)) {
|
|
|
|
liveUris.push(uri);
|
|
|
|
// This live channel has been accounted for, so remove it.
|
|
|
|
liveChannelIds.splice(liveChannelIds.indexOf(claim.signing_channel.claim_id), 1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3. Finalize uris by putting live livestreams in front.
|
2021-05-05 04:36:14 +02:00
|
|
|
const newUris = liveUris.concat(uris.filter((uri) => !liveUris.includes(uri)));
|
2021-04-29 07:20:20 +02:00
|
|
|
uris.splice(0, uris.length, ...newUris);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ****************************************************************************
|
|
|
|
// ClaimTilesDiscover
|
|
|
|
// ****************************************************************************
|
|
|
|
|
2020-01-20 17:47:03 +01:00
|
|
|
type Props = {
|
2020-04-17 18:21:00 +02:00
|
|
|
prefixUris?: Array<string>,
|
2021-09-09 10:06:04 +02:00
|
|
|
pinUrls?: Array<string>,
|
2020-01-20 17:47:03 +01:00
|
|
|
uris: Array<string>,
|
2021-09-09 10:06:04 +02:00
|
|
|
liveLivestreamsFirst?: boolean,
|
|
|
|
livestreamMap?: { [string]: any },
|
|
|
|
showNoSourceClaims?: boolean,
|
|
|
|
renderProperties?: (Claim) => ?Node,
|
2021-09-09 09:02:31 +02:00
|
|
|
fetchViewCount?: boolean,
|
2020-01-20 17:47:03 +01:00
|
|
|
// claim search options are below
|
|
|
|
tags: Array<string>,
|
2020-10-05 19:38:40 +02:00
|
|
|
claimIds?: Array<string>,
|
2020-01-20 17:47:03 +01:00
|
|
|
channelIds?: Array<string>,
|
|
|
|
pageSize: number,
|
|
|
|
orderBy?: Array<string>,
|
|
|
|
releaseTime?: string,
|
2020-09-11 20:21:02 +02:00
|
|
|
languages?: Array<string>,
|
2021-03-26 00:52:28 +01:00
|
|
|
claimType?: string | Array<string>,
|
2021-04-16 04:52:41 +02:00
|
|
|
streamTypes?: Array<string>,
|
2020-01-20 17:47:03 +01:00
|
|
|
timestamp?: string,
|
2020-05-21 17:38:28 +02:00
|
|
|
feeAmount?: string,
|
2020-08-20 17:23:10 +02:00
|
|
|
limitClaimsPerChannel?: number,
|
2021-04-29 11:33:24 +02:00
|
|
|
hasSource?: boolean,
|
2021-04-23 21:59:48 +02:00
|
|
|
hasNoSource?: boolean,
|
2021-09-09 10:06:04 +02:00
|
|
|
// --- select ---
|
|
|
|
claimSearchByQuery: { [string]: Array<string> },
|
|
|
|
claimsByUri: { [string]: any },
|
|
|
|
fetchingClaimSearchByQuery: { [string]: boolean },
|
|
|
|
showNsfw: boolean,
|
|
|
|
hideReposts: boolean,
|
|
|
|
mutedUris: Array<string>,
|
|
|
|
blockedUris: Array<string>,
|
|
|
|
// --- perform ---
|
|
|
|
doClaimSearch: ({}) => void,
|
2021-09-09 09:02:31 +02:00
|
|
|
doFetchViewCount: (claimIdCsv: string) => void,
|
2020-01-20 17:47:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
function ClaimTilesDiscover(props: Props) {
|
|
|
|
const {
|
|
|
|
doClaimSearch,
|
|
|
|
claimSearchByQuery,
|
2021-04-28 11:53:04 +02:00
|
|
|
claimsByUri,
|
2020-01-20 17:47:03 +01:00
|
|
|
showNsfw,
|
2020-04-15 18:43:22 +02:00
|
|
|
hideReposts,
|
2021-09-09 09:02:31 +02:00
|
|
|
fetchViewCount,
|
2020-01-20 17:47:03 +01:00
|
|
|
// Below are options to pass that are forwarded to claim_search
|
|
|
|
tags,
|
|
|
|
channelIds,
|
2020-10-05 19:38:40 +02:00
|
|
|
claimIds,
|
2020-01-20 17:47:03 +01:00
|
|
|
orderBy,
|
|
|
|
pageSize = 8,
|
|
|
|
releaseTime,
|
2020-09-11 20:21:02 +02:00
|
|
|
languages,
|
2020-01-20 17:47:03 +01:00
|
|
|
claimType,
|
2021-04-16 04:52:41 +02:00
|
|
|
streamTypes,
|
2020-01-20 17:47:03 +01:00
|
|
|
timestamp,
|
2020-05-21 17:38:28 +02:00
|
|
|
feeAmount,
|
2020-08-20 17:23:10 +02:00
|
|
|
limitClaimsPerChannel,
|
2020-09-11 21:48:23 +02:00
|
|
|
fetchingClaimSearchByQuery,
|
2021-04-29 11:33:24 +02:00
|
|
|
hasSource,
|
2021-04-23 21:59:48 +02:00
|
|
|
hasNoSource,
|
|
|
|
renderProperties,
|
2021-04-24 06:44:42 +02:00
|
|
|
blockedUris,
|
|
|
|
mutedUris,
|
2021-04-28 14:49:38 +02:00
|
|
|
liveLivestreamsFirst,
|
2021-04-28 11:53:04 +02:00
|
|
|
livestreamMap,
|
2021-07-17 20:55:18 +02:00
|
|
|
pinUrls,
|
2021-05-05 04:36:14 +02:00
|
|
|
prefixUris,
|
2021-07-08 18:21:42 +02:00
|
|
|
showNoSourceClaims,
|
2021-09-09 09:02:31 +02:00
|
|
|
doFetchViewCount,
|
2020-01-20 17:47:03 +01:00
|
|
|
} = props;
|
2021-04-24 06:44:42 +02:00
|
|
|
|
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;
|
2021-07-15 22:22:44 +02:00
|
|
|
const mutedAndBlockedChannelIds = Array.from(
|
|
|
|
new Set(mutedUris.concat(blockedUris).map((uri) => splitBySeparator(uri)[1]))
|
|
|
|
);
|
2021-04-28 11:53:04 +02:00
|
|
|
const liveUris = [];
|
2021-08-01 22:08:32 +02:00
|
|
|
let streamTypesParam;
|
|
|
|
if (streamTypes) {
|
|
|
|
streamTypesParam = streamTypes;
|
|
|
|
} else if (SIMPLE_SITE && !hasNoSource && streamTypes !== null) {
|
|
|
|
streamTypesParam = [CS.FILE_VIDEO, CS.FILE_AUDIO];
|
|
|
|
}
|
2021-04-24 06:44:42 +02:00
|
|
|
|
2021-05-27 09:50:03 +02:00
|
|
|
const [prevUris, setPrevUris] = React.useState([]);
|
|
|
|
|
2020-01-20 17:47:03 +01:00
|
|
|
const options: {
|
|
|
|
page_size: number,
|
|
|
|
no_totals: boolean,
|
|
|
|
any_tags: Array<string>,
|
|
|
|
channel_ids: Array<string>,
|
2020-10-05 19:38:40 +02:00
|
|
|
claim_ids?: Array<string>,
|
2020-01-20 17:47:03 +01:00
|
|
|
not_channel_ids: Array<string>,
|
|
|
|
not_tags: Array<string>,
|
|
|
|
order_by: Array<string>,
|
2020-09-11 20:21:02 +02:00
|
|
|
languages?: Array<string>,
|
2020-01-20 17:47:03 +01:00
|
|
|
release_time?: string,
|
2021-03-26 00:52:28 +01:00
|
|
|
claim_type?: string | Array<string>,
|
2020-01-20 17:47:03 +01:00
|
|
|
timestamp?: string,
|
2020-05-21 17:38:28 +02:00
|
|
|
fee_amount?: string,
|
2020-08-20 17:23:10 +02:00
|
|
|
limit_claims_per_channel?: number,
|
2020-10-05 19:38:40 +02:00
|
|
|
stream_types?: Array<string>,
|
2021-03-18 16:41:21 +01:00
|
|
|
has_source?: boolean,
|
|
|
|
has_no_source?: boolean,
|
2020-01-20 17:47:03 +01:00
|
|
|
} = {
|
|
|
|
page_size: pageSize,
|
2021-08-27 16:57:54 +02:00
|
|
|
claim_type: claimType || ['stream', 'repost', 'channel'],
|
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 : [],
|
2020-09-11 20:21:02 +02:00
|
|
|
any_languages: languages,
|
2020-01-20 17:47:03 +01:00
|
|
|
channel_ids: channelIds || [],
|
2021-04-24 06:44:42 +02:00
|
|
|
not_channel_ids: mutedAndBlockedChannelIds,
|
2020-01-20 17:47:03 +01:00
|
|
|
order_by: orderBy || ['trending_group', 'trending_mixed'],
|
2021-08-01 22:08:32 +02:00
|
|
|
stream_types: streamTypesParam,
|
2020-01-20 17:47:03 +01:00
|
|
|
};
|
|
|
|
|
2021-04-23 21:59:48 +02:00
|
|
|
if (ENABLE_NO_SOURCE_CLAIMS && hasNoSource) {
|
|
|
|
options.has_no_source = true;
|
2021-04-29 11:33:24 +02:00
|
|
|
} else if (hasSource || (!ENABLE_NO_SOURCE_CLAIMS && (!claimType || claimType === 'stream'))) {
|
2021-03-17 18:49:55 +01:00
|
|
|
options.has_source = true;
|
2021-03-18 16:41:21 +01:00
|
|
|
}
|
|
|
|
|
2020-01-20 17:47:03 +01:00
|
|
|
if (releaseTime) {
|
|
|
|
options.release_time = releaseTime;
|
|
|
|
}
|
|
|
|
|
2020-05-21 17:38:28 +02:00
|
|
|
if (feeAmountParam) {
|
|
|
|
options.fee_amount = feeAmountParam;
|
|
|
|
}
|
|
|
|
|
2020-08-20 17:23:10 +02:00
|
|
|
if (limitClaimsPerChannel) {
|
|
|
|
options.limit_claims_per_channel = limitClaimsPerChannel;
|
|
|
|
}
|
|
|
|
|
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)) {
|
2021-05-05 04:36:14 +02:00
|
|
|
options.claim_type = options.claim_type.filter((claimType) => claimType !== 'repost');
|
2020-04-15 18:43:22 +02:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
2020-10-05 19:38:40 +02:00
|
|
|
if (claimIds) {
|
|
|
|
options.claim_ids = claimIds;
|
|
|
|
}
|
|
|
|
|
2021-05-27 09:50:03 +02:00
|
|
|
const mainSearchKey = createNormalizedClaimSearchKey(options);
|
|
|
|
const livestreamSearchKey = liveLivestreamsFirst
|
|
|
|
? createNormalizedClaimSearchKey(getLivestreamOnlyOptions(options))
|
|
|
|
: undefined;
|
|
|
|
|
|
|
|
let uris = (prefixUris || []).concat(claimSearchByQuery[mainSearchKey] || []);
|
|
|
|
|
|
|
|
const isLoading = fetchingClaimSearchByQuery[mainSearchKey];
|
2021-04-28 11:53:04 +02:00
|
|
|
|
2021-07-07 20:21:11 +02:00
|
|
|
if (liveLivestreamsFirst && livestreamMap && !isLoading) {
|
2021-04-29 07:20:20 +02:00
|
|
|
prioritizeActiveLivestreams(uris, liveUris, livestreamMap, claimsByUri, claimSearchByQuery, options);
|
2021-04-28 11:53:04 +02:00
|
|
|
}
|
|
|
|
|
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);
|
2020-09-11 21:48:23 +02:00
|
|
|
const shouldPerformSearch = !isLoading && uris.length === 0;
|
2020-01-20 17:47:03 +01:00
|
|
|
|
2021-05-27 09:50:03 +02:00
|
|
|
if (
|
|
|
|
prefixUris === undefined &&
|
|
|
|
(claimSearchByQuery[mainSearchKey] === undefined ||
|
|
|
|
(livestreamSearchKey && claimSearchByQuery[livestreamSearchKey] === undefined))
|
|
|
|
) {
|
|
|
|
// This is a new query and we don't have results yet ...
|
|
|
|
if (prevUris.length !== 0) {
|
|
|
|
// ... but we have previous results. Use it until new results are here.
|
|
|
|
uris = prevUris;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-09 10:06:04 +02:00
|
|
|
const modifiedUris = uris ? uris.slice() : [];
|
|
|
|
const fixUris = pinUrls || [];
|
|
|
|
|
|
|
|
if (pinUrls && modifiedUris && modifiedUris.length > 2 && window.location.pathname === '/') {
|
|
|
|
fixUris.forEach((fixUri) => {
|
|
|
|
if (modifiedUris.indexOf(fixUri) !== -1) {
|
|
|
|
modifiedUris.splice(modifiedUris.indexOf(fixUri), 1);
|
|
|
|
} else {
|
|
|
|
modifiedUris.pop();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
modifiedUris.splice(2, 0, ...fixUris);
|
|
|
|
}
|
|
|
|
|
|
|
|
// **************************************************************************
|
|
|
|
// **************************************************************************
|
|
|
|
|
|
|
|
function resolveLive(index) {
|
|
|
|
if (liveLivestreamsFirst && livestreamMap && index < liveUris.length) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2021-09-09 09:02:31 +02:00
|
|
|
function fetchViewCountForUris(uris) {
|
|
|
|
const claimIds = [];
|
2021-09-09 16:05:22 +02:00
|
|
|
|
|
|
|
if (uris) {
|
|
|
|
uris.forEach((uri) => {
|
|
|
|
if (claimsByUri[uri]) {
|
|
|
|
claimIds.push(claimsByUri[uri].claim_id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (claimIds.length > 0) {
|
|
|
|
doFetchViewCount(claimIds.join(','));
|
|
|
|
}
|
2021-09-09 09:02:31 +02:00
|
|
|
}
|
|
|
|
|
2021-09-09 10:06:04 +02:00
|
|
|
// **************************************************************************
|
|
|
|
// **************************************************************************
|
|
|
|
|
2020-01-20 17:47:03 +01:00
|
|
|
React.useEffect(() => {
|
|
|
|
if (shouldPerformSearch) {
|
|
|
|
const searchOptions = JSON.parse(optionsStringForEffect);
|
|
|
|
doClaimSearch(searchOptions);
|
2021-04-30 22:45:18 +02:00
|
|
|
|
2021-04-28 14:49:38 +02:00
|
|
|
if (liveLivestreamsFirst) {
|
2021-05-15 07:52:24 +02:00
|
|
|
doClaimSearch(getLivestreamOnlyOptions(searchOptions));
|
2021-04-28 14:49:38 +02:00
|
|
|
}
|
2020-01-20 17:47:03 +01:00
|
|
|
}
|
2021-04-28 14:49:38 +02:00
|
|
|
}, [doClaimSearch, shouldPerformSearch, optionsStringForEffect, liveLivestreamsFirst]);
|
2020-01-20 17:47:03 +01:00
|
|
|
|
2021-05-27 09:50:03 +02:00
|
|
|
React.useEffect(() => {
|
|
|
|
if (JSON.stringify(prevUris) !== JSON.stringify(uris) && !shouldPerformSearch) {
|
2021-09-09 09:02:31 +02:00
|
|
|
// Stash new results for next render cycle:
|
2021-05-27 09:50:03 +02:00
|
|
|
setPrevUris(uris);
|
2021-09-09 09:02:31 +02:00
|
|
|
// Fetch view count:
|
|
|
|
if (fetchViewCount) {
|
|
|
|
fetchViewCountForUris(uris);
|
|
|
|
}
|
2021-05-27 09:50:03 +02:00
|
|
|
}
|
2021-09-09 09:02:31 +02:00
|
|
|
}, [shouldPerformSearch, prevUris, uris]); // eslint-disable-line react-hooks/exhaustive-deps
|
2021-05-27 09:50:03 +02:00
|
|
|
|
2021-09-09 10:06:04 +02:00
|
|
|
// **************************************************************************
|
|
|
|
// **************************************************************************
|
2021-07-17 20:55:18 +02:00
|
|
|
|
2020-01-20 17:47:03 +01:00
|
|
|
return (
|
|
|
|
<ul className="claim-grid">
|
2021-07-17 20:55:18 +02:00
|
|
|
{modifiedUris && modifiedUris.length
|
|
|
|
? modifiedUris.map((uri, index) => (
|
2021-07-23 04:15:45 +02:00
|
|
|
<ClaimPreviewTile
|
|
|
|
showNoSourceClaims={hasNoSource || showNoSourceClaims}
|
|
|
|
key={uri}
|
|
|
|
uri={uri}
|
|
|
|
properties={renderProperties}
|
|
|
|
live={resolveLive(index)}
|
|
|
|
/>
|
2021-04-28 11:53:04 +02:00
|
|
|
))
|
2021-07-08 18:21:42 +02:00
|
|
|
: new Array(pageSize)
|
|
|
|
.fill(1)
|
|
|
|
.map((x, i) => (
|
|
|
|
<ClaimPreviewTile showNoSourceClaims={hasNoSource || showNoSourceClaims} key={i} placeholder />
|
|
|
|
))}
|
2020-01-20 17:47:03 +01:00
|
|
|
</ul>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
export default ClaimTilesDiscover;
|