"use client"; import { useEffect, useState, Suspense } from "react"; import { useSearchParams } from "next/navigation"; import { Play, Pause, Clock, User, Music, Plus, ChevronDown, ChevronUp } from "lucide-react"; import { usePlayer } from "@/context/PlayerContext"; import CoverImage from "@/components/CoverImage"; import AddToPlaylistModal from "@/components/AddToPlaylistModal"; import Link from "next/link"; import { Track } from "@/types"; function ArtistPageContent() { const searchParams = useSearchParams(); const artistName = searchParams.get("name") || ""; const [allSongs, setAllSongs] = useState([]); const [artistPhoto, setArtistPhoto] = useState(null); const [isLoading, setIsLoading] = useState(true); const [showAllSongs, setShowAllSongs] = useState(false); const { playTrack, currentTrack, isPlaying } = usePlayer(); // Modal State const [isAddToPlaylistOpen, setIsAddToPlaylistOpen] = useState(false); const [trackToAdd, setTrackToAdd] = useState(null); useEffect(() => { if (!artistName) return; setIsLoading(true); const apiUrl = process.env.NEXT_PUBLIC_API_URL || ''; // Fetch artist info (photo) and songs in parallel Promise.all([ fetch(`${apiUrl}/api/artist/info?name=${encodeURIComponent(artistName)}`).then(r => r.json()), fetch(`${apiUrl}/api/search?query=${encodeURIComponent(artistName)}`).then(r => r.json()) ]) .then(([artistInfo, searchData]) => { if (artistInfo.photo) { setArtistPhoto(artistInfo.photo); } setAllSongs(searchData.tracks || []); }) .catch(err => console.error("Failed to load artist:", err)) .finally(() => setIsLoading(false)); }, [artistName]); // Categorize songs const topSongs = allSongs.slice(0, 5); // Most popular const hitSongs = allSongs.slice(5, 10); // More hits const recentSongs = allSongs.slice(10, 15); // Recent-ish const otherSongs = allSongs.slice(15); // Rest const handlePlay = (track: Track, queue: Track[]) => { playTrack(track, queue); }; const openAddToPlaylist = (e: React.MouseEvent, track: Track) => { e.stopPropagation(); setTrackToAdd(track); setIsAddToPlaylistOpen(true); }; // Get first letter for avatar fallback const artistInitials = artistName.substring(0, 2).toUpperCase(); if (!artistName) { return (

No artist specified

); } return (
{/* Artist Header */}
{/* Artist Avatar - Use real photo or fallback */} {artistPhoto ? ( {artistName} ) : (
{artistInitials}
)}

Artist

{artistName}

{allSongs.length} songs found

{/* Play Button */}
{isLoading ? (
Loading songs...
) : allSongs.length === 0 ? (

No songs found for this artist

) : (
{/* Popular / Top Songs */} {topSongs.length > 0 && (

Popular

)} {/* Hit Songs */} {hitSongs.length > 0 && (
🔥

Hits

)} {/* Recent Releases */} {recentSongs.length > 0 && (

More Songs

)} {/* All Other Songs */} {otherSongs.length > 0 && (
{showAllSongs && ( )}
)}
)} setIsAddToPlaylistOpen(false)} />
); } // Song List Component interface SongListProps { songs: Track[]; allSongs: Track[]; onPlay: (track: Track, queue: Track[]) => void; onAddToPlaylist: (e: React.MouseEvent, track: Track) => void; currentTrack: Track | null; isPlaying: boolean; } function SongList({ songs, allSongs, onPlay, onAddToPlaylist, currentTrack, isPlaying }: SongListProps) { return (
{songs.map((track, index) => { const isCurrent = currentTrack?.id === track.id; return (
onPlay(track, allSongs)} className={`flex items-center gap-4 p-3 rounded-md hover:bg-[#282828] cursor-pointer group transition ${isCurrent ? 'bg-[#282828]' : ''}`} > {/* Track Number / Play Icon */}
{isCurrent ? ( {isPlaying ? '▶' : '❚❚'} ) : ( {index + 1} )}
{isCurrent && isPlaying ? ( ) : ( )}
{/* Cover */}
{/* Track Info */}

{track.title}

{track.artist}

{/* Album */}

{track.album}

{/* Duration */}
{track.duration ? formatDuration(track.duration) : '--:--'}
{/* Add to Playlist */}
); })}
); } function formatDuration(seconds: number): string { const mins = Math.floor(seconds / 60); const secs = Math.floor(seconds % 60); return `${mins}:${secs.toString().padStart(2, '0')}`; } export default function ArtistPage() { return ( Loading...}> ); }