import React, { useEffect, useState } from 'react'; import { SubsonicPlaylist, getPlaylists, getPlaylist, deletePlaylist } from '../api/subsonic'; import { usePlayerStore } from '../store/playerStore'; import { ListMusic, Play, Trash2 } from 'lucide-react'; import { useTranslation } from 'react-i18next'; export default function Playlists() { const { t } = useTranslation(); const [playlists, setPlaylists] = useState([]); const [loading, setLoading] = useState(true); const playTrack = usePlayerStore(s => s.playTrack); const clearQueue = usePlayerStore(s => s.clearQueue); const fetchPlaylists = () => { setLoading(true); getPlaylists() .then(data => { setPlaylists(data); setLoading(false); }) .catch(err => { console.error('Failed to load playlists', err); setLoading(false); }); }; useEffect(() => { fetchPlaylists(); }, []); const handlePlay = async (id: string) => { try { const data = await getPlaylist(id); const tracks = data.songs.map((s: any) => ({ id: s.id, title: s.title, artist: s.artist, album: s.album, albumId: s.albumId, artistId: s.artistId, duration: s.duration, coverArt: s.coverArt, track: s.track, year: s.year, bitRate: s.bitRate, suffix: s.suffix, userRating: s.userRating, })); if (tracks.length > 0) { clearQueue(); playTrack(tracks[0], tracks); } } catch (e) { console.error('Failed to play playlist', e); } }; const handleDelete = async (id: string, name: string) => { if (confirm(t('playlists.confirmDelete', { name }))) { try { await deletePlaylist(id); fetchPlaylists(); } catch (e) { console.error('Failed to delete playlist', e); } } }; return (

{t('playlists.title')}

{loading ? (
{t('playlists.loading')}
) : playlists.length === 0 ? (
{t('playlists.empty')}
) : (
{playlists.map(p => (

{p.name}

{t('playlists.track', { count: p.songCount })} • {Math.floor(p.duration / 60)} {t('playlists.minutes')}

))}
)}
); }