"use client"; import { useEffect, useState } from "react"; import { Plus, X } from "lucide-react"; interface AddToPlaylistModalProps { track: any; isOpen: boolean; onClose: () => void; } export default function AddToPlaylistModal({ track, isOpen, onClose }: AddToPlaylistModalProps) { const [playlists, setPlaylists] = useState([]); useEffect(() => { if (isOpen) { const apiUrl = process.env.NEXT_PUBLIC_API_URL || ''; fetch(`${apiUrl}/api/playlists`) .then(res => res.json()) .then(data => setPlaylists(data)) .catch(err => console.error(err)); } }, [isOpen]); const handleAddToPlaylist = async (playlistId: string) => { try { const apiUrl = process.env.NEXT_PUBLIC_API_URL || ''; await fetch(`${apiUrl}/api/playlists/${playlistId}/tracks`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(track) }); alert(`Added to playlist!`); onClose(); } catch (error) { console.error("Failed to add track:", error); } }; if (!isOpen) return null; return (

Add to Playlist

{playlists.length === 0 ? (
No playlists found. Create one first!
) : ( playlists.map((playlist) => (
handleAddToPlaylist(playlist.id)} className="flex items-center gap-3 p-3 hover:bg-[#3e3e3e] rounded-md cursor-pointer transition text-white" >
{playlist.cover_url && !playlist.cover_url.includes("placehold") ? ( ) : ( 🎵 )}
{playlist.title}
)) )}
); }