mirror of
https://github.com/dergigi/boris.git
synced 2025-12-18 23:24:22 +01:00
feat: automatic offline sync - rebroadcast local events when back online
- Create offlineSyncService to sync local-only events to remote relays - Create useOfflineSync hook to detect online/offline transitions - When user comes back online (remote relays connect), automatically: - Query local relays for user's events from last 24 hours - Rebroadcast highlights and bookmarks to remote relays - Integrate sync into Bookmarks component - Enables seamless offline workflow: - User can work offline with local relays - Events are automatically synced when connection restored - No manual intervention required
This commit is contained in:
57
src/hooks/useOfflineSync.ts
Normal file
57
src/hooks/useOfflineSync.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { useEffect, useRef } from 'react'
|
||||
import { RelayPool } from 'applesauce-relay'
|
||||
import { IAccount } from 'applesauce-core/helpers'
|
||||
import { syncLocalEventsToRemote } from '../services/offlineSyncService'
|
||||
import { isLocalRelay } from '../utils/helpers'
|
||||
import { RelayStatus } from '../services/relayStatusService'
|
||||
|
||||
interface UseOfflineSyncParams {
|
||||
relayPool: RelayPool | null
|
||||
account: IAccount | null
|
||||
relayStatuses: RelayStatus[]
|
||||
enabled?: boolean
|
||||
}
|
||||
|
||||
export function useOfflineSync({
|
||||
relayPool,
|
||||
account,
|
||||
relayStatuses,
|
||||
enabled = true
|
||||
}: UseOfflineSyncParams) {
|
||||
const previousStateRef = useRef<{
|
||||
hasRemoteRelays: boolean
|
||||
initialized: boolean
|
||||
}>({
|
||||
hasRemoteRelays: false,
|
||||
initialized: false
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
if (!enabled || !relayPool || !account) return
|
||||
|
||||
const connectedRelays = relayStatuses.filter(r => r.isInPool)
|
||||
const hasRemoteRelays = connectedRelays.some(r => !isLocalRelay(r.url))
|
||||
const hasLocalRelays = connectedRelays.some(r => isLocalRelay(r.url))
|
||||
|
||||
// Skip the first check to avoid syncing on initial load
|
||||
if (!previousStateRef.current.initialized) {
|
||||
previousStateRef.current = {
|
||||
hasRemoteRelays,
|
||||
initialized: true
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Detect transition: from local-only to having remote relays
|
||||
const wasLocalOnly = !previousStateRef.current.hasRemoteRelays && hasLocalRelays
|
||||
const isNowOnline = hasRemoteRelays
|
||||
|
||||
if (wasLocalOnly && isNowOnline) {
|
||||
console.log('✈️ Detected transition: Flight Mode → Online')
|
||||
syncLocalEventsToRemote(relayPool, account, true)
|
||||
}
|
||||
|
||||
previousStateRef.current.hasRemoteRelays = hasRemoteRelays
|
||||
}, [relayPool, account, relayStatuses, enabled])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user