import { useEffect, useState } from "react"; import { Kitty } from "../Kitty"; import { SpotifyPlayer } from "./SpotifyPlayer"; import { useApp } from "~/hooks/useApp"; import { cn, hideIf } from "~/utils/react"; import { Cava } from "./Cava"; export type CurrentlyPlaying = { item: { album: { name: string; }; name: string; artists: { name: string }[]; duration_ms: number; }; progress_ms: number; }; export const Music = () => { const { screenWidth } = useApp(); const [playing, setPlaying] = useState(null); useEffect(() => { const fetchCurrentlyPlaying = () => fetch("https://api.pihkaal.me/currently-playing?format=json") .then((r) => r.json()) .then((data: CurrentlyPlaying) => { data.progress_ms = Math.max(0, data.progress_ms - 1500); setPlaying(data); }) .catch(() => setPlaying(null)); const interval = setInterval(() => { setPlaying((prev) => { if (prev === null) return null; if (prev.progress_ms >= prev.item.duration_ms) { void fetchCurrentlyPlaying(); return prev; } return { ...prev, progress_ms: Math.min(prev.item.duration_ms, prev.progress_ms + 1000), }; }); }, 1000); void fetchCurrentlyPlaying(); return () => { clearInterval(interval); }; }, []); return (
); };