import { useEffect, useState } from 'react'; import { RefreshCw, Trash2, Download, ChevronRight, Music, Disc, FolderOpen, Loader2, } from 'lucide-react'; import { getLibrary, deleteLibraryItem, getDownloadUrl } from '../api/client'; import type { Artist, Album, Track } from '../api/types'; function formatSize(bytes: number): string { if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(0)} KB`; return `${(bytes / (1024 * 1024)).toFixed(1)} MB`; } export default function Library() { const [artists, setArtists] = useState([]); const [loading, setLoading] = useState(true); const [selectedArtist, setSelectedArtist] = useState(null); const [selectedAlbum, setSelectedAlbum] = useState(null); const [deleting, setDeleting] = useState(null); async function load() { setLoading(true); try { const data = await getLibrary(); setArtists(data); // Re-sync selections in case they were deleted setSelectedArtist(a => data.find(x => x.artist === a?.artist) ?? null); setSelectedAlbum(al => { const parent = data.find(x => x.artist === selectedArtist?.artist); return parent?.albums.find(x => x.album === al?.album) ?? null; }); } finally { setLoading(false); } } useEffect(() => { load(); }, []); async function handleDelete(path: string, label: string) { if (!confirm(`Delete "${label}" from your library?`)) return; setDeleting(path); try { await deleteLibraryItem(path); await load(); } catch { alert('Delete failed.'); } finally { setDeleting(null); } } if (loading) { return (
Loading library…
); } if (artists.length === 0) { return (

Your library is empty.

Search for songs and download them — they'll appear here.

); } return (
{/* Artist column */}
Artists
    {artists.map(artist => (
  • { setSelectedArtist(artist); setSelectedAlbum(null); }} > {artist.artist}
    e.stopPropagation()}>
  • ))}
{/* Album column */}
Albums
{selectedArtist ? (
    {selectedArtist.albums.map(album => (
  • setSelectedAlbum(album)} > {album.album}
    e.stopPropagation()}>
  • ))}
) : (
Select an artist
)}
{/* Tracks column */}
Tracks
{selectedAlbum ? (
    {selectedAlbum.tracks.map((track: Track) => (
  • {track.filename} {formatSize(track.size)}
    e.stopPropagation()}>
  • ))}
) : (
Select an album
)}
); }