diff --git a/src/hooks/useOfflineSync.ts b/src/hooks/useOfflineSync.ts index c1be13eb..03dcafa6 100644 --- a/src/hooks/useOfflineSync.ts +++ b/src/hooks/useOfflineSync.ts @@ -48,7 +48,7 @@ export function useOfflineSync({ if (wasLocalOnly && isNowOnline) { console.log('✈️ Detected transition: Flight Mode → Online') - syncLocalEventsToRemote(relayPool, account, true) + syncLocalEventsToRemote(relayPool, account) } previousStateRef.current.hasRemoteRelays = hasRemoteRelays diff --git a/src/services/offlineSyncService.ts b/src/services/offlineSyncService.ts index 5c1a71bb..7b282efb 100644 --- a/src/services/offlineSyncService.ts +++ b/src/services/offlineSyncService.ts @@ -4,36 +4,22 @@ import { IAccount } from 'applesauce-core/helpers' import { RELAYS } from '../config/relays' import { isLocalRelay } from '../utils/helpers' -interface SyncState { - lastRemoteConnectionTime: number - wasLocalOnly: boolean - isSyncing: boolean -} - -const syncState: SyncState = { - lastRemoteConnectionTime: 0, - wasLocalOnly: false, - isSyncing: false -} +let isSyncing = false /** * Syncs local-only events to remote relays when coming back online */ export async function syncLocalEventsToRemote( relayPool: RelayPool, - account: IAccount | null, - hasRemoteRelays: boolean + account: IAccount ): Promise { - if (!account || syncState.isSyncing) return - - // Detect transition from local-only to having remote relays - const justCameOnline = !syncState.wasLocalOnly && hasRemoteRelays - syncState.wasLocalOnly = !hasRemoteRelays - - if (!justCameOnline) return + if (isSyncing) { + console.log('⏳ Sync already in progress, skipping...') + return + } console.log('🔄 Coming back online - syncing local events to remote relays...') - syncState.isSyncing = true + isSyncing = true try { const localRelays = RELAYS.filter(isLocalRelay) @@ -79,7 +65,7 @@ export async function syncLocalEventsToRemote( if (eventsToSync.length === 0) { console.log('✅ No local events to sync') - syncState.isSyncing = false + isSyncing = false return } @@ -102,25 +88,10 @@ export async function syncLocalEventsToRemote( } console.log(`✅ Synced ${successCount}/${uniqueEvents.length} events to remote relays`) - syncState.lastRemoteConnectionTime = Date.now() } catch (error) { console.error('❌ Error during offline sync:', error) } finally { - syncState.isSyncing = false + isSyncing = false } } -/** - * Checks if we should sync based on current relay state - */ -export function shouldSync( - hasRemoteRelays: boolean, - account: IAccount | null -): boolean { - if (!account || syncState.isSyncing) return false - - // Only sync if we just came online (transition from local-only to having remote relays) - const justCameOnline = syncState.wasLocalOnly && hasRemoteRelays - return justCameOnline -} -