From ad966e56d6e30a70637a08448f8708ce5cb3163c Mon Sep 17 00:00:00 2001 From: JSR Date: Thu, 19 Mar 2026 17:46:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8A=A0=E5=87=8F=E5=88=86?= =?UTF-8?q?=E5=90=8E=E9=A2=91=E7=B9=81=E5=BC=B9=E5=87=BA=E8=BF=9C=E7=AB=AF?= =?UTF-8?q?=E6=9C=AC=E5=9C=B0=E5=86=B2=E7=AA=81=E9=80=89=E6=8B=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.tsx | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index bf28952..9e06850 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -61,6 +61,7 @@ function MainContent(): React.JSX.Element { const [syncApplyLoading, setSyncApplyLoading] = useState(false) const syncCheckingRef = useRef(false) const syncApplyLoadingRef = useRef(false) + const lastLocalMutationAtRef = useRef(0) const activeMenu = useMemo(() => { const p = location.pathname @@ -112,7 +113,9 @@ function MainContent(): React.JSX.Element { messageApi.success( res.data.message || `同步完成(同步 ${res.data.synced_records} 条,解决冲突 ${res.data.resolved_conflicts} 条)` ) - window.dispatchEvent(new CustomEvent("ss:data-updated", { detail: { category: "all" } })) + window.dispatchEvent( + new CustomEvent("ss:data-updated", { detail: { category: "all", source: "sync" } }) + ) } else { messageApi.error(res?.data?.message || res?.message || "同步失败") } @@ -148,6 +151,20 @@ function MainContent(): React.JSX.Element { const conflicts = previewRes.data.conflicts || [] if (conflicts.length > 0) { + const recentLocalMutation = Date.now() - lastLocalMutationAtRef.current < 15000 + if (recentLocalMutation) { + const autoApplyRes = await api.dbSyncApply("keep_local") + if ( + autoApplyRes?.success && + autoApplyRes?.data?.success && + autoApplyRes?.data?.synced_records > 0 + ) { + window.dispatchEvent( + new CustomEvent("ss:data-updated", { detail: { category: "all", source: "sync" } }) + ) + } + return + } setSyncConflicts(conflicts) setSyncConflictVisible(true) return @@ -155,7 +172,9 @@ function MainContent(): React.JSX.Element { const applyRes = await api.dbSyncApply("keep_local") if (applyRes?.success && applyRes?.data?.success && applyRes?.data?.synced_records > 0) { - window.dispatchEvent(new CustomEvent("ss:data-updated", { detail: { category: "all" } })) + window.dispatchEvent( + new CustomEvent("ss:data-updated", { detail: { category: "all", source: "sync" } }) + ) } } catch (error) { console.error("Auto sync failed:", error) @@ -166,7 +185,11 @@ function MainContent(): React.JSX.Element { checkAndSync() const timer = window.setInterval(checkAndSync, 30000) - const onDataUpdated = () => { + const onDataUpdated = (e: Event) => { + const customEvent = e as CustomEvent<{ source?: string }> + if (customEvent?.detail?.source !== "sync") { + lastLocalMutationAtRef.current = Date.now() + } window.setTimeout(() => { checkAndSync().catch(() => void 0) }, 1200)