'use client'; import { useEffect, useState } from 'react'; import { useParams } from 'next/navigation'; import Image from 'next/image'; import { Playlist, Song } from '@/lib/navidrome'; import { useNavidrome } from '@/app/components/NavidromeContext'; import { useAudioPlayer } from '@/app/components/AudioPlayerContext'; import { Play, Heart, Plus } from 'lucide-react'; import { Button } from '@/components/ui/button'; import Loading from "@/app/components/loading"; import { Separator } from '@/components/ui/separator'; export default function PlaylistPage() { const { id } = useParams(); const [playlist, setPlaylist] = useState(null); const [tracklist, setTracklist] = useState([]); const [loading, setLoading] = useState(true); const { getPlaylist } = useNavidrome(); const { playTrack, addToQueue } = useAudioPlayer(); useEffect(() => { const fetchPlaylist = async () => { setLoading(true); console.log(`Fetching playlist with id: ${id}`); try { const playlistData = await getPlaylist(id as string); setPlaylist(playlistData.playlist); setTracklist(playlistData.songs); console.log(`Playlist found: ${playlistData.playlist.name}`); } catch (error) { console.error('Failed to fetch playlist:', error); } setLoading(false); }; if (id) { fetchPlaylist(); } }, [id, getPlaylist]); const handlePlayClick = (song: Song) => { const track = { id: song.id, name: song.title, url: '', // Will be set by the context artist: song.artist, album: song.album, duration: song.duration, coverArt: song.coverArt, albumId: song.albumId, artistId: song.artistId }; playTrack(track); }; const handleAddToQueue = (song: Song) => { const track = { id: song.id, name: song.title, url: '', // Will be set by the context artist: song.artist, album: song.album, duration: song.duration, coverArt: song.coverArt, albumId: song.albumId, artistId: song.artistId }; addToQueue(track); }; const formatDuration = (seconds: number) => { const minutes = Math.floor(seconds / 60); const secs = Math.floor(seconds % 60).toString().padStart(2, '0'); return `${minutes}:${secs}`; }; if (loading) { return ; } if (!playlist) { return (

Playlist not found

The playlist you're looking for doesn't exist.

); } return (

{playlist.name}

{playlist.comment && (

{playlist.comment}

)}

{playlist.songCount} songs • {formatDuration(playlist.duration || 0)}

{playlist.public !== undefined && (

{playlist.public ? 'Public' : 'Private'} playlist

)}
{tracklist.length > 0 ? ( tracklist.map((song, index) => (
handlePlayClick(song)}>
{index + 1}

{song.title}

{song.artist}

{formatDuration(song.duration)}

)) ) : (

This playlist is empty

)}
); }