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.

History: saving the workout editor without a change leaves the workout as it was, so it no longer outranks an edit another device has not synced yet

Duarte Santos5e8dfff49362
diff --git a/frontend/src/lib/session-edit.js b/frontend/src/lib/session-edit.js
index 5b5a48379..5c82f2f82 100644
--- a/frontend/src/lib/session-edit.js
+++ b/frontend/src/lib/session-edit.js
@@ -34,6 +34,20 @@ const keyOf = w => (w?.id != null ? w.id : legacySyncKey(w))
const SESSION_FIELDS = ['note', 'name', 'bw']
const sessionField = (w, k) => (k === 'note' ? (w?.note || '').trim() : (w?.[k] ?? null))
+// The same data whatever order its keys were written in. A key holding undefined is no key, as it
+// is once the record is stored.
+function sameData(a, b) {
+ if (a === b) return true
+ if (!a || !b || typeof a !== 'object' || typeof b !== 'object' || Array.isArray(a) !== Array.isArray(b)) return false
+ const keys = o => Object.keys(o).filter(k => o[k] !== undefined)
+ const ka = keys(a)
+ return ka.length === keys(b).length && ka.every(k => sameData(a[k], b[k]))
+}
+// What Save would write, apart from what it works out again (the volume and the badges), the stamp
+// and the id it freezes for a workout from before ids.
+const DERIVED = ['id', 'vol', 'prs', '_ts']
+const savedData = w => Object.fromEntries(Object.entries(w).filter(([k]) => !DERIVED.includes(k)))
+
// The best load one workout logged for an exercise, across every occurrence of it.
function bestIn(workout, id) {
let best = 0
@@ -115,6 +129,13 @@ export function saveWorkoutEdit(state, now = Date.now()) {
if (value == null || value === '') delete record[k]
else record[k] = value
}
+ // A Save that changed nothing closes the editor and leaves the record as it is. A new stamp
+ // would outrank an edit another device made since and has not synced yet (sets added on the
+ // phone), for nothing — the date and duration rows skip an unchanged save the same way.
+ if (sameData(savedData(record), savedData(current))) {
+ state.active = null
+ return current
+ }
record.vol = workoutVolume(record)
stampWorkout(record, now)
state.workouts[index] = record
diff --git a/frontend/src/lib/session-edit.test.js b/frontend/src/lib/session-edit.test.js
index 9978346bc..d57356be5 100644
--- a/frontend/src/lib/session-edit.test.js
+++ b/frontend/src/lib/session-edit.test.js
@@ -187,6 +187,22 @@ describe('saved workout editing', () => {
expect(lastEntryFor(state, '0025').sets[0].w).toBe(80)
})
+ // A stamp outranks what another device wrote since. Opening the editor and saving without a
+ // change must not give the record one, or it beats sets added on the phone that have not synced.
+ it('leaves the record and its stamp alone when Save changed nothing', () => {
+ const state = fixture()
+ state.workouts[0] = { ...state.workouts[0], name: 'Legs', routineIds: [], routineId: null, entries: [{ ...entry(40), topW: 40 }], vol: 200, _ts: 7 }
+ const before = structuredClone(state.workouts)
+ editCompletedSession(state, 'workout')
+ expect(saveWorkoutEdit(state, 1234)).toEqual(before[0])
+ expect(state.workouts).toEqual(before)
+ expect(state.active).toBeNull()
+
+ editCompletedSession(state, 'workout')
+ state.active.entries[0].sets[0].r = 6
+ expect(saveWorkoutEdit(state, 1234)._ts).toBe(1234)
+ })
+
it('stamps the edit, and the edit replaces the old copy by id in a merge whichever copy is newer', () => {
const state = fixture()
editCompletedSession(state, 'workout')