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.

feat(muscles): derive planned weekly volume

Emil Zapata3cbcf9af61ab
diff --git a/frontend/src/lib/muscles.js b/frontend/src/lib/muscles.js
index 92a96c5cf..1896d4ad1 100644
--- a/frontend/src/lib/muscles.js
+++ b/frontend/src/lib/muscles.js
@@ -284,8 +284,25 @@ export function muscleBalanceWindow(workouts, win, now = Date.now(), today = tod
}
/** Load a routine *would* produce, from its planned set counts. */
-export const loadOfRoutine = routine =>
- loadOf((routine?.ex || []).map(c => ({ id: c.id, ex: c, sets: c.sets || 1 })))
+export const loadOfRoutine = (routine, exercises = {}) =>
+ loadOf((routine?.ex || []).map(c => ({ id: c.id, ex: exercises[c.id] || c, sets: c.sets || 1 })))
+
+/** Effective sets programmed by the live recurring week; overrides and history are not plans. */
+export function loadOfWeeklyPlan(S) {
+ const routines = new Map((S?.routines || []).map(routine => [routine.id, routine]))
+ const exercises = Object.fromEntries((S?.customEx || []).map(exercise => [exercise.id, exercise]))
+ const load = {}
+ for (const value of Object.values(S?.week || {})) {
+ for (const id of [].concat(value || [])) {
+ const routine = routines.get(id)
+ if (!routine) continue
+ for (const [slug, sets] of Object.entries(loadOfRoutine(routine, exercises))) {
+ load[slug] = (load[slug] || 0) + sets
+ }
+ }
+ }
+ return load
+}
/** Load for a workout still in progress — the sets ticked so far. */
export const loadOfActive = active =>
diff --git a/frontend/src/lib/muscles.test.js b/frontend/src/lib/muscles.test.js
index 1d30971fd..dba5a412e 100644
--- a/frontend/src/lib/muscles.test.js
+++ b/frontend/src/lib/muscles.test.js
@@ -3,7 +3,7 @@ import { EXIDX, EXDB, smOf } from './exercises.js'
import { LANGS, DERIVED_LOCALES } from './i18n-core.js'
import {
MUSCLE_NAME, exerciseMuscleSnapshot, hasExplicitMuscleMetadata, levelsOf, loadOf,
- loadOfWorkouts, matchesMuscleGroups, muscleBalanceWindow, muscleGroupsOf, musclesOf, rankOf
+ loadOfWorkouts, loadOfWeeklyPlan, matchesMuscleGroups, muscleBalanceWindow, muscleGroupsOf, musclesOf, rankOf
} from './muscles.js'
describe('multi-muscle exercise metadata', () => {
@@ -147,6 +147,37 @@ describe('map load with warm-up phases', () => {
})
})
+describe('planned weekly muscle volume', () => {
+ const state = {
+ customEx: [
+ { id: 'press', primaries: ['chest'], secondaries: ['triceps'] },
+ { id: 'curl', primaries: ['biceps'], secondaries: [] },
+ ],
+ routines: [
+ { id: 'push', ex: [{ id: 'press', sets: 2 }] },
+ { id: 'arms', ex: [{ id: 'curl', sets: 3 }] },
+ ],
+ week: { 0: 'arms', 1: ['push', 'missing'], 3: ['push'] },
+ dayPlan: { '2026-08-18': 'arms' },
+ workouts: [{ entries: [{ id: 'snapshot-only', muscleSnapshot: { muscleWeights: { quadriceps: 1 } } }] }],
+ }
+
+ it('aggregates arrays and legacy scalar assignments, including routines repeated in the week', () => {
+ expect(loadOfWeeklyPlan(state)).toEqual({ chest: 4, triceps: 1.6, biceps: 3 })
+ })
+
+ it('ignores stale routine ids, date overrides, and completed-workout snapshots', () => {
+ const load = loadOfWeeklyPlan(state)
+ expect(load).not.toHaveProperty('quadriceps')
+ expect(load.biceps).toBe(3)
+ })
+
+ it('returns an empty projection for missing or empty plans', () => {
+ expect(loadOfWeeklyPlan()).toEqual({})
+ expect(loadOfWeeklyPlan({ routines: [], week: { 1: ['missing'] } })).toEqual({})
+ })
+})
+
// A custom exercise that was deleted from the catalogue survives in history only as the
// muscleSnapshot finish-workout wrote. Reading it back is what keeps those sessions in the
// body map and in Stats instead of silently contributing nothing.