Merge !91: feat(workout): colour-coded RIR/RPE quick picker

# Conflicts:
#	frontend/src/lib/pt-br-locale.test.js
#	frontend/src/locales/pt-BR.js
#	frontend/src/sheets.jsx
#	frontend/src/views/Workout.jsx
#	frontend/src/views/Workout.test.jsx
This commit is contained in:
Duarte Santos committed 2026-09-05 13:44:02 +02:00
commit 3773f65355
21 files changed
+502 -23

No files matched your search

+30 -6
View File
@@ -12,7 +12,8 @@ import { t, exerciseNameFor } from '../lib/i18n.js'
import { api } from '../lib/api.js'
import { insertionIndexAfterCurrentUnit, nextUnfinishedUnit, setProgressHighWater, supersetFlowStep, restAfterSet, restOnRecheck, restSecFor } from '../lib/supersetFlow.js'
import Media from '../components/Media.jsx'
import { startFlow, exercisePicker, exConfigSheet, exerciseDetailSheet, finishWorkout, workoutCompleteSheet, confirmSheet, exerciseNoteSheet, sessionNoteSheet, swapActiveWorkoutExercise, barWeightSheet, menuSheet } from '../sheets.jsx'
import { startFlow, exercisePicker, exConfigSheet, exerciseDetailSheet, finishWorkout, workoutCompleteSheet, confirmSheet, exerciseNoteSheet, sessionNoteSheet, swapActiveWorkoutExercise, barWeightSheet, menuSheet, effortPickerSheet } from '../sheets.jsx'
import { effortColor } from '../lib/effort.js'
import Icon from '../components/Icon.jsx'
import { Button, Check, NumberField } from '../components/ui.jsx'
import { nextPrescription, applyPrescription, defaultIncrement, weightIncrement, stepWeight } from '../lib/progression.js'
@@ -136,7 +137,9 @@ function ExerciseBlock({ entryIdx, compact, onToggle, onField, onAddSet, onRemov
// because an unlogged effort is not the same as 0 — RIR 0 says the set went to failure.
const kind = effortOf(S)
const eff = EFFORT[kind]
const col3 = mode === 'reps' && eff ? { ...eff, eff: kind, dec: true, opt: true, hd: t(eff.hd) } : null
// The effort column carries the scale key (`eff`) and its field name; unlike weight/reps it
// is not a stepper — it opens a colour-coded picker (see effortCell). `f` is s.rir or s.rpe.
const col3 = mode === 'reps' && eff ? { f: eff.f, eff: kind, hd: t(eff.hd) } : null
// The effort column walks its own scale — see stepEffort. Weight and reps step up from 0
// with no ceiling, as they always did.
const bump = (s, i, col, dir) => {
@@ -147,7 +150,6 @@ function ExerciseBlock({ entryIdx, compact, onToggle, onField, onAddSet, onRemov
// closure problem entirely and keeps every tap operating on the real current value.
const fresh = useStore.getState().S.active?.entries[entryIdx]?.sets[i]
const cur = fresh ? fresh[col.f] : s[col.f]
if (col.eff) return onField(i, col.f, stepEffort(col.eff, cur, dir))
if (mode === 'reps' && col.f === 'w') return onField(i, col.f, stepWeight(cur, col.step, dir))
onField(i, col.f, Math.max(0, Math.round(((cur || 0) + dir * col.step) * 100) / 100))
}
@@ -160,9 +162,8 @@ function ExerciseBlock({ entryIdx, compact, onToggle, onField, onAddSet, onRemov
const cell = (s, i, col, cls) => (
<div className={'stp ' + cls + (wc.steppers ? '' : ' plain')}>
{wc.steppers && <button aria-label="Decrease" onClick={() => bump(s, i, col, -1)}><Icon name="minus" /></button>}
{/* a typed effort is capped — there is no RPE 12, and 12 reps in reserve is a warm-up */}
<span className="val"><NumberField decimal={col.dec} nullable={col.opt} value={s[col.f] ?? ''}
onChange={v => onField(i, col.f, col.eff ? capEffort(col.eff, v) : v)} /></span>
onChange={v => onField(i, col.f, v)} /></span>
{wc.steppers && <button aria-label="Increase" onClick={() => bump(s, i, col, 1)}><Icon name="plus" /></button>}
</div>
)
@@ -207,6 +208,29 @@ function ExerciseBlock({ entryIdx, compact, onToggle, onField, onAddSet, onRemov
],
})
}
// The effort cell is a single button, not a stepper: it shows the rating in the profile's
// scale, tinted by how close to failure it was, and opens the picker on tap — to set a
// rating or change one. rirOf-via-effortColor keeps the colour identical whether the value
// is logged as RIR or RPE. With the +/- buttons switched off (Workout controls) a logged
// rating is just the tinted value, which still opens the picker.
const effortCell = (s, i, col) => {
const v = s[col.f] ?? null
const rir = col.eff === 'rpe' ? (v == null ? null : 10 - v) : v
const color = effortColor(rir)
const open = () => effortPickerSheet(col.eff, v, nv => onField(i, col.f, nv))
if (v == null) return (
<button className="effcell is-empty" aria-label={col.hd} onClick={open}>{col.hd}</button>
)
const step = dir => onField(i, col.f, stepEffort(col.eff, v, dir))
return (
<div className={'stp effcell-stp' + (wc.steppers ? '' : ' plain')}
style={color ? { color, borderColor: color, background: `color-mix(in srgb, ${color} 20%, var(--surface-2))` } : undefined}>
{wc.steppers && <button aria-label="Decrease" onClick={() => step(-1)}><Icon name="minus" /></button>}
<button className="val" aria-label={col.hd} onClick={open}>{fmtNum(v)}</button>
{wc.steppers && <button aria-label="Increase" onClick={() => step(1)}><Icon name="plus" /></button>}
</div>
)
}
// A smaller stepper for a drop's weight/reps or a burst's reps — editing what the plan (or a
// live "+ Drop"/"+ Burst" tap) already put on the row, not typing into a fresh field.
const miniStepper = (value, step, dec, onChange, snapWeightStep = false) => (
@@ -279,7 +303,7 @@ function ExerciseBlock({ entryIdx, compact, onToggle, onField, onAddSet, onRemov
<button type="button" className="n" aria-label={t('Set {0}', phaseNum)} title={t('More')} onClick={() => openSetMenu(s, i)}>{phaseNum}</button>
{cell(s, i, col1, 'w')}
{col2 && cell(s, i, col2, 'r')}
{col3 && cell(s, i, col3, 'eff')}
{col3 && effortCell(s, i, col3)}
{/* A timed set is started, not typed: the timer counts the hold down and checks the
set off itself. The checkbox stays for anyone who timed it on their own watch. */}
{timed && <button className="setgo" aria-label={t('Start set')} disabled={s.done || !!working}
+127
View File
@@ -22,6 +22,7 @@ const mocks = vi.hoisted(() => {
scrollCalls: [],
swapActiveWorkoutExercise: vi.fn(),
menuSheet: vi.fn(),
effortPickerSheet: vi.fn(),
}
state.stopRest = vi.fn(() => { state.timer = null })
state.stopWork = vi.fn(() => { state.work = null })
@@ -69,10 +70,12 @@ vi.mock('../sheets.jsx', () => ({
confirmSheet: mocks.confirmSheet,
swapActiveWorkoutExercise: mocks.swapActiveWorkoutExercise,
menuSheet: mocks.menuSheet,
barWeightSheet: vi.fn(),
// Both note sheets belong here even though the tests never open one: Workout.jsx reads
// sessionNoteSheet during render, so a missing export is a render crash, not a no-op.
exerciseNoteSheet: vi.fn(),
sessionNoteSheet: vi.fn(),
effortPickerSheet: mocks.effortPickerSheet,
}))
vi.mock('../components/Media.jsx', () => ({ default: () => null }))
// api.js reads navigator.userAgent at module scope. This file installs its own DOM inside the
@@ -693,6 +696,130 @@ describe('progression guidance', () => {
})
})
describe('effort cell (colour-coded RIR/RPE quick picker)', () => {
// A rep exercise whose sets can carry an effort rating. `rir` per set is optional — an
// unrated set simply omits the key, which is what the empty cell has to represent.
const effExercise = (rirs) => ({
id: 'plain-bench',
target: { mode: 'reps', reps: 5, weight: 60, bodyweight: false },
sets: rirs.map(rir => ({ w: 60, r: 5, done: false, ...(rir == null ? {} : { rir }) })),
})
// A cell is one of two shapes: an empty `.effcell` button (label, opens picker) or, once a
// rating is logged, a `.effcell-stp` −/value/+ group. `.effcell-list` returns the outer
// element of each (carrying the colour on the logged one); `effCells` normalises them to the
// value-bearing, picker-opening element so the existing assertions read the same either way:
// for the empty button that is the button itself, for the stepper it is the `.val` button.
const effCellList = () => [...container.querySelectorAll('.effcell,.effcell-stp')]
const effCells = () => effCellList().map(el =>
el.classList.contains('effcell-stp') ? el.querySelector('.val') : el)
async function mountEffort(rirs, scale = 'rir') {
mocks.S = workout([effExercise(rirs)])
mocks.S.effort = scale
installDom()
await act(async () => { root.render(React.createElement(Workout)) })
}
it('shows the effort column only when the profile logs a scale', async () => {
await mount([exercise('plain-bench', [false])]) // effort: 'none' from workout()
expect(effCells()).toHaveLength(0)
await unmount()
await mountEffort([null])
expect(effCells()).toHaveLength(1)
})
it('labels an unrated cell with the scale name, not a value or a colour', async () => {
await mountEffort([null], 'rir')
const cell = effCells()[0]
expect(cell.textContent).toBe('RIR')
expect(cell.className).toContain('is-empty')
// no rating means no inline colour on the button
expect(cell.getAttribute('style') || '').not.toMatch(/color/)
})
it('uses the profile scale for the empty label — RPE profile reads "RPE"', async () => {
await mountEffort([null], 'rpe')
expect(effCells()[0].textContent).toBe('RPE')
})
it('shows a logged rating as its number, tinted by the band it falls in', async () => {
await mountEffort([0, 2, null])
const cells = effCells()
const outer = effCellList()
expect(cells[0].textContent).toBe('0')
expect(outer[0].className).toContain('effcell-stp') // logged: the stepper, not the label
// 0 RIR = to failure = purple; 2 RIR = yellow (the colours effortColor assigns) — the
// colour rides the outer stepper (border + tinted background), not the inner value button
expect(outer[0].getAttribute('style')).toContain('--purple')
expect(cells[1].textContent).toBe('2')
expect(outer[1].getAttribute('style')).toContain('--yellow')
expect(cells[2].textContent).toBe('RIR') // the unrated one stays a label
expect(outer[2].className).toContain('is-empty')
})
it('displays a logged value on the profile scale — RIR 2 reads as RPE 8', async () => {
// the set is stored on whatever scale the profile logs; an RPE profile stores s.rpe
mocks.S = workout([{
id: 'plain-bench',
target: { mode: 'reps', reps: 5, weight: 60, bodyweight: false },
sets: [{ w: 60, r: 5, done: false, rpe: 8 }],
}])
mocks.S.effort = 'rpe'
installDom()
await act(async () => { root.render(React.createElement(Workout)) })
expect(effCells()[0].textContent).toBe('8')
// RPE 8 == RIR 2 == yellow: the colour is the effort, independent of the scale shown
expect(effCellList()[0].getAttribute('style')).toContain('--yellow')
})
it('opens the picker for the set on tap, passing scale, current value and a writer', async () => {
await mountEffort([2])
await act(async () => {
effCells()[0].dispatchEvent(new dom.Event('click', { bubbles: true }))
})
expect(mocks.effortPickerSheet).toHaveBeenCalledOnce()
const [scale, value, onPick] = mocks.effortPickerSheet.mock.calls[0]
expect(scale).toBe('rir')
expect(value).toBe(2)
// the writer stores the chosen value back on the set, and null clears the key
onPick(1)
expect(mocks.S.active.entries[0].sets[0].rir).toBe(1)
onPick(null)
expect('rir' in mocks.S.active.entries[0].sets[0]).toBe(false)
})
// The mock store is a plain snapshot with no subscription, so a click updates mocks.S but
// does not re-render on its own; each step is checked from its own mount rather than chained.
const clickStep = async label => {
await act(async () => {
effCellList()[0].querySelector(`button[aria-label="${label}"]`)
.dispatchEvent(new dom.Event('click', { bubbles: true }))
})
}
it('steps a logged rating up 0.5 on the scale with the + button, not through the picker', async () => {
await mountEffort([2])
expect(effCellList()[0].querySelectorAll('button[aria-label="Increase"],button[aria-label="Decrease"]')).toHaveLength(2)
await clickStep('Increase')
expect(mocks.S.active.entries[0].sets[0].rir).toBe(2.5)
expect(mocks.effortPickerSheet).not.toHaveBeenCalled()
})
it('steps a logged rating down 0.5 with the − button', async () => {
await mountEffort([2])
await clickStep('Decrease')
expect(mocks.S.active.entries[0].sets[0].rir).toBe(1.5)
})
it('clears the rating when stepped down off the floor', async () => {
// RIR 0 is the bottom of the scale — one more − is a mistap-undo, dropping the key rather
// than sticking at 0 (which reads as "went to failure")
await mountEffort([0])
await clickStep('Decrease')
expect('rir' in mocks.S.active.entries[0].sets[0]).toBe(false)
})
})
describe('superset flow survives an exercise being removed mid-session', () => {
// removeActiveExercise splices A.entries, shifting every index above the removal down.
// The high-water marks are index-keyed, so without re-baselining the shifted exercise