feat(music): better sychronization with spotify

This commit is contained in:
Pihkaal
2024-09-12 15:54:43 +02:00
parent 5e36ec379c
commit ee4fd63e17

View File

@@ -6,6 +6,7 @@ import { cn, hideIf } from "~/utils/react";
import { Cava } from "./Cava"; import { Cava } from "./Cava";
export type CurrentlyPlaying = { export type CurrentlyPlaying = {
is_playing: boolean;
item: { item: {
album: { album: {
name: string; name: string;
@@ -26,12 +27,19 @@ export const Music = () => {
fetch("https://api.pihkaal.me/currently-playing?format=json") fetch("https://api.pihkaal.me/currently-playing?format=json")
.then((r) => r.json()) .then((r) => r.json())
.then((data: CurrentlyPlaying) => { .then((data: CurrentlyPlaying) => {
data.progress_ms = Math.max(0, data.progress_ms - 1500); if (data.is_playing) {
setPlaying(data); data.progress_ms = Math.max(0, data.progress_ms - 1500);
}) setPlaying(data);
.catch(() => setPlaying(null)); } else {
setPlaying(null);
}
});
const interval = setInterval(() => { const updatePlayingInterval = setInterval(() => {
void fetchCurrentlyPlaying();
}, 1000 * 10);
const updateTimeInterval = setInterval(() => {
setPlaying((prev) => { setPlaying((prev) => {
if (prev === null) return null; if (prev === null) return null;
@@ -50,7 +58,8 @@ export const Music = () => {
void fetchCurrentlyPlaying(); void fetchCurrentlyPlaying();
return () => { return () => {
clearInterval(interval); clearInterval(updateTimeInterval);
clearInterval(updatePlayingInterval);
}; };
}, []); }, []);