'use client'; import { useState, useEffect, useContext } from 'react'; import { SessionContext } from '@/context/SessionContext'; import { OfflineContext } from '@/context/OfflineContext'; import { useTranslations } from 'next-intl'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faCloudDownloadAlt, faWifi, faCheckCircle, faSpinner, faBook } from '@fortawesome/free-solid-svg-icons'; interface SyncOption { id: 'current' | 'recent' | 'all' | 'skip'; label: string; description: string; estimatedSize?: string; bookCount?: number; } export default function OfflineSyncManager() { const t = useTranslations(); const { session } = useContext(SessionContext); const { syncBooks, isOnline } = useContext(OfflineContext); const [showDialog, setShowDialog] = useState(false); const [isFirstSync, setIsFirstSync] = useState(false); const [syncProgress, setSyncProgress] = useState(0); const [selectedOption, setSelectedOption] = useState(''); useEffect(() => { // Check if this is first time user sees sync options const hasSeenSync = localStorage.getItem('hasSeenSyncDialog'); if (!hasSeenSync && isOnline && session?.user) { setIsFirstSync(true); setShowDialog(true); } }, [session, isOnline]); const syncOptions: SyncOption[] = [ { id: 'current', label: t('sync.currentBook'), description: t('sync.currentBookDesc'), estimatedSize: '~5 MB', bookCount: 1 }, { id: 'recent', label: t('sync.recentBooks'), description: t('sync.recentBooksDesc'), estimatedSize: '~25 MB', bookCount: 3 }, { id: 'all', label: t('sync.allBooks'), description: t('sync.allBooksDesc'), estimatedSize: '~120 MB', bookCount: 12 }, { id: 'skip', label: t('sync.skipForNow'), description: t('sync.skipDesc'), } ]; const handleSync = async (option: string) => { if (option === 'skip') { localStorage.setItem('hasSeenSyncDialog', 'true'); setShowDialog(false); return; } setSyncProgress(0); try { // Simulate progressive download const interval = setInterval(() => { setSyncProgress(prev => { if (prev >= 100) { clearInterval(interval); return 100; } return prev + 10; }); }, 200); // Actual sync logic here // await syncBooks(option); localStorage.setItem('hasSeenSyncDialog', 'true'); localStorage.setItem('syncPreference', option); setTimeout(() => { setShowDialog(false); }, 2000); } catch (error) { console.error('Sync failed:', error); } }; if (!showDialog) return null; return (
{/* Header */}
{isOnline && ( )}

{isFirstSync ? t('sync.welcomeOffline') : t('sync.syncOptions')}

{t('sync.workAnywhere')}

{/* Sync Options */} {syncProgress === 0 ? (
{syncOptions.map((option) => ( ))}
) : ( /* Progress Bar */
{t('sync.downloading')}... {syncProgress}%
{syncProgress === 100 && (

{t('sync.complete')}!

)}
)} {/* Actions */} {syncProgress === 0 && (
)}
); }