From 803c3468545078c8ae98a1b115a6683d04c64590 Mon Sep 17 00:00:00 2001 From: KimiSwitch Dev Date: Thu, 8 Oct 2026 00:28:39 +0800 Subject: [PATCH] =?UTF-8?q?fix(dashboard):=20useDashboard=20=E5=8A=A0?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E4=B8=96=E4=BB=A3=E6=A0=A1=E9=AA=8C=20?= =?UTF-8?q?=E2=80=94=20=E5=BF=AB=E9=80=9F=E8=BF=9E=E7=BB=AD=E5=88=87?= =?UTF-8?q?=E6=8D=A2=E6=97=B6=E9=97=B4=E8=8C=83=E5=9B=B4=E6=97=B6=E5=B9=B6?= =?UTF-8?q?=E5=8F=91=20get=5Fsummary=20=E5=93=8D=E5=BA=94=E5=8F=AF?= =?UTF-8?q?=E8=83=BD=E4=B9=B1=E5=BA=8F=E8=BF=94=E5=9B=9E=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E6=A0=87=E7=AD=BE=E4=B8=8E=E6=95=B0=E6=8D=AE=E9=94=99=E4=BD=8D?= =?UTF-8?q?=EF=BC=9B=E5=8F=82=E7=85=A7=20useUsageQuery=20=E7=9A=84=20genRe?= =?UTF-8?q?f=20=E6=A8=A1=E5=BC=8F=E4=B8=A2=E5=BC=83=E8=BF=87=E6=9C=9F?= =?UTF-8?q?=E5=93=8D=E5=BA=94=EF=BC=88data/loadStats/error/loading=20?= =?UTF-8?q?=E5=85=A8=E9=83=A8=E5=8F=97=E4=BF=9D=E6=8A=A4=EF=BC=89=EF=BC=8C?= =?UTF-8?q?=E5=8D=B8=E8=BD=BD=E6=97=B6=E4=BD=BF=E4=B8=96=E4=BB=A3=E5=A4=B1?= =?UTF-8?q?=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/useDashboard.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/hooks/useDashboard.ts b/src/hooks/useDashboard.ts index 6880fe2..4e7ef2c 100644 --- a/src/hooks/useDashboard.ts +++ b/src/hooks/useDashboard.ts @@ -1,4 +1,4 @@ -import { useCallback, useEffect, useState } from "react"; +import { useCallback, useEffect, useRef, useState } from "react"; import { invoke } from "@tauri-apps/api/core"; import type { SummaryResult } from "../types/dashboard"; @@ -24,8 +24,11 @@ export function useDashboard() { const [error, setError] = useState(null); /** Round-trip timing for the last get_summary call (user-perceived lag). */ const [loadStats, setLoadStats] = useState<{ ms: number } | null>(null); + // Generation counter: stale responses (superseded range / unmounted) are dropped. + const genRef = useRef(0); const refresh = useCallback(async (force = false) => { + const gen = ++genRef.current; setLoading(true); setError(null); const start = performance.now(); @@ -34,13 +37,15 @@ export function useDashboard() { range, refresh: force, }); + if (genRef.current !== gen) return; setData(result); setLoadStats({ ms: Math.round(performance.now() - start) }); } catch (err) { + if (genRef.current !== gen) return; const msg = err instanceof Error ? err.message : String(err); setError(msg); } finally { - setLoading(false); + if (genRef.current === gen) setLoading(false); } }, [range]); @@ -48,6 +53,13 @@ export function useDashboard() { refresh(); }, [refresh]); + // Ignore late responses after unmount. + useEffect(() => { + return () => { + genRef.current += 1; + }; + }, []); + const changeRange = useCallback((next: DashboardRange) => { setRange(next); try {