Last active
June 27, 2023 19:19
-
-
Save jshmllr/91333c86d15152d6bec7cb201166548f to your computer and use it in GitHub Desktop.
Framer YouTube Live Badge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React, { useState, useEffect } from "react"; | |
| import { motion } from "framer-motion"; | |
| const getLiveStatus = async (channelId) => { | |
| const response = await fetch( | |
| `https://www.googleapis.com/youtube/v3/search?part=snippet&eventType=live&type=video&channelId=${"channelId"}&key=YOUR_API_KEY` | |
| ); | |
| const data = await response.json(); | |
| // Check if any items were returned | |
| if (data.items && data.items.length > 0) { | |
| return data.items[0].snippet.liveBroadcastDetails.isLive; | |
| } else { | |
| // If no items were returned, the channel is not live | |
| return false; | |
| } | |
| }; | |
| const Badge = () => { | |
| const [isLive, setIsLive] = useState(false); | |
| useEffect(() => { | |
| const checkLiveStatus = async () => { | |
| const liveStatus = await getLiveStatus("channelId"); | |
| setIsLive(liveStatus); | |
| }; | |
| const interval = setInterval(() => { | |
| checkLiveStatus(); | |
| }, 10000); // Check if the user is live every 10 seconds | |
| return () => clearInterval(interval); // Clean up on unmount | |
| }, []); | |
| return ( | |
| <div style={{ display: "flex", alignItems: "center" }}> | |
| <motion.div | |
| style={{ | |
| width: "10px", | |
| height: "10px", | |
| borderRadius: "5px", | |
| backgroundColor: isLive ? "red" : "grey", | |
| marginRight: "6px" | |
| }} | |
| animate={{ scale: isLive ? 1.5 : 1 }} | |
| transition={{ duration: 0.5 }} | |
| /> | |
| <p>{isLive ? "Currently Live on YouTube" : "Not on YouTube"}</p> | |
| </div> | |
| ); | |
| }; | |
| export default Badge; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a React component named
Badgewhich uses the YouTube API to check if a specified YouTube channel is currently live or not. It displays a small badge that changes color and size based on the live status of the channel.Here's a breakdown of the component:
getLiveStatusis an asynchronous function that takes a YouTube channel ID as a parameter, sends a request to the YouTube API to check if there are any live videos from this channel, and returnstrueif the channel is live,falseotherwise.useStateis a React hook that creates a state variableisLiveand a functionsetIsLiveto update it. The initial value ofisLiveisfalse.useEffectis a React hook that runs the code in its body after every render. In this case, it checks the live status of the channel every 10 seconds and updatesisLiveaccordingly. The interval is cleared when the component is unmounted.Inside the return statement, a
divis returned that contains amotion.div(from the Framer Motion library) and apelement. Themotion.divis a small dot that changes color and size based on the live status of the channel. Thepelement displays a message that changes based on the live status of the channel.To use this component, you need to:
"channelId"with your actual YouTube channel ID in thegetLiveStatusfunction and in theuseEffecthook.'YOUR_API_KEY'with your actual YouTube API key in thegetLiveStatusfunction.In Framer here's an example on how to use it in a parent component:
This will render the Badge component inside the App component.
Contact: @_jshmllr