Fix: Export polling constants and synchronize retry intervals in SyncStatusIndicator

This commit is contained in:
Mo Tarbin
2026-06-30 01:21:17 -04:00
parent e85220d727
commit 43f804c7a8
2 changed files with 43 additions and 10 deletions

View File

@@ -8,9 +8,9 @@ import { isOfflineFeatureEnabled } from '../utils/OfflineFeatureToggle'
import { syncEngine } from '../utils/SyncEngine' import { syncEngine } from '../utils/SyncEngine'
import { networkManager } from './NetworkManager' import { networkManager } from './NetworkManager'
const PENDING_POLL_MS = 30_000 // retry pending commands every 30s export const PENDING_POLL_MS = 30_000 // retry pending commands every 30s
export const SERVER_PROBE_MS = 15_000 // probe server when marked unreachable but device has network
const CACHE_REFRESH_MS = 5 * 60_000 // refresh IDB cache every 5 min while online const CACHE_REFRESH_MS = 5 * 60_000 // refresh IDB cache every 5 min while online
const SERVER_PROBE_MS = 15_000 // probe server when marked unreachable but device has network
export function useSyncOnReconnect() { export function useSyncOnReconnect() {
const queryClient = useQueryClient() const queryClient = useQueryClient()

View File

@@ -23,8 +23,12 @@ import {
Typography, Typography,
} from '@mui/joy' } from '@mui/joy'
import { useQueryClient } from '@tanstack/react-query' import { useQueryClient } from '@tanstack/react-query'
import { useEffect, useState } from 'react' import { useEffect, useMemo, useState } from 'react'
import { networkManager } from '../../hooks/NetworkManager' import { networkManager } from '../../hooks/NetworkManager'
import {
PENDING_POLL_MS,
SERVER_PROBE_MS,
} from '../../hooks/useSyncOnReconnect'
import { commandQueue } from '../../utils/CommandQueue' import { commandQueue } from '../../utils/CommandQueue'
import { import {
isOfflineFeatureEnabled, isOfflineFeatureEnabled,
@@ -57,8 +61,6 @@ const formatCommandLabel = commandType => {
) )
} }
const RETRY_INTERVAL = 30
function SyncStatusIndicator() { function SyncStatusIndicator() {
const queryClient = useQueryClient() const queryClient = useQueryClient()
const [pendingCommands, setPendingCommands] = useState([]) const [pendingCommands, setPendingCommands] = useState([])
@@ -71,7 +73,17 @@ function SyncStatusIndicator() {
const [isOnline, setIsOnline] = useState(networkManager.isOnline) const [isOnline, setIsOnline] = useState(networkManager.isOnline)
const [offlineSince, setOfflineSince] = useState(networkManager.offlineSince) const [offlineSince, setOfflineSince] = useState(networkManager.offlineSince)
const [offlineReason, setOfflineReason] = useState(networkManager.offlineReason) const [offlineReason, setOfflineReason] = useState(networkManager.offlineReason)
const [retryIn, setRetryIn] = useState(RETRY_INTERVAL)
// Mirror the actual intervals used by useSyncOnReconnect so the countdown is accurate
const retryInterval = useMemo(
() =>
!isOnline && offlineReason === 'server'
? SERVER_PROBE_MS / 1000
: PENDING_POLL_MS / 1000,
[isOnline, offlineReason],
)
const [retryIn, setRetryIn] = useState(retryInterval)
const [offlineFeatureEnabled, setOfflineFeatureEnabled] = useState( const [offlineFeatureEnabled, setOfflineFeatureEnabled] = useState(
isOfflineFeatureEnabled(), isOfflineFeatureEnabled(),
) )
@@ -90,9 +102,9 @@ function SyncStatusIndicator() {
useEffect(() => { useEffect(() => {
if (!syncState.syncing) { if (!syncState.syncing) {
setRetryIn(RETRY_INTERVAL) setRetryIn(retryInterval)
} }
}, [syncState.syncing, syncState.lastSync]) }, [syncState.syncing, syncState.lastSync, retryInterval])
useEffect(() => { useEffect(() => {
networkManager.registerNetworkListener(online => { networkManager.registerNetworkListener(online => {
@@ -108,10 +120,31 @@ function SyncStatusIndicator() {
if (isOnline && pendingCommands.length === 0) return if (isOnline && pendingCommands.length === 0) return
if (!isOnline && offlineReason === 'device') return if (!isOnline && offlineReason === 'device') return
const interval = setInterval(() => { const interval = setInterval(() => {
setRetryIn(prev => (prev <= 1 ? RETRY_INTERVAL : prev - 1)) setRetryIn(prev => {
if (prev <= 1) {
console.debug('[SyncStatusIndicator] Retry timer fired', {
isOnline,
offlineReason,
syncing: syncState.syncing,
lastSync: syncState.lastSync,
error: syncState.error,
pendingCommands: pendingCommands.length,
})
return retryInterval
}
return prev - 1
})
}, 1000) }, 1000)
return () => clearInterval(interval) return () => clearInterval(interval)
}, [isOnline, offlineReason, syncState.syncing, syncState.lastSync, pendingCommands.length]) }, [
isOnline,
offlineReason,
syncState.syncing,
syncState.lastSync,
syncState.error,
pendingCommands.length,
retryInterval,
])
useEffect(() => { useEffect(() => {
const update = async () => { const update = async () => {