import React, { useState, useEffect } from "react"; import PropTypes from "prop-types"; import { Link } from "react-router-dom"; import * as config from "../../config"; import * as util from "../util"; // Components import Streams from "./Streams"; // Mock data import { mockStreams } from "../../__test__/mocks/streams-mocks"; const Home = (props) => { const [hasFetchedStreams, setHasFetchedStreams] = useState(false); const [streams, setStreams] = useState([]); const { signedIn, showSignIn, username, currentPath } = props; useEffect(() => { const getLiveStreams = async () => { try { const baseUrl = util.getApiUrlBase(); const auth = util.getWithExpiry("ugc"); const url = `${baseUrl}${ config.SHOW_OFFLINE_STREAMS ? "channels" : "live-channels" }`; const response = await fetch(url); if (response.status === 200) { const streams = await response.json(); setStreams(streams); setHasFetchedStreams(true); } else { throw new Error("Unable to get live streams."); } } catch (error) { console.log(error.message); } }; // Get live streams if (config.USE_MOCK_DATA) { const { streams } = mockStreams; setStreams(streams); setHasFetchedStreams(true); } else { getLiveStreams(); } }, [signedIn]); // Check if any streams are live const streamsExist = streams.length ? true : false; let componentToRender =
; if (hasFetchedStreams) { if (streamsExist) { componentToRender =