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.
JavaScript apple-watchbodyweightdockerfitnessfitness-trackergymhealthiosmcpnodejspasskeysprogressive-web-applicationspwareactself-hostedself-hostingvitewebauthnweightliftingworkout-tracker
tabbar: the buttons are built once, not on every store write
Boris 'B' Kurktchiev9edce222eeec
diff --git a/frontend/src/components/TabBar.jsx b/frontend/src/components/TabBar.jsxindex d33c7a2a6..6918ee611 100644--- a/frontend/src/components/TabBar.jsx+++ b/frontend/src/components/TabBar.jsx@@ -5,6 +5,18 @@ import { todayISO } from '../lib/format.js'import { t } from '../lib/i18n.js'import Icon from './Icon.jsx'+// Module scope, not TabBar's render body. Declared inside it, `Tab` was a new function on every+// render, so React saw a different component type each time and threw the button away and built a+// fresh one — on a bar that is fixed on screen, and once a second for the whole of a rest. The+// state it took with it is the DOM node itself: focus, the :active tint, any in-flight transition.+function Tab({ active, icon, label, onClick }) {+ return (+ <button className={active ? 'on' : ''} onClick={onClick}>+ <Icon name={icon} /><span>{label}</span>+ </button>+ )+}+export default function TabBar({ onStart }) {const nav = useNavigate()const loc = useLocation()@@ -23,16 +35,11 @@ export default function TabBar({ onStart }) {}nav('/workout')}- const Tab = ({ k, icon, to, label }) => (- <button className={on(k) ? 'on' : ''} onClick={() => nav(to)}>- <Icon name={icon} /><span>{label}</span>- </button>- )return (<nav id="tabbar">- <Tab k="home" icon="house" to="/home" label={t('Home')} />- <Tab k="plan" icon="calendar" to="/plan" label={t('Plan')} />+ <Tab active={on('home')} icon="house" label={t('Home')} onClick={() => nav('/home')} />+ <Tab active={on('plan')} icon="calendar" label={t('Plan')} onClick={() => nav('/plan')} />{/* On the workout screen itself there is nothing to resume, so the button reads as thetab it is and stays lit (#29); anywhere else it brings you back to the exercise youwere on — the marker is kept in S.active.cur and never moves on its own (#21). */}@@ -40,8 +47,8 @@ export default function TabBar({ onStart }) {<span className="cir"><Icon name={S.active ? (cur === 'workout' ? 'dumbbell' : 'play') : 'dumbbell'} /></span><span>{S.active ? (cur === 'workout' ? t('Workout') : t('Resume')) : t('Start')}</span></button>- <Tab k="stats" icon="chart" to="/stats" label={t('Stats')} />- <Tab k="library" icon="list" to="/library" label={t('Exercises')} />+ <Tab active={on('stats')} icon="chart" label={t('Stats')} onClick={() => nav('/stats')} />+ <Tab active={on('library')} icon="list" label={t('Exercises')} onClick={() => nav('/library')} /></nav>)}diff --git a/frontend/src/components/TabBar.test.jsx b/frontend/src/components/TabBar.test.jsxnew file mode 100644index 000000000..7ee4250f5--- /dev/null+++ b/frontend/src/components/TabBar.test.jsx@@ -0,0 +1,62 @@+// @vitest-environment happy-dom+// The tab bar is fixed on screen and re-renders on every store write — once a second for the+// whole of a rest. Its buttons must survive that: a component declared inside the render body is+// a new function each time, which React reads as a different type and rebuilds from scratch.+import React, { act } from 'react'+import { createRoot } from 'react-dom/client'+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'+import TabBar from './TabBar.jsx'+import { DEF, useStore } from '../store/useStore.js'++vi.mock('react-router-dom', () => ({+ useNavigate: () => () => {},+ useLocation: () => ({ pathname: '/home' }),+}))++globalThis.IS_REACT_ACT_ENVIRONMENT = true++let host, root, originalS, originalUser++beforeEach(() => {+ const store = useStore.getState()+ originalS = store.S+ originalUser = store.user+ useStore.setState({ S: JSON.parse(JSON.stringify(DEF)), user: { id: 1, name: 'test' } })+ host = document.createElement('div')+ document.body.appendChild(host)+ root = createRoot(host)+})++afterEach(() => {+ act(() => root.unmount())+ host.remove()+ useStore.setState({ S: originalS, user: originalUser })+})++const tabs = () => [...host.querySelectorAll('#tabbar button')]++describe('the tab bar across a store write', () => {+ it('keeps the very same buttons instead of rebuilding them every tick', () => {+ act(() => { root.render(<TabBar onStart={() => {}} />) })+ const before = tabs()+ expect(before).toHaveLength(5)++ // What a rest does once a second: S is replaced, so everything reading it re-renders.+ act(() => { useStore.getState().update(s => { s.restSec = 91 }, false) })++ const after = tabs()+ expect(after).toHaveLength(before.length)+ after.forEach((el, i) => expect(el).toBe(before[i]))+ })++ it('still lights the tab for the route it is on, and follows a change of state', () => {+ act(() => { root.render(<TabBar onStart={() => {}} />) })+ expect(tabs()[0].className).toBe('on')+ expect(tabs()[1].className).toBe('')+ expect(tabs()[2].className).toBe('start')++ act(() => { useStore.getState().update(s => { s.active = { id: 'a', entries: [], cur: 0 } }, false) })+ expect(tabs()[2].className).toBe('start rec')+ expect(tabs()[0].className).toBe('on')+ })+})