Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.
JavaScript apple-watchbodyweightdockerfitnessfitness-trackergymhealthiosmcpnodejspasskeysprogressive-web-applicationspwareactself-hostedself-hostingvitewebauthnweightliftingworkout-tracker
Exercise settings: the drop-set, rest-pause and deload fields can be emptied to type a new number too, so a weight drop retyped as 20 saves 20% instead of 520% and a deload typed as 80 saves 80% instead of 95%; the limits apply on leaving the field
Duarte Santosf259757d8a69
diff --git a/frontend/src/components/ui.jsx b/frontend/src/components/ui.jsxindex d63061079..3aa7f5473 100644--- a/frontend/src/components/ui.jsx+++ b/frontend/src/components/ui.jsx@@ -126,11 +126,12 @@ export function Segmented({ options, value, onChange, className = '' }) {/* ============================ stepper ============================ */-// `min` holds for the buttons at once, but for typing only once the field is left: clamped on-// every keystroke, a field emptied to type a new number snapped to the minimum, and the digits-// typed next landed after it (a 48 retyped as 75 saved 175).-export function Stepper({ value, step = 1, min = 0, onChange, decimal = true, className = '', label, unit, invalid = false }) {- const set = v => onChange(Math.max(min, Math.round((v || 0) * 100) / 100))+// `min` and `max` hold for the buttons at once, but for typing only once the field is left:+// clamped on every keystroke, a field emptied to type a new number snapped to the minimum, and+// the digits typed next landed after it (a 48 retyped as 75 saved 175).+export function Stepper({ value, step = 1, min = 0, max = Infinity, onChange, decimal = true, className = '', label, unit, invalid = false }) {+ const clamp = v => Math.min(max, Math.max(min, v))+ const set = v => onChange(clamp(Math.round((v || 0) * 100) / 100))// Holding a button repeats the step; the latest value/step live in a ref so// the interval doesn't keep stepping from the value it was started with.const live = useRef({ value, step, set })@@ -170,7 +171,7 @@ export function Stepper({ value, step = 1, min = 0, onChange, decimal = true, clconst inner = (<div className={'stp ' + className}><button {...holdProps(-1)} aria-label="Decrease"><Icon name="minus" /></button>- <span className="val" onBlur={() => { if ((+value || 0) < min) onChange(min) }}>+ <span className="val" onBlur={() => { const v = +value || 0; if (clamp(v) !== v) onChange(clamp(v)) }}><NumberField value={value} decimal={decimal} onChange={onChange} aria-invalid={invalid ? 'true' : undefined} />{unit && <i>{unit}</i>}</span>diff --git a/frontend/src/sheets.config-typing.test.jsx b/frontend/src/sheets.config-typing.test.jsxnew file mode 100644index 000000000..5488a1d1b--- /dev/null+++ b/frontend/src/sheets.config-typing.test.jsx@@ -0,0 +1,92 @@+// @vitest-environment happy-dom+import React, { act } from 'react'+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'+import { createRoot } from 'react-dom/client'+import { EXDB } from './lib/exercises.js'+import { useStore } from './store/useStore.js'+import { useUI } from './store/useUI.js'+import { exConfigSheet } from './sheets.jsx'++// The exercise settings' number fields with a floor (a drop-set's drops and weight drop, a+// rest-pause's reps and rest) or a range (the Epley deload) were clamped on every keystroke, like+// the workout duration was: an emptied field snapped back to its minimum and the digits typed next+// landed after it, so a weight drop retyped as 20 read 520 and a deload typed as 80 read 95.+const ex = EXDB.find(e => e.id === '0009') // a weighted machine exercise: Epley deload applies+const mounted = []++function renderConfig(cfg) {+ const onSave = vi.fn()+ exConfigSheet(ex, { sets: 3, reps: 10, weight: 40, mode: 'reps', ...cfg }, onSave)+ const sheet = useUI.getState().sheets.at(-1)+ const host = document.createElement('div')+ document.body.appendChild(host)+ const root = createRoot(host)+ mounted.push(root)+ act(() => root.render(sheet.render(() => useUI.getState().closeSheet(sheet.id))))+ return { host, onSave }+}+// The last field so labelled: "Rest (s)" is also the exercise's own rest, further up the sheet.+const field = (host, label) => [...host.querySelectorAll('.stp-w')].filter(w => w.querySelector('.stp-l')?.textContent === label).at(-1).querySelector('input.num')+function type(el, value) {+ Object.getOwnPropertyDescriptor(el.constructor.prototype, 'value').set.call(el, value)+ el.dispatchEvent(new Event('input', { bubbles: true }))+}+const leave = el => el.dispatchEvent(new FocusEvent('focusout', { bubbles: true }))+const save = host => [...host.querySelectorAll('button')].find(b => b.textContent.trim() === 'Save').click()++describe('exercise settings: typing into a field with a minimum', () => {+ beforeEach(() => {+ globalThis.IS_REACT_ACT_ENVIRONMENT = true+ useUI.setState({ sheets: [] })+ useStore.setState(s => ({ S: { ...s.S, unit: 'kg' } }))+ document.body.innerHTML = ''+ })+ afterEach(() => { act(() => { mounted.splice(0).forEach(root => root.unmount()) }) })++ it('lets a drop-set field be emptied and retyped, and saves what was typed', () => {+ const { host, onSave } = renderConfig({ intensifier: { type: 'dropset', count: 2, pct: 10 } })+ const pct = field(host, 'Weight drop (%)')+ act(() => type(pct, ''))+ expect(pct.value).toBe('')+ act(() => type(pct, '20'))+ expect(pct.value).toBe('20')+ const drops = field(host, 'Drops')+ act(() => type(drops, ''))+ act(() => type(drops, '3'))+ expect(drops.value).toBe('3')+ act(() => save(host))+ expect(onSave.mock.calls[0][0].intensifier).toEqual({ type: 'dropset', count: 3, pct: 20 })+ })++ it('holds a field left below its minimum to it, on leaving it or on saving straight from it', () => {+ const { host, onSave } = renderConfig({ intensifier: { type: 'restpause', totalReps: 8, restSec: 15 } })+ const rest = field(host, 'Rest (s)')+ act(() => type(rest, '2'))+ expect(rest.value).toBe('2')+ act(() => leave(rest))+ expect(rest.value).toBe('5')+ act(() => type(field(host, 'Rest-pause reps'), '')) // saved without leaving the field+ act(() => save(host))+ expect(onSave.mock.calls[0][0].intensifier).toEqual({ type: 'restpause', totalReps: 1, restSec: 5 })+ })++ it('lets the deload percentage be retyped, held to 50–95 on leaving the field', () => {+ const { host, onSave } = renderConfig({ prog: 'linear' })+ const deload = field(host, 'Deload 1RM (%)')+ expect(deload.value).toBe('90')+ act(() => type(deload, ''))+ expect(deload.value).toBe('')+ act(() => type(deload, '8'))+ expect(deload.value).toBe('8')+ act(() => type(deload, '80'))+ expect(deload.value).toBe('80')+ act(() => leave(deload))+ expect(deload.value).toBe('80')+ act(() => type(deload, '99'))+ act(() => leave(deload))+ expect(deload.value).toBe('95')+ act(() => type(deload, '85'))+ act(() => save(host))+ expect(onSave.mock.calls[0][0].deloadFactor).toBe(0.85)+ })+})diff --git a/frontend/src/sheets.jsx b/frontend/src/sheets.jsxindex 017da77ae..dbf65a836 100644--- a/frontend/src/sheets.jsx+++ b/frontend/src/sheets.jsx@@ -1268,7 +1268,9 @@ function ProgressionFields({ ex, mode, c, setC, routine, unit, perSide }) {const stride = mode === 'reps' && perSide ? 2 : 1const range = active === 'double' ? normalizeRepRange(c.reps, c.repsMin, stride) : nullconst epleyEligible = mode === 'reps' && !isBw({ ...c, id: ex.id }) && (active === 'linear' || active === 'double')- const deloadPercent = Math.round((Number(c.deloadFactor) > 0 ? Number(c.deloadFactor) : 0.9) * 100)+ // What is typed is shown as typed, 0 for an emptied field included: shown as the default 90+ // instead, the field could not be emptied to type a new percentage.+ const deloadPercent = Math.round((c.deloadFactor != null && Number.isFinite(Number(c.deloadFactor)) ? Number(c.deloadFactor) : 0.9) * 100)const setRule = v => setC(x => {const next = { ...x, prog: v || undefined }return policyFor({ ...next, id: ex.id }, routine, mode) === 'double'@@ -1297,8 +1299,8 @@ function ProgressionFields({ ex, mode, c, setC, routine, unit, perSide }) {<Stepper label={t('Reps up to')} value={c.reps ?? range.reps} step={stride} decimal={false}onChange={v => setC(x => ({ ...x, reps: v }))} /></>}- {epleyEligible && <Stepper label={t('Deload 1RM (%)')} value={deloadPercent} step={5} decimal={false}- onChange={v => setC(x => ({ ...x, deloadFactor: Math.max(0.5, Math.min(0.95, Number(v) / 100)) }))} />}+ {epleyEligible && <Stepper label={t('Deload 1RM (%)')} value={deloadPercent} step={5} min={50} max={95} decimal={false}+ onChange={v => setC(x => ({ ...x, deloadFactor: Number(v) / 100 }))} />}</div>}{invalid && <div className="small" role="alert" style={{ color: 'var(--red)', marginTop: -10, marginBottom: 18 }}>{t('Enter a positive step to use this progression rule.')}@@ -1306,6 +1308,15 @@ function ProgressionFields({ ex, mode, c, setC, routine, unit, perSide }) {</>}+// The drop-set and rest-pause fields take what is typed and are held to their minimums once left+// (Stepper `min`). A sheet saved from a field still being typed in is held to them here: an+// emptied weight drop would otherwise plan drops at the same weight.+const intensifierToSave = x => (x.type === 'dropset'+ ? { ...x, count: Math.max(1, Math.round(x.count) || 0), pct: Math.max(5, Number(x.pct) || 0) }+ : x.type === 'restpause'+ ? { ...x, totalReps: Math.max(1, Math.round(x.totalReps) || 0), restSec: Math.max(5, Number(x.restSec) || 0) }+ : x)+function ExConfig({ ex, existing, onSave, onDelete, onReplace, close, routine, initial, saveLabel }) {const st = useStore(s => s.S)const cardio = isCardio(ex.id)@@ -1389,7 +1400,7 @@ function ExConfig({ ex, existing, onSave, onDelete, onReplace, close, routine, iif (bw && !(out.weight > 0) && c.repsMax > 0) out.repsMax = Math.max(reps, Math.round(c.repsMax))// Every set in this exercise becomes a drop-set/rest-pause (buildSets stamps the rows) —// decided here, in the plan, not re-decided live each time you train it.- if (c.intensifier && c.intensifier.type) out.intensifier = c.intensifier+ if (c.intensifier && c.intensifier.type) out.intensifier = intensifierToSave(c.intensifier)onSave(out)}}@@ -1524,16 +1535,16 @@ function ExConfig({ ex, existing, onSave, onDelete, onReplace, close, routine, i]} /></div>{c.intensifier?.type === 'dropset' && <div className="row cfgrow" style={{ marginBottom: 8 }}>- <Stepper label={t('Drops')} value={c.intensifier.count} step={1} decimal={false}- onChange={v => setC(x => ({ ...x, intensifier: { ...x.intensifier, count: Math.max(1, v) } }))} />- <Stepper label={t('Weight drop (%)')} value={c.intensifier.pct} step={5} decimal={false}- onChange={v => setC(x => ({ ...x, intensifier: { ...x.intensifier, pct: Math.max(5, v) } }))} />+ <Stepper label={t('Drops')} value={c.intensifier.count} step={1} min={1} decimal={false}+ onChange={v => setC(x => ({ ...x, intensifier: { ...x.intensifier, count: v } }))} />+ <Stepper label={t('Weight drop (%)')} value={c.intensifier.pct} step={5} min={5} decimal={false}+ onChange={v => setC(x => ({ ...x, intensifier: { ...x.intensifier, pct: v } }))} /></div>}{c.intensifier?.type === 'restpause' && <div className="row cfgrow" style={{ marginBottom: 8 }}>- <Stepper label={t('Rest-pause reps')} value={c.intensifier.totalReps} step={1} decimal={false}- onChange={v => setC(x => ({ ...x, intensifier: { ...x.intensifier, totalReps: Math.max(1, v) } }))} />- <Stepper label={t('Rest (s)')} value={c.intensifier.restSec} step={5} decimal={false}- onChange={v => setC(x => ({ ...x, intensifier: { ...x.intensifier, restSec: Math.max(5, v) } }))} />+ <Stepper label={t('Rest-pause reps')} value={c.intensifier.totalReps} step={1} min={1} decimal={false}+ onChange={v => setC(x => ({ ...x, intensifier: { ...x.intensifier, totalReps: v } }))} />+ <Stepper label={t('Rest (s)')} value={c.intensifier.restSec} step={5} min={5} decimal={false}+ onChange={v => setC(x => ({ ...x, intensifier: { ...x.intensifier, restSec: v } }))} /></div>}{c.intensifier?.type && <div className="small dim" style={{ marginTop: -2, marginBottom: 18 }}>{c.intensifier.type === 'dropset'