2021-04-28 08:48:42 +02:00
|
|
|
// @flow
|
|
|
|
import React from 'react';
|
|
|
|
import { BITWAVE_LIVE_API } from 'constants/livestream';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets latest livestream info list. Returns null (instead of a blank object)
|
|
|
|
* when there are no active livestreams.
|
|
|
|
*
|
2021-04-29 10:25:06 +02:00
|
|
|
* @param minViewers
|
2021-04-28 08:48:42 +02:00
|
|
|
* @param refreshMs
|
|
|
|
* @returns {{livestreamMap: null, loading: boolean}}
|
|
|
|
*/
|
2021-04-29 10:25:06 +02:00
|
|
|
export default function useGetLivestreams(minViewers: number = 0, refreshMs: number = 0) {
|
2021-04-28 08:48:42 +02:00
|
|
|
const [loading, setLoading] = React.useState(true);
|
|
|
|
const [livestreamMap, setLivestreamMap] = React.useState(null);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
function checkCurrentLivestreams() {
|
|
|
|
fetch(BITWAVE_LIVE_API)
|
|
|
|
.then((res) => res.json())
|
|
|
|
.then((res) => {
|
|
|
|
setLoading(false);
|
|
|
|
if (!res.data) {
|
|
|
|
setLivestreamMap(null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const livestreamMap = res.data.reduce((acc, curr) => {
|
2021-04-29 10:25:06 +02:00
|
|
|
if (curr.viewCount >= minViewers) {
|
|
|
|
acc[curr.claimId] = curr;
|
|
|
|
}
|
2021-04-28 08:48:42 +02:00
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
setLivestreamMap(livestreamMap);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
setLoading(false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
checkCurrentLivestreams();
|
|
|
|
|
|
|
|
if (refreshMs > 0) {
|
|
|
|
let fetchInterval = setInterval(checkCurrentLivestreams, refreshMs);
|
|
|
|
return () => {
|
|
|
|
if (fetchInterval) {
|
|
|
|
clearInterval(fetchInterval);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return { livestreamMap, loading };
|
|
|
|
}
|