'use client'; import { useEffect, useState } from 'react'; import { useParams } from 'next/navigation'; import { Album, Artist, Song } from '@/lib/navidrome'; import { useNavidrome } from '@/app/components/NavidromeContext'; import { useAudioPlayer } from '@/app/components/AudioPlayerContext'; import { AlbumArtwork } from '@/app/components/album-artwork'; import { PopularSongs } from '@/app/components/PopularSongs'; import { SimilarArtists } from '@/app/components/SimilarArtists'; import { ArtistBio } from '@/app/components/ArtistBio'; import Image from 'next/image'; import { Button } from '@/components/ui/button'; import { Heart, Play } from 'lucide-react'; import { ScrollArea, ScrollBar } from '@/components/ui/scroll-area'; import Loading from '@/app/components/loading'; import { getNavidromeAPI } from '@/lib/navidrome'; import { useToast } from '@/hooks/use-toast'; import { useIsMobile } from '@/hooks/use-mobile'; export default function ArtistPage() { const { artist: artistId } = useParams(); const [isStarred, setIsStarred] = useState(false); const [artistAlbums, setArtistAlbums] = useState([]); const [popularSongs, setPopularSongs] = useState([]); const [loading, setLoading] = useState(true); const [artist, setArtist] = useState(null); const [isPlayingArtist, setIsPlayingArtist] = useState(false); const { getArtist, starItem, unstarItem } = useNavidrome(); const { playArtist } = useAudioPlayer(); const { toast } = useToast(); const isMobile = useIsMobile(); const api = getNavidromeAPI(); useEffect(() => { const fetchArtistData = async () => { setLoading(true); try { if (artistId && api) { const artistData = await getArtist(artistId as string); setArtist(artistData.artist); setArtistAlbums(artistData.albums); setIsStarred(!!artistData.artist.starred); // Fetch popular songs for the artist try { const songs = await api.getArtistTopSongs(artistData.artist.name, 10); setPopularSongs(songs); } catch (error) { console.error('Failed to fetch popular songs:', error); } } } catch (error) { console.error('Failed to fetch artist data:', error); } setLoading(false); }; fetchArtistData(); }, [artistId, getArtist, api]); const handleStar = async () => { if (!artist) return; try { if (isStarred) { await unstarItem(artist.id, 'artist'); setIsStarred(false); } else { await starItem(artist.id, 'artist'); setIsStarred(true); } } catch (error) { console.error('Failed to star/unstar artist:', error); } }; const handlePlayArtist = async () => { if (!artist) return; setIsPlayingArtist(true); try { await playArtist(artist.id); } catch (error) { console.error('Failed to play artist:', error); toast({ title: "Error", description: "Failed to play artist albums.", variant: "destructive" }); } finally { setIsPlayingArtist(false); } }; if (loading) { return ; } if (!artist || artistAlbums.length === 0) { return (

No albums found for this artist

); } // Get artist image URL with proper fallback const artistImageUrl = artist.coverArt && api ? api.getCoverArtUrl(artist.coverArt, 1200) : '/default-user.jpg'; return (
{/* Artist Header */}
{artist.name}

{artist.name}

{artist.albumCount} albums

{/* About Section */} {/* Popular Songs Section */} {!isMobile && popularSongs.length > 0 && ( )} {/* Albums Section */}

Discography

{artistAlbums.map((album) => ( ))}
{/* Similar Artists Section */}
); }