2021-03-10 19:34:21 +01:00
|
|
|
// @flow
|
2021-07-27 22:35:22 +02:00
|
|
|
import { LIVESTREAM_LIVE_API } from 'constants/livestream';
|
2021-04-27 17:18:09 +02:00
|
|
|
import * as CS from 'constants/claim_search';
|
2021-03-10 19:34:21 +01:00
|
|
|
import React from 'react';
|
|
|
|
import Card from 'component/common/card';
|
|
|
|
import ClaimPreview from 'component/claimPreview';
|
2021-10-08 05:47:39 +02:00
|
|
|
import Lbry from 'lbry';
|
2021-04-23 21:59:48 +02:00
|
|
|
import { useHistory } from 'react-router';
|
|
|
|
import { formatLbryUrlForWeb } from 'util/url';
|
2021-03-10 19:34:21 +01:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
channelClaim: ChannelClaim,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function LivestreamLink(props: Props) {
|
|
|
|
const { channelClaim } = props;
|
2021-04-23 21:59:48 +02:00
|
|
|
const { push } = useHistory();
|
2021-03-10 19:34:21 +01:00
|
|
|
const [livestreamClaim, setLivestreamClaim] = React.useState(false);
|
|
|
|
const [isLivestreaming, setIsLivestreaming] = React.useState(false);
|
2021-07-20 03:45:51 +02:00
|
|
|
const livestreamChannelId = (channelClaim && channelClaim.claim_id) || ''; // TODO: fail in a safer way, probably
|
2021-10-02 19:58:39 +02:00
|
|
|
// TODO: pput this back when hubs claims_in_channel are fixed
|
|
|
|
const isChannelEmpty = !channelClaim || !channelClaim.meta;
|
|
|
|
// ||
|
|
|
|
// !channelClaim.meta.claims_in_channel;
|
2021-03-10 19:34:21 +01:00
|
|
|
|
|
|
|
React.useEffect(() => {
|
2021-07-22 22:09:28 +02:00
|
|
|
// Don't search empty channels
|
|
|
|
if (livestreamChannelId && !isChannelEmpty) {
|
2021-03-25 21:26:33 +01:00
|
|
|
Lbry.claim_search({
|
|
|
|
channel_ids: [livestreamChannelId],
|
2021-07-23 08:47:01 +02:00
|
|
|
page: 1,
|
2021-07-23 19:47:59 +02:00
|
|
|
page_size: 1,
|
2021-07-23 08:47:01 +02:00
|
|
|
no_totals: true,
|
2021-03-25 21:26:33 +01:00
|
|
|
has_no_source: true,
|
|
|
|
claim_type: ['stream'],
|
2021-04-27 17:18:09 +02:00
|
|
|
order_by: CS.ORDER_BY_NEW_VALUE,
|
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) {
|
2021-07-20 03:45:51 +02:00
|
|
|
const claim = res.items[0];
|
2021-10-08 05:47:39 +02:00
|
|
|
// $FlowFixMe Too many Claim GenericClaim etc types.
|
2021-03-25 21:26:33 +01:00
|
|
|
setLivestreamClaim(claim);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(() => {});
|
|
|
|
}
|
2021-07-22 22:09:28 +02:00
|
|
|
}, [livestreamChannelId, isChannelEmpty]);
|
2021-03-10 19:34:21 +01:00
|
|
|
|
|
|
|
React.useEffect(() => {
|
2021-03-19 21:14:31 +01:00
|
|
|
function fetchIsStreaming() {
|
2021-07-27 22:35:22 +02:00
|
|
|
// $FlowFixMe livestream API can handle garbage
|
|
|
|
fetch(`${LIVESTREAM_LIVE_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;
|
2021-07-22 22:09:28 +02:00
|
|
|
// Only call livestream api if channel has livestream claims
|
|
|
|
if (livestreamChannelId && livestreamClaim) {
|
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-07-22 22:09:28 +02:00
|
|
|
// Prevent any more api calls on update
|
|
|
|
if (!livestreamChannelId || !livestreamClaim) {
|
|
|
|
if (interval) {
|
|
|
|
clearInterval(interval);
|
|
|
|
}
|
|
|
|
}
|
2021-03-19 21:14:31 +01:00
|
|
|
return () => {
|
|
|
|
if (interval) {
|
|
|
|
clearInterval(interval);
|
|
|
|
}
|
|
|
|
};
|
2021-07-22 22:09:28 +02:00
|
|
|
}, [livestreamChannelId, livestreamClaim]);
|
2021-03-10 19:34:21 +01:00
|
|
|
|
|
|
|
if (!livestreamClaim || !isLivestreaming) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-03-30 01:05:18 +02:00
|
|
|
// gonna pass the wrapper in so I don't have to rewrite the dmca/blocking logic in claimPreview.
|
|
|
|
const element = (props: { children: any }) => (
|
2021-04-23 21:59:48 +02:00
|
|
|
<Card
|
|
|
|
className="livestream__channel-link"
|
|
|
|
title={__('Live stream in progress')}
|
|
|
|
onClick={() => {
|
|
|
|
push(formatLbryUrlForWeb(livestreamClaim.canonical_url));
|
|
|
|
}}
|
|
|
|
>
|
2021-03-30 01:05:18 +02:00
|
|
|
{props.children}
|
|
|
|
</Card>
|
2021-03-10 19:34:21 +01:00
|
|
|
);
|
2021-03-30 01:05:18 +02:00
|
|
|
|
|
|
|
return <ClaimPreview uri={livestreamClaim.canonical_url} wrapperElement={element} type="inline" />;
|
2021-03-10 19:34:21 +01:00
|
|
|
}
|