'use client'; import React, { useState, useEffect } from 'react'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { Progress } from '@/components/ui/progress'; import { Badge } from '@/components/ui/badge'; import { Separator } from '@/components/ui/separator'; import { useToast } from '@/hooks/use-toast'; import { useOfflineLibrary } from '@/hooks/use-offline-library'; import { Download, Trash2, RefreshCw, Wifi, WifiOff, Database, Clock, AlertCircle, CheckCircle, Music, User, List, HardDrive } from 'lucide-react'; function 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]; } function formatDate(date: Date | null): string { if (!date) return 'Never'; return date.toLocaleDateString() + ' at ' + date.toLocaleTimeString(); } export function OfflineManagement() { const { toast } = useToast(); const [isClearing, setIsClearing] = useState(false); const { isInitialized, isOnline, isSyncing, lastSync, stats, syncProgress, syncLibraryFromServer, syncPendingOperations, clearOfflineData, refreshStats } = useOfflineLibrary(); // Refresh stats periodically useEffect(() => { const interval = setInterval(() => { if (isInitialized && !isSyncing) { refreshStats(); } }, 10000); // Every 10 seconds return () => clearInterval(interval); }, [isInitialized, isSyncing, refreshStats]); const handleFullSync = async () => { try { await syncLibraryFromServer(); toast({ title: "Sync Complete", description: "Your music library has been synced for offline use.", }); } catch (error) { console.error('Full sync failed:', error); toast({ title: "Sync Failed", description: "Failed to sync library. Check your connection and try again.", variant: "destructive" }); } }; const handlePendingSync = async () => { try { await syncPendingOperations(); toast({ title: "Pending Operations Synced", description: "All pending changes have been synced to the server.", }); } catch (error) { console.error('Pending sync failed:', error); toast({ title: "Sync Failed", description: "Failed to sync pending operations. Will retry automatically when online.", variant: "destructive" }); } }; const handleClearData = async () => { if (!confirm('Are you sure you want to clear all offline data? This cannot be undone.')) { return; } setIsClearing(true); try { await clearOfflineData(); toast({ title: "Offline Data Cleared", description: "All offline music data has been removed.", }); } catch (error) { console.error('Clear data failed:', error); toast({ title: "Clear Failed", description: "Failed to clear offline data. Please try again.", variant: "destructive" }); } finally { setIsClearing(false); } }; if (!isInitialized) { return ( Offline Library Setting up offline library...

Initializing offline storage...

); } return (
{/* Connection Status */} {isOnline ? ( ) : ( )} Connection Status
{isOnline ? "Online" : "Offline"} {isOnline ? "Connected to Navidrome server" : "Working offline"}
{stats.pendingOperations > 0 && (
{stats.pendingOperations} pending operation{stats.pendingOperations !== 1 ? 's' : ''}
)}
{/* Sync Status */} Library Sync Keep your offline library up to date {isSyncing && syncProgress && (
{syncProgress.stage} {syncProgress.current}%
)}

Last Sync

{formatDate(lastSync)}

{stats.pendingOperations > 0 && isOnline && ( )}
{/* Library Statistics */} Offline Library Stats Your offline music collection

{stats.albums.toLocaleString()}

Albums

{stats.artists.toLocaleString()}

Artists

{stats.songs.toLocaleString()}

Songs

{stats.playlists.toLocaleString()}

Playlists

Storage Used
{formatBytes(stats.storageSize)}
{/* Offline Features */} Offline Features What works when you're offline

Browse & Search

Browse your synced albums, artists, and search offline

Favorites & Playlists

Star songs/albums and create playlists (syncs when online)

Play Downloaded Music

Play songs you've downloaded for offline listening

Auto-Sync

Changes sync automatically when you reconnect

{/* Danger Zone */} Danger Zone Permanently delete all offline data

Clear All Offline Data

This will remove all synced library data and downloaded audio

); }