2021-03-10 19:34:21 +01:00
|
|
|
// @flow
|
2021-03-19 21:14:31 +01:00
|
|
|
import { BITWAVE_API } from 'constants/livestream';
|
2021-03-10 19:34:21 +01:00
|
|
|
import React from 'react';
|
|
|
|
import Card from 'component/common/card';
|
|
|
|
import ClaimPreview from 'component/claimPreview';
|
|
|
|
import { Lbry } from 'lbry-redux';
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
channelClaim: ChannelClaim,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function LivestreamLink(props: Props) {
|
|
|
|
const { channelClaim } = props;
|
|
|
|
const [livestreamClaim, setLivestreamClaim] = React.useState(false);
|
|
|
|
const [isLivestreaming, setIsLivestreaming] = React.useState(false);
|
2021-03-03 20:23:42 +01:00
|
|
|
const livestreamChannelId = (channelClaim && channelClaim.claim_id) || ''; // TODO: fail in a safer way, probably
|
2021-03-10 19:34:21 +01:00
|
|
|
|
|
|
|
React.useEffect(() => {
|
2021-03-25 21:26:33 +01:00
|
|
|
if (livestreamChannelId) {
|
|
|
|
Lbry.claim_search({
|
|
|
|
channel_ids: [livestreamChannelId],
|
|
|
|
has_no_source: true,
|
|
|
|
claim_type: ['stream'],
|
2021-03-10 19:34:21 +01:00
|
|
|
})
|
2021-03-25 21:26:33 +01:00
|
|
|
.then((res) => {
|
|
|
|
if (res && res.items && res.items.length > 0) {
|
|
|
|
const claim = res.items[res.items.length - 1];
|
|
|
|
setLivestreamClaim(claim);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(() => {});
|
|
|
|
}
|
2021-03-10 19:34:21 +01:00
|
|
|
}, [livestreamChannelId]);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
2021-03-19 21:14:31 +01:00
|
|
|
function fetchIsStreaming() {
|
2021-03-22 23:42:51 +01:00
|
|
|
// $FlowFixMe Bitwave's API can handle garbage
|
2021-03-22 05:16:39 +01:00
|
|
|
fetch(`${BITWAVE_API}/${livestreamChannelId}`)
|
2021-03-19 21:14:31 +01:00
|
|
|
.then((res) => res.json())
|
|
|
|
.then((res) => {
|
2021-03-22 06:06:11 +01:00
|
|
|
if (res && res.success && res.data && res.data.live) {
|
2021-03-19 21:14:31 +01:00
|
|
|
setIsLivestreaming(true);
|
|
|
|
} else {
|
|
|
|
setIsLivestreaming(false);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((e) => {});
|
|
|
|
}
|
2021-03-22 10:42:02 +01:00
|
|
|
|
2021-03-19 21:14:31 +01:00
|
|
|
let interval;
|
|
|
|
if (livestreamChannelId) {
|
2021-03-22 06:06:11 +01:00
|
|
|
if (!interval) fetchIsStreaming();
|
2021-03-22 10:42:02 +01:00
|
|
|
interval = setInterval(fetchIsStreaming, 10 * 1000);
|
2021-03-19 21:14:31 +01:00
|
|
|
}
|
2021-03-22 10:42:02 +01:00
|
|
|
|
2021-03-19 21:14:31 +01:00
|
|
|
return () => {
|
|
|
|
if (interval) {
|
|
|
|
clearInterval(interval);
|
|
|
|
}
|
|
|
|
};
|
2021-03-10 19:34:21 +01:00
|
|
|
}, [livestreamChannelId]);
|
|
|
|
|
|
|
|
if (!livestreamClaim || !isLivestreaming) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-03-26 01:46:15 +01:00
|
|
|
// gonna pass the wrapper in so I don't have to rewrite the dmca/blocking logic in claimPreview.
|
|
|
|
const element = (props: { children: any }) => (
|
|
|
|
<Card className="livestream__channel-link" title={__('Live stream in progress')}>
|
|
|
|
{props.children}
|
|
|
|
</Card>
|
2021-03-10 19:34:21 +01:00
|
|
|
);
|
2021-03-26 01:46:15 +01:00
|
|
|
|
|
|
|
return <ClaimPreview uri={livestreamClaim.canonical_url} wrapperElement={element} type="inline" />;
|
2021-03-10 19:34:21 +01:00
|
|
|
}
|