From edf67c2b692a9d87312ef8b35cd74e43faf2cdbc Mon Sep 17 00:00:00 2001 From: Pavlenex <36959754+pavlenex@users.noreply.github.com> Date: Tue, 24 Mar 2026 10:58:13 +0100 Subject: [PATCH] Fix hashrate chart stuck after navigating to settings and back --- src/hooks/useHashrateHistory.ts | 55 ++++++++++++++------------------- 1 file changed, 24 insertions(+), 31 deletions(-) diff --git a/src/hooks/useHashrateHistory.ts b/src/hooks/useHashrateHistory.ts index a44060b..d762444 100644 --- a/src/hooks/useHashrateHistory.ts +++ b/src/hooks/useHashrateHistory.ts @@ -18,41 +18,34 @@ const SAMPLE_INTERVAL_MS = 5000; // Sample every 5 seconds */ export function useHashrateHistory(currentHashrate: number | undefined): HashrateDataPoint[] { const [history, setHistory] = useState([]); - const lastSampleTime = useRef(0); + const hashrateRef = useRef(currentHashrate); + // Keep ref in sync with latest value without re-creating the interval useEffect(() => { - if (currentHashrate === undefined || currentHashrate === null) return; + hashrateRef.current = currentHashrate; + }, [currentHashrate]); - const now = Date.now(); - - // Only add a new sample if enough time has passed - if (now - lastSampleTime.current < SAMPLE_INTERVAL_MS) return; - - lastSampleTime.current = now; - - const timeStr = new Date(now).toLocaleTimeString('en-US', { - hour: '2-digit', - minute: '2-digit', - hour12: false, - }); + useEffect(() => { + const interval = setInterval(() => { + const value = hashrateRef.current; + if (!value) return; // Skip zero and undefined (loading state) - setHistory(prev => { - const newPoint: HashrateDataPoint = { - time: timeStr, - timestamp: now, - hashrate: currentHashrate, - }; - - const updated = [...prev, newPoint]; - - // Keep only the last MAX_HISTORY_POINTS - if (updated.length > MAX_HISTORY_POINTS) { - return updated.slice(-MAX_HISTORY_POINTS); - } - - return updated; - }); - }, [currentHashrate]); + const now = Date.now(); + const timeStr = new Date(now).toLocaleTimeString('en-US', { + hour: '2-digit', + minute: '2-digit', + hour12: false, + }); + + setHistory(prev => { + const newPoint: HashrateDataPoint = { time: timeStr, timestamp: now, hashrate: value }; + const updated = [...prev, newPoint]; + return updated.length > MAX_HISTORY_POINTS ? updated.slice(-MAX_HISTORY_POINTS) : updated; + }); + }, SAMPLE_INTERVAL_MS); + + return () => clearInterval(interval); + }, []); // Run once on mount, clean up on unmount return history; }