import React, { useState } from 'react' import { useBackgroundDownloadManager } from '../hooks/useBackgroundDownloadManager' import './BackgroundDownloadManager.css' interface BackgroundDownloadManagerProps { className?: string } const BackgroundDownloadManager: React.FC = ({ className = '' }) => { const { stats, settings, queue, isInitialized, updateSettings, isDownloading, canDownload, isOnWifiOnly, dailyLimitReached } = useBackgroundDownloadManager() const [showSettings, setShowSettings] = useState(false) const [showQueue, setShowQueue] = useState(false) if (!isInitialized) { return (
Initializing Download Manager...
) } const formatBytes = (bytes: number): string => { if (bytes === 0) return '0 Bytes' const k = 1024 const sizes = ['Bytes', 'KB', 'MB', 'GB'] const i = Math.floor(Math.log(bytes) / Math.log(k)) return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i] } const formatTime = (minutes: number): string => { if (minutes < 60) return `${Math.round(minutes)}m` const hours = Math.floor(minutes / 60) const mins = Math.round(minutes % 60) return `${hours}h ${mins}m` } const getStatusColor = (): string => { if (!canDownload) return 'error' if (isDownloading) return 'downloading' if (queue.length > 0) return 'queued' return 'idle' } const getStatusText = (): string => { if (!navigator.onLine) return 'Offline' if (dailyLimitReached) return 'Daily limit reached' if (isOnWifiOnly && !canDownload) return 'Waiting for WiFi' if (isDownloading) return 'Downloading...' if (queue.length > 0) return `${queue.length} queued` return 'Ready' } return (
{/* Status Header */}
{getStatusText()}
{stats.completedJobs}/{stats.totalJobs} songs {formatBytes(stats.totalDownloaded)} {isDownloading && stats.estimatedTimeRemaining > 0 && ( {formatTime(stats.estimatedTimeRemaining)} left )}
{/* Progress Bar */} {isDownloading && (
{formatBytes(stats.downloadSpeed)}/s {Math.round((stats.completedJobs / stats.totalJobs) * 100)}%
)} {/* Data Usage Indicator */}
Daily: {formatBytes(stats.dailyDataUsage)} Limit: {formatBytes(settings.dataUsageLimit * 1024 * 1024)}
{/* Action Buttons */}
{/* Queue Display */} {showQueue && (

Download Queue

{queue.length === 0 ? (

No downloads queued

) : (
{queue.slice(0, 10).map((job) => (
{job.mediaItem.Name} {job.mediaItem.Artists?.[0]}
{job.priority.level} {job.status} {job.status === 'downloading' && ( {Math.round(job.progress)}% )}
))} {queue.length > 10 && (
+{queue.length - 10} more items
)}
)}
)} {/* Settings Panel */} {showSettings && (

Download Settings

{settings.overnightCachingEnabled && (
)}
{settings.pauseOnLowBattery && (
)}
)} {/* Overnight Caching Indicator */} {settings.overnightCachingEnabled && (
🌙 Overnight caching: {settings.overnightStartTime} - {settings.overnightEndTime}
)}
) } export default BackgroundDownloadManager