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

78 lines
2 KiB
React
Raw Normal View History

2021-04-23 21:59:48 +02:00
// @flow
import * as ICONS from 'constants/icons';
2022-04-22 18:48:35 +02:00
import { NEW_LIVESTREAM_LIVE_API } from 'constants/livestream';
2021-04-23 21:59:48 +02:00
import React from 'react';
import Icon from 'component/common/icon';
import Spinner from 'component/spinner';
import ClaimTilesDiscover from 'component/claimTilesDiscover';
const LIVESTREAM_POLL_IN_MS = 10 * 1000;
export default function LivestreamList() {
const [loading, setLoading] = React.useState(true);
const [livestreamMap, setLivestreamMap] = React.useState();
React.useEffect(() => {
function checkCurrentLivestreams() {
2022-04-22 18:48:35 +02:00
fetch(`${NEW_LIVESTREAM_LIVE_API}/all`)
2021-04-23 21:59:48 +02:00
.then((res) => res.json())
.then((res) => {
setLoading(false);
if (!res.data) {
setLivestreamMap({});
return;
}
const livestreamMap = res.data.reduce((acc, curr) => {
return {
...acc,
2022-04-22 18:48:35 +02:00
[curr.ChannelClaimID]: curr,
2021-04-23 21:59:48 +02:00
};
}, {});
setLivestreamMap(livestreamMap);
})
.catch((err) => {
setLoading(false);
});
}
checkCurrentLivestreams();
let fetchInterval = setInterval(checkCurrentLivestreams, LIVESTREAM_POLL_IN_MS);
return () => {
if (fetchInterval) {
clearInterval(fetchInterval);
}
};
}, []);
return (
<>
{loading && (
<div className="main--empty">
<Spinner delayed />
</div>
)}
{livestreamMap && Object.keys(livestreamMap).length > 0 && (
<ClaimTilesDiscover
2021-07-08 18:21:42 +02:00
showNoSourceClaims
2021-04-23 21:59:48 +02:00
hasNoSource
streamTypes={null}
2021-04-23 21:59:48 +02:00
channelIds={Object.keys(livestreamMap)}
limitClaimsPerChannel={1}
2021-05-04 20:12:51 +02:00
pageSize={50}
2021-04-23 21:59:48 +02:00
renderProperties={(claim) => {
const livestream = livestreamMap[claim.signing_channel.claim_id];
return (
<span className="livestream__viewer-count">
2022-04-22 18:48:35 +02:00
{livestream.ViewerCount} <Icon icon={ICONS.EYE} />
2021-04-23 21:59:48 +02:00
</span>
);
}}
/>
)}
</>
);
}