Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

perf: Stats taps no longer recompute fatigue and strength; clocks rest while hidden

Duarte Santos7b189ed6b18b
diff --git a/frontend/src/components/Elapsed.jsx b/frontend/src/components/Elapsed.jsx
index 0bf45fc86..adea3c573 100644
--- a/frontend/src/components/Elapsed.jsx
+++ b/frontend/src/components/Elapsed.jsx
@@ -7,7 +7,12 @@ export default function Elapsed({ start }) {
const [t, setT] = useState('0:00')
useEffect(() => {
const tick = () => { const s = Math.max(0, Math.floor((Date.now() - start) / 1000)); setT(Math.floor(s / 60) + ':' + String(s % 60).padStart(2, '0')) }
- tick(); const iv = setInterval(tick, 1000); return () => clearInterval(iv)
+ // Hidden (another app, the screen locked), nobody reads it: no re-render a second, and the
+ // count is right again the moment the page shows.
+ const onTick = () => { if (!document.hidden) tick() }
+ tick(); const iv = setInterval(onTick, 1000)
+ document.addEventListener('visibilitychange', onTick)
+ return () => { clearInterval(iv); document.removeEventListener('visibilitychange', onTick) }
}, [start])
return <span>{t}</span>
}
diff --git a/frontend/src/views/Stats.jsx b/frontend/src/views/Stats.jsx
index 4ffd3a403..249aa2771 100644
--- a/frontend/src/views/Stats.jsx
+++ b/frontend/src/views/Stats.jsx
@@ -74,13 +74,20 @@ export const STRENGTH_LEVELS = [
{ at: 1, level: 4 },
]
-function useNow() {
- const [, setTick] = useState(0)
+// The time the fatigue and strength figures are worked out for: taken again once a minute (they
+// decay with it) and whenever the data they read changes, but not on every render. It used to be
+// Date.now() of each render, so every useMemo below that depends on it ran again on any tap on
+// this screen (a chip, a week arrow): the whole history walked twice per tap. Hidden, the minute
+// is not counted; shown again, it is taken at once.
+function useNow(...data) {
+ const [tick, setTick] = useState(0)
useEffect(() => {
- const iv = setInterval(() => setTick(tick => tick + 1), 60000)
- return () => clearInterval(iv)
+ const onTick = () => { if (!document.hidden) setTick(n => n + 1) }
+ const iv = setInterval(onTick, 60000)
+ document.addEventListener('visibilitychange', onTick)
+ return () => { clearInterval(iv); document.removeEventListener('visibilitychange', onTick) }
}, [])
- return Date.now()
+ return useMemo(() => Date.now(), [tick, ...data])
}
/**
@@ -132,7 +139,7 @@ function MuscleBalance({ S }) {
const [hard, setHard] = useState(false)
const [sel, setSel] = useState(null)
const [weeklyExpanded, setWeeklyExpanded] = useState(false)
- const now = useNow()
+ const now = useNow(S)
const lang = getLang()
const workouts = S.workouts
const bodyweightKg = useMemo(() => profileBodyweightKg(S), [S.bodyweight, S.unit])