'use client' import { useState, useEffect } from 'react' import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, } from '@/components/ui/dialog' import { ScrollArea } from '@/components/ui/scroll-area' import { Button } from '@/components/ui/button' import { ListPlus, Plus, Loader2, CheckCircle } from 'lucide-react' import { useMusicPlayer } from './music-provider' interface Playlist { id: string name: string songCount: number coverArt: string } export function PlaylistPicker({ open, onOpenChange, }: { open: boolean onOpenChange: (open: boolean) => void }) { const { state } = useMusicPlayer() const [playlists, setPlaylists] = useState([]) const [loading, setLoading] = useState(false) const [adding, setAdding] = useState(null) const [added, setAdded] = useState(null) const [creating, setCreating] = useState(false) const [newName, setNewName] = useState('') useEffect(() => { if (!open) return setLoading(true) setAdded(null) fetch('/api/music/playlists') .then((r) => r.json()) .then((d) => setPlaylists(d.playlists || [])) .catch(() => {}) .finally(() => setLoading(false)) }, [open]) const songId = state.currentTrack?.id if (!songId) return null const addToPlaylist = async (playlistId: string) => { setAdding(playlistId) try { await fetch(`/api/music/playlist/${playlistId}`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ songId }), }) setAdded(playlistId) } catch {} setAdding(null) } const createPlaylist = async () => { if (!newName.trim()) return setCreating(true) try { await fetch('/api/music/playlist/create', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: newName.trim(), songId }), }) setAdded('new') setNewName('') } catch {} setCreating(false) } return ( Add to Playlist Add “{state.currentTrack?.title}” to an existing playlist or create a new one. {loading ? (
) : ( <>
{playlists.map((p) => ( ))} {playlists.length === 0 && (

No playlists yet. Create one below.

)}
setNewName(e.target.value)} placeholder="New playlist name..." className="flex-1 px-3 py-2 text-sm rounded-md border border-border bg-background focus:outline-none focus:ring-2 focus:ring-ring" onKeyDown={(e) => e.key === 'Enter' && createPlaylist()} />
{added === 'new' && (

Playlist created with song added

)} )}
) }