feat(workout): add active unit move controls

This commit is contained in:
Space_Hermes committed 2026-08-27 12:32:52 +02:00
1 parent 5314b342ff
commit d5e40db774
17 files changed
+226 -2

No files matched your search

+20
View File
@@ -16,6 +16,7 @@ import { Button, Check, NumberField } from '../components/ui.jsx'
import { nextPrescription, applyPrescription, defaultIncrement } from '../lib/progression.js'
import { glyphOf } from '../lib/glyphs.js'
import { isWarmupRow, isDropSet, isRestPauseSet, dropsOf, clustersOf, addDrop, addCluster, removeDropAt, removeClusterAt, setDropAt, setClusterAt, nextDropWeight, nextBurstReps } from '../lib/workout-model.js'
import { canMoveActiveWorkoutUnit, moveActiveWorkoutUnit } from '../lib/active-workout-order.js'
/* ---------- start chooser (no active workout) ---------- */
function StartChooser() {
@@ -334,6 +335,18 @@ function ActiveWorkout() {
})
const onPairPrev = !isSuperset && cur > 0 ? () => pairAt(cur - 1, cur) : null
const onPairNext = !isSuperset && cur < A.entries.length - 1 ? () => pairAt(cur, cur + 1) : null
const moveCurrentUnit = direction => {
const ui = useUI.getState()
const active = useStore.getState().S.active
if (ui.work || !canMoveActiveWorkoutUnit(active, active?.cur, direction)) return
// Invalidate an old timed callback before indexes shift, and cancel a displaced rest timer.
ui.stopWork()
ui.stopRest()
update(s => {
const moved = moveActiveWorkoutUnit(s.active, s.active?.cur, direction)
if (moved) progressHighWater.current = moved.indices.map(index => progressHighWater.current[index])
}, true)
}
// Remove a whole exercise from the session. The confirmation always asks first; in a
// superset it asks WHICH exercise of the group to remove.
@@ -526,6 +539,13 @@ function ActiveWorkout() {
}), null, routine, seed)
})} icon="plus">{t('Add exercise')}</Button>
{A.entries.length > 0 && <>
<div style={{ height: 6 }} />
<div className="row">
<Button size="sm" icon="chevronUp" aria-label={t('Move up')}
disabled={!!work || !canMoveActiveWorkoutUnit(A, cur, -1)} onClick={() => moveCurrentUnit(-1)}>{t('Move up')}</Button>
<Button size="sm" trailingIcon="chevronDown" aria-label={t('Move down')}
disabled={!!work || !canMoveActiveWorkoutUnit(A, cur, 1)} onClick={() => moveCurrentUnit(1)}>{t('Move down')}</Button>
</div>
<div style={{ height: 6 }} />
<div style={{ display: 'flex', justifyContent: 'center' }}>
<Button size="sm" icon="minus" style={{ color: 'var(--red)' }} disabled={!!work} onClick={removeExerciseSheet}>{t('Remove exercise')}</Button>
+63 -1
View File
@@ -7,8 +7,10 @@ import Workout from './Workout.jsx'
const mocks = vi.hoisted(() => {
const state = {
S: null,
work: null,
startRest: vi.fn(),
stopRest: vi.fn(),
stopWork: vi.fn(),
topWeightSheet: vi.fn(),
}
state.storeSnapshot = () => ({
@@ -17,9 +19,10 @@ const mocks = vi.hoisted(() => {
update: mut => mut(state.S),
})
state.uiSnapshot = () => ({
work: null,
work: state.work,
startRest: state.startRest,
stopRest: state.stopRest,
stopWork: state.stopWork,
startWork: vi.fn(),
toast: vi.fn(),
})
@@ -114,6 +117,7 @@ async function toggleSet(index) {
beforeEach(() => {
vi.clearAllMocks()
mocks.work = null
})
afterEach(async () => {
@@ -187,3 +191,61 @@ describe('superset flow survives an exercise being removed mid-session', () => {
expect(mocks.startRest).toHaveBeenCalledWith(90)
})
})
describe('active workout whole-unit move controls', () => {
const action = label => container.querySelector(`button[aria-label="${label}"]`)
it('shows labelled controls and moves the selected standalone exercise one unit', async () => {
const selected = exercise('duplicate', [false], {
occurrenceId: 'duplicate#2',
target: { mode: 'reps', reps: 7, weight: 82.5, notes: 'Keep this target' },
sets: [{ w: 77.5, r: 6, done: true, rir: 2 }],
})
await mount([
exercise('duplicate', [false], { occurrenceId: 'duplicate#1' }),
exercise('middle', [false]),
selected,
], 2)
expect(action('Move up')?.textContent.trim()).toBe('Move up')
expect(action('Move down')?.textContent.trim()).toBe('Move down')
await act(async () => { action('Move up').dispatchEvent(new dom.Event('click', { bubbles: true })) })
expect(mocks.S.active.entries.map(entry => entry.occurrenceId || entry.id)).toEqual(['duplicate#1', 'duplicate#2', 'middle'])
expect(mocks.S.active.entries[1]).toBe(selected)
expect(mocks.S.active.entries[1].target).toEqual({ mode: 'reps', reps: 7, weight: 82.5, notes: 'Keep this target' })
expect(mocks.S.active.entries[1].sets).toEqual([{ w: 77.5, r: 6, done: true, rir: 2 }])
expect(mocks.S.active.cur).toBe(1)
expect(mocks.stopWork).toHaveBeenCalledOnce()
expect(mocks.stopRest).toHaveBeenCalledOnce()
})
it('moves the selected contiguous group as one unit without changing its metadata', async () => {
const first = exercise('group-a', [false], { sg: 'pair', occurrenceId: 'group-a#1' })
const selected = exercise('group-b', [true], { sg: 'pair', occurrenceId: 'group-b#1' })
const groupMeta = { pair: { kind: 'complex', label: 'Carry pair', cues: 'Stay braced.' } }
await mount([
exercise('before', [false]),
first,
selected,
exercise('after', [false]),
], 2)
mocks.S.active.groupMeta = groupMeta
await act(async () => { action('Move up').dispatchEvent(new dom.Event('click', { bubbles: true })) })
expect(mocks.S.active.entries.map(entry => entry.id)).toEqual(['group-a', 'group-b', 'before', 'after'])
expect(mocks.S.active.entries.slice(0, 2)).toEqual([first, selected])
expect(mocks.S.active.entries.slice(0, 2).map(entry => entry.sg)).toEqual(['pair', 'pair'])
expect(mocks.S.active.groupMeta).toBe(groupMeta)
expect(mocks.S.active.entries[mocks.S.active.cur]).toBe(selected)
})
it('disables both moves while a work timer can still write by index', async () => {
mocks.work = { left: 5, total: 5, endsAt: Date.now() + 5000 }
await mount([exercise('first', [false]), exercise('second', [false])], 1)
expect(action('Move up')?.disabled).toBe(true)
expect(action('Move down')?.disabled).toBe(true)
})
})