feat: create and remove session supersets
This commit is contained in:
1 parent
35181af2aa
commit
b29a3f6bb0
4 files changed
+118
-5
No files matched your search
@@ -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)
|
||||
|
||||
@@ -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')
|
||||
|
||||
@@ -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
|
||||
<div style={{ fontSize: compact ? 17 : 20, fontWeight: 600, letterSpacing: '-.02em', textTransform: 'capitalize', lineHeight: 1.2 }}>{ex.n}</div>
|
||||
<button className="iconbtn" aria-label={t('Details')} onClick={() => exerciseDetailSheet(ex)}><Icon name="info" /></button>
|
||||
</div>
|
||||
{!compact && (onPairPrev || onPairNext) && <div className="row" style={{ gap: 6, flexWrap: 'wrap', marginBottom: 8 }}>
|
||||
{onPairPrev && <Button size="xs" variant="tinted" icon="link" title={t('Make superset with previous')} onClick={onPairPrev}>{t('Make superset with previous')}</Button>}
|
||||
{onPairNext && <Button size="xs" variant="tinted" icon="link" title={t('Make superset with next')} onClick={onPairNext}>{t('Make superset with next')}</Button>}
|
||||
</div>}
|
||||
<div className="row" style={{ gap: 6, flexWrap: 'wrap', marginBottom: 8 }}>
|
||||
{cardio && <span className="tag acc"><Icon name="figureRun" />{t('Cardio')}</span>}
|
||||
{/* 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() {
|
||||
<div className="muted small" style={{ marginBottom: 6 }}>{isSuperset ? t('Superset {0} / {1}', unitIdx + 1, units.length) : t('Exercise {0} / {1}', unitIdx + 1, units.length)}</div>
|
||||
{isSuperset ? (
|
||||
<div className="ss-card">
|
||||
<div className="ss-hd"><Icon name="link" />{t('Superset · do these back-to-back, rest after both')}</div>
|
||||
<div className="ss-hd" style={{ justifyContent: 'space-between' }}>
|
||||
<span className="row" style={{ gap: 5 }}><Icon name="link" />{t('Superset · do these back-to-back, rest after both')}</span>
|
||||
<Button size="xs" variant="ghost" icon="link" title={t('Unpair')} onClick={() => unpairAt(cur)}>{t('Unpair')}</Button>
|
||||
</div>
|
||||
{unit.map((idx, k) => <div key={idx} className="ss-ex">
|
||||
{k > 0 && <div className="ss-amp">+</div>}
|
||||
<ExerciseBlock entryIdx={idx} compact
|
||||
@@ -294,7 +309,7 @@ function ActiveWorkout() {
|
||||
</div>)}
|
||||
</div>
|
||||
) : (
|
||||
<ExerciseBlock entryIdx={cur} onToggle={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)} />
|
||||
<ExerciseBlock entryIdx={cur} onToggle={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} />
|
||||
)}
|
||||
</> : <div className="empty"><div className="ico"><Icon name="shuffle" /></div>{t('Freestyle workout — add your first exercise.')}</div>}
|
||||
|
||||
|
||||
Reference in new issue
Block a user