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
fix: capture working weight automatically
Manuel697e482033c4
diff --git a/frontend/src/lib/finish-workout.js b/frontend/src/lib/finish-workout.jsindex 1151ed4ee..cd2d82762 100644--- a/frontend/src/lib/finish-workout.js+++ b/frontend/src/lib/finish-workout.js@@ -1,11 +1,13 @@// The persisted boundary for a finished session. Keep this pure so compatibility tests can// exercise the exact shape the UI writes without mounting React or mutating store state.+import { bestWeightForEntry } from './history.js'+export function buildCompletedWorkout(active, { end = Date.now(), prs = [], snapshotFor } = {}) {const entries = (active?.entries || []).map(entry => {const completed = {id: entry.id,sets: entry.sets,- topW: entry.topW || null,+ topW: bestWeightForEntry(entry) || null,target: entry.target || null,}const snapshot = typeof snapshotFor === 'function' ? snapshotFor(entry) : nulldiff --git a/frontend/src/lib/finish-workout.test.js b/frontend/src/lib/finish-workout.test.jsindex 3c6641acc..434d1950a 100644--- a/frontend/src/lib/finish-workout.test.js+++ b/frontend/src/lib/finish-workout.test.js@@ -15,6 +15,36 @@ describe('completed workout boundary', () => {})})+ it('derives topW from the highest completed non-warm-up work set, not stale entry data', () => {+ const active = {+ id: 'active-1', d: '2026-08-08', start: 1000,+ entries: [{+ id: '0025', topW: 80, target: { mode: 'reps', sets: 2, reps: 8 },+ sets: [+ { phase: 'warmup', done: true, w: 120, r: 8 },+ { done: true, w: 75, r: 8 },+ { done: true, w: 85, r: 7 },+ { done: false, w: 100, r: 8 },+ ],+ }],+ }++ expect(buildCompletedWorkout(active).entries[0].topW).toBe(85)+ expect(buildCompletedWorkout({+ ...active,+ entries: [{ ...active.entries[0], topW: 120 }],+ }).entries[0].topW).toBe(85)+ })++ it('keeps a legacy topW when old completed rows have no usable weight', () => {+ const active = {+ id: 'active-1', d: '2026-08-08', start: 1000,+ entries: [{ id: '0025', topW: 60, sets: [{ done: true, r: 8 }] }],+ }++ expect(buildCompletedWorkout(active).entries[0].topW).toBe(60)+ })+it('persists progression exclusion only for a marked session', () => {const active = {id: 'active-1', d: '2026-08-08', start: 1000, routineId: 'routine-1', name: 'Deload', bw: 80,diff --git a/frontend/src/lib/history.js b/frontend/src/lib/history.jsindex a44751e26..bde541a0e 100644--- a/frontend/src/lib/history.js+++ b/frontend/src/lib/history.js@@ -626,20 +626,24 @@ export function bestWeightForEntry(entry = {}) {? entry.sets.filter(s => phaseForSet(s) === 'work'): []const repsRows = metricRowsForEntry(entry, 'reps')- if (!repsRows.length) {- return workRows.reduce((best, set) => {- if (set?.done !== true || isWarmupRow(set)) return best- const weight = Number(set.w)- return Number.isFinite(weight) && weight > best ? weight : best- }, 0)- }-+ // Reps rows are the authoritative load metric for a mixed entry. Otherwise use every+ // completed work row (timed holds can carry an added load too).+ const completedRows = repsRows.length+ ? repsRows+ : workRows.filter(set => set?.done === true && !isWarmupRow(set))let best = 0- repsRows.forEach(set => {+ let hasUsableWeight = false+ completedRows.forEach(set => {const weight = Number(set?.w)- if (Number.isFinite(weight) && weight > best) best = weight+ if (!Number.isFinite(weight)) return+ hasUsableWeight = true+ if (weight > best) best = weight})+ // A real completed row, including an explicit zero for an unloaded bodyweight set, always+ // wins. A manual topW is only useful for old records whose rows did not carry a usable load.+ if (hasUsableWeight) return best+const parentMode = modeForSet({}, target)const hasNonRepsWorkRow = workRows.some(set => modeForSet(set, target) !== 'reps')const hasWarmupRow = Array.isArray(entry.sets) && entry.sets.some(isWarmupRow)diff --git a/frontend/src/sheets.jsx b/frontend/src/sheets.jsxindex 8ccba30f9..0716f725a 100644--- a/frontend/src/sheets.jsx+++ b/frontend/src/sheets.jsx@@ -4,7 +4,7 @@ import { useUI } from './store/useUI.js'import { EXDB, EXIDX, BODYPARTS, isCardio, isBodyweightEq, allExercises, equipmentOf, smOf, matchExercise, exOr } from './lib/exercises.js'import { activeProfile, exAvailable, ALL_EQUIPMENT, newProfile } from './lib/equipment.js'import { fmtDate, fmtNum, fmtVol, fmtDur, durPart, todayISO, isoOf, uid, exCount, DAYN, DAYS, weekOrder, weekStartOf, weekDayOffset, MONTHS_LONG, ACCENTS } from './lib/format.js'-import { lastEntryFor, bestWeightFor, buildSets, effectiveRoutineId, workoutVolume, setsDone, setsDoneActive, lastBW, supersetUnits, unitOf, setLabel, defaultConfig, cleanupSg, modeOf, effortOf, isBw, isPerSide, sideReps, workSetsDone, applyIntensifierPlan, MAX_PLANNED_WARMUPS, NOTE_MAX } from './lib/history.js'+import { lastEntryFor, bestWeightFor, bestWeightForEntry, buildSets, effectiveRoutineId, workoutVolume, setsDone, setsDoneActive, lastBW, supersetUnits, unitOf, setLabel, defaultConfig, cleanupSg, modeOf, effortOf, isBw, isPerSide, sideReps, workSetsDone, applyIntensifierPlan, MAX_PLANNED_WARMUPS, NOTE_MAX } from './lib/history.js'import { usesBar, barWeightFor, defaultBarWeight, hasBarOverride } from './lib/bar.js'import { beep, vibrate } from './lib/sound.js'import { t, instrFor, exerciseNameFor, getLang, INSTR_LANGS } from './lib/i18n.js'@@ -1740,7 +1740,7 @@ function doFinishWorkout() {s.workouts = completeBackfill(s.workouts, A, w)} else {w.entries.forEach(e => {- const mx = Math.max(0, ...e.sets.filter(x => x.done && !isWarmupRow(x)).map(x => x.w || 0), e.topW || 0)+ const mx = bestWeightForEntry(e)if (mx > 0) { const cur = s.exWeights[e.id]; if (!cur || mx > cur.w) s.exWeights[e.id] = { w: mx, d: w.d } }})s.workouts.push(w)diff --git a/frontend/src/views/Workout.jsx b/frontend/src/views/Workout.jsxindex a71bfc005..39a8765bf 100644--- a/frontend/src/views/Workout.jsx+++ b/frontend/src/views/Workout.jsx@@ -4,14 +4,14 @@ import { useStore } from '../store/useStore.js'import { useUI } from '../store/useUI.js'import { exOr } from '../lib/exercises.js'import { usesBar, barWeightFor, plateSplit } from '../lib/bar.js'-import { effectiveRoutine, lastEntryFor, bestWeightFor, buildSets, freestyleConfig, defaultConfig, setsDoneActive, supersetUnits, unitOf, setLabel, modeOf, isBw, isPerSide, sideReps, repStep, EFFORT, effortOf, stepEffort, capEffort, cascadeWeight, insertWarmupRow, removeRowAt, pairAdjacent, unpairSuperset, cleanupSg, applyIntensifierPlan, pinnedNoteFor, exNoteFor } from '../lib/history.js'+import { effectiveRoutine, lastEntryFor, bestWeightFor, bestWeightForEntry, buildSets, freestyleConfig, defaultConfig, setsDoneActive, supersetUnits, unitOf, setLabel, modeOf, isBw, isPerSide, sideReps, repStep, EFFORT, effortOf, stepEffort, capEffort, cascadeWeight, insertWarmupRow, removeRowAt, pairAdjacent, unpairSuperset, cleanupSg, applyIntensifierPlan, pinnedNoteFor, exNoteFor } from '../lib/history.js'import { fmtNum, fmtDate, todayISO, exCount, DAYN } from '../lib/format.js'import { beep, vibrate } from '../lib/sound.js'import { t, exerciseNameFor } from '../lib/i18n.js'import { api } from '../lib/api.js'import { insertionIndexAfterCurrentUnit, nextUnfinishedUnit, setProgressHighWater, supersetFlowStep, restAfterSet, restOnRecheck, restSecFor } from '../lib/supersetFlow.js'import Media from '../components/Media.jsx'-import { startFlow, exercisePicker, exConfigSheet, exerciseDetailSheet, topWeightSheet, finishWorkout, workoutCompleteSheet, confirmSheet, exerciseNoteSheet, sessionNoteSheet, swapActiveWorkoutExercise, barWeightSheet } from '../sheets.jsx'+import { startFlow, exercisePicker, exConfigSheet, exerciseDetailSheet, finishWorkout, workoutCompleteSheet, confirmSheet, exerciseNoteSheet, sessionNoteSheet, swapActiveWorkoutExercise, barWeightSheet } from '../sheets.jsx'import Icon from '../components/Icon.jsx'import { Button, Check, NumberField } from '../components/ui.jsx'import { nextPrescription, applyPrescription, defaultIncrement } from '../lib/progression.js'@@ -107,8 +107,7 @@ function ExerciseBlock({ entryIdx, compact, onToggle, onField, onAddSet, onRemov// Only worth surfacing while there is still work left: once the exercise is finished, a note// telling you what to do in it is behind you, and the block is already long.const pinnedNote = entry.sets.some(s => !s.done) ? pinnedNoteFor(S, entry.id) : null- // The same number the "confirm your working weight" sheet calls your best, so the two- // never disagree inside one session: heaviest logged set, or the working weight you kept.+ // The number is the heaviest logged set, or the working weight you kept.const best = cardio ? 0 : Math.max(bestWeightFor(S, entry.id), (S.exWeights[entry.id] || {}).w || 0)// What the progression policy decided for this session, and why (issue #17). Computed when// the session was built so the reason matches the numbers already in the rows.@@ -530,25 +529,24 @@ function ActiveWorkout() {const toggle = (idx, i) => {const m = modeAt(idx)const cardioEntry = m === 'cardio'- let askTop = false, exJustDone = false, workoutDone = false, checked = false- mutEntry(idx, e => {+ let exJustDone = false, workoutDone = false, checked = false+ update(s => {+ const e = s.active.entries[idx]e.sets[i].done = !e.sets[i].donechecked = e.sets[i].doneif (e.sets[i].done) {beep(S.sound, 1040, 0.12); vibrate(30)const unitDone = unit.every(ui => (ui === idx ? e : A.entries[ui]).sets.every(x => x.done))if (unitDone) workoutDone = !nextUnfinishedUnit(A.entries, supersetUnits(A.entries), idx)- // Only loaded reps training has a "working weight" worth confirming — a bodyweight- // plank has nothing to put in that slider, and neither does a set of push-ups- // (issue #32: the fewest taps that still record what happened).- const loaded = m === 'reps' && !(isBw({ ...(e.target || {}), id: e.id }) && !e.sets.some(x => x.w > 0))- if (e.sets.every(x => x.done)) { exJustDone = true; if (loaded && !e.asked) { e.asked = true; askTop = true } }+ if (e.sets.every(x => x.done)) {+ exJustDone = true+ const mx = bestWeightForEntry(e)+ e.topW = mx || null+ if (!s.active.backfill && mx > 0) { const cur = s.exWeights[e.id]; if (!cur || mx > cur.w) s.exWeights[e.id] = { w: mx, d: todayISO() } }+ }}- })- // reps: topWeight first (it chains into the finish/continue prompt on the last unit).- // cardio/timed or already-confirmed: go straight to the prompt.- if (askTop) topWeightSheet(idx)- else if (workoutDone) workoutCompleteSheet()+ }, true)+ if (workoutDone) workoutCompleteSheet()else if (exJustDone && cardioEntry) useUI.getState().toast(t('Cardio logged'))else if (exJustDone && m === 'time') useUI.getState().toast(t('Hold logged'))@@ -584,7 +582,7 @@ function ActiveWorkout() {// stopRest() first so a rest that belongs after this set replaces the one that was running.if (freshUnitDone) stopRest()if (!freshUnit || freshUnit.length <= 1) {- if (freshUnitDone && !askTop && nextUnit?.length) update(s => { if (s.active) s.active.cur = nextUnit[0] })+ if (freshUnitDone && nextUnit?.length) update(s => { if (s.active) s.active.cur = nextUnit[0] })if (!restBeforeWarmup && restAfterSet({ unitDone: freshUnitDone, lastUnit: freshWorkoutDone })) startRest(restSec, idx)return}@@ -593,8 +591,7 @@ function ActiveWorkout() {if (!step) returnif (step.unitDone) {if (nextUnit?.length) {- // The top-weight sheet's explicit "Just close" path owns the choice not to advance.- if (!askTop) update(s => { if (s.active) s.active.cur = nextUnit[0] })+ update(s => { if (s.active) s.active.cur = nextUnit[0] })if (!restBeforeWarmup) startRest(restSec, idx)}} else {diff --git a/frontend/src/views/Workout.test.jsx b/frontend/src/views/Workout.test.jsxindex 72c008299..9092791cc 100644--- a/frontend/src/views/Workout.test.jsx+++ b/frontend/src/views/Workout.test.jsx@@ -205,6 +205,40 @@ describe('Workout set completion flow', () => {expect(mocks.startRest).not.toHaveBeenCalled()})+ it('auto-captures a completed weighted exercise without prompting and keeps navigation/rest', async () => {+ await mount([exercise('plain-bench', [false]), exercise('next', [false])])++ await toggleSet(0)++ expect(mocks.S.active.entries[0].topW).toBe(60)+ expect(mocks.S.exWeights['plain-bench'].w).toBe(60)+ expect(mocks.topWeightSheet).not.toHaveBeenCalled()+ expect(mocks.S.active.cur).toBe(1)+ expect(mocks.startRest).toHaveBeenCalledWith(90, expect.any(Number))+ })++ it('auto-captures a completed superset exercise without prompting and preserves superset flow', async () => {+ const group = 'superset-1'+ await mount([+ exercise('superset-a', [false], { sg: group }),+ exercise('superset-b', [false], { sg: group }),+ exercise('next', [false]),+ ])++ await toggleSet(0)++ expect(mocks.topWeightSheet).not.toHaveBeenCalled()+ expect(mocks.S.active.cur).toBe(1)+ expect(mocks.startRest).not.toHaveBeenCalled()++ await rerender()+ await toggleSet(1)++ expect(mocks.topWeightSheet).not.toHaveBeenCalled()+ expect(mocks.S.active.cur).toBe(2)+ expect(mocks.startRest).toHaveBeenCalledWith(90, expect.any(Number))+ })+it.each(['warmup', 'warm-up', 'warm_up'])('does not start transition rest before an incomplete %s row in the next ordinary exercise',async phase => {@@ -245,7 +279,7 @@ describe('Workout set completion flow', () => {expect(mocks.startRest).not.toHaveBeenCalled()})- it('does not start transition rest before a warm-up while top-weight confirmation owns navigation', async () => {+ it('advances without transition rest before an incomplete warm-up', async () => {await mount([exercise('current-loaded', [false]),exercise('next', [false, false], {@@ -259,8 +293,8 @@ describe('Workout set completion flow', () => {await toggleSet(0)- expect(mocks.topWeightSheet).toHaveBeenCalledWith(0)- expect(mocks.S.active.cur).toBe(0)+ expect(mocks.topWeightSheet).not.toHaveBeenCalled()+ expect(mocks.S.active.cur).toBe(1)expect(mocks.startRest).not.toHaveBeenCalled()})@@ -284,7 +318,7 @@ describe('Workout set completion flow', () => {expect(mocks.startRest).not.toHaveBeenCalled()})- it('leaves a completed superset selected while its top-weight sheet owns the advance choice', async () => {+ it('advances after a completed superset without opening a top-weight sheet', async () => {const group = 'superset-1'await mount([exercise('superset-a', [true, true, true], { sg: group, asked: true }),@@ -293,8 +327,8 @@ describe('Workout set completion flow', () => {], 1)await toggleSet(5)- expect(mocks.topWeightSheet).toHaveBeenCalledWith(1)- expect(mocks.S.active.cur).toBe(1)+ expect(mocks.topWeightSheet).not.toHaveBeenCalled()+ expect(mocks.S.active.cur).toBe(2)expect(mocks.startRest).toHaveBeenCalledWith(90, expect.any(Number))})@@ -327,7 +361,7 @@ describe('Workout set completion flow', () => {expect(mocks.startRest).toHaveBeenCalledWith(90, expect.any(Number))})- it('keeps top-weight confirmation in control without declaring completion while work remains', async () => {+ it('advances a completed ordinary exercise without declaring completion while work remains', async () => {await mount([exercise('current-loaded', [false]),exercise('pending', [false], { asked: true }),@@ -335,9 +369,10 @@ describe('Workout set completion flow', () => {await toggleSet(0)- expect(mocks.topWeightSheet).toHaveBeenCalledWith(0)+ expect(mocks.topWeightSheet).not.toHaveBeenCalled()expect(mocks.workoutCompleteSheet).not.toHaveBeenCalled()- expect(mocks.S.active.cur).toBe(0)+ expect(mocks.S.active.cur).toBe(1)+ expect(mocks.startRest).toHaveBeenCalledWith(90, expect.any(Number))})it('shows workout completion only when no unfinished unit remains', async () => {