"use client"; import { useCallback, useEffect, useRef, useState } from "react"; import { useRouter } from "next/navigation"; import Link from "next/link"; import { api, type Song } from "@/lib/api"; import { useAuth } from "@/lib/auth"; import { SongCard } from "@/components/SongCard"; export default function LibraryPage() { const { user, loading } = useAuth(); const router = useRouter(); const [songs, setSongs] = useState(null); const pollRef = useRef | null>(null); const load = useCallback(async () => { const { items } = await api.mySongs(); setSongs(items); }, []); useEffect(() => { if (!loading && !user) router.push("/login"); }, [loading, user, router]); useEffect(() => { if (!user) return; load(); }, [user, load]); useEffect(() => { if (!songs) return; const hasActive = songs.some((s) => s.status === "pending" || s.status === "processing"); if (hasActive && !pollRef.current) { pollRef.current = setInterval(load, 3000); } else if (!hasActive && pollRef.current) { clearInterval(pollRef.current); pollRef.current = null; } return () => { if (pollRef.current) { clearInterval(pollRef.current); pollRef.current = null; } }; }, [songs, load]); if (loading || !user) return null; return (

Your library

+ New song
{songs === null ? (

Loading…

) : songs.length === 0 ? (

Nothing yet.{" "} Generate your first song .

) : (
{songs.map((song) => ( setSongs((prev) => prev?.map((s) => (s.id === updated.id ? updated : s)) ?? null)} /> ))}
)}
); }