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.

Sets: copyRowAt and insertRowAt, the copy and undo paths for swipe on sets

Duarte Santos2dd62549d430
diff --git a/frontend/src/lib/history.copy-row.test.js b/frontend/src/lib/history.copy-row.test.js
new file mode 100644
index 000000000..c1256ff5c
--- /dev/null
+++ b/frontend/src/lib/history.copy-row.test.js
@@ -0,0 +1,89 @@
+import { describe, it, expect } from 'vitest'
+import { copyRowAt, copySpanAt, insertRowAt, removeRowAt } from './history.js'
+import { makeSideSet, setSideField, toggleSide, isSideSet } from './workout-model.js'
+
+// Swipe on sets (v1.3.11): swipe right copies a set, swipe left removes one with an Undo.
+describe('copyRowAt', () => {
+ it('puts an unticked copy of a work set right below it, keeping weight, reps and effort', () => {
+ const rows = [{ w: 60, r: 8, done: true, rir: 2, at: 123, weightOrigin: 'manual' }, { w: 60, r: 8, done: false }]
+ const out = copyRowAt(rows, 0)
+ expect(out).toHaveLength(3)
+ expect(out[1]).toEqual({ w: 60, r: 8, rir: 2, done: false })
+ expect(out[0]).toBe(rows[0])
+ expect(rows).toHaveLength(2) // input untouched
+ })
+
+ it('keeps a warm-up a warm-up, so a ladder grows by copying', () => {
+ const rows = [{ w: 40, r: 10, done: true, phase: 'warmup', warmup: true }, { w: 80, r: 5, done: false }]
+ const out = copyRowAt(rows, 0)
+ expect(out[1]).toEqual({ w: 40, r: 10, done: false, phase: 'warmup', warmup: true })
+ expect(out[2]).toEqual({ w: 80, r: 5, done: false })
+ })
+
+ it('drops drop sets and bursts on the copy', () => {
+ const rows = [{ w: 100, r: 6, done: true, type: 'dropset', drops: [{ w: 80, r: 5 }] }]
+ expect(copyRowAt(rows, 0)[1]).toEqual({ w: 100, r: 6, done: false })
+ const rp = [{ w: 50, r: 14, done: false, type: 'restpause', clusters: [{ r: 4, restSec: 15 }] }]
+ expect(copyRowAt(rp, 0)[1]).toEqual({ w: 50, r: 14, done: false })
+ })
+
+ it('copies a timed hold with its time and a pyramid Max with its flag, but not the set-aside plan', () => {
+ expect(copyRowAt([{ sec: 38, w: 0, done: true, planSec: 45 }], 0)[1]).toEqual({ sec: 38, w: 0, done: false })
+ expect(copyRowAt([{ w: 60, r: 12, done: false, max: true }], 0)[1]).toEqual({ w: 60, r: 12, done: false, max: true })
+ })
+
+ it('copies a per-side set with both sides unticked and their own values', () => {
+ let row = makeSideSet({ w: 16, r: 20 })
+ row = setSideField(row, 'L', 'r', 11)
+ row = setSideField(row, 'R', 'w', 18)
+ row = toggleSide(row, 'L')
+ row = { ...row, sides: { ...row.sides, L: { ...row.sides.L, type: 'dropset', drops: [{ w: 10, r: 6 }] } } }
+ const out = copyRowAt([row], 0)
+ const c = out[1]
+ expect(isSideSet(c)).toBe(true)
+ expect(c.sides.L).toEqual({ w: 16, r: 11, done: false })
+ expect(c.sides.R).toEqual({ w: 18, r: 10, done: false })
+ expect(c.done).toBe(false)
+ expect(c.r).toBe(21)
+ expect(c.w).toBe(18)
+ expect(c.type).toBeUndefined()
+ })
+
+ it('copies a timed per-side hold as its L/R pair, after the pair', () => {
+ const rows = [{ sec: 30, w: 0, side: 'L', done: true }, { sec: 30, w: 0, side: 'R', done: false }, { sec: 30, w: 0, side: 'L', done: false }, { sec: 30, w: 0, side: 'R', done: false }]
+ for (const i of [0, 1]) {
+ const out = copyRowAt(rows, i)
+ expect(out.map(r => r.side)).toEqual(['L', 'R', 'L', 'R', 'L', 'R'])
+ expect(out[2]).toEqual({ sec: 30, w: 0, side: 'L', done: false })
+ expect(copySpanAt(rows, i)).toEqual({ start: 0, end: 1 })
+ }
+ })
+
+ it('leaves the rows alone for an index that is not there', () => {
+ const rows = [{ w: 1, r: 1, done: false }]
+ expect(copyRowAt(rows, 4)).toEqual(rows)
+ })
+})
+
+describe('insertRowAt (undo of a removed set)', () => {
+ it('puts the exact row back where it was, tick and sub-rows included', () => {
+ const rows = [{ w: 60, r: 8, done: true }, { w: 60, r: 8, done: true, type: 'dropset', drops: [{ w: 50, r: 6 }] }, { w: 60, r: 8, done: false }]
+ const removed = rows[1]
+ const after = removeRowAt(rows, 1)
+ expect(after).toHaveLength(2)
+ const back = insertRowAt(after, 1, removed)
+ expect(back).toEqual(rows)
+ expect(back[1]).toBe(removed)
+ })
+
+ it('lands it last when the entry lost rows in the meantime', () => {
+ expect(insertRowAt([{ w: 1 }], 5, { w: 2 })).toEqual([{ w: 1 }, { w: 2 }])
+ expect(insertRowAt([{ w: 1 }], undefined, { w: 2 })).toEqual([{ w: 1 }, { w: 2 }])
+ expect(insertRowAt([{ w: 1 }], -3, { w: 2 })).toEqual([{ w: 2 }, { w: 1 }])
+ })
+
+ it('never lets removeRowAt take the last row', () => {
+ const rows = [{ w: 1, r: 1, done: false }]
+ expect(removeRowAt(rows, 0)).toEqual(rows)
+ })
+})
diff --git a/frontend/src/lib/history.js b/frontend/src/lib/history.js
index 76b5ef1b8..9104866e4 100644
--- a/frontend/src/lib/history.js
+++ b/frontend/src/lib/history.js
@@ -943,6 +943,51 @@ const placeRow = (rows, i, row) => {
return next
}
+// What a copied set leaves behind (v1.3.11, swipe right or "Copy this set"): the tick and when it
+// happened, a hold's set-aside plan, the manual-weight marker (the copy follows a weight change
+// above it like any inherited row) and the drop/burst sub-rows, which belong to the set that was
+// actually done. Weight, reps, time, speed, effort, the warm-up phase, a pyramid's Max and the
+// side layout all come along.
+const NOT_COPIED = ['done', 'at', 'planSec', 'weightOrigin', 'type', 'drops', 'clusters']
+const bare = src => {
+ const out = { ...src }
+ for (const k of NOT_COPIED) delete out[k]
+ return out
+}
+function copyOfRow(src) {
+ if (isSideSet(src)) {
+ const side = sd => ({ ...bare(sd), done: false })
+ return syncSideAggregate({ ...bare(src), sides: { L: side(src.sides.L), R: side(src.sides.R) }, done: false })
+ }
+ return { ...bare(src), done: false }
+}
+
+/** "One more like this one": a copy of row `i` right below it, unticked and without sub-rows.
+ * A timed per-side hold is planned as an L row then an R row (buildSets), so copying either
+ * half copies the pair and puts it after the pair, keeping every L next to its R. */
+export function copySpanAt(rows, i) {
+ const work = r => r && !isWarmupRow(r) ? r.side : null
+ const start = work(rows[i]) === 'R' && work(rows[i - 1]) === 'L' ? i - 1 : i
+ const end = work(rows[start]) === 'L' && work(rows[start + 1]) === 'R' ? start + 1 : start
+ return { start, end }
+}
+export function copyRowAt(rows, i) {
+ if (!rows[i]) return rows.slice()
+ const { start, end } = copySpanAt(rows, i)
+ const next = rows.slice()
+ next.splice(end + 1, 0, ...rows.slice(start, end + 1).map(copyOfRow))
+ return next
+}
+
+/** Undo for a removed set: `row` back at index `i` exactly as it was (tick, values, sub-rows).
+ * An index past the end (the entry lost rows since) lands it last. */
+export function insertRowAt(rows, i, row) {
+ const next = rows.slice()
+ const at = Number.isInteger(i) ? Math.max(0, Math.min(i, next.length)) : next.length
+ next.splice(at, 0, row)
+ return next
+}
+
/** Remove the row at `i`, never emptying the entry below one row. */
export function removeRowAt(rows, i) {
if (rows.length <= 1) return rows.slice()