import { useState, useEffect } from 'react' import { Link } from 'react-router-dom' import { ListMusic, Plus, Loader2, Music, ChevronRight, Download, X } from 'lucide-react' import { getPlaylists, getSpotifyPlaylists, importSpotifyPlaylist, type PlaylistResponse, type SpotifyPlaylistItem } from '../lib/api' export default function Playlists() { const [playlists, setPlaylists] = useState([]) const [spotifyPlaylists, setSpotifyPlaylists] = useState([]) const [showImport, setShowImport] = useState(false) const [importing, setImporting] = useState(null) const [loadingSpotify, setLoadingSpotify] = useState(false) const [loading, setLoading] = useState(true) const [error, setError] = useState('') useEffect(() => { loadPlaylists() }, []) const loadPlaylists = async () => { try { const data = await getPlaylists() setPlaylists(data) } catch { setError('Failed to load playlists') } finally { setLoading(false) } } const openImportModal = async () => { setShowImport(true) setLoadingSpotify(true) try { const data = await getSpotifyPlaylists() setSpotifyPlaylists(data) } catch { setError('Failed to load Spotify playlists. Make sure your Spotify account is connected.') } finally { setLoadingSpotify(false) } } const handleImport = async (playlistId: string) => { setImporting(playlistId) try { const imported = await importSpotifyPlaylist(playlistId) setPlaylists((prev) => [...prev, imported]) setSpotifyPlaylists((prev) => prev.filter((p) => p.id !== playlistId)) } catch (err: any) { setError(err.response?.data?.detail || 'Failed to import playlist') } finally { setImporting(null) } } if (loading) { return (
) } return (

Playlists

Manage your imported playlists

{error && (
{error}
)} {/* Playlist Grid */} {playlists.length === 0 ? (

No playlists yet

Import your Spotify playlists to start getting personalized music recommendations

) : (
{playlists.map((playlist) => (
{playlist.image_url ? ( {playlist.name} ) : ( )}

{playlist.name}

{playlist.track_count} tracks

{playlist.source}
))}
)} {/* Import Modal */} {showImport && (

Import from Spotify

{loadingSpotify ? (
) : spotifyPlaylists.length === 0 ? (

No playlists found on Spotify

) : (
{spotifyPlaylists.map((sp) => (
{sp.image_url ? ( {sp.name} ) : ( )}

{sp.name}

{sp.track_count} tracks · {sp.owner}

))}
)}
)}
) }