diff --git a/src/components/blocks/NowPlaying.astro b/src/components/blocks/NowPlaying.astro deleted file mode 100644 index d41968b..0000000 --- a/src/components/blocks/NowPlaying.astro +++ /dev/null @@ -1,24 +0,0 @@ ---- -import { fetchNowPlaying } from "@utils/data/nowPlaying.js"; - -const isProduction = import.meta.env.MODE === "production"; -const nowPlayingData = await fetchNowPlaying(); ---- - - - - -{isProduction && ()} \ No newline at end of file diff --git a/src/components/blocks/NowPlaying.jsx b/src/components/blocks/NowPlaying.jsx new file mode 100644 index 0000000..8a1b2dd --- /dev/null +++ b/src/components/blocks/NowPlaying.jsx @@ -0,0 +1,35 @@ +import { useEffect, useState } from "react"; + +export default function NowPlaying({ staticContent }) { + const [nowPlaying, setNowPlaying] = useState(staticContent); + + useEffect(() => { + if (import.meta.env.MODE !== "production") return; + + async function fetchNowPlaying() { + try { + const response = await fetch("/api/now-playing"); + if (response.ok) { + const data = await response.json(); + setNowPlaying(data.content); + } else { + setNowPlaying("Nothing playing."); + } + } catch (error) { + console.error("Error fetching now playing:", error); + setNowPlaying("Unable to fetch data."); + } + } + + fetchNowPlaying(); + const interval = setInterval(fetchNowPlaying, 180000); + + return () => clearInterval(interval); + }, []); + + return ( + + + + ); +} diff --git a/src/components/home/Intro.astro b/src/components/home/Intro.astro index e3331df..a0d39a7 100644 --- a/src/components/home/Intro.astro +++ b/src/components/home/Intro.astro @@ -1,10 +1,20 @@ --- -import NowPlaying from "@components/blocks/NowPlaying.astro"; +import NowPlaying from "@components/blocks/NowPlaying.jsx"; +import { fetchNowPlaying } from "@utils/data/nowPlaying.js"; const { intro } = Astro.props; + +let nowPlayingStaticContent = "Nothing playing."; + +if (import.meta.env.MODE === "development") { + const nowPlayingData = await fetchNowPlaying(); + nowPlayingStaticContent = nowPlayingData.content; +} ---
-

+

+ +