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: deleting a workout from before ids deletes only that one, and its detail sheet keeps saving the note, date and duration after a sync gave it its frozen id

Duarte Santose40720a975b4
diff --git a/frontend/src/lib/workout-date.js b/frontend/src/lib/workout-date.js
index bd725c046..80628fe89 100644
--- a/frontend/src/lib/workout-date.js
+++ b/frontend/src/lib/workout-date.js
@@ -28,9 +28,12 @@ export const startTimeOf = w => {
}
// Which workout an edit is about. Ids are the identity everywhere else, but a legacy record
-// has none and `x.id === undefined` would happily match the wrong one.
-export const sameWorkout = (a, b) =>
- a?.id != null || b?.id != null ? a?.id === b?.id : legacySyncKey(a) === legacySyncKey(b)
+// has none and `x.id === undefined` would happily match the wrong one, so it is compared by the
+// key the sync gives it (`workoutKey` in sync-merge.js). That is also what a move or an edit
+// freezes as its id — so a sheet still holding the copy from before that (the detail sheet, when
+// a sync brings the frozen record in while it is open) still finds its workout.
+const syncKeyOf = w => (w?.id != null ? w.id : legacySyncKey(w))
+export const sameWorkout = (a, b) => syncKeyOf(a) === syncKeyOf(b)
// The workout as it is after the move: same session, new position in time. The duration is
// carried rather than recomputed, so a session that ran 47 minutes still ran 47 minutes.
diff --git a/frontend/src/lib/workout-date.test.js b/frontend/src/lib/workout-date.test.js
index 979ba6141..9aca58f72 100644
--- a/frontend/src/lib/workout-date.test.js
+++ b/frontend/src/lib/workout-date.test.js
@@ -32,6 +32,12 @@ describe('sameWorkout / legacySyncKey', () => {
expect(sameWorkout({ d: '2026-01-01', start: 5 }, { d: '2026-01-02', start: 5 })).toBe(false)
expect(legacySyncKey({ d: '2026-01-01', start: 5 })).toBe('2026-01-01|5')
})
+ it('recognises a record from before ids once a move or an edit froze its old key as its id', () => {
+ const before = { d: '2026-01-01', start: 5 }
+ expect(sameWorkout(before, { id: '2026-01-01|5', d: '2026-01-03', start: 9 })).toBe(true)
+ expect(sameWorkout({ id: '2026-01-01|5', d: '2026-01-03', start: 9 }, before)).toBe(true)
+ expect(sameWorkout({ d: '2026-01-03', start: 9 }, { id: '2026-01-01|5', d: '2026-01-03', start: 9 })).toBe(false)
+ })
})
describe('retimeWorkout', () => {
diff --git a/frontend/src/sheets.jsx b/frontend/src/sheets.jsx
index e63dc411c..47b46fb7f 100644
--- a/frontend/src/sheets.jsx
+++ b/frontend/src/sheets.jsx
@@ -1860,7 +1860,9 @@ function WorkoutDetail({ w, close }) {
}
})}>{t('Save as routine')}</Button>
<div style={{ height: 10 }} />
- <Button variant="danger" onClick={() => confirmSheet({ title: t('Delete workout?'), message: t('This removes it from your history for good.'), confirmText: t('Delete'), danger: true, onConfirm: () => { update(s => { s.workouts = s.workouts.filter(x => x.id !== w.id) }); close(); toast(t('Workout deleted')) } })}>{t('Delete workout')}</Button>
+ {/* Matched the way the edits above are, not by id: a workout from before ids has none, and
+ filtering on `x.id !== undefined` took every other one of them with it. */}
+ <Button variant="danger" onClick={() => confirmSheet({ title: t('Delete workout?'), message: t('This removes it from your history for good.'), confirmText: t('Delete'), danger: true, onConfirm: () => { update(s => { s.workouts = s.workouts.filter(x => !sameWorkout(x, w)) }); close(); toast(t('Workout deleted')) } })}>{t('Delete workout')}</Button>
</>
}
export const workoutDetailSheet = w => ui().openSheet(close => <WorkoutDetail w={w} close={close} />)
diff --git a/frontend/src/sheets.workoutdate.test.jsx b/frontend/src/sheets.workoutdate.test.jsx
index 9e4960979..05c7093b8 100644
--- a/frontend/src/sheets.workoutdate.test.jsx
+++ b/frontend/src/sheets.workoutdate.test.jsx
@@ -143,6 +143,30 @@ describe('changing the date of a saved workout', () => {
expect(history()[0].d).toBe('2026-08-18')
})
+ // The sheet keeps the copy it opened with. A sync that brings in the same record with its old
+ // key frozen as its id (moved or edited on the other device) must not make its note and date
+ // rows quietly write nowhere.
+ it('keeps writing to a record from before ids after a sync froze its key as its id', () => {
+ const legacy = { d: '2026-08-25', start: 1767636000000, end: 1767639600000, name: 'Old', vol: 0, entries: [], prs: [] }
+ setHistory([legacy])
+ const host = render(() => workoutDetailSheet(legacy))
+ setHistory([{ ...legacy, id: '2026-08-25|1767636000000', d: '2026-08-24', _ts: 5 }])
+ act(() => { type(host.querySelector('textarea'), 'felt strong') })
+ act(() => { host.querySelector('textarea').dispatchEvent(new FocusEvent('focusout', { bubbles: true })) })
+ expect(history()).toHaveLength(1)
+ expect(history()[0].note).toBe('felt strong')
+ })
+
+ it('deletes only the workout it shows, also among records written before ids', () => {
+ const a = { d: '2026-08-20', start: 1767200000000, end: 1767203600000, name: 'A', vol: 0, entries: [], prs: [] }
+ const b = { d: '2026-08-25', start: 1767636000000, end: 1767639600000, name: 'B', vol: 0, entries: [], prs: [] }
+ setHistory([a, b, history()[1]])
+ const host = render(() => workoutDetailSheet(b))
+ const confirm = render(() => button(host, 'Delete workout').click())
+ act(() => { button(confirm, 'Delete').click() })
+ expect(history().map(w => w.name)).toEqual(['A', 'late'])
+ })
+
it('does nothing when the workout was deleted from another sheet meanwhile', () => {
const host = render(() => workoutDateSheet(history()[1]))
setHistory([history()[0]])