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.

Routine editor: Replace opens an exercise you already train at its own weight, not the load of the one it replaces (#110)

Duarte Santosde95559935ab
diff --git a/frontend/src/lib/routines.js b/frontend/src/lib/routines.js
index d40f8f580..0175d0499 100644
--- a/frontend/src/lib/routines.js
+++ b/frontend/src/lib/routines.js
@@ -1,6 +1,7 @@
import { uid } from './format.js'
import { defaultConfig, isBw, modeOf } from './history.js'
import { isBodyweightEq, isCardio } from './exercises.js'
+import { sessionsFor } from './progression.js'
/**
* Create a deep copy of a routine with a new id and a "(Copy)" suffix.
@@ -33,8 +34,16 @@ export function copyRoutine(routine, suffix = 'Copy') {
* What is not carried is history. Progress belongs to a routine and an exercise together (#216),
* so the new exercise starts on its own line — its own sessions in this routine, and until there
* are any, the slot's numbers — and the old exercise keeps its sessions, ready if it comes back.
+ * That is also why the old exercise's weight gives way to the new one's own wherever the new one
+ * has been logged (`S`, and the routine `rid` the slot is in): the next session reads a slot
+ * weight that differs from the one its last session was planned at as a deliberate edit, and
+ * opens there (#275, nextPrescription). An 80 kg barbell bench carried onto dumbbells trained at
+ * 30 would open the dumbbells at 80 — and only when the two rep schemes differed, because a
+ * plan whose sets and reps match just carries on. With the new exercise's own planned weight in
+ * the slot, its history decides either way. One never logged keeps the slot's weight, since
+ * there is nothing better to start from.
*/
-export function replaceSlotExercise(slot, id) {
+export function replaceSlotExercise(slot, id, S, rid) {
const old = slot || {}
if ((modeOf(old) === 'cardio') !== isCardio(id)) {
const kept = ['sg', 'note', 'restSec'].filter(key => old[key] != null)
@@ -42,10 +51,18 @@ export function replaceSlotExercise(slot, id) {
}
const { id: _replaced, bodyweight: _flag, ...carried } = old
const out = { id, ...carried }
- if (!isCardio(id) && isBw(old) !== isBodyweightEq(id)) {
+ if (isCardio(id)) return out
+ if (isBw(old) !== isBodyweightEq(id)) {
out.weight = 0
// A rep ceiling belongs to bodyweight work: it adds sets where there is no load to add.
delete out.repsMax
}
+ // The session the next prescription will read for this slot (nextPrescription), so the weight
+ // put here is the one that session was planned at: the restart rule then sees no edit and
+ // holds at what was lifted, or the plan carries on from it. A session saved before plans were
+ // stamped has no planned weight, and what was lifted in it is the nearest thing.
+ const mode = modeOf(out)
+ const last = S ? sessionsFor(S, id, out, rid).filter(s => s.mode === mode).at(-1) : null
+ if (last) out.weight = last.planned?.weight ?? last.weight ?? 0
return out
}
diff --git a/frontend/src/lib/routines.replace.test.js b/frontend/src/lib/routines.replace.test.js
index 7568d5581..fdc0209a8 100644
--- a/frontend/src/lib/routines.replace.test.js
+++ b/frontend/src/lib/routines.replace.test.js
@@ -87,7 +87,7 @@ describe('history after a replace', () => {
const [bench] = buildCombinedEntries(st, ['A']).entries
expect(work(bench).map(s => s.w)).toEqual([65, 65])
- st.routines[0].ex[0] = replaceSlotExercise(st.routines[0].ex[0], DB_BENCH)
+ st.routines[0].ex[0] = replaceSlotExercise(st.routines[0].ex[0], DB_BENCH, st, 'A')
const [db] = buildCombinedEntries(st, ['A']).entries
expect(db.id).toBe(DB_BENCH)
// Not the bench's 65: the dumbbells have never been logged, so they open at the slot's own
@@ -98,8 +98,52 @@ describe('history after a replace', () => {
// The bench's sessions in this routine are still there if it comes back.
expect(lastEntryFor(st, BENCH, 'A')?.sets.map(s => s.w)).toEqual([62.5, 62.5])
- st.routines[0].ex[0] = replaceSlotExercise(st.routines[0].ex[0], BENCH)
+ st.routines[0].ex[0] = replaceSlotExercise(st.routines[0].ex[0], BENCH, st, 'A')
+ // The bench's own planned weight goes back into the slot, not whatever the dumbbells had.
+ expect(st.routines[0].ex[0].weight).toBe(60)
const [back] = buildCombinedEntries(st, ['A']).entries
expect(work(back).map(s => s.w)).toEqual([65, 65])
})
+
+ // An exercise the user already trains in another routine brings its own numbers. The slot's
+ // weight belongs to the exercise that was in it, and the next session reads a slot weight that
+ // differs from its last planned one as an edit to open at (#275) — so an 80 kg barbell bench
+ // carried onto dumbbells trained at 30 opened them at 80, and only when the rep schemes
+ // differed: a matching scheme carried on from the dumbbells' own history instead.
+ function twoRoutines(dbReps) {
+ const st = state()
+ st.routines = [
+ { id: 'A', name: 'Push', ex: [{ id: BENCH, sets: 3, mode: 'reps', reps: 8, weight: 80 }] },
+ { id: 'B', name: 'Upper', ex: [{ id: DB_BENCH, sets: 3, mode: 'reps', reps: dbReps, weight: 30 }] },
+ ]
+ train(st, 'B')
+ train(st, 'B')
+ return st
+ }
+
+ it('opens a replacement that has history elsewhere at its own weight when the rep schemes differ', () => {
+ const st = twoRoutines(10)
+ // Routine B's dumbbells: 30, then 32.5, and 35 next.
+ expect(work(buildCombinedEntries(st, ['B']).entries[0]).map(s => s.w)).toEqual([35, 35, 35])
+
+ st.routines[0].ex[0] = replaceSlotExercise(st.routines[0].ex[0], DB_BENCH, st, 'A')
+ expect(st.routines[0].ex[0]).toEqual({ id: DB_BENCH, sets: 3, mode: 'reps', reps: 8, weight: 30 })
+ const [db] = buildCombinedEntries(st, ['A']).entries
+ // Routine A's 3 × 8, at what the dumbbells were last lifted at — not the bench's 80.
+ expect(work(db).map(s => [s.w, s.r])).toEqual([[32.5, 8], [32.5, 8], [32.5, 8]])
+ expect(db.plan.why[0]).toBe('First time in this routine — starting from its own target.')
+ })
+
+ it('carries on from the replacement\'s own history when the rep schemes match', () => {
+ const st = twoRoutines(8)
+ st.routines[0].ex[0] = replaceSlotExercise(st.routines[0].ex[0], DB_BENCH, st, 'A')
+ const [db] = buildCombinedEntries(st, ['A']).entries
+ expect(work(db).map(s => [s.w, s.r])).toEqual([[35, 8], [35, 8], [35, 8]])
+ })
+
+ it('keeps the slot\'s weight for a replacement that has never been logged', () => {
+ const st = twoRoutines(10)
+ st.routines[0].ex[0] = replaceSlotExercise(st.routines[0].ex[0], '0047', st, 'A')
+ expect(st.routines[0].ex[0].weight).toBe(80)
+ })
})
diff --git a/frontend/src/views/RoutineEdit.jsx b/frontend/src/views/RoutineEdit.jsx
index aef7ca517..3919ee537 100644
--- a/frontend/src/views/RoutineEdit.jsx
+++ b/frontend/src/views/RoutineEdit.jsx
@@ -356,8 +356,11 @@ export default function RoutineEdit() {
if (done) toast(t('Replaced with “{0}”', capWords(exerciseNameFor(ex))))
}
const picker = exercisePicker((ex, quick) => {
- if (quick) { commit(ex, slot => replaceSlotExercise(slot, ex.id)); return }
- const next = replaceSlotExercise(r.ex[i], ex.id)
+ // The new exercise's weight comes from its own sessions (lib/routines.js), read as they are
+ // now rather than as this render saw them: a sync can land while the picker is open.
+ const live = useStore.getState().S
+ if (quick) { commit(ex, slot => replaceSlotExercise(slot, ex.id, live, id)); return }
+ const next = replaceSlotExercise(r.ex[i], ex.id, live, id)
exConfigSheet(ex, next, cfg => commit(ex, slot => ({ id: ex.id, sg: slot.sg, ...cfg })), null, r)
}, { title: t('Replace exercise') })
}
diff --git a/frontend/src/views/RoutineEdit.replace.test.jsx b/frontend/src/views/RoutineEdit.replace.test.jsx
index 77865680d..c65d638ce 100644
--- a/frontend/src/views/RoutineEdit.replace.test.jsx
+++ b/frontend/src/views/RoutineEdit.replace.test.jsx
@@ -26,9 +26,10 @@ const benchSlot = { id: BENCH, sg: 'g1', sets: 4, mode: 'reps', reps: 6, weight:
const rowSlot = { id: ROW, sg: 'g1', sets: 3, mode: 'reps', reps: 10, weight: 50 }
let root, host, picker
-function mount() {
+function mount(workouts = [], routines = []) {
const S = clone(DEF)
- S.routines = [{ id: 'r1', name: 'Push', emoji: 'dumbbell', ex: [clone(benchSlot), clone(rowSlot)] }]
+ S.routines = [{ id: 'r1', name: 'Push', emoji: 'dumbbell', ex: [clone(benchSlot), clone(rowSlot)] }, ...routines]
+ S.workouts = workouts
useStore.setState({ S, user: null })
host = document.createElement('div')
document.body.appendChild(host)
@@ -87,6 +88,20 @@ describe('RoutineEdit — Replace exercise', () => {
expect(picker.close).toHaveBeenCalledOnce()
})
+ it('gives a replacement trained in another routine its own weight, not the old exercise\'s', () => {
+ // The dumbbells were planned at 30 in the upper-body routine: the bench's 80 is not theirs.
+ const upper = { id: 'r2', name: 'Upper', emoji: 'dumbbell', ex: [{ id: DB_BENCH, sets: 3, mode: 'reps', reps: 10, weight: 30 }] }
+ const planned = { sets: 3, reps: 10, weight: 30 }
+ const workout = {
+ id: 'w1', d: '2026-09-01', start: 1, end: 2, name: 'Upper', routineIds: ['r2'],
+ entries: [{ id: DB_BENCH, rid: 'r2', target: { sets: 3, mode: 'reps', reps: 10 }, planned, sets: [1, 2, 3].map(() => ({ w: 30, r: 10, done: true })) }],
+ }
+ mount([workout], [upper])
+ const onPick = openReplace()
+ act(() => onPick(EXIDX[DB_BENCH], true))
+ expect(slots()[0]).toEqual({ ...benchSlot, id: DB_BENCH, weight: 30 })
+ })
+
it('leaves a slot alone that changed under the picker', () => {
mount()
const onPick = openReplace()