mirror of
https://github.com/dergigi/boris.git
synced 2025-12-18 15:14:20 +01:00
- Fix empty catch blocks by adding explanatory comments - Remove unused variables or prefix with underscore - Remove orphaned object literals from removed console.log statements - Fix unnecessary dependency array entries - Ensure all empty code blocks have comments to satisfy eslint no-empty rule
65 lines
1.9 KiB
TypeScript
65 lines
1.9 KiB
TypeScript
import { useEffect, useRef } from 'react'
|
|
import { RelayPool } from 'applesauce-relay'
|
|
import { IAccount } from 'applesauce-accounts'
|
|
import { IEventStore } from 'applesauce-core'
|
|
import { syncLocalEventsToRemote } from '../services/offlineSyncService'
|
|
import { isLocalRelay } from '../utils/helpers'
|
|
import { RelayStatus } from '../services/relayStatusService'
|
|
|
|
interface UseOfflineSyncParams {
|
|
relayPool: RelayPool | null
|
|
account: IAccount | null
|
|
eventStore: IEventStore | null
|
|
relayStatuses: RelayStatus[]
|
|
enabled?: boolean
|
|
}
|
|
|
|
export function useOfflineSync({
|
|
relayPool,
|
|
account: _account,
|
|
eventStore,
|
|
relayStatuses,
|
|
enabled = true
|
|
}: UseOfflineSyncParams) {
|
|
const previousStateRef = useRef<{
|
|
hasRemoteRelays: boolean
|
|
initialized: boolean
|
|
}>({
|
|
hasRemoteRelays: false,
|
|
initialized: false
|
|
})
|
|
|
|
useEffect(() => {
|
|
if (!enabled || !relayPool || !_account || !eventStore) 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) {
|
|
// Coming back online, sync events
|
|
|
|
// Wait a moment for relays to fully establish connections
|
|
setTimeout(() => {
|
|
syncLocalEventsToRemote(relayPool, eventStore)
|
|
}, 2000)
|
|
}
|
|
|
|
previousStateRef.current.hasRemoteRelays = hasRemoteRelays
|
|
}, [relayPool, _account, eventStore, relayStatuses, enabled])
|
|
}
|
|
|