From 448ea4857a846b3a75b5774bd51e53b30360c6dc Mon Sep 17 00:00:00 2001 From: weeihan Date: Sat, 11 Jul 2026 19:11:16 +0800 Subject: [PATCH] fix: offline sync basePath prefix and stable syncNow callback MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - fetch URL: /api/incidents → /ims/api/incidents (basePath not auto-prepended in client fetch) - replace syncing state guard with syncingRef to give syncNow a stable reference, preventing useEffect from re-registering the online listener on every sync cycle - keep syncing state for UI rendering only Co-Authored-By: Claude Sonnet 4.6 Claude-Session: https://claude.ai/code/session_01FFDuBhMKvoWjrWT3ZnGmmr --- components/incidents/offline-sync.tsx | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/components/incidents/offline-sync.tsx b/components/incidents/offline-sync.tsx index 3ff20c3..b1165ce 100644 --- a/components/incidents/offline-sync.tsx +++ b/components/incidents/offline-sync.tsx @@ -1,10 +1,11 @@ 'use client' -import { useEffect, useState, useCallback } from 'react' +import { useEffect, useState, useCallback, useRef } from 'react' export function OfflineSync() { const [pendingCount, setPendingCount] = useState(0) const [syncing, setSyncing] = useState(false) + const syncingRef = useRef(false) async function checkPending() { const { getPendingCount } = await import('@/lib/offline/db') @@ -12,7 +13,8 @@ export function OfflineSync() { } const syncNow = useCallback(async () => { - if (syncing) return + if (syncingRef.current) return + syncingRef.current = true setSyncing(true) try { const { getPendingReports, removePendingReport } = await import('@/lib/offline/db') @@ -27,7 +29,7 @@ export function OfflineSync() { if (report.medical_status) fd.append('medical_status', report.medical_status) try { - const res = await fetch('/api/incidents', { method: 'POST', body: fd }) + const res = await fetch('/ims/api/incidents', { method: 'POST', body: fd }) if (res.ok && report.id != null) { await removePendingReport(report.id) } @@ -36,10 +38,11 @@ export function OfflineSync() { } } } finally { + syncingRef.current = false await checkPending() setSyncing(false) } - }, [syncing]) + }, []) useEffect(() => { // eslint-disable-next-line react-hooks/set-state-in-effect