"use client"; import { useState } from "react"; import { dbService } from "@/services/db"; import { useLibrary } from "@/context/LibraryContext"; import Link from "next/link"; import { Plus } from "lucide-react"; import CreatePlaylistModal from "@/components/CreatePlaylistModal"; export default function LibraryPage() { const { userPlaylists: playlists, libraryItems, refreshLibrary: refresh, activeFilter: activeTab, setActiveFilter: setActiveTab } = useLibrary(); const [isCreateModalOpen, setIsCreateModalOpen] = useState(false); const handleCreatePlaylist = async (name: string) => { await dbService.createPlaylist(name); refresh(); }; const showPlaylists = activeTab === 'all' || activeTab === 'playlists'; const showAlbums = activeTab === 'all' || activeTab === 'albums'; const showArtists = activeTab === 'all' || activeTab === 'artists'; // Filter items based on type const albums = libraryItems.filter(item => item.type === 'Album'); const artists = libraryItems.filter(item => item.type === 'Artist'); const browsePlaylists = libraryItems.filter(item => item.type === 'Playlist'); return (

Your Library

{/* Playlists & Liked Songs */} {showPlaylists && ( <>

Liked Songs

Auto-generated

{playlists.map((playlist) => (
{playlist.cover_url && !playlist.cover_url.includes("placehold") ? ( {playlist.title} ) : (
🎵
)}

{playlist.title}

Playlist • You

))} {browsePlaylists.map((playlist) => (
{playlist.cover_url && !playlist.cover_url.includes("placehold") ? ( {playlist.title} ) : (
🎵
)}

{playlist.title}

Playlist • Made for you

))} )} {/* Artists Content (Circular Images) */} {showArtists && artists.map((artist) => (
{artist.cover_url ? ( {artist.title} { e.currentTarget.onerror = null; // Prevent infinite loop e.currentTarget.style.display = 'none'; e.currentTarget.parentElement?.classList.add('bg-[#333]'); }} /> ) : null}
🎤

{artist.title}

Artist

))} {/* Albums Content */} {showAlbums && albums.map((album) => (
{album.cover_url ? ( {album.title} { e.currentTarget.onerror = null; // Prevent infinite loop e.currentTarget.style.display = 'none'; // Hide broken image e.currentTarget.parentElement?.classList.add('bg-[#333]'); // add background // Show fallback icon sibling if possible, distinct from React state }} /> ) : null} {/* Fallback overlay (shown if image missing or hidden via CSS logic would need state, but simpler: just render icon behind it or use state) */}
💿

{album.title}

Album • {album.creator || 'Spotify'}

))}
setIsCreateModalOpen(false)} onCreate={handleCreatePlaylist} />
); }