import { useState, useEffect } from 'react' import { nostrAuthService } from '@/lib/nostrAuth' import { syncUserContentToCache, type SyncProgress } from '@/lib/userContentSync' import { getLastSyncDate, getCurrentTimestamp, calculateDaysBetween } from '@/lib/syncStorage' import { MIN_EVENT_DATE } from '@/lib/platformConfig' import { t } from '@/lib/i18n' export function SyncProgressBar(): React.ReactElement | null { const [syncProgress, setSyncProgress] = useState(null) const [isSyncing, setIsSyncing] = useState(false) const [lastSyncDate, setLastSyncDate] = useState(null) const [totalDays, setTotalDays] = useState(0) useEffect(() => { void loadSyncStatus() }, []) async function loadSyncStatus(): Promise { try { const state = nostrAuthService.getState() if (!state.connected || !state.pubkey) { return } const storedLastSyncDate = await getLastSyncDate() const currentTimestamp = getCurrentTimestamp() const days = calculateDaysBetween(storedLastSyncDate, currentTimestamp) setLastSyncDate(storedLastSyncDate) setTotalDays(days) // If everything is synced (no days to sync), don't show the progress bar if (days === 0 && storedLastSyncDate >= currentTimestamp) { setSyncProgress(null) } } catch (error) { console.error('Error loading sync status:', error) } } useEffect(() => { void loadSyncStatus() }, []) async function startSync(): Promise { try { const state = nostrAuthService.getState() if (!state.connected || !state.pubkey) { return } setIsSyncing(true) setSyncProgress({ currentStep: 0, totalSteps: 6, completed: false }) await syncUserContentToCache(state.pubkey, (progress) => { setSyncProgress(progress) if (progress.completed) { setIsSyncing(false) void loadSyncStatus() } }) } catch (error) { console.error('Error starting sync:', error) setIsSyncing(false) } } // Don't show if not connected or if everything is synced const state = nostrAuthService.getState() if (!state.connected || !state.pubkey) { return null } // If everything is synced, don't show the progress bar if (totalDays === 0 && lastSyncDate !== null && lastSyncDate >= getCurrentTimestamp()) { return null } // If sync is completed and no days to sync, don't show if (syncProgress?.completed && totalDays === 0) { return null } const progressPercentage = syncProgress && syncProgress.totalSteps > 0 ? Math.min(100, (syncProgress.currentStep / syncProgress.totalSteps) * 100) : 0 const formatDate = (timestamp: number): string => { const date = new Date(timestamp * 1000) const locale = typeof window !== 'undefined' ? navigator.language : 'fr-FR' return date.toLocaleDateString(locale, { day: '2-digit', month: '2-digit', year: 'numeric' }) } const getStartDate = (): number => { if (lastSyncDate !== null) { return lastSyncDate } return MIN_EVENT_DATE } const startDate = getStartDate() const endDate = getCurrentTimestamp() return (

{t('settings.sync.title')}

{!isSyncing && totalDays > 0 && ( )}
{totalDays > 0 && (

{t('settings.sync.daysRange', { startDate: formatDate(startDate), endDate: formatDate(endDate), days: totalDays, })}

)} {isSyncing && syncProgress && (
{t('settings.sync.progress', { current: syncProgress.currentStep, total: syncProgress.totalSteps, })} {Math.round(progressPercentage)}%
)} {!isSyncing && totalDays === 0 && lastSyncDate !== null && (

{t('settings.sync.completed')}

)}
) }