diff --git a/frontend/node_modules b/frontend/node_modules new file mode 120000 index 0000000..41b9e25 --- /dev/null +++ b/frontend/node_modules @@ -0,0 +1 @@ +/tailnet-share/personal/projects/openGym/frontend/node_modules \ No newline at end of file diff --git a/frontend/src/lib/history.js b/frontend/src/lib/history.js index 43251af..d261345 100644 --- a/frontend/src/lib/history.js +++ b/frontend/src/lib/history.js @@ -142,6 +142,60 @@ export function cleanupSg(ex) { }) } +// Return the contiguous run around an entry that shares its superset id. A repeated id in a +// separated part of the list is deliberately not included: the display semantics are adjacent +// entries sharing one id, not every entry that happens to carry that id. +function contiguousSgGroup(items, idx) { + const sg = items[idx]?.sg + if (!sg) return [idx] + let first = idx + let last = idx + while (first > 0 && items[first - 1]?.sg === sg) first-- + while (last + 1 < items.length && items[last + 1]?.sg === sg) last++ + return Array.from({ length: last - first + 1 }, (_, i) => first + i) +} + +function freshSg(items, first, second) { + const base = `sg-${Math.min(first, second)}-${Math.max(first, second)}` + let sg = base + let n = 2 + while (items.some(e => e.sg === sg)) sg = `${base}-${n++}` + return sg +} + +// Purely pair two adjacent entries. Existing contiguous groups on either side are merged, so +// pairing the end of one group with the start of another produces one display unit. A caller can +// provide a group id (useful when restoring a known id); otherwise an existing id is preferred, +// with a deterministic unused id for two previously ungrouped entries. +export function pairAdjacent(items, first, second, groupId) { + if (!Array.isArray(items)) throw new TypeError('Superset entries must be an array') + if (!Number.isInteger(first) || !Number.isInteger(second) || !items[first] || !items[second]) { + throw new RangeError('Superset entry indexes are invalid') + } + if (Math.abs(first - second) !== 1) throw new RangeError('Superset entries must be adjacent') + + const next = items.map(e => ({ ...e })) + const left = Math.min(first, second) + const right = Math.max(first, second) + const group = groupId || next[left].sg || next[right].sg || freshSg(next, left, right) + const members = new Set([...contiguousSgGroup(next, left), ...contiguousSgGroup(next, right)]) + members.forEach(i => { next[i].sg = group }) + return next +} + +// Remove one entry from its superset and clean any ids that no longer have an adjacent partner. +// This is pure so the active workout can replace its entries atomically through the store. +export function unpairSuperset(items, idx) { + if (!Array.isArray(items)) throw new TypeError('Superset entries must be an array') + if (!Number.isInteger(idx) || !items[idx]) throw new RangeError('Superset entry index is invalid') + const next = items.map(e => ({ ...e })) + delete next[idx].sg + next.forEach((e, i) => { + if (e.sg && !(next[i - 1]?.sg === e.sg || next[i + 1]?.sg === e.sg)) delete e.sg + }) + return next +} + export function lastEntryFor(S, exId) { for (let i = S.workouts.length - 1; i >= 0; i--) { const en = S.workouts[i].entries.find(e => e.id === exId) diff --git a/frontend/src/lib/history.test.js b/frontend/src/lib/history.test.js index 3bc2218..68d1f0a 100644 --- a/frontend/src/lib/history.test.js +++ b/frontend/src/lib/history.test.js @@ -1,5 +1,5 @@ import { describe, it, expect } from 'vitest' -import { modeOf, isTimed, fmtSec, setLabel, defaultConfig, buildSets, freestyleConfig, exLine, workoutVolume, effortOf, stepEffort, capEffort, isBw, isPerSide, sideReps, repStep, cascadeWeight, insertWarmupRow, removeRowAt } from './history.js' +import { modeOf, isTimed, fmtSec, setLabel, defaultConfig, buildSets, freestyleConfig, exLine, workoutVolume, effortOf, stepEffort, capEffort, isBw, isPerSide, sideReps, repStep, cascadeWeight, insertWarmupRow, removeRowAt, pairAdjacent, unpairSuperset, supersetUnits } from './history.js' import { EXDB } from './exercises.js' // Real ids out of the shipped catalogue, so the body-part fallback is exercised for real. @@ -9,6 +9,49 @@ const CARDIO = EXDB.find(e => e.bp === 'cardio').id const LIFT = EXDB.find(e => e.bp !== 'cardio' && e.eq !== 'body weight').id const BW = EXDB.find(e => e.eq === 'body weight').id +describe('superset editing', () => { + it('pairs adjacent entries without mutating the source and keeps the display units contiguous', () => { + const entries = [{ id: 'a' }, { id: 'b' }, { id: 'c' }] + + const paired = pairAdjacent(entries, 1, 2, 'sg-new') + + expect(paired).toEqual([{ id: 'a' }, { id: 'b', sg: 'sg-new' }, { id: 'c', sg: 'sg-new' }]) + expect(entries).toEqual([{ id: 'a' }, { id: 'b' }, { id: 'c' }]) + expect(supersetUnits(paired)).toEqual([[0], [1, 2]]) + }) + + it('merges both contiguous groups when their boundary entries are paired', () => { + const entries = [ + { id: 'a', sg: 'left' }, { id: 'b', sg: 'left' }, + { id: 'c', sg: 'right' }, { id: 'd', sg: 'right' } + ] + + const merged = pairAdjacent(entries, 1, 2) + + expect(merged.map(e => e.sg)).toEqual(['left', 'left', 'left', 'left']) + expect(entries.map(e => e.sg)).toEqual(['left', 'left', 'right', 'right']) + }) + + it('unpairs one entry and removes sg values left without an adjacent partner', () => { + const entries = [ + { id: 'a', sg: 'group' }, { id: 'b', sg: 'group' }, { id: 'c', sg: 'group' }, + { id: 'd', sg: 'orphan' } + ] + + const unpaired = unpairSuperset(entries, 1) + + expect(unpaired).toEqual([{ id: 'a' }, { id: 'b' }, { id: 'c' }, { id: 'd' }]) + expect(entries.map(e => e.sg)).toEqual(['group', 'group', 'group', 'orphan']) + }) + + it('rejects a non-adjacent pairing request', () => { + const entries = [{ id: 'a' }, { id: 'b' }, { id: 'c' }] + + expect(() => pairAdjacent(entries, 0, 2, 'sg-invalid')).toThrow(/adjacent/) + expect(entries).toEqual([{ id: 'a' }, { id: 'b' }, { id: 'c' }]) + }) +}) + describe('modeOf', () => { it('falls back to the body part when a plan has no mode — every existing plan keeps working', () => { expect(modeOf({ id: CARDIO })).toBe('cardio') diff --git a/frontend/src/views/Workout.jsx b/frontend/src/views/Workout.jsx index 2b1a2fd..28cc9e6 100644 --- a/frontend/src/views/Workout.jsx +++ b/frontend/src/views/Workout.jsx @@ -3,7 +3,7 @@ import { useNavigate } from 'react-router-dom' import { useStore } from '../store/useStore.js' import { useUI } from '../store/useUI.js' import { exOr } from '../lib/exercises.js' -import { effectiveRoutine, lastEntryFor, bestWeightFor, buildSets, freestyleConfig, defaultConfig, setsDoneActive, supersetUnits, unitOf, setLabel, modeOf, isBw, isPerSide, sideReps, repStep, EFFORT, effortOf, stepEffort, capEffort, cascadeWeight, insertWarmupRow, removeRowAt } from '../lib/history.js' +import { effectiveRoutine, lastEntryFor, bestWeightFor, buildSets, freestyleConfig, defaultConfig, setsDoneActive, supersetUnits, unitOf, setLabel, modeOf, isBw, isPerSide, sideReps, repStep, EFFORT, effortOf, stepEffort, capEffort, cascadeWeight, insertWarmupRow, removeRowAt, pairAdjacent, unpairSuperset } from '../lib/history.js' import { fmtNum, fmtDate, todayISO, exCount, DAYN } from '../lib/format.js' import { beep, vibrate } from '../lib/sound.js' import { t } from '../lib/i18n.js' @@ -54,7 +54,7 @@ function Elapsed({ start }) { } /* ---------- one exercise block (reps: weight×reps · time: a held duration · cardio: duration+speed) ---------- */ -function ExerciseBlock({ entryIdx, compact, onToggle, onField, onAddSet, onRemoveSet, onAddWarmup, onRemoveSetAt, onStartTimed }) { +function ExerciseBlock({ entryIdx, compact, onToggle, onField, onAddSet, onRemoveSet, onAddWarmup, onRemoveSetAt, onStartTimed, onPairPrev, onPairNext }) { const S = useStore(s => s.S) const working = useUI(s => s.work) const entry = S.active.entries[entryIdx] @@ -114,6 +114,10 @@ function ExerciseBlock({ entryIdx, compact, onToggle, onField, onAddSet, onRemov
{ex.n}
+ {!compact && (onPairPrev || onPairNext) &&
+ {onPairPrev && } + {onPairNext && } +
}
{cardio && {t('Cardio')}} {/* You log the total; this is the split, so the set in front of you is unambiguous @@ -205,6 +209,14 @@ function ActiveWorkout() { e.sets = insertWarmupRow(e.sets, m, e.target || {}) }) const removeSetAt = (idx, i) => mutEntry(idx, e => { e.sets = removeRowAt(e.sets, i) }) + const pairAt = (first, second) => update(s => { + s.active.entries = pairAdjacent(s.active.entries, first, second) + }) + const unpairAt = idx => update(s => { + s.active.entries = unpairSuperset(s.active.entries, idx) + }) + const onPairPrev = !isSuperset && cur > 0 ? () => pairAt(cur - 1, cur) : null + const onPairNext = !isSuperset && cur < A.entries.length - 1 ? () => pairAt(cur, cur + 1) : null // A timed set is held, not typed. The work timer records what was actually held — an early // finish logs 0:38 of a 0:45 target rather than crediting the full prescription — and then @@ -286,7 +298,10 @@ function ActiveWorkout() {
{isSuperset ? t('Superset {0} / {1}', unitIdx + 1, units.length) : t('Exercise {0} / {1}', unitIdx + 1, units.length)}
{isSuperset ? (
-
{t('Superset · do these back-to-back, rest after both')}
+
+ {t('Superset · do these back-to-back, rest after both')} + +
{unit.map((idx, k) =>
{k > 0 &&
+
} )}
) : ( - toggle(cur, i)} onField={(i, f, v) => setField(cur, i, f, v)} onAddSet={() => addSet(cur)} onRemoveSet={() => removeSet(cur)} onAddWarmup={() => addWarmup(cur)} onRemoveSetAt={i => removeSetAt(cur, i)} onStartTimed={i => startTimed(cur, i)} /> + toggle(cur, i)} onField={(i, f, v) => setField(cur, i, f, v)} onAddSet={() => addSet(cur)} onRemoveSet={() => removeSet(cur)} onAddWarmup={() => addWarmup(cur)} onRemoveSetAt={i => removeSetAt(cur, i)} onStartTimed={i => startTimed(cur, i)} onPairPrev={onPairPrev} onPairNext={onPairNext} /> )} :
{t('Freestyle workout — add your first exercise.')}
}