import React, { useState } from 'react'; import { Song, Playlist } from '../types'; import { Heart, Plus, Music, Play, MoreHorizontal, Trash2 } from 'lucide-react'; import { useAuth } from '../context/AuthContext'; import { SongDropdownMenu } from './SongDropdownMenu'; import { ShareModal } from './ShareModal'; import { AlbumCover } from './AlbumCover'; import { useI18n } from '../context/I18nContext'; interface LibraryViewProps { allSongs: Song[]; likedSongs: Song[]; playlists: Playlist[]; referenceTracks: ReferenceTrack[]; onPlaySong: (song: Song, list?: Song[]) => void; onCreatePlaylist: () => void; onSelectPlaylist: (playlist: Playlist) => void; onAddToPlaylist: (song: Song) => void; onOpenVideo?: (song: Song) => void; onReusePrompt?: (song: Song) => void; onDeleteSong?: (song: Song) => void; onDeleteReferenceTrack?: (trackId: string) => void; } interface ReferenceTrack { id: string; filename: string; storage_key: string; duration: number | null; file_size_bytes: number | null; tags: string[] | null; created_at: string; audio_url: string; } export const LibraryView: React.FC = ({ allSongs, likedSongs, playlists, referenceTracks, onPlaySong, onCreatePlaylist, onSelectPlaylist, onAddToPlaylist, onOpenVideo, onReusePrompt, onDeleteSong, onDeleteReferenceTrack, }) => { const { t } = useI18n(); const { user } = useAuth(); const [activeTab, setActiveTab] = useState<'all' | 'playlists' | 'liked' | 'uploads'>('all'); const [shareModalOpen, setShareModalOpen] = useState(false); const [shareSong, setShareSong] = useState(null); const formatBytes = (bytes?: number | null) => { if (!bytes || bytes <= 0) return '0 B'; const units = ['B', 'KB', 'MB', 'GB']; let size = bytes; let unit = 0; while (size >= 1024 && unit < units.length - 1) { size /= 1024; unit += 1; } return `${size.toFixed(size >= 10 || unit === 0 ? 0 : 1)} ${units[unit]}`; }; return ( <>

{t('yourLibrary')}

{/* Tabs */}
{/* Content */} {activeTab === 'all' && (
{allSongs.length === 0 ? (
No songs yet.
) : ( allSongs.map((song, idx) => (
onPlaySong(song, allSongs)}> {idx + 1} {song.coverUrl ? ( { e.currentTarget.style.display = 'none'; }} /> ) : ( )}
{song.title}
{song.style}
{song.duration}
setShareSong(null)} isOwner={user ? song.userId === user.id : false} onCreateVideo={() => onOpenVideo?.(song)} onReusePrompt={() => onReusePrompt?.(song)} onAddToPlaylist={() => onAddToPlaylist(song)} onDelete={() => onDeleteSong?.(song)} onShare={() => { setShareModalOpen(true); }} />
)) )}
)} {activeTab === 'liked' && (
likedSongs.length > 0 && onPlaySong(likedSongs[0], likedSongs)}>

{t('playlist')}

{t('likedSongs')}

{likedSongs.length} {t('songs')}
{likedSongs.map((song, idx) => (
onPlaySong(song, likedSongs)}> {idx + 1} {song.coverUrl ? ( { e.currentTarget.style.display = 'none'; }} /> ) : ( )}
{song.title}
{song.style}
{song.duration}
setShareSong(null)} isOwner={user ? song.userId === user.id : false} onCreateVideo={() => onOpenVideo?.(song)} onReusePrompt={() => onReusePrompt?.(song)} onAddToPlaylist={() => onAddToPlaylist(song)} onDelete={() => onDeleteSong?.(song)} onShare={() => { setShareModalOpen(true); }} />
))}
)} {activeTab === 'playlists' && (
{playlists.map((playlist) => (
onSelectPlaylist(playlist)}>
{playlist.coverUrl ? ( {playlist.name} { e.currentTarget.style.display = 'none'; }} /> ) : ( )}

{playlist.name}

{playlist.description || t('byYou')}

))}
)} {activeTab === 'uploads' && (
{referenceTracks.length === 0 ? (
No uploads yet.
) : ( referenceTracks.map((track) => (
{track.filename}
{formatBytes(track.file_size_bytes)} • {new Date(track.created_at).toLocaleDateString()}
)) )}
)}
{shareSong && ( { setShareModalOpen(false); setShareSong(null); }} song={shareSong} /> )} ); };