2021-03-02 03:07:10 +01:00
|
|
|
// @flow
|
|
|
|
import React from 'react';
|
2021-10-18 17:54:59 +02:00
|
|
|
import { lazyImport } from 'util/lazyImport';
|
2021-03-02 03:07:10 +01:00
|
|
|
import Page from 'component/page';
|
2021-03-15 15:32:51 +01:00
|
|
|
import LivestreamLayout from 'component/livestreamLayout';
|
|
|
|
import analytics from 'analytics';
|
2021-12-16 22:59:13 +01:00
|
|
|
import moment from 'moment';
|
|
|
|
import { LIVESTREAM_STARTS_SOON_BUFFER, LIVESTREAM_STARTED_RECENTLY_BUFFER } from 'constants/livestream';
|
2021-03-02 03:07:10 +01:00
|
|
|
|
2021-10-18 17:54:59 +02:00
|
|
|
const LivestreamComments = lazyImport(() => import('component/livestreamComments' /* webpackChunkName: "comments" */));
|
|
|
|
|
2021-03-02 03:07:10 +01:00
|
|
|
type Props = {
|
2021-03-15 15:32:51 +01:00
|
|
|
uri: string,
|
|
|
|
claim: StreamClaim,
|
|
|
|
doSetPlayingUri: ({ uri: ?string }) => void,
|
|
|
|
isAuthenticated: boolean,
|
|
|
|
doUserSetReferrer: (string) => void,
|
2021-12-09 10:23:23 +01:00
|
|
|
channelClaimId: ?string,
|
2021-06-03 17:55:16 +02:00
|
|
|
chatDisabled: boolean,
|
2021-12-16 22:59:13 +01:00
|
|
|
doCommentSocketConnect: (string, string) => void,
|
|
|
|
doCommentSocketDisconnect: (string) => void,
|
|
|
|
doFetchActiveLivestream: (string) => void,
|
|
|
|
currentChannelStatus: LivestreamChannelStatus,
|
2021-03-02 03:07:10 +01:00
|
|
|
};
|
|
|
|
|
2021-03-15 15:32:51 +01:00
|
|
|
export default function LivestreamPage(props: Props) {
|
2021-12-16 22:59:13 +01:00
|
|
|
const {
|
|
|
|
uri,
|
|
|
|
claim,
|
|
|
|
doSetPlayingUri,
|
|
|
|
isAuthenticated,
|
|
|
|
doUserSetReferrer,
|
|
|
|
channelClaimId,
|
|
|
|
chatDisabled,
|
|
|
|
doCommentSocketConnect,
|
|
|
|
doCommentSocketDisconnect,
|
|
|
|
doFetchActiveLivestream,
|
|
|
|
currentChannelStatus,
|
|
|
|
} = props;
|
2021-03-30 01:05:18 +02:00
|
|
|
|
2021-11-24 17:03:21 +01:00
|
|
|
React.useEffect(() => {
|
|
|
|
// TODO: This should not be needed one we unify the livestream player (?)
|
|
|
|
analytics.playerLoadedEvent('livestream', false);
|
|
|
|
}, []);
|
|
|
|
|
2021-12-16 22:59:13 +01:00
|
|
|
const claimId = claim && claim.claim_id;
|
|
|
|
|
|
|
|
// Establish web socket connection for viewer count.
|
2021-03-15 15:32:51 +01:00
|
|
|
React.useEffect(() => {
|
2021-12-16 22:59:13 +01:00
|
|
|
if (claimId) {
|
|
|
|
doCommentSocketConnect(uri, claimId);
|
2021-03-30 01:05:18 +02:00
|
|
|
}
|
2021-03-15 15:32:51 +01:00
|
|
|
|
2021-12-16 22:59:13 +01:00
|
|
|
return () => {
|
|
|
|
if (claimId) {
|
|
|
|
doCommentSocketDisconnect(claimId);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}, [claimId, uri, doCommentSocketConnect, doCommentSocketDisconnect]);
|
|
|
|
|
|
|
|
const [isInitialized, setIsInitialized] = React.useState(false);
|
|
|
|
const [isChannelBroadcasting, setIsChannelBroadcasting] = React.useState(false);
|
|
|
|
const [isCurrentClaimLive, setIsCurrentClaimLive] = React.useState(false);
|
|
|
|
|
|
|
|
const livestreamChannelId = channelClaimId || '';
|
|
|
|
|
|
|
|
// Find out current channels status + active live claim.
|
2021-03-15 15:32:51 +01:00
|
|
|
React.useEffect(() => {
|
2021-12-16 22:59:13 +01:00
|
|
|
doFetchActiveLivestream(livestreamChannelId);
|
|
|
|
const intervalId = setInterval(() => doFetchActiveLivestream(livestreamChannelId), 30000);
|
|
|
|
return () => clearInterval(intervalId);
|
|
|
|
}, [livestreamChannelId, doFetchActiveLivestream]);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
const initialized = currentChannelStatus.channelId === livestreamChannelId;
|
|
|
|
setIsInitialized(initialized);
|
|
|
|
if (initialized) {
|
|
|
|
setIsChannelBroadcasting(currentChannelStatus.isBroadcasting);
|
|
|
|
setIsCurrentClaimLive(currentChannelStatus.liveClaim.claimId === claimId);
|
|
|
|
}
|
|
|
|
}, [currentChannelStatus, livestreamChannelId, claimId]);
|
2021-03-02 03:07:10 +01:00
|
|
|
|
2021-12-16 22:59:13 +01:00
|
|
|
const [activeStreamUri, setActiveStreamUri] = React.useState(false);
|
2021-03-02 03:07:10 +01:00
|
|
|
|
2021-12-16 22:59:13 +01:00
|
|
|
React.useEffect(() => {
|
|
|
|
setActiveStreamUri(!isCurrentClaimLive && isChannelBroadcasting ? currentChannelStatus.liveClaim.claimUri : false);
|
|
|
|
}, [isCurrentClaimLive, isChannelBroadcasting]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
|
|
|
|
|
|
// $FlowFixMe
|
|
|
|
const release = moment.unix(claim.value.release_time);
|
|
|
|
|
|
|
|
const [showLivestream, setShowLivestream] = React.useState(false);
|
|
|
|
const [showScheduledInfo, setShowScheduledInfo] = React.useState(false);
|
|
|
|
const [hideComments, setHideComments] = React.useState(false);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (!isInitialized) return;
|
|
|
|
|
|
|
|
const claimReleaseInFuture = () => release.isAfter();
|
2021-03-02 03:07:10 +01:00
|
|
|
|
2021-12-16 22:59:13 +01:00
|
|
|
const claimReleaseInPast = () => release.isBefore();
|
|
|
|
|
|
|
|
const claimReleaseStartingSoon = () =>
|
|
|
|
release.isBetween(moment(), moment().add(LIVESTREAM_STARTS_SOON_BUFFER, 'minutes'));
|
|
|
|
|
|
|
|
const claimReleaseStartedRecently = () =>
|
|
|
|
release.isBetween(moment().subtract(LIVESTREAM_STARTED_RECENTLY_BUFFER, 'minutes'), moment());
|
|
|
|
|
|
|
|
const checkShowLivestream = () =>
|
|
|
|
isChannelBroadcasting && isCurrentClaimLive && (claimReleaseInPast() || claimReleaseStartingSoon());
|
|
|
|
|
|
|
|
const checkShowScheduledInfo = () =>
|
|
|
|
(!isChannelBroadcasting && (claimReleaseInFuture() || claimReleaseStartedRecently())) ||
|
|
|
|
(isChannelBroadcasting &&
|
|
|
|
((!isCurrentClaimLive && (claimReleaseInFuture() || claimReleaseStartedRecently())) ||
|
|
|
|
(isCurrentClaimLive && claimReleaseInFuture() && !claimReleaseStartingSoon())));
|
|
|
|
|
|
|
|
const checkCommentsDisabled = () => chatDisabled || (claimReleaseInFuture() && !claimReleaseStartingSoon());
|
|
|
|
|
|
|
|
const calculateStreamReleaseState = () => {
|
|
|
|
setShowLivestream(checkShowLivestream());
|
|
|
|
setShowScheduledInfo(checkShowScheduledInfo());
|
|
|
|
setHideComments(checkCommentsDisabled());
|
|
|
|
};
|
|
|
|
|
|
|
|
calculateStreamReleaseState();
|
|
|
|
const intervalId = setInterval(calculateStreamReleaseState, 1000);
|
|
|
|
|
|
|
|
if (isCurrentClaimLive && claimReleaseInPast() && isChannelBroadcasting === true) {
|
|
|
|
clearInterval(intervalId);
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => clearInterval(intervalId);
|
|
|
|
}, [chatDisabled, isChannelBroadcasting, release, isCurrentClaimLive, isInitialized]);
|
|
|
|
|
|
|
|
const stringifiedClaim = JSON.stringify(claim);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (uri && stringifiedClaim) {
|
|
|
|
const jsonClaim = JSON.parse(stringifiedClaim);
|
2021-03-15 15:32:51 +01:00
|
|
|
if (!isAuthenticated) {
|
|
|
|
const uri = jsonClaim.signing_channel && jsonClaim.signing_channel.permanent_url;
|
|
|
|
if (uri) {
|
2021-12-16 22:59:13 +01:00
|
|
|
doUserSetReferrer(uri.replace('lbry://', '')); //
|
2021-03-15 15:32:51 +01:00
|
|
|
}
|
2021-03-02 03:07:10 +01:00
|
|
|
}
|
|
|
|
}
|
2021-12-16 22:59:13 +01:00
|
|
|
}, [uri, stringifiedClaim, isAuthenticated]); // eslint-disable-line react-hooks/exhaustive-deps
|
2021-03-02 03:07:10 +01:00
|
|
|
|
|
|
|
React.useEffect(() => {
|
2021-03-15 15:32:51 +01:00
|
|
|
// Set playing uri to null so the popout player doesnt start playing the dummy claim if a user navigates back
|
2021-07-27 22:35:22 +02:00
|
|
|
// This can be removed when we start using the app video player, not a LIVESTREAM iframe
|
2021-03-15 15:32:51 +01:00
|
|
|
doSetPlayingUri({ uri: null });
|
|
|
|
}, [doSetPlayingUri]);
|
2021-03-02 03:07:10 +01:00
|
|
|
|
|
|
|
return (
|
2021-12-16 22:59:13 +01:00
|
|
|
<Page
|
|
|
|
className="file-page"
|
|
|
|
noFooter
|
|
|
|
livestream
|
|
|
|
chatDisabled={hideComments}
|
|
|
|
rightSide={
|
|
|
|
!hideComments &&
|
|
|
|
isInitialized && (
|
|
|
|
<React.Suspense fallback={null}>
|
|
|
|
<LivestreamComments uri={uri} />
|
|
|
|
</React.Suspense>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{isInitialized && (
|
|
|
|
<LivestreamLayout
|
|
|
|
uri={uri}
|
|
|
|
hideComments={hideComments}
|
|
|
|
release={release}
|
|
|
|
isCurrentClaimLive={isCurrentClaimLive}
|
|
|
|
showLivestream={showLivestream}
|
|
|
|
showScheduledInfo={showScheduledInfo}
|
|
|
|
activeStreamUri={activeStreamUri}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</Page>
|
2021-03-02 03:07:10 +01:00
|
|
|
);
|
|
|
|
}
|