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
timer: a hold a rest displaces keeps the time it held
Boris 'B' Kurktchieve01d482d5251
diff --git a/frontend/src/lib/finish-workout.js b/frontend/src/lib/finish-workout.jsindex 47390b62a..2aa821f08 100644--- a/frontend/src/lib/finish-workout.js+++ b/frontend/src/lib/finish-workout.js@@ -7,7 +7,16 @@ export function buildCompletedWorkout(active, { end = Date.now(), prs = [], snapconst entries = (active?.entries || []).map(entry => {const completed = {id: entry.id,- sets: entry.sets,+ // planSec is live-session bookkeeping: a hold displaced before it finished puts its plan+ // aside so the row still knows what it is asking for (Workout.startTimed). A finished+ // session keeps only what was logged, so the key never reaches S.workouts — an unfinished+ // row goes back to recording its plan, exactly as it did before any of this. Rows without+ // it are passed through by reference, so an ordinary session is the shape it always was.+ sets: (entry.sets || []).map(set => {+ if (!set || set.planSec == null) return set+ const { planSec, ...rest } = set+ return rest.done ? rest : { ...rest, sec: planSec }+ }),topW: bestWeightForEntry(entry) || null,target: entry.target || null,// Which routine this entry came from, and whether it counts for progression. Writtendiff --git a/frontend/src/lib/finish-workout.test.js b/frontend/src/lib/finish-workout.test.jsindex cb9439483..2807f6b44 100644--- a/frontend/src/lib/finish-workout.test.js+++ b/frontend/src/lib/finish-workout.test.js@@ -16,6 +16,36 @@ describe('completed workout boundary', () => {})})+ // planSec is live-session bookkeeping — the plan a hold displaced before it finished put aside+ // (Workout.startTimed). A finished session keeps only what was logged, so an unfinished row goes+ // back to recording its plan and the key never reaches S.workouts.+ it('never stores planSec: an unfinished row goes back to recording its plan', () => {+ const active = {+ id: 'a', d: '2026-09-18', start: 1000,+ entries: [{+ id: 'plank',+ sets: [+ { done: true, sec: 30, w: 0 },+ { done: false, sec: 3, planSec: 30, w: 0 },+ { done: true, sec: 28, planSec: 45, w: 0 },+ ],+ }],+ }+ const sets = buildCompletedWorkout(active, { end: 2000 }).entries[0].sets+ expect(sets).toEqual([+ { done: true, sec: 30, w: 0 },+ { done: false, sec: 30, w: 0 }, // the plan it was asking for, and what it held is gone+ { done: true, sec: 28, w: 0 }, // a logged row keeps its log+ ])+ expect(JSON.stringify(sets)).not.toContain('planSec')+ })++ it('leaves the rows it has nothing to strip exactly as they are', () => {+ const rows = [{ done: true, w: 60, r: 8 }]+ const active = { id: 'a', d: '2026-09-18', start: 1000, entries: [{ id: '0025', sets: rows }] }+ expect(buildCompletedWorkout(active, { end: 2000 }).entries[0].sets[0]).toBe(rows[0])+ })+it('mirrors routineIds → routineId, and tolerates a legacy scalar active.routineId', () => {const base = {id: 'w', d: '2026-08-08', start: 1,diff --git a/frontend/src/store/useUI.js b/frontend/src/store/useUI.jsindex 41ec61fc4..18940b91a 100644--- a/frontend/src/store/useUI.js+++ b/frontend/src/store/useUI.js@@ -98,8 +98,9 @@ export const useUI = create((set, get) => ({// zero under a rest that was still counting down, beeping its own end and logging the full// target for a set nobody was holding any more. Below the guard, not above it: a rest that// does not start has nothing to run alongside the hold, and taking the hold down for it- // would throw away a plank in progress for nothing.- get().stopWork()+ // would throw away a plank in progress for nothing. What it held is kept either way —+ // abandonWork, not stopWork.+ get().abandonWork()// Nothing clears pageHiddenAt but a tick, so an app switch with no timer running left it set// for good. The next timer's first tick then read it as "this countdown ran out while the app// was away" and finished in silence — a one-second rest, started on screen, over on screen,@@ -166,7 +167,7 @@ export const useUI = create((set, get) => ({finish; the elapsed time is what actually gets logged, so stopping at 0:38 of a 0:45hold records 0:38 rather than crediting the full target. */startWork(sec, label, onDone) {- get().stopWork()+ get().abandonWork() // a hold this one replaces keeps what it held, same as a rest replacing oneget().stopRest()const total = Math.max(1, Math.round(sec) || 1)const endsAt = Date.now() + total * 1000@@ -207,6 +208,24 @@ export const useUI = create((set, get) => ({get().stopWork()if (done) done(elapsed)},+ // A rest is starting while a hold runs that is not the one being ticked — a set finished on+ // another row, or on another exercise, which the List layout puts one tap away. The hold cannot+ // survive (the two must never run together) but the time it held is real, so it is handed back+ // before it goes and its own row keeps it. The `abandoned` flag tells the owner this was not a+ // finish: the row is not ticked off and earns no rest of its own, since the rest that displaced+ // the hold is the one now running.+ abandonWork() {+ const wk = get().work+ if (!wk) { get().stopWork(); return }+ const elapsed = wk.total - wk.left+ const done = workDone+ get().stopWork()+ // Under two seconds there is nothing to keep: that is a play button tapped by accident, or+ // tapped and thought better of, and rounding it up to one second the way an early finish does+ // would write a one-second plank over a real plan. (finishWorkEarly's Math.max(1, …) is right+ // for what it is: you pressed Done, so you held it, however briefly.)+ if (done && elapsed >= 2) done(elapsed, true)+ },// Abandon without logging anything.stopWork() {if (workInt) clearInterval(workInt); workInt = nulldiff --git a/frontend/src/store/useUI.test.js b/frontend/src/store/useUI.test.jsindex 61e944346..bc2b3e58c 100644--- a/frontend/src/store/useUI.test.js+++ b/frontend/src/store/useUI.test.js@@ -147,11 +147,56 @@ describe('a rest and a hold never run together', () => {it('so a left-over hold cannot reach zero under a running rest and log a set nobody held', () => {const holdDone = vi.fn()- useUI.getState().startWork(3, 'Plank', holdDone)- useUI.getState().startRest(90, 0)- vi.advanceTimersByTime(3000) // where the hold would have run out- expect(useUI.getState().timer.left).toBe(87) // the rest is still counting, untouched+ useUI.getState().startWork(30, 'Plank', holdDone)+ vi.advanceTimersByTime(12_000) // 12 s of the plank held+ useUI.getState().startRest(90, 1)+ vi.advanceTimersByTime(30_000) // past where the hold would have run out+ expect(useUI.getState().timer.left).toBe(60) // the rest is still counting, untouched+ expect(useUI.getState().work).toBe(null)+ // Once, on the way out, and never again — and with the 12 s it actually held, marked as no+ // finish. The count alone would not say which: a hold left running reaches its own zero and+ // calls back too, with the full 30 s target for a set that stopped being held at 12.+ expect(holdDone).toHaveBeenCalledTimes(1)+ expect(holdDone).toHaveBeenCalledWith(12, true)+ })++ // The hold cannot survive the rest, but the time it held is real: it is handed back on the way+ // out so its own row keeps it. Before this a plank in progress vanished without a trace every+ // time a set was ticked somewhere else — one tap away in the List layout.+ it('the displaced hold hands back what it held, marked as no finish', () => {+ const holdDone = vi.fn()+ useUI.getState().startWork(45, 'Plank', holdDone)+ vi.advanceTimersByTime(18_000)+ useUI.getState().startRest(90, 1)+ expect(useUI.getState().work).toBe(null)+ expect(holdDone).toHaveBeenCalledTimes(1)+ expect(holdDone).toHaveBeenCalledWith(18, true) // the seconds held, and: abandoned+ })++ it('under two seconds there is nothing to hand back — that was a play button by accident', () => {+ const holdDone = vi.fn()+ useUI.getState().startWork(45, 'Plank', holdDone)+ vi.advanceTimersByTime(1000)+ useUI.getState().startRest(90, 1)expect(useUI.getState().work).toBe(null)expect(holdDone).not.toHaveBeenCalled()})++ it('a hold displaced by another hold hands back what it held too', () => {+ const first = vi.fn()+ useUI.getState().startWork(45, 'Plank', first)+ vi.advanceTimersByTime(18_000)+ useUI.getState().startWork(60, 'Side plank', vi.fn())+ expect(first).toHaveBeenCalledWith(18, true)+ expect(useUI.getState().work.total).toBe(60)+ })++ it('and a rest that never starts hands back nothing, because the hold is still going', () => {+ const holdDone = vi.fn()+ useUI.getState().startWork(45, 'Plank', holdDone)+ vi.advanceTimersByTime(18_000)+ useUI.getState().startRest(0, 1)+ expect(useUI.getState().work).not.toBe(null)+ expect(holdDone).not.toHaveBeenCalled()+ })})diff --git a/frontend/src/views/Workout.jsx b/frontend/src/views/Workout.jsxindex bed11c5e8..7350cb660 100644--- a/frontend/src/views/Workout.jsx+++ b/frontend/src/views/Workout.jsx@@ -631,6 +631,10 @@ function ActiveWorkout() {// what was actually logged — in the session, in history and in a backup.const setField = (idx, i, field, v) => mutEntry(idx, e => {if (v == null) delete e.sets[i][field]; else e.sets[i][field] = v+ // Typing a duration IS the new plan, the same way ticking the row ends it: a plan a displaced+ // hold put aside must not outrank what you just typed, or the field would read 45 and the ▶+ // would still hold the 30 the row was asking for before.+ if (field === 'sec') delete e.sets[i].planSec// Changing a weight cascades to the following sets of the same phase, so a// heavier bar carries through the set instead of retyping every row.if (field === 'w') {@@ -854,8 +858,27 @@ function ActiveWorkout() {// This tap may be the only one before the hold's countdown beeps (a timed first exercise):// get the audio context running while it still counts as a gesture (iOS, #152).unlock(S.sound)- useUI.getState().startWork(e.sets[i].sec || 45, exerciseNameFor(exOr(e.id)), elapsed => {- mutEntry(idx, en => { en.sets[i].sec = elapsed })+ // How long to hold: the row's own seconds, unless it is carrying a plan from a hold that was+ // displaced before it finished. `sec` on a timed row is both the plan and the log, so writing+ // what a part-held set managed would otherwise become the next hold's target — 3 seconds of a+ // 30 second plank, and every hold after it is 3 seconds. planSec keeps the plan aside until+ // the row is held to the end, ticked, or given a duration you typed yourself, and it never+ // reaches S.workouts (lib/finish-workout.js).+ const plan = (!e.sets[i].done && e.sets[i].planSec) || e.sets[i].sec || 45+ useUI.getState().startWork(plan, exerciseNameFor(exOr(e.id)), (elapsed, abandoned) => {+ // A hold a rest displaced (useUI.abandonWork: a set ticked on another row, or another+ // exercise) keeps its seconds and nothing else. It is not a finish: the row stays unticked+ // and starts no rest, because the rest that displaced the hold is already counting down —+ // and the plan it was held against is put aside so the row still knows what it is asking for.+ if (abandoned) {+ // A row ticked by hand while its hold ran comes through here too — toggle starts the rest+ // that displaces the hold, so the hand-back lands on a row that is already ticked. It+ // still wants the seconds (that is what was held, not the target), but a finished row has+ // no use for a plan set aside.+ mutEntry(idx, en => { if (en.sets[i].planSec == null && !en.sets[i].done) en.sets[i].planSec = plan; en.sets[i].sec = elapsed })+ return+ }+ mutEntry(idx, en => { en.sets[i].sec = elapsed; delete en.sets[i].planSec })if (!useStore.getState().S.active.entries[idx].sets[i].done) toggle(idx, i)})}@@ -876,6 +899,8 @@ function ActiveWorkout() {if (side) e.sets[i] = toggleSide(e.sets[i], side)else e.sets[i].done = !e.sets[i].donechecked = e.sets[i].done+ // A finished row has no use for a plan set aside by a hold that did not finish.+ if (checked && e.sets[i].planSec != null) delete e.sets[i].planSecif (e.sets[i].done) {beep(S.sound, 1040, 0.12); vibrate(30)// The unit that owns the ticked set — not the marked one. Since !92 the marker no longerdiff --git a/frontend/src/views/Workout.test.jsx b/frontend/src/views/Workout.test.jsxindex 452fcecf7..e28e8774a 100644--- a/frontend/src/views/Workout.test.jsx+++ b/frontend/src/views/Workout.test.jsx@@ -11,6 +11,7 @@ const mocks = vi.hoisted(() => {timer: null,work: null,startRest: vi.fn(),+ startWork: vi.fn(),stopRest: null,stopWork: null,confirmSheet: vi.fn(),@@ -45,7 +46,7 @@ const mocks = vi.hoisted(() => {stopRest: state.stopRest,stopWork: state.stopWork,shiftRestOwner: vi.fn(),- startWork: vi.fn(),+ startWork: state.startWork,toast: state.toast,})return state@@ -564,6 +565,116 @@ describe('active workout weight controls', () => {})})+// A rest that starts while a hold is running takes the hold down (useUI: the two must never run+// together), so the hold hands back what it held on the way out and its own row keeps it. It is+// explicitly not a finish: the row stays unticked and starts no rest of its own, because the rest+// that displaced it is the one counting down. And `sec` on a timed row is both the plan and the+// log, so a part-held set must not become the next hold's target.+describe('a hold a rest displaced', () => {+ const timed = (sec = 30) => exercise('timed-plank', [false, false], {+ target: { mode: 'time', sec, weight: 0, bodyweight: true },+ sets: [{ sec, w: 0, done: false }, { sec, w: 0, done: false }],+ })+ const pressStart = async (index = 0) => {+ const button = container.querySelectorAll('button.setgo')[index]+ expect(button).toBeTruthy()+ await act(async () => { button.dispatchEvent(new dom.Event('click', { bubbles: true })) })+ await rerender()+ }+ // What useUI.abandonWork hands the owner: the seconds held, and "this was not a finish".+ const handBack = async (elapsed, call = 0) => {+ await act(async () => { mocks.startWork.mock.calls[call][2](elapsed, true) })+ await rerender()+ }++ it('keeps its seconds, stays unticked and starts no rest', async () => {+ await mount([timed()])+ await pressStart(0)+ mocks.startRest.mockClear()++ await handBack(18)++ expect(mocks.S.active.entries[0].sets[0]).toMatchObject({ sec: 18, done: false })+ expect(mocks.startRest).not.toHaveBeenCalled()+ })++ it('shows what it held without becoming the next hold\'s target', async () => {+ await mount([timed(30)])+ await pressStart(0)+ expect(mocks.startWork.mock.calls[0][0]).toBe(30)++ await handBack(3)+ expect(mocks.S.active.entries[0].sets[0]).toMatchObject({ sec: 3, planSec: 30, done: false })++ await pressStart(0) // hold it again+ expect(mocks.startWork.mock.calls[1][0]).toBe(30) // the plan, not the 3 s it managed+ })++ it('a plan you edited yourself survives the same way', async () => {+ await mount([timed(55)])+ await pressStart(0)+ expect(mocks.startWork.mock.calls[0][0]).toBe(55)+ await handBack(4)+ await pressStart(0)+ expect(mocks.startWork.mock.calls[1][0]).toBe(55)+ })++ it('and typing a duration is the new plan, so the plan it kept aside goes', async () => {+ await mount([timed(30)])+ await pressStart(0)+ await handBack(3)+ expect(mocks.S.active.entries[0].sets[0].planSec).toBe(30)++ // The seconds stepper on a timed row is the '.stp.w' one (the first column).+ const button = container.querySelector('.setrow .stp.w button[aria-label="Increase"]')+ expect(button).toBeTruthy()+ await act(async () => { button.dispatchEvent(new dom.Event('click', { bubbles: true })) })+ await rerender()++ expect(mocks.S.active.entries[0].sets[0].planSec).toBeUndefined()+ const typed = mocks.S.active.entries[0].sets[0].sec+ await pressStart(0)+ expect(mocks.startWork.mock.calls[1][0]).toBe(typed) // what the field says, not the old plan+ })++ it('and once the row is ticked, the plan it kept aside goes', async () => {+ await mount([timed(30)])+ await pressStart(0)+ await handBack(3)+ expect(mocks.S.active.entries[0].sets[0].planSec).toBe(30)++ await toggleSet(0) // ticked by hand+ expect(mocks.S.active.entries[0].sets[0].planSec).toBeUndefined()+ expect(mocks.S.active.entries[0].sets[0].done).toBe(true)+ })++ // Ticking the held row's own Check is the same mechanism from the other side: the tick starts+ // the rest, the rest displaces the hold, and the hand-back lands on the row the tick just+ // ticked. So the row logs what was actually held rather than its target, and keeps no plan.+ it('ticking the held row by hand logs what was held, not the target', async () => {+ await mount([timed(30)])+ await pressStart(0)+ mocks.work = { left: 12, total: 30, endsAt: Date.now() + 12_000, label: 'timed-plank' }+ await rerender()++ await toggleSet(0)+ await handBack(18) // what useUI.abandonWork hands back under that tick++ expect(mocks.S.active.entries[0].sets[0]).toMatchObject({ sec: 18, done: true })+ expect(mocks.S.active.entries[0].sets[0].planSec).toBeUndefined()+ })++ it('a hold held to the end still logs and ticks, and keeps no plan behind', async () => {+ await mount([timed(30)])+ await pressStart(0)+ await act(async () => { mocks.startWork.mock.calls[0][2](30) }) // no abandoned flag: a finish+ await rerender()++ expect(mocks.S.active.entries[0].sets[0]).toMatchObject({ sec: 30, done: true })+ expect(mocks.S.active.entries[0].sets[0].planSec).toBeUndefined()+ })+})+describe('Workout discard timer lifecycle', () => {it('preserves active timers while discard is awaiting confirmation', async () => {const timer = { left: 30, total: 90, endsAt: Date.now() + 30_000 }