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

This commit is contained in:
Sebastian G committed 2026-09-02 17:22:02 +02:00
1 parent 272bf785ee
commit 5e42bd29d6
21 files changed
+411 -29

No files matched your search

+25 -11
View File
@@ -4,14 +4,15 @@ import { useStore } from '../store/useStore.js'
import { useUI } from '../store/useUI.js'
import { exOr } from '../lib/exercises.js'
import { usesBar, barWeightFor, plateSplit } from '../lib/bar.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, cleanupSg, applyIntensifierPlan, pinnedNoteFor, exNoteFor } from '../lib/history.js'
import { effectiveRoutine, lastEntryFor, bestWeightFor, buildSets, freestyleConfig, defaultConfig, setsDoneActive, supersetUnits, unitOf, setLabel, modeOf, isBw, isPerSide, sideReps, repStep, EFFORT, effortOf, cascadeWeight, insertWarmupRow, removeRowAt, pairAdjacent, unpairSuperset, cleanupSg, applyIntensifierPlan, pinnedNoteFor, exNoteFor } from '../lib/history.js'
import { fmtNum, fmtDate, todayISO, exCount, DAYN } from '../lib/format.js'
import { beep, vibrate } from '../lib/sound.js'
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, topWeightSheet, finishWorkout, workoutCompleteSheet, confirmSheet, exerciseNoteSheet, sessionNoteSheet, swapActiveWorkoutExercise, barWeightSheet } from '../sheets.jsx'
import { startFlow, exercisePicker, exConfigSheet, exerciseDetailSheet, topWeightSheet, finishWorkout, workoutCompleteSheet, confirmSheet, exerciseNoteSheet, sessionNoteSheet, swapActiveWorkoutExercise, barWeightSheet, 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 } from '../lib/progression.js'
@@ -135,24 +136,37 @@ 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 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) => {
if (col.eff) return onField(i, col.f, stepEffort(col.eff, s[col.f], dir))
// 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
// Weight and reps step up from 0 with no ceiling, as they always did.
const bump = (s, i, col, dir) =>
onField(i, col.f, Math.max(0, Math.round(((s[col.f] || 0) + dir * col.step) * 100) / 100))
}
// Uses the shared stepper markup so a set row picks up the same control styling
// as every other +/- field in the app.
const cell = (s, i, col, cls) => (
<div className={'stp ' + cls}>
<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>
<button aria-label="Increase" onClick={() => bump(s, i, col, 1)}><Icon name="plus" /></button>
</div>
)
// 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. Empty reads as a neutral dash; rirOf-via-effortColor keeps the
// colour identical whether the value is logged as RIR or RPE.
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)
return <button className={'effcell' + (v == null ? ' is-empty' : '')}
style={color ? { color, borderColor: color } : undefined}
aria-label={col.hd}
onClick={() => effortPickerSheet(col.eff, v, nv => onField(i, col.f, nv))}>
{v == null ? col.hd : fmtNum(v)}
</button>
}
// 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) => (
@@ -236,7 +250,7 @@ function ExerciseBlock({ entryIdx, compact, onToggle, onField, onAddSet, onRemov
<div className="n">{phaseNum}</div>
{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}
+87
View File
@@ -20,6 +20,7 @@ const mocks = vi.hoisted(() => {
toast: vi.fn(),
scrollCalls: [],
swapActiveWorkoutExercise: vi.fn(),
effortPickerSheet: vi.fn(),
}
state.stopRest = vi.fn(() => { state.timer = null })
state.stopWork = vi.fn(() => { state.work = null })
@@ -66,10 +67,12 @@ vi.mock('../sheets.jsx', () => ({
workoutCompleteSheet: mocks.workoutCompleteSheet,
confirmSheet: mocks.confirmSheet,
swapActiveWorkoutExercise: mocks.swapActiveWorkoutExercise,
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
@@ -535,6 +538,90 @@ 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 }) })),
})
const effCells = () => [...container.querySelectorAll('.effcell')]
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()
expect(cells[0].textContent).toBe('0')
expect(cells[0].className).not.toContain('is-empty')
// 0 RIR = to failure = purple; 2 RIR = yellow (the colours effortColor assigns)
expect(cells[0].getAttribute('style')).toContain('--purple')
expect(cells[1].textContent).toBe('2')
expect(cells[1].getAttribute('style')).toContain('--yellow')
expect(cells[2].textContent).toBe('RIR') // the unrated one stays a label
})
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)) })
const cell = effCells()[0]
expect(cell.textContent).toBe('8')
// RPE 8 == RIR 2 == yellow: the colour is the effort, independent of the scale shown
expect(cell.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)
})
})
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