"use client" import { useEffect, useState } from "react" import { useParams, useRouter } from "next/navigation" import { Button } from "@/components/ui/button" import { Card, CardContent, CardHeader, CardTitle, CardDescription } from "@/components/ui/card" import { ArrowLeft, Trash2, Calendar, Music, User as UserIcon, PlayCircle, MoreHorizontal } from "lucide-react" import Link from "next/link" import { getApiUrl } from "@/lib/api-config" import { useToast } from "@/components/ui/use-toast" import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu" export default function PlaylistDetailPage() { const params = useParams() const router = useRouter() const { toast } = useToast() const [playlist, setPlaylist] = useState(null) const [loading, setLoading] = useState(true) const [currentUser, setCurrentUser] = useState(null) useEffect(() => { const token = localStorage.getItem("token") // Fetch current user if (token) { fetch(`${getApiUrl()}/auth/users/me`, { headers: { Authorization: `Bearer ${token}` } }) .then(res => res.ok ? res.json() : null) .then(data => setCurrentUser(data)) } // Fetch playlist fetch(`${getApiUrl()}/playlists/${params.id}`, { headers: token ? { Authorization: `Bearer ${token}` } : {} }) .then(res => { if (!res.ok) throw new Error("Failed to fetch playlist") return res.json() }) .then(data => setPlaylist(data)) .catch(err => { console.error(err) toast({ title: "Error", description: "Could not load playlist", variant: "destructive" }) }) .finally(() => setLoading(false)) }, [params.id]) const handleDeletePlaylist = async () => { if (!confirm("Are you sure you want to delete this playlist?")) return const token = localStorage.getItem("token") try { const res = await fetch(`${getApiUrl()}/playlists/${params.id}`, { method: "DELETE", headers: { Authorization: `Bearer ${token}` } }) if (res.ok) { toast({ title: "Playlist deleted" }) router.push("/profile") } else { throw new Error("Failed to delete") } } catch (error) { toast({ title: "Error", description: "Could not delete playlist", variant: "destructive" }) } } const handleRemoveTrack = async (performanceId: number) => { const token = localStorage.getItem("token") try { const res = await fetch(`${getApiUrl()}/playlists/${params.id}/performances/${performanceId}`, { method: "DELETE", headers: { Authorization: `Bearer ${token}` } }) if (res.ok) { // Optimistic update setPlaylist((prev: any) => ({ ...prev, performances: prev.performances.filter((p: any) => p.performance_id !== performanceId) })) toast({ title: "Track removed" }) } } catch (error) { toast({ title: "Error", description: "Could not remove track", variant: "destructive" }) } } if (loading) return
Loading playlist...
if (!playlist) return
Playlist not found
const isOwner = currentUser && currentUser.id === playlist.user_id return (
Back to Profile

{playlist.name}

{!playlist.is_public && ( Private )}

{playlist.description}

{playlist.username}
{new Date(playlist.created_at).toLocaleDateString()}
{playlist.performances.length} tracks
{isOwner && ( )}
Tracks {playlist.performances.length === 0 ? "No tracks added yet." : "Performances in this collection."} {playlist.performances.length > 0 && (
{playlist.performances.map((perf: any, index: number) => (
{index + 1}

{perf.song_title}

{new Date(perf.show_date).toLocaleDateString()} {perf.notes && ( - {perf.notes} )}
{perf.show_slug && ( )} {isOwner && ( handleRemoveTrack(perf.performance_id)} > Remove from Playlist )}
))}
)}
) }