From d5e40db774f3fbe04026c3cc9334935692098517 Mon Sep 17 00:00:00 2001 From: Space_Hermes Date: Thu, 27 Aug 2026 12:32:52 +0200 Subject: [PATCH] feat(workout): add active unit move controls --- frontend/src/lib/active-workout-order.js | 31 +++++++ frontend/src/lib/active-workout-order.test.js | 87 +++++++++++++++++++ frontend/src/lib/pt-br-locale.test.js | 2 +- frontend/src/locales/de.js | 2 + frontend/src/locales/es.js | 2 + frontend/src/locales/fr.js | 2 + frontend/src/locales/hi.js | 2 + frontend/src/locales/it.js | 2 + frontend/src/locales/ko.js | 2 + frontend/src/locales/pl.js | 2 + frontend/src/locales/pt-BR.js | 2 + frontend/src/locales/pt.js | 2 + frontend/src/locales/ru.js | 2 + frontend/src/locales/tr.js | 2 + frontend/src/locales/zh.js | 2 + frontend/src/views/Workout.jsx | 20 +++++ frontend/src/views/Workout.test.jsx | 64 +++++++++++++- 17 files changed, 226 insertions(+), 2 deletions(-) create mode 100644 frontend/src/lib/active-workout-order.js create mode 100644 frontend/src/lib/active-workout-order.test.js diff --git a/frontend/src/lib/active-workout-order.js b/frontend/src/lib/active-workout-order.js new file mode 100644 index 0000000..57a0089 --- /dev/null +++ b/frontend/src/lib/active-workout-order.js @@ -0,0 +1,31 @@ +import { supersetUnits } from './history.js' + +function moveTarget(active, index, direction) { + if (!active || !Array.isArray(active.entries) || (direction !== -1 && direction !== 1)) return null + const units = supersetUnits(active.entries) + const source = units.findIndex(unit => unit.includes(index)) + const target = source + direction + if (source < 0 || target < 0 || target >= units.length) return null + return { units, source, target } +} + +export function canMoveActiveWorkoutUnit(active, index, direction) { + return moveTarget(active, index, direction) !== null +} + +export function moveActiveWorkoutUnit(active, index, direction) { + const move = moveTarget(active, index, direction) + if (!move) return null + + const selected = active.entries[index] + const reorderedUnits = [...move.units] + const sourceUnit = reorderedUnits[move.source] + reorderedUnits[move.source] = reorderedUnits[move.target] + reorderedUnits[move.target] = sourceUnit + const indices = reorderedUnits.flat() + const reorderedEntries = indices.map(entryIndex => active.entries[entryIndex]) + + active.entries.splice(0, active.entries.length, ...reorderedEntries) + active.cur = active.entries.indexOf(selected) + return { indices } +} diff --git a/frontend/src/lib/active-workout-order.test.js b/frontend/src/lib/active-workout-order.test.js new file mode 100644 index 0000000..64cc286 --- /dev/null +++ b/frontend/src/lib/active-workout-order.test.js @@ -0,0 +1,87 @@ +import { describe, expect, it } from 'vitest' +import { canMoveActiveWorkoutUnit, moveActiveWorkoutUnit } from './active-workout-order.js' +import { LANGS } from './i18n-core.js' +import { PT_BR_OVERRIDES } from '../locales/pt-BR.js' + +const entry = (id, extra = {}) => ({ + id, + target: { sets: 1, reps: 5 }, + sets: [{ w: 0, r: 5, done: false }], + ...extra, +}) + +describe('active workout whole-unit order', () => { + it('moves one standalone occurrence without conflating duplicate exercise ids', () => { + const duplicateA = entry('duplicate', { occurrenceId: 'duplicate#1' }) + const selected = entry('duplicate', { + occurrenceId: 'duplicate#2', + target: { sets: 2, reps: 7, weight: 82.5, notes: 'Keep this target' }, + sets: [{ w: 77.5, r: 6, done: true, rir: 2 }], + }) + const active = { cur: 2, entries: [duplicateA, entry('middle'), selected] } + + expect(moveActiveWorkoutUnit(active, active.cur, -1)?.indices).toEqual([0, 2, 1]) + expect(active.entries.map(item => item.occurrenceId || item.id)).toEqual(['duplicate#1', 'duplicate#2', 'middle']) + expect(active.entries[0]).toBe(duplicateA) + expect(active.entries[1]).toBe(selected) + expect(active.entries[1].target).toBe(selected.target) + expect(active.entries[1].sets).toBe(selected.sets) + expect(active.cur).toBe(1) + }) + + it('moves a complete contiguous group one unit and preserves the selected member identity', () => { + const first = entry('group-a', { sg: 'pair', occurrenceId: 'group-a#1' }) + const selected = entry('group-b', { sg: 'pair', occurrenceId: 'group-b#1' }) + const groupMeta = { pair: { kind: 'complex', label: 'Carry pair', cues: 'Stay braced.' } } + const active = { cur: 2, entries: [entry('before'), first, selected, entry('after')], groupMeta } + + expect(moveActiveWorkoutUnit(active, active.cur, -1)?.indices).toEqual([1, 2, 0, 3]) + expect(active.entries.map(item => item.id)).toEqual(['group-a', 'group-b', 'before', 'after']) + expect(active.entries.slice(0, 2)).toEqual([first, selected]) + expect(active.entries.map(item => item.sg)).toEqual(['pair', 'pair', undefined, undefined]) + expect(active.groupMeta).toBe(groupMeta) + expect(active.entries[active.cur]).toBe(selected) + }) + + it('moves a group down by exactly one neighbouring unit', () => { + const first = entry('group-a', { sg: 'pair' }) + const selected = entry('group-b', { sg: 'pair' }) + const active = { cur: 1, entries: [first, selected, entry('middle'), entry('last')] } + + expect(moveActiveWorkoutUnit(active, active.cur, 1)?.indices).toEqual([2, 0, 1, 3]) + expect(active.entries.map(item => item.id)).toEqual(['middle', 'group-a', 'group-b', 'last']) + expect(active.entries[active.cur]).toBe(selected) + }) + + it('rejects boundaries and invalid directions without mutating the active workout', () => { + const active = { cur: 0, entries: [entry('first'), entry('last')] } + const entries = [...active.entries] + + expect(canMoveActiveWorkoutUnit(active, 0, -1)).toBe(false) + expect(canMoveActiveWorkoutUnit(active, 1, 1)).toBe(false) + expect(moveActiveWorkoutUnit(active, 0, 0)).toBeNull() + expect(moveActiveWorkoutUnit(active, 0, -1)).toBeNull() + expect(active.entries).toEqual(entries) + expect(active.cur).toBe(0) + }) +}) + +describe('active workout move locale coverage', () => { + const packs = import.meta.glob('../locales/*.js', { eager: true, import: 'default' }) + const localeCodes = Object.keys(LANGS).filter(code => code !== 'en') + + it('defines both visible move labels in every current locale pack', () => { + expect(Object.keys(packs)).toHaveLength(localeCodes.length) + for (const code of localeCodes) { + const pack = packs[`../locales/${code}.js`] + expect(pack, `${code} locale pack is missing`).toBeTruthy() + for (const key of ['Move up', 'Move down']) { + expect(Object.hasOwn(pack, key), `${code} is missing ${key}`).toBe(true) + expect(pack[key], `${code} has a blank ${key}`).toEqual(expect.any(String)) + expect(pack[key].trim(), `${code} has a blank ${key}`).not.toBe('') + } + } + expect(Object.hasOwn(PT_BR_OVERRIDES, 'Move up')).toBe(true) + expect(Object.hasOwn(PT_BR_OVERRIDES, 'Move down')).toBe(true) + }) +}) diff --git a/frontend/src/lib/pt-br-locale.test.js b/frontend/src/lib/pt-br-locale.test.js index 91a4a63..7e4beb1 100644 --- a/frontend/src/lib/pt-br-locale.test.js +++ b/frontend/src/lib/pt-br-locale.test.js @@ -27,7 +27,7 @@ describe('Brazilian Portuguese locale', () => { .sort(byCodeUnit) const fingerprint = createHash('sha256').update(JSON.stringify(inherited)).digest('hex') - expect(Object.keys(PT_BR_OVERRIDES)).toHaveLength(291) + expect(Object.keys(PT_BR_OVERRIDES)).toHaveLength(293) expect(inherited).toHaveLength(449) // If this fails, review the changed keys and wording before accepting a new hash. From // frontend/: node scripts/pt-br-inheritance-fingerprint.mjs --list diff --git a/frontend/src/locales/de.js b/frontend/src/locales/de.js index 8c91155..7a68ad0 100644 --- a/frontend/src/locales/de.js +++ b/frontend/src/locales/de.js @@ -36,6 +36,8 @@ export default { 'rescheduled': 'verschoben', 'Prev': 'Zurück', 'Next': 'Weiter', + 'Move up': 'Nach oben', + 'Move down': 'Nach unten', // --- days & months --- 'Sunday': 'Sonntag', 'Monday': 'Montag', 'Tuesday': 'Dienstag', 'Wednesday': 'Mittwoch', 'Thursday': 'Donnerstag', 'Friday': 'Freitag', 'Saturday': 'Samstag', diff --git a/frontend/src/locales/es.js b/frontend/src/locales/es.js index b1ea117..b784179 100644 --- a/frontend/src/locales/es.js +++ b/frontend/src/locales/es.js @@ -35,6 +35,8 @@ export default { 'rescheduled': 'reprogramado', 'Prev': 'Anterior', 'Next': 'Siguiente', + 'Move up': 'Subir', + 'Move down': 'Bajar', 'Sunday': 'Domingo', 'Monday': 'Lunes', 'Tuesday': 'Martes', 'Wednesday': 'Miércoles', 'Thursday': 'Jueves', 'Friday': 'Viernes', 'Saturday': 'Sábado', 'Su': 'Do', 'Mo': 'Lu', 'Tu': 'Ma', 'We': 'Mi', 'Th': 'Ju', 'Fr': 'Vi', 'Sa': 'Sá', diff --git a/frontend/src/locales/fr.js b/frontend/src/locales/fr.js index 387bf25..b48e3e2 100644 --- a/frontend/src/locales/fr.js +++ b/frontend/src/locales/fr.js @@ -35,6 +35,8 @@ export default { 'rescheduled': 'reprogrammé', 'Prev': 'Préc.', 'Next': 'Suiv.', + 'Move up': 'Monter', + 'Move down': 'Descendre', 'Sunday': 'Dimanche', 'Monday': 'Lundi', 'Tuesday': 'Mardi', 'Wednesday': 'Mercredi', 'Thursday': 'Jeudi', 'Friday': 'Vendredi', 'Saturday': 'Samedi', 'Su': 'Di', 'Mo': 'Lu', 'Tu': 'Ma', 'We': 'Me', 'Th': 'Je', 'Fr': 'Ve', 'Sa': 'Sa', diff --git a/frontend/src/locales/hi.js b/frontend/src/locales/hi.js index 09038df..11ca1a4 100644 --- a/frontend/src/locales/hi.js +++ b/frontend/src/locales/hi.js @@ -35,6 +35,8 @@ export default { 'rescheduled': 'बदला गया', 'Prev': 'पिछला', 'Next': 'अगला', + 'Move up': 'ऊपर ले जाएँ', + 'Move down': 'नीचे ले जाएँ', 'Sunday': 'रविवार', 'Monday': 'सोमवार', 'Tuesday': 'मंगलवार', 'Wednesday': 'बुधवार', 'Thursday': 'गुरुवार', 'Friday': 'शुक्रवार', 'Saturday': 'शनिवार', 'Su': 'र', 'Mo': 'सो', 'Tu': 'मं', 'We': 'बु', 'Th': 'गु', 'Fr': 'शु', 'Sa': 'श', diff --git a/frontend/src/locales/it.js b/frontend/src/locales/it.js index 1c53c34..d50843e 100644 --- a/frontend/src/locales/it.js +++ b/frontend/src/locales/it.js @@ -35,6 +35,8 @@ export default { 'rescheduled': 'riprogrammato', 'Prev': 'Prec.', 'Next': 'Succ.', + 'Move up': 'Sposta su', + 'Move down': 'Sposta giù', 'Sunday': 'Domenica', 'Monday': 'Lunedì', 'Tuesday': 'Martedì', 'Wednesday': 'Mercoledì', 'Thursday': 'Giovedì', 'Friday': 'Venerdì', 'Saturday': 'Sabato', 'Su': 'Do', 'Mo': 'Lu', 'Tu': 'Ma', 'We': 'Me', 'Th': 'Gi', 'Fr': 'Ve', 'Sa': 'Sa', diff --git a/frontend/src/locales/ko.js b/frontend/src/locales/ko.js index 170f6d3..addeb66 100644 --- a/frontend/src/locales/ko.js +++ b/frontend/src/locales/ko.js @@ -35,6 +35,8 @@ export default { 'rescheduled': '변경됨', 'Prev': '이전', 'Next': '다음', + 'Move up': '위로 이동', + 'Move down': '아래로 이동', 'Sunday': '일요일', 'Monday': '월요일', 'Tuesday': '화요일', 'Wednesday': '수요일', 'Thursday': '목요일', 'Friday': '금요일', 'Saturday': '토요일', 'Su': '일', 'Mo': '월', 'Tu': '화', 'We': '수', 'Th': '목', 'Fr': '금', 'Sa': '토', diff --git a/frontend/src/locales/pl.js b/frontend/src/locales/pl.js index b914fbc..dc4bc14 100644 --- a/frontend/src/locales/pl.js +++ b/frontend/src/locales/pl.js @@ -35,6 +35,8 @@ export default { 'rescheduled': 'przełożono', 'Prev': 'Wstecz', 'Next': 'Dalej', + 'Move up': 'Przenieś w górę', + 'Move down': 'Przenieś w dół', 'Sunday': 'Niedziela', 'Monday': 'Poniedziałek', 'Tuesday': 'Wtorek', 'Wednesday': 'Środa', 'Thursday': 'Czwartek', 'Friday': 'Piątek', 'Saturday': 'Sobota', 'Su': 'Nd', 'Mo': 'Pn', 'Tu': 'Wt', 'We': 'Śr', 'Th': 'Cz', 'Fr': 'Pt', 'Sa': 'So', diff --git a/frontend/src/locales/pt-BR.js b/frontend/src/locales/pt-BR.js index 11022ba..c76f829 100644 --- a/frontend/src/locales/pt-BR.js +++ b/frontend/src/locales/pt-BR.js @@ -180,6 +180,8 @@ export const PT_BR_OVERRIDES = { // Regional wording inherited from pt-PT would otherwise leak into pt-BR. 'Next': 'Próximo', + 'Move up': 'Mover para cima', + 'Move down': 'Mover para baixo', '{0} to gain': '{0} para ganhar', '{0} to lose': '{0} para perder', '{0} set still unchecked. Finish the workout now?': 'Falta {0} série sem marcar. Finalizar o treino agora?', diff --git a/frontend/src/locales/pt.js b/frontend/src/locales/pt.js index d14ccb1..00000da 100644 --- a/frontend/src/locales/pt.js +++ b/frontend/src/locales/pt.js @@ -35,6 +35,8 @@ export default { 'rescheduled': 'reagendado', 'Prev': 'Anterior', 'Next': 'Seguinte', + 'Move up': 'Mover para cima', + 'Move down': 'Mover para baixo', 'Sunday': 'Domingo', 'Monday': 'Segunda-feira', 'Tuesday': 'Terça-feira', 'Wednesday': 'Quarta-feira', 'Thursday': 'Quinta-feira', 'Friday': 'Sexta-feira', 'Saturday': 'Sábado', 'Su': 'Do', 'Mo': 'Se', 'Tu': 'Te', 'We': 'Qa', 'Th': 'Qi', 'Fr': 'Sx', 'Sa': 'Sá', diff --git a/frontend/src/locales/ru.js b/frontend/src/locales/ru.js index 1e260d1..02674a9 100644 --- a/frontend/src/locales/ru.js +++ b/frontend/src/locales/ru.js @@ -35,6 +35,8 @@ export default { 'rescheduled': 'перенесено', 'Prev': 'Назад', 'Next': 'Далее', + 'Move up': 'Переместить вверх', + 'Move down': 'Переместить вниз', 'Sunday': 'Воскресенье', 'Monday': 'Понедельник', 'Tuesday': 'Вторник', 'Wednesday': 'Среда', 'Thursday': 'Четверг', 'Friday': 'Пятница', 'Saturday': 'Суббота', 'Su': 'Вс', 'Mo': 'Пн', 'Tu': 'Вт', 'We': 'Ср', 'Th': 'Чт', 'Fr': 'Пт', 'Sa': 'Сб', diff --git a/frontend/src/locales/tr.js b/frontend/src/locales/tr.js index 79b3125..3c990e5 100644 --- a/frontend/src/locales/tr.js +++ b/frontend/src/locales/tr.js @@ -35,6 +35,8 @@ export default { 'rescheduled': 'ertelendi', 'Prev': 'Önceki', 'Next': 'Sonraki', + 'Move up': 'Yukarı taşı', + 'Move down': 'Aşağı taşı', 'Sunday': 'Pazar', 'Monday': 'Pazartesi', 'Tuesday': 'Salı', 'Wednesday': 'Çarşamba', 'Thursday': 'Perşembe', 'Friday': 'Cuma', 'Saturday': 'Cumartesi', 'Su': 'Pz', 'Mo': 'Pt', 'Tu': 'Sa', 'We': 'Ça', 'Th': 'Pe', 'Fr': 'Cu', 'Sa': 'Ct', diff --git a/frontend/src/locales/zh.js b/frontend/src/locales/zh.js index 533434d..5cbc9b0 100644 --- a/frontend/src/locales/zh.js +++ b/frontend/src/locales/zh.js @@ -35,6 +35,8 @@ export default { 'rescheduled': '已改期', 'Prev': '上一个', 'Next': '下一个', + 'Move up': '上移', + 'Move down': '下移', 'Sunday': '星期日', 'Monday': '星期一', 'Tuesday': '星期二', 'Wednesday': '星期三', 'Thursday': '星期四', 'Friday': '星期五', 'Saturday': '星期六', 'Su': '日', 'Mo': '一', 'Tu': '二', 'We': '三', 'Th': '四', 'Fr': '五', 'Sa': '六', diff --git a/frontend/src/views/Workout.jsx b/frontend/src/views/Workout.jsx index d0a6279..0074f63 100644 --- a/frontend/src/views/Workout.jsx +++ b/frontend/src/views/Workout.jsx @@ -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')} {A.entries.length > 0 && <> +
+
+ + +
diff --git a/frontend/src/views/Workout.test.jsx b/frontend/src/views/Workout.test.jsx index 0564b8a..9278c6a 100644 --- a/frontend/src/views/Workout.test.jsx +++ b/frontend/src/views/Workout.test.jsx @@ -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) + }) +})