lbry-desktop/ui/page/embedWrapper/view.jsx

240 lines
7.4 KiB
React
Raw Normal View History

// @flow
import { SITE_NAME } from 'config';
2022-03-15 17:47:36 +01:00
import React from 'react';
import classnames from 'classnames';
import FileRender from 'component/fileRender';
import FileViewerEmbeddedTitle from 'component/fileViewerEmbeddedTitle';
import Spinner from 'component/spinner';
import Button from 'component/button';
2020-11-10 07:23:08 +01:00
import Card from 'component/common/card';
2022-03-15 17:47:36 +01:00
import { formatLbryUrlForWeb, formatLbryChannelName } from 'util/url';
import { useHistory } from 'react-router';
2022-03-16 15:28:59 +01:00
import { getThumbnailCdnUrl } from 'util/thumbnail';
// $FlowFixMe cannot resolve ...
import FileRenderPlaceholder from 'static/img/fileRenderPlaceholder.png';
2022-03-15 17:47:36 +01:00
2022-03-16 14:54:15 +01:00
const LIVESTREAM_STATUS_CHECK_INTERVAL = 30 * 1000;
type Props = {
uri: string,
2022-03-15 17:47:36 +01:00
claim: ?any,
2020-05-22 03:26:46 +02:00
costInfo: any,
streamingUrl: string,
isResolvingUri: boolean,
2022-03-15 17:47:36 +01:00
blackListedOutpoints: Array<{ txid: string, nout: number }>,
isCurrentClaimLive: boolean,
isLivestreamClaim: boolean,
2022-03-16 15:28:59 +01:00
claimThumbnail?: string,
obscurePreview: boolean,
2022-03-15 17:47:36 +01:00
doResolveUri: (uri: string) => void,
doPlayUri: (uri: string) => void,
doFetchCostInfoForUri: (uri: string) => void,
doFetchChannelLiveStatus: (string) => void,
doCommentSocketConnect: (string, string, string) => void,
doCommentSocketDisconnect: (string, string) => void,
doFetchActiveLivestreams: () => void,
};
export const EmbedContext = React.createContext<any>();
2022-03-15 17:47:36 +01:00
const EmbedWrapperPage = (props: Props) => {
2020-11-10 07:23:08 +01:00
const {
uri,
2022-03-15 17:47:36 +01:00
claim,
2020-11-10 07:23:08 +01:00
costInfo,
streamingUrl,
isResolvingUri,
blackListedOutpoints,
2022-03-15 17:47:36 +01:00
isCurrentClaimLive,
2022-03-16 15:47:06 +01:00
isLivestreamClaim,
2022-03-16 15:28:59 +01:00
claimThumbnail,
obscurePreview,
2022-03-15 17:47:36 +01:00
doResolveUri,
doPlayUri,
doFetchCostInfoForUri,
doFetchChannelLiveStatus,
doCommentSocketConnect,
doCommentSocketDisconnect,
doFetchActiveLivestreams,
2020-11-10 07:23:08 +01:00
} = props;
const {
location: { search },
2022-03-16 15:28:59 +01:00
push,
} = useHistory();
2022-03-15 17:47:36 +01:00
const { claim_id: claimId, canonical_url: canonicalUrl, signing_channel: channelClaim } = claim || {};
2022-03-16 15:28:59 +01:00
const containerRef = React.useRef<any>();
const [thumbnail, setThumbnail] = React.useState(FileRenderPlaceholder);
2022-03-16 15:38:34 +01:00
const [livestreamsFetched, setLivestreamsFetched] = React.useState(false);
2022-03-16 15:28:59 +01:00
2022-03-15 17:47:36 +01:00
const channelUrl = channelClaim && formatLbryChannelName(channelClaim.canonical_url);
const urlParams = new URLSearchParams(search);
const embedLightBackground = urlParams.get('embedBackgroundLight');
2020-05-21 23:11:56 +02:00
const haveClaim = Boolean(claim);
2022-03-15 17:47:36 +01:00
const readyToDisplay = isCurrentClaimLive || (claim && streamingUrl);
const loading = !claim && isResolvingUri;
const noContentFound = !claim && !isResolvingUri;
const isPaidContent = costInfo && costInfo.cost > 0;
const contentLink = formatLbryUrlForWeb(uri);
2020-11-10 07:23:08 +01:00
const signingChannel = claim && claim.signing_channel;
const isClaimBlackListed =
claim &&
blackListedOutpoints &&
blackListedOutpoints.some(
(outpoint) =>
2020-11-10 07:23:08 +01:00
(signingChannel && outpoint.txid === signingChannel.txid && outpoint.nout === signingChannel.nout) ||
(outpoint.txid === claim.txid && outpoint.nout === claim.nout)
);
2022-03-16 15:47:06 +01:00
const isLiveClaimStopped = isLivestreamClaim && !readyToDisplay;
2022-03-16 15:28:59 +01:00
React.useEffect(() => {
if (!claimThumbnail) return;
setTimeout(() => {
let newThumbnail = claimThumbnail;
if (
containerRef.current &&
containerRef.current.parentElement &&
containerRef.current.parentElement.offsetWidth
) {
const w = containerRef.current.parentElement.offsetWidth;
newThumbnail = getThumbnailCdnUrl({ thumbnail: newThumbnail, width: w, height: w });
}
2020-01-31 20:33:40 +01:00
2022-03-16 15:28:59 +01:00
if (newThumbnail !== thumbnail) {
setThumbnail(newThumbnail);
}
}, 200);
}, [claimThumbnail, thumbnail]);
React.useEffect(() => {
2022-03-16 15:47:06 +01:00
if (doFetchActiveLivestreams && isLivestreamClaim) {
2022-03-16 15:38:34 +01:00
doFetchActiveLivestreams();
setLivestreamsFetched(true);
}
2022-03-16 15:47:06 +01:00
}, [doFetchActiveLivestreams, isLivestreamClaim]);
2022-03-15 17:47:36 +01:00
2022-03-16 12:35:58 +01:00
// Establish web socket connection for viewer count.
React.useEffect(() => {
2022-03-16 15:47:06 +01:00
if (!isLivestreamClaim || !claimId || !channelUrl || !canonicalUrl) return;
2022-03-16 12:35:58 +01:00
const channelName = formatLbryChannelName(channelUrl);
doCommentSocketConnect(canonicalUrl, channelName, claimId);
return () => {
if (claimId) {
doCommentSocketDisconnect(claimId, channelName);
}
};
2022-03-16 15:47:06 +01:00
}, [canonicalUrl, channelUrl, claimId, doCommentSocketConnect, doCommentSocketDisconnect, isLivestreamClaim]);
2022-03-15 17:47:36 +01:00
React.useEffect(() => {
if (doResolveUri && uri && !haveClaim) {
doResolveUri(uri);
}
2022-03-16 15:47:06 +01:00
2020-05-22 03:26:46 +02:00
if (uri && haveClaim && costInfo && costInfo.cost === 0) {
2020-05-21 23:11:56 +02:00
doPlayUri(uri);
}
2022-03-15 17:47:36 +01:00
}, [doResolveUri, uri, doPlayUri, haveClaim, costInfo]);
2020-05-22 03:26:46 +02:00
2022-03-15 17:47:36 +01:00
React.useEffect(() => {
2020-05-22 03:26:46 +02:00
if (haveClaim && uri && doFetchCostInfoForUri) {
doFetchCostInfoForUri(uri);
}
}, [uri, haveClaim, doFetchCostInfoForUri]);
2022-03-15 17:47:36 +01:00
// Find out current channels status + active live claim every 30 seconds
React.useEffect(() => {
2022-03-16 15:38:34 +01:00
if (!channelClaim || !livestreamsFetched) return;
2022-03-15 17:47:36 +01:00
const { claim_id: channelClaimId } = channelClaim || {};
doFetchChannelLiveStatus(channelClaimId);
const intervalId = setInterval(() => doFetchChannelLiveStatus(channelClaimId), LIVESTREAM_STATUS_CHECK_INTERVAL);
return () => clearInterval(intervalId);
2022-03-16 15:38:34 +01:00
}, [livestreamsFetched, channelClaim, doFetchChannelLiveStatus]);
2022-03-15 17:47:36 +01:00
2020-11-10 07:23:08 +01:00
if (isClaimBlackListed) {
return (
<Card
title={uri}
subtitle={__(
'In response to a complaint we received under the US Digital Millennium Copyright Act, we have blocked access to this content from our applications.'
)}
actions={
<div className="section__actions">
<Button button="link" href="https://odysee.com/@OdyseeHelp:b/copyright:f" label={__('Read More')} />
2020-11-10 07:23:08 +01:00
</div>
}
/>
);
}
2020-01-31 19:25:48 +01:00
return (
2022-03-16 15:28:59 +01:00
<div
className={
isLiveClaimStopped
? 'embed__inline-button'
: classnames('embed__wrapper', { 'embed__wrapper--light-background': embedLightBackground })
}
style={
isLiveClaimStopped
? thumbnail && !obscurePreview
? { backgroundImage: `url("${thumbnail}")`, height: '100%' }
: {}
: undefined
}
>
{!isLiveClaimStopped ? (
<EmbedContext.Provider value>
{readyToDisplay ? (
<FileRender uri={uri} embedded />
) : (
<div className="embed__loading">
<FileViewerEmbeddedTitle uri={uri} />
<div className="embed__loading-text">
{loading && <Spinner delayed light />}
2022-03-16 15:47:06 +01:00
2022-03-16 15:28:59 +01:00
{noContentFound && <h1>{__('No content found.')}</h1>}
2022-03-16 15:47:06 +01:00
2022-03-16 15:28:59 +01:00
{isPaidContent && (
<div>
<h1>{__('Paid content cannot be embedded.')}</h1>
<div className="section__actions--centered">
<Button label={__('Watch on %SITE_NAME%', { SITE_NAME })} button="primary" href={contentLink} />
</div>
</div>
2022-03-16 15:28:59 +01:00
)}
</div>
</div>
2022-03-16 15:28:59 +01:00
)}
</EmbedContext.Provider>
) : (
<>
<FileViewerEmbeddedTitle uri={uri} />
<Button
onClick={() => {
const formattedUrl = formatLbryUrlForWeb(uri);
push(formattedUrl);
}}
iconSize={30}
title={__('View')}
className="button--icon button--view"
/>
</>
)}
2020-01-31 19:25:48 +01:00
</div>
);
};
export default EmbedWrapperPage;