Files
Vault/hooks/usePlaylist.ts
twotalesanimation 81ad7e4ea9 Initial commit
2026-06-11 10:46:09 +02:00

43 lines
993 B
TypeScript

'use client';
import { useEffect, useState } from 'react';
type Playlist = any;
export function usePlaylist(id?: string) {
const [playlist, setPlaylist] = useState<Playlist | null>(null);
const [isLoading, setIsLoading] = useState<boolean>(!!id);
const [error, setError] = useState<any>(null);
useEffect(() => {
if (!id) {
setPlaylist(null);
setIsLoading(false);
setError(null);
return;
}
let cancelled = false;
setIsLoading(true);
setError(null);
fetch(`/api/playlists/${id}`)
.then(async (res) => {
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const json = await res.json();
if (!cancelled) setPlaylist(json.playlist ?? null);
})
.catch((err) => {
if (!cancelled) setError(err);
})
.finally(() => {
if (!cancelled) setIsLoading(false);
});
return () => {
cancelled = true;
};
}, [id]);
return { playlist, isLoading, error };
}