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

211 lines
6.1 KiB
React
Raw Normal View History

// @flow
import { SHOW_ADS, AD_KEYWORD_BLOCKLIST, AD_KEYWORD_BLOCKLIST_CHECK_DESCRIPTION } from 'config';
import React from 'react';
2019-06-19 07:05:43 +02:00
import ClaimList from 'component/claimList';
import ClaimListDiscover from 'component/claimListDiscover';
import Ads from 'web/component/ads';
2020-04-29 21:31:11 +02:00
import Card from 'component/common/card';
import { useIsMobile, useIsMediumScreen } from 'effects/use-screensize';
import Button from 'component/button';
2021-04-08 17:21:45 +02:00
import classnames from 'classnames';
import RecSys from 'recsys';
2021-04-08 17:21:45 +02:00
const VIEW_ALL_RELATED = 'view_all_related';
const VIEW_MORE_FROM = 'view_more_from';
type Props = {
uri: string,
recommendedContentUris: Array<string>,
nextRecommendedUri: string,
isSearching: boolean,
doFetchRecommendedContent: (string) => void,
2020-03-26 22:47:07 +01:00
isAuthenticated: boolean,
2021-04-08 17:21:45 +02:00
claim: ?StreamClaim,
claimId: string,
metadata: any,
};
export default React.memo<Props>(function RecommendedContent(props: Props) {
const {
uri,
doFetchRecommendedContent,
recommendedContentUris,
nextRecommendedUri,
isSearching,
isAuthenticated,
2021-04-08 17:21:45 +02:00
claim,
claimId,
metadata,
} = props;
let { description, title } = metadata;
if (description) {
description = description.toLowerCase();
}
if (title) {
title = title.toLowerCase();
}
2021-12-06 19:22:09 +01:00
let claimNameToCheckAgainst;
if (claim) {
claimNameToCheckAgainst = claim.name && claim.name.toLowerCase();
}
const checkDescriptionForBlacklistWords = AD_KEYWORD_BLOCKLIST_CHECK_DESCRIPTION === 'true';
let triggerBlacklist = false;
if (AD_KEYWORD_BLOCKLIST) {
2021-12-06 19:22:09 +01:00
const termsToCheck = AD_KEYWORD_BLOCKLIST.toLowerCase().split(',');
// eslint-disable-next-line no-unused-vars
if (title) {
for (const term of termsToCheck) {
2021-12-06 19:46:45 +01:00
if (claimNameToCheckAgainst && claimNameToCheckAgainst.includes(term)) {
triggerBlacklist = true;
}
if (title.includes(term)) {
triggerBlacklist = true;
}
if (description && checkDescriptionForBlacklistWords && description.includes(term)) {
triggerBlacklist = true;
}
}
}
}
2021-04-08 17:21:45 +02:00
const [viewMode, setViewMode] = React.useState(VIEW_ALL_RELATED);
const signingChannel = claim && claim.signing_channel;
const channelName = signingChannel ? signingChannel.name : null;
const isMobile = useIsMobile();
const isMedium = useIsMediumScreen();
const { onRecsLoaded: onRecommendationsLoaded, onClickedRecommended: onRecommendationClicked } = RecSys;
React.useEffect(() => {
doFetchRecommendedContent(uri);
}, [uri, doFetchRecommendedContent]);
2018-08-09 18:41:14 +02:00
React.useEffect(() => {
// Right now we only want to record the recs if they actually saw them.
if (
recommendedContentUris &&
recommendedContentUris.length &&
nextRecommendedUri &&
viewMode === VIEW_ALL_RELATED
) {
onRecommendationsLoaded(claimId, recommendedContentUris);
}
}, [recommendedContentUris, onRecommendationsLoaded, claimId, nextRecommendedUri, viewMode]);
function handleRecommendationClicked(e, clickedClaim) {
if (claim) {
onRecommendationClicked(claim.claim_id, clickedClaim.claim_id);
}
}
return (
<Card
isBodyList
smallTitle={!isMobile && !isMedium}
className="file-page__recommended"
title={__('Related')}
2021-04-08 17:21:45 +02:00
titleActions={
signingChannel && (
<div className="recommended-content__bubble">
2021-04-08 17:21:45 +02:00
<Button
className={classnames('button-bubble', {
'button-bubble--active': viewMode === VIEW_ALL_RELATED,
2021-04-08 17:21:45 +02:00
})}
label={__('Related')}
2021-04-08 17:21:45 +02:00
onClick={() => setViewMode(VIEW_ALL_RELATED)}
/>
<Button
className={classnames('button-bubble', {
'button-bubble--active': viewMode === VIEW_MORE_FROM,
2021-04-08 17:21:45 +02:00
})}
label={__('More from %claim_name%', { claim_name: channelName })}
2021-04-08 17:21:45 +02:00
onClick={() => setViewMode(VIEW_MORE_FROM)}
/>
2021-04-08 17:21:45 +02:00
</div>
)
}
body={
<div>
2021-04-08 17:21:45 +02:00
{viewMode === VIEW_ALL_RELATED && (
<ClaimList
type="small"
loading={isSearching}
uris={recommendedContentUris}
hideMenu={isMobile}
injectedItem={
SHOW_ADS &&
IS_WEB &&
!isAuthenticated && <Ads small type={'video'} triggerBlacklist={triggerBlacklist} />
}
empty={__('No related content found')}
onClick={handleRecommendationClicked}
/>
)}
2021-04-08 17:21:45 +02:00
{viewMode === VIEW_MORE_FROM && signingChannel && (
<ClaimListDiscover
2021-04-08 17:21:45 +02:00
hideAdvancedFilter
tileLayout={false}
showHeader={false}
type="small"
claimType={['stream']}
orderBy="new"
pageSize={20}
infiniteScroll={false}
2021-04-08 17:21:45 +02:00
hideFilters
channelIds={[signingChannel.claim_id]}
loading={isSearching}
hideMenu={isMobile}
2021-06-22 18:33:18 +02:00
injectedItem={SHOW_ADS && IS_WEB && !isAuthenticated && <Ads small type={'video'} />}
empty={__('No related content found')}
/>
)}
</div>
}
/>
);
}, areEqual);
function areEqual(prevProps: Props, nextProps: Props) {
const a = prevProps;
const b = nextProps;
if (
a.uri !== b.uri ||
a.nextRecommendedUri !== b.nextRecommendedUri ||
a.isAuthenticated !== b.isAuthenticated ||
a.isSearching !== b.isSearching ||
(a.recommendedContentUris && !b.recommendedContentUris) ||
(!a.recommendedContentUris && b.recommendedContentUris) ||
(a.claim && !b.claim) ||
(!a.claim && b.claim)
) {
return false;
}
if (a.claim && b.claim && a.claim.claim_id !== b.claim.claim_id) {
return false;
}
if (a.recommendedContentUris && b.recommendedContentUris) {
if (a.recommendedContentUris.length !== b.recommendedContentUris.length) {
return false;
}
let i = a.recommendedContentUris.length;
while (i--) {
if (a.recommendedContentUris[i] !== b.recommendedContentUris[i]) {
return false;
}
}
}
return true;
}