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
import { useDeferredValue, useEffect, useMemo, useRef, useState } from 'react'
import { useStore } from './store/useStore.js'
import { useUI } from './store/useUI.js'
import { EXDB, EXIDX, BODYPARTS, isCardio, isBodyweightEq, allExercises, equipmentOf, byCategory, smOf, searchExercises, similarExercises, exOr, isAssisted, betterWeight, beatsWeight, isCustomEx } from './lib/exercises.js'
import { activeProfile, exAvailable, ALL_EQUIPMENT, newProfile, profileEquipment, ACC_V } from './lib/equipment.js'
import { fmtDate, fmtDateRange, fmtNum, fmtPlate, exerciseNameText, fmtVol, fmtDur, durPart, todayISO, isoOf, uid, exCount, routineCount, setsWorkCount, DAYN, DAYS, weekOrder, weekStartOf, weekDayOffset, MONTHS_LONG, ACCENTS } from './lib/format.js'
import { lastEntryFor, bestWeightFor, bestWeightForEntry, buildSets, effectiveRoutineIds, workoutDay, workoutVolume, sessionVolume, countsWarmups, setsDone, setsDoneActive, setUnitsTotal, lastBW, sessionSections, setLabel, defaultConfig, pickedExerciseConfig, cleanupSg, modeOf, effortOf, EFFORT, capEffort, stepEffort, isBw, isPerSide, sideReps, workSetsDone, applyIntensifierPlan, MAX_PLANNED_WARMUPS, NOTE_MAX } from './lib/history.js'
import { usesBar, hasBarOverride, isNoBar, barTypeWeight, ownBarDefault, barPicks, withBarDefault, withStandardBars, defaultBarWeight, BAR_TYPES } from './lib/bar.js'
import { PLATE_SIZES, pairsOf, ownsPlates, withPlatePairs, withStandardPlates, withLoadKind, loadKindFor, baseWeightFor, dropGrid } from './lib/plates.js'
import { toScale, rirOf, EFFORT_PRESETS, effortColor } from './lib/effort.js'
import { beep, vibrate } from './lib/sound.js'
import { t, tn, dateLocale, instrFor, instrTranslated, descFor, exerciseNameFor, exerciseNameClass, getLang, useLang } from './lib/i18n.js'
import { nav, closeThenNav, navToWorkout } from './lib/nav.js'
import { buildStarterPlan, starterPlanDays, starterPlanOptions } from './lib/starter.js'
import Media, { Thumb } from './components/Media.jsx'
import CustomMediaField from './components/CustomMediaField.jsx'
import WorkoutMediaSection, { workoutMediaCount } from './components/WorkoutMedia.jsx'
import { mediaOf, normalizeMediaRef, cleanUrl, workoutMediaOf } from './lib/media-refs.js'
import { syncMedia } from './lib/media-sync.js'
import LineChart from './components/LineChart.jsx'
import Stepper from './components/Stepper.jsx'
import { durationSheet } from './components/DurationWheel.jsx'
import { cleanBackups, withBackups, MAX_BACKUPS, backupSlotConfig } from './lib/routine-backups.js'
import { normalizeAliases } from './lib/exercise-aliases.js'
import ExerciseAliasesEditor, { ExerciseAliasesField, ExerciseAliasesLink } from './components/ExerciseAliases.jsx'
import { SG_NAME_MAX } from './lib/superset-meta.js'
import { REST_MAX, fmtRest, restWheelValue } from './lib/duration.js'
import Icon from './components/Icon.jsx'
import { SeasonDeco } from './components/Seasonal.jsx'
import { Button, Slider, Switch, Segmented, SelectRow, Row, TextField, NumberField, MultiSelectRow } from './components/ui.jsx'
import { glyphOf, GLYPH_GROUPS, DEFAULT_GLYPH } from './lib/glyphs.js'
import BodyMap from './components/BodyMap.jsx'
import MuscleExplorer from './components/MuscleExplorer.jsx'
import ExerciseViewToggle from './components/ExerciseViewToggle.jsx'
import { sameMuscleFirst, swapMuscleOf } from './lib/similar-exercises.js'
import { exerciseMuscleSnapshot, loadOfWorkouts, MUSCLES, MUSCLE_NAME, muscleWeightsOf, muscleGroupsOf, hasExplicitMuscleMetadata, inMuscleOrder } from './lib/muscles.js'
import { parseImport, mergeImport } from './lib/import-csv.js'
import { importHevyData, HevyApiError, HEVY_DEV_SETTINGS, mergeHevyRoutines, hevySummaryCounts } from './lib/import-hevy.js'
import { buildPlanBundle, parsePlan, mergePlan, printPlan, planPrintHTML } from './lib/plan-share.js'
import { printOptions } from './lib/print-doc.js'
import { printWorkout } from './lib/report-print.js'
import { estimate1RM, best1RM, is1RMRecord, REP_CAP, formulaOf, FORMULA_NAMES } from './lib/onerm.js'
import { exerciseHistory } from './lib/exercise-history.js'
import { relativeSeries } from './lib/relative-strength.js'
import { isBellEx, dbLoadOf, dbLoadFor, withDbLoad, entryDbLoad, currentDbLoad, isOneArm, dumbbellsOf, ownsDumbbells, withDumbbells, ownsNoDumbbells, withNoDumbbells, presetWeights, cleanWeights, canHaveStack, stackOf, withStack, stackFromStep } from './lib/dumbbells.js'
import { policyFor, defaultIncrement, POLICIES_FOR, POLICY_NAME, POLICY_DESC, MAX_BW_SETS, MAX_TRIPLE_SETS } from './lib/progression.js'
import { normalizeRepRange } from './lib/rep-range.js'
import { isBackoff, backoffWeights } from './lib/backoff.js'
import { isPyramid, normalizePyramid, normalizePyramidRest, normalizePyramidWeight, pyramidFromFlat, flatFromPyramid, pyramidLabel, PYRAMID_MAX, MAX_PYRAMID_SETS, PYRAMID_PRESETS } from './lib/pyramid.js'
import { MOBILE, shareExport, shareExportBlob, printHtml } from './lib/mobile.js'
import { speedUnitOf, toSpeed, fromSpeed } from './lib/speed.js'
import { clampIncline, inclineFits, INCLINE_MAX, INCLINE_STEP } from './lib/incline.js'
import { buildCompletedWorkout, sessionEnd } from './lib/finish-workout.js'
import { refillAfter } from './lib/rotation.js'
import { isWarmupRow, hasCompletedWork } from './lib/workout-model.js'
import { ownExerciseRest } from './lib/supersetFlow.js'
import { saveSessionAsRoutine, routineAdditions, addToRoutine } from './lib/session-routines.js'
import { repeatSessionEntries } from './lib/session-repeat.js'
import { swapActiveExercise } from './lib/active-exercise-swap.js'
import { useSheetKeyboard, useRevealActiveChip, tappable } from './lib/use-sheet-keyboard.js'
import { useAutoMore } from './lib/use-auto-more.js'
import { isFav, toggleFav, sortFavouritesFirst, sortYoursFirst } from './lib/favourites.js'
import { buildSessionEntries, buildPlannedEntry, buildSessionEntry, plannedConfigOf } from './lib/session-start.js'
import { finishCompare, finishCardio } from './lib/finish-compare.js'
import { workoutFit, fitFileName } from './lib/fit-export.js'
import { joinSessionNoProg } from './lib/session-noprog.js'
import { buildCombinedEntries, deriveSessionName } from './lib/session-merge.js'
import { workoutsOn, backfillStart, backfillEnd, completeBackfill, historyAsOf, sessionHistory } from './lib/backfill.js'
import { moveWorkout, sameWorkout, startTimeOf, durationMinOf, setWorkoutDuration, rebuildPrHistory, MAX_DURATION_MIN, endsInFuture } from './lib/workout-date.js'
import { editCompletedSession, editLeftEmpty, editedRecord, editChangesNothing } from './lib/session-edit.js'
import { stampWorkout } from './lib/sync-merge.js'
import { hrLabel } from './lib/heart-rate.js'
import { weeklyWeights } from './lib/bodyweight.js'
import { workoutText } from './lib/workout-text.js'
import { workoutCardModel, layoutWorkoutCard, drawWorkoutCard, viewAspect, CARD_FONT } from './lib/workout-card.js'
import { copyText } from './lib/clipboard.js'
import { queueRemaining, pinState } from './lib/queue.js'
import { DAY_NOTE_TAGS, DAY_NOTE_LABEL, DAY_NOTE_ICON, dayNoteOf, dayNoteLine, canNoteDay, withDayNote } from './lib/day-notes.js'
const S = () => useStore.getState().S
const update = (...a) => useStore.getState().update(...a)
const ui = () => useUI.getState()
const toast = m => ui().toast(m)
const snd = () => S().sound
/* ============================ custom confirm dialog ============================ */
function ConfirmDialog({ title, message, confirmText, cancelText, danger, onConfirm, onCancel, close }) {
return <div style={{ textAlign: 'center', padding: '4px 0' }}>
{title && <h3 style={{ marginBottom: 8 }}>{title}</h3>}
<div className="muted" style={{ marginBottom: 18, lineHeight: 1.5 }}>{message}</div>
<button className={'btn ' + (danger ? 'danger' : 'primary')} onClick={() => { close(); onConfirm && onConfirm() }}>{confirmText || t('Confirm')}</button>
<div style={{ height: 8 }} />
<Button variant="ghost" className="dim" onClick={() => { close(); onCancel && onCancel() }}>{cancelText || t('Cancel')}</Button>
</div>
}
// "1 workouts and 1 weigh-ins" read wrong: a count of one takes the singular, per noun. Four
// whole sentences rather than two spliced counts, so every language keeps its own word order.
export function addDeviceDataMessage(workouts, weighIns) {
const one = n => Number(n) === 1
return one(workouts)
? one(weighIns)
? t('{0} workout and {1} weigh-in were logged on this device while signed out. Add them to your profile, or keep the profile exactly as it is on the server.', workouts, weighIns)
: t('{0} workout and {1} weigh-ins were logged on this device while signed out. Add them to your profile, or keep the profile exactly as it is on the server.', workouts, weighIns)
: one(weighIns)
? t('{0} workouts and {1} weigh-in were logged on this device while signed out. Add them to your profile, or keep the profile exactly as it is on the server.', workouts, weighIns)
: t('{0} workouts and {1} weigh-ins were logged on this device while signed out. Add them to your profile, or keep the profile exactly as it is on the server.', workouts, weighIns)
}
// Sign-in found workouts on this device that the profile does not have (logged while signed
// out). The profile is the truth — settings and plan come from the server either way — the
// question is only whether these entries are added to it or dropped. Resolves true to add.
// Routines and the rest of a plan made here while signed out: asked about as well (they come along
// with "Add them"), in their own words when there is nothing else.
export function addDeviceSetupMessage(routines) {
const n = Number(routines) || 0
return n === 0
? t('Your plan, notes or gym cards on this device were set up while signed out. Add them to your profile, or keep the profile exactly as it is on the server.')
: n === 1
? t('{0} routine was made on this device while signed out. Add it to your profile, or keep the profile exactly as it is on the server.', n)
: t('{0} routines were made on this device while signed out. Add them to your profile, or keep the profile exactly as it is on the server.', n)
}
export function askAddDeviceData(extras) {
const logged = extras.workouts || extras.bodyweight || extras.customEx
return new Promise(resolve => confirmSheet({
title: logged ? t('Add this device\'s workouts to your profile?') : t('Add this device\'s plan to your profile?'),
message: logged ? addDeviceDataMessage(extras.workouts, extras.bodyweight) : addDeviceSetupMessage(extras.routines),
confirmText: t('Add them'), cancelText: t('Keep profile as is'),
onConfirm: () => resolve(true), onCancel: () => resolve(false), locked: true
}))
}
/* ============================ menu sheet ============================ */
// A list of actions, one per row, closing on tap. This is where the workout screen parks
// everything that is not a set you are about to log: the point of a single "more" button is
// that the ten things you do once a session stop competing with the two you do every set.
// items: [{ icon, label, sub, onClick, danger, disabled, on }] — `on` draws a check for toggles.
// sections: [{ title, items }] instead of `items` splits a longer menu into groups, each with an
// optional small header; a group whose items are all falsy is left out, header and all.
// `accent` tints a row's icon, for the one entry a menu leads with.
// A title is shown as written: most are sentences ("Convert to lb?", "Set 2"), which a default
// title-casing turned into "Convert To Lb?". An exercise name passes exerciseNameClass as
// titleClass, which title-cases the lower-case packs and leaves a cased one alone.
function MenuSheet({ title, titleClass = '', subtitle, items, sections, close }) {
const groups = (sections || [{ items }])
.map(g => ({ title: g.title, items: (g.items || []).filter(Boolean) }))
.filter(g => g.items.length)
return <>
{title && <h3 className={titleClass || undefined} style={{ marginBottom: subtitle ? 2 : 10 }}>{title}</h3>}
{subtitle && <div className="muted small" style={{ marginBottom: 10 }}>{subtitle}</div>}
{groups.map((g, gi) => <div key={gi} className={'menu-group' + (sections ? ' grouped' : '')}>
{g.title && <div className="menu-sec">{g.title}</div>}
<div className="list menu-list">
{g.items.map((it, i) => <div key={i}
className={'item menu-item' + (it.danger ? ' danger' : '') + (it.disabled ? ' disabled' : '') + (it.accent ? ' accent' : '')}
aria-disabled={it.disabled || undefined}
{...tappable(it.disabled ? null : () => { close(); it.onClick && it.onClick() })}>
{it.icon && <span className="lrow-i"><Icon name={it.icon} /></span>}
<div className="grow"><div className="tt">{it.label}</div>{it.sub && <div className="ss">{it.sub}</div>}</div>
{it.on != null && <span className={'menu-on' + (it.on ? ' is-on' : '')}><Icon name="check" /></span>}
{it.chevron && <Icon name="chevronRight" className="menu-chev" />}
</div>)}
</div>
</div>)}
</>
}
export function menuSheet(opts) {
ui().openSheet(close => <MenuSheet {...opts} close={close} />)
}
// Themed replacement for window.confirm — callback-based (no blocking).
export function confirmSheet(opts) {
ui().openSheet(close => <ConfirmDialog {...opts} close={close} />, { kind: 'center', ...(opts.locked ? { locked: true } : {}) })
}
/* ============================ starter plan ============================ */
// Plan names and blurbs live here, not in lib/starter.js: check-source-strings.mjs only finds
// string literals written inside a t() call, so copy parked in the catalog and passed in as a
// variable is invisible to it — it would quietly stay English in every language.
const PLAN_COPY = {
ppl: () => ({ name: t('Push / Pull / Legs'), about: t('Push, pull and legs each get their own day.') }),
'upper-lower': () => ({ name: t('Upper / Lower'), about: t('Upper body twice, lower body twice.') }),
'full-body': () => ({ name: t('Full Body'), about: t('Three sessions, the whole body each time.') }),
'5x5': () => ({ name: t('5×5'), about: t('Five sets of five on the main barbell lifts.') })
}
// Adds the plan's routines (reusing one you already have under the same name) and puts them on
// its weekdays. Existing routines are never touched
// and only the weekdays the plan asks for are reassigned; an id with no plan behind it changes
// nothing at all. planId is deliberately required — a default invites `onClick={loadStarterPlan}`,
// which hands the click event in as the plan and silently loads nothing.
export function loadStarterPlan(planId, profile) {
const plan = buildStarterPlan(planId, profile ? profileEquipment(profile) : undefined)
if (!plan) return false
update(st => {
// Loading the same plan again reuses the routines it added last time (same name, same
// exercises) for the weekdays instead of a second Upper A, Lower A, ... under the same names.
// A plan fitted to different equipment has other exercises, so it is added alongside.
const idOf = {}
const sameLift = (a, b) => a.ex.length === b.ex.length && a.ex.every((e, i) => e.id === b.ex[i].id)
plan.routines.forEach(r => {
const have = st.routines.find(x => x.name === r.name && sameLift(x, r))
if (have) idOf[r.id] = have.id
else { st.routines.push(r); idOf[r.id] = r.id }
})
plan.schedule.forEach(({ day, routineId }) => { st.week[day] = [idOf[routineId]] })
})
if (plan.fit) toast(t('{0} loaded, fitted to "{1}": {2} swapped, {3} dropped', PLAN_COPY[planId]().name, profile.name, plan.fit.swapped, plan.fit.dropped))
else toast(t('{0} loaded', PLAN_COPY[planId]().name))
return true
}
// Intl joins the days the way each language does it — "and" vs "und", "、" in Chinese.
const dayList = days => new Intl.ListFormat(dateLocale()).format(days.map(d => t(DAYN[d])))
function StarterPlanChooser({ close }) {
const week = useStore(s => s.S.week)
const routines = useStore(s => s.S.routines)
const profiles = useStore(s => s.S.equipProfiles) || []
// Fitting starts on for the profile you already filter by; '' leaves the plan as written.
const [fitId, setFitId] = useState(() => activeProfile(useStore.getState().S)?.id || '')
const fit = profiles.find(p => p.id === fitId) || null
const choose = (id, name) => {
const days = starterPlanDays(id)
close()
// A confirmation is only worth showing when one of those days is actually occupied — by a
// routine that still exists, not by a stale id the Plan already shows as "Rest".
const taken = day => [].concat(week[day] || []).some(id => routines.some(r => r.id === id))
if (!days.some(taken)) { loadStarterPlan(id, fit); return }
confirmSheet({
title: t('Load {0}?', name),
message: t('The new plan goes on {0}. Your existing routines stay; only those days of the weekly plan change.', dayList(days)),
confirmText: t('Load plan'),
onConfirm: () => loadStarterPlan(id, fit)
})
}
return <>
<h3>{t('Choose starter plan')}</h3>
{profiles.length > 0
? <div className="chips" style={{ margin: '0 0 10px' }}>
<div className="small dim" style={{ width: '100%' }}>{t('Fit to my equipment')}</div>
<button className={'chip nocap' + (!fit ? ' on' : '')} onClick={() => setFitId('')}>{t('As written')}</button>
{profiles.map(p => <button key={p.id} className={'chip nocap' + (fit?.id === p.id ? ' on' : '')} onClick={() => setFitId(p.id)}>{p.name}</button>)}
</div>
: <div className="small dim" style={{ margin: '0 0 10px' }}>{t('Add an equipment profile in Settings to fit a plan to what you own.')}</div>}
<div className="list">
{starterPlanOptions().map(({ id, days }) => {
const { name, about } = PLAN_COPY[id]()
return <div key={id} className="item" {...tappable(() => choose(id, name))}>
<span className="lrow-i" style={{ background: 'var(--surface-3)' }}><Icon name="clipboard" /></span>
<div className="grow"><div className="tt">{name}</div><div className="ss">{t('{0} days per week', days)} · {about}</div></div>
<Icon name="chevronRight" className="chev" />
</div>
})}
</div>
</>
}
export const starterPlanSheet = () => ui().openSheet(close => <StarterPlanChooser close={close} />)
/* ============================ weight picker (shared: body weight + goal) ============================ */
// Fixed range, not a moving window — a window that resizes itself mid-drag (the previous
// attempt) makes the thumb's position unpredictable: every time it grows, everything already
// placed on it shifts toward one side. A static range never has that problem, at the cost of
// coarser precision per pixel — the +/- buttons, and typing straight into the read-out, cover
// exact values.
// The ceiling follows the profile's unit: 300 covers a body weight or a working weight in
// kg, but as pounds it cut off at 136 kg — below plenty of people's body weight, and well
// below an everyday squat.
const W_LO = 1
const wHi = unit => (unit === 'lb' ? 660 : 300)
// A typed body weight may sit past the slider (a 320 kg lifter is real), but not past any
// person: 9999 saved, drew the chart from -0 to 10,000 and set a goal 9,922 kg away.
const bwMax = unit => (unit === 'lb' ? 1500 : 700)
const bwTooHeavy = (n, unit) => {
if (n <= bwMax(unit)) return false
toast(t('That looks too heavy. Keep it at {0} or less.', fmtNum(bwMax(unit)) + ' ' + unit))
return true
}
function WeightInput({ value, setValue, unit }) {
const W_HI = wHi(unit)
const clamp = x => Math.max(W_LO, Math.min(W_HI, Math.round((x || 0) * 10) / 10))
const sv = Math.max(W_LO, Math.min(W_HI, value))
const onSlide = v => setValue(clamp(v))
const onType = v => setValue(v)
return <>
<div className="bwstep">
<button className="bw-pm" onClick={() => onSlide(value - 0.1)} aria-label={t('Decrease by {0}', fmtNum(0.1))}><Icon name="minus" /></button>
<label className="bw-read">
<NumberField fit value={value} onChange={onType} aria-label={t('Weight ({0})', unit)} enterKeyHint="done"
onKeyDown={e => { if (e.key === 'Enter') e.currentTarget.blur() }} />
<span className="u"> {unit}</span>
</label>
<button className="bw-pm" onClick={() => onSlide(value + 0.1)} aria-label={t('Increase by {0}', fmtNum(0.1))}><Icon name="plus" /></button>
</div>
<div className="chips" style={{ justifyContent: 'center', margin: '8px 0' }}>
<button className="chip" onClick={() => onSlide(value - 1)}>−1</button>
<button className="chip" onClick={() => onSlide(value - 0.5)}>−0.5</button>
<button className="chip" onClick={() => onSlide(value + 0.5)}>+0.5</button>
<button className="chip" onClick={() => onSlide(value + 1)}>+1</button>
</div>
<Slider value={sv} min={W_LO} max={W_HI} step={0.5} onChange={onSlide} />
</>
}
/* ============================ body weight ============================ */
function BwSheet({ required, onDone, close }) {
const st = useStore(s => s.S)
const unit = st.unit
const bw = lastBW(st)
const [v, setV] = useState(bw ? bw.w : 70)
const [date, setDate] = useState(todayISO())
const save = () => {
const n = Math.round((v || 0) * 10) / 10
if (!n || n <= 0) { toast(t('Enter a valid weight')); return }
if (bwTooHeavy(n, unit)) return
if (!/^\d{4}-\d{2}-\d{2}$/.test(date) || date > todayISO()) { toast(t('Choose a valid date')); return }
// Today's weigh-in is replaced by a second one, as it always was; an earlier day's is not.
if (date !== todayISO() && st.bodyweight.some(b => b.d === date)) { toast(t('That date already has a weigh-in. Delete it first.')); return }
update(s => {
const ex = s.bodyweight.find(b => b.d === date)
if (ex) { ex.w = n; ex.t = Date.now(); delete ex.src } else s.bodyweight.push({ d: date, w: n, t: Date.now() })
s.bodyweight.sort((a, b) => (a.d < b.d ? -1 : 1))
})
close()
if (onDone) onDone(n); else toast(t('Weight saved'))
}
const recent = [...st.bodyweight].reverse().slice(0, 3)
return <>
{/* This sheet opens `locked` — swipe/backdrop/Escape/Android-back all no-op on it (see
Modals.jsx) so an accidental tap on "Start" can't be walked back by reflex the way
every other sheet in the app can. The two buttons below already cover leaving it
deliberately; this is the same close a normal sheet gets everywhere else, just
opted back in explicitly instead of by omission. Plain close() — no onDone, no
nav — so it's a true no-op: the screen underneath is exactly where you left it. */}
{required
? <div className="row between" style={{ marginBottom: 14 }}>
<h3 style={{ marginBottom: 0 }}>{t('Quick check-in')}</h3>
<button className="iconbtn" aria-label={t('Cancel')} onClick={() => close()}><Icon name="xmark" /></button>
</div>
: <h3>{t('Log body weight')}</h3>}
{required
? <div className="muted small">{t('Slide or tap to set your weight. We ask before every workout so your curve stays honest.')}</div>
: <label className="measure-date">
<span>{t('Date')}</span>
<input className="field" type="date" value={date} max={todayISO()} onChange={e => setDate(e.target.value)} />
</label>}
<WeightInput value={v} setValue={setV} unit={unit} />
<div style={{ height: 14 }} />
<Button variant="primary" onClick={save}>{required ? t('Save & start workout') : t('Save')}</Button>
{required && <>
<div style={{ height: 8 }} /><Button variant="ghost" className="dim" onClick={() => { close(); onDone && onDone(null) }}>{t('Start without weighing in')}</Button>
<div style={{ height: 2 }} /><Button variant="ghost" className="dim" icon="swap" onClick={() => closeThenNav(close, '/workout')}>{t('Choose a different workout')}</Button>
</>}
{!required && recent.length > 0 && <>
<h4 className="sec">{t('Recent weigh-ins')}</h4>
<div className="list" style={{ gap: 0 }}>
{recent.map(b => <WeighInRow key={b.d} b={b} unit={unit} />)}
</div>
{st.bodyweight.length > recent.length && <div className="row" style={{ justifyContent: 'flex-end', marginTop: 6 }}>
<Button size="sm" variant="ghost" trailingIcon="chevronRight" onClick={weighInsSheet}>{t('All weigh-ins')}</Button>
</div>}
</>}
</>
}
export function bwSheet(opts = {}) {
const h = ui().openSheet(close => <BwSheet {...opts} close={close} />, { locked: !!opts.required })
return h
}
// One weigh-in with its delete button, in the log sheet's recent three and in the full list.
// The full list asks first (`confirm`): it is months of history scrolled through on a phone, and a
// stray tap on one of its trash buttons took a past weigh-in with nothing to bring it back. The log
// sheet's three are the ones just typed, where taking back a typo in one tap is the point.
// Either way the toast offers Undo (#756): the weigh-in comes back as it was, and the store
// records it as added back (stampDeletions), so a device that synced the removal keeps it too.
function WeighInRow({ b, unit, confirm = false }) {
const delEntry = () => {
const kept = { ...b }
update(s => { s.bodyweight = s.bodyweight.filter(x => x.d !== b.d) })
ui().toast(t('Weigh-in deleted'), {
action: t('Undo'),
onAction: () => update(s => {
// A weigh-in logged for that day since is the newer word on it.
if (s.bodyweight.some(x => x.d === kept.d)) return
s.bodyweight.push(kept)
s.bodyweight.sort((x, y) => (x.d < y.d ? -1 : 1))
}),
})
}
const ask = () => confirmSheet({
title: t('Delete weigh-in?'), message: `${fmtDate(b.d, true)} · ${fmtNum(b.w)} ${unit}`,
confirmText: t('Delete'), danger: true, onConfirm: delEntry,
})
return <div className="row between" style={{ padding: '9px 2px', borderBottom: '1px solid var(--sep)' }}>
<span className="small muted">{fmtDate(b.d, true)}</span>
<span className="row" style={{ gap: 12 }}><b>{fmtNum(b.w)} {unit}</b>
<button className="iconbtn" style={{ width: 32, height: 30, borderRadius: 8, fontSize: 15, color: 'var(--red)' }} onClick={confirm ? ask : delEntry} aria-label={t('Delete weigh-in')}><Icon name="trash" /></button></span>
</div>
}
/* ============================ weigh-ins ============================ */
// Every weigh-in, week by week (Discord 'Weight'): each week under its mean and how far that
// moved from the week before, the numbers behind the curve on the card. The chart plots the
// weekly means, since the swing from one morning to the next is what makes a single weigh-in
// hard to read. The weeks start on the profile's first day of the week.
function WeighIns() {
const st = useStore(s => s.S)
const ws = weekStartOf(st)
const weeks = useMemo(() => weeklyWeights(st.bodyweight, ws), [st.bodyweight, ws])
const points = useMemo(() => [...weeks].reverse()
.map(w => ({ t: new Date(w.key + 'T12:00:00').getTime(), y: Math.round(w.avg * 100) / 100, d: w.key })), [weeks])
const n = weeks.reduce((sum, w) => sum + w.n, 0)
if (!n) return <>
<h3>{t('Weigh-ins')}</h3>
<div className="empty"><div className="ico"><Icon name="scale" /></div>{t('No weigh-ins yet. Log your weight to start the curve.')}</div>
</>
return <>
<h3 style={{ marginBottom: 2 }}>{t('Weigh-ins')}</h3>
<div className="muted small" style={{ marginBottom: 10 }}>{tn('{0} weigh-in', '{0} weigh-ins', n)}</div>
<div className="chart"><LineChart points={points} h={140} unit={st.unit} goal={st.targetW} /></div>
<div className="small dim" style={{ marginTop: 6 }}>{t('Weekly average')}</div>
{weeks.map(w => {
// Only a change the display can show: at one decimal, 0.02 kg is "0", and an arrow over a
// zero says something moved when, as far as the screen goes, nothing did.
const moved = w.delta != null && fmtNum(Math.abs(w.delta)) !== fmtNum(0)
return <div key={w.key} data-week={w.key}>
<div className="row between" style={{ margin: '16px 2px 4px', gap: 8 }}>
<span className="small" style={{ fontWeight: 600 }}>{t('Week of {0}', fmtDate(w.key))}</span>
<span className="small row" style={{ gap: 8 }}>
{moved && <span className="row" style={{ gap: 2, fontWeight: 500, color: bwDeltaColor(w.delta, w.avg) }}>
<Icon name={w.delta > 0 ? 'arrowUp' : 'arrowDown'} style={{ fontSize: 12 }} />{fmtNum(Math.abs(w.delta))}
</span>}
<span className="muted" style={{ whiteSpace: 'nowrap' }}>{t('Average {0}', fmtNum(w.avg) + ' ' + st.unit)}</span>
</span>
</div>
<div className="list" style={{ gap: 0 }}>{w.entries.map(b => <WeighInRow key={b.d} b={b} unit={st.unit} confirm />)}</div>
</div>
})}
</>
}
export const weighInsSheet = () => ui().openSheet(close => <WeighIns close={close} />)
/* ============================ import from another app ============================ */
// "12 sets bring an RPE with them": the file rated its sets. The column is off by default, so
// with it off the line says where to switch it on.
const effortLine = (effort, n, scale) => effort === 'none'
? tn('{0} set brings an {1} with it. Switch on Effort per set in Settings to see it.', '{0} sets bring an {1} with them. Switch on Effort per set in Settings to see it.', n, scale)
: tn('{0} set brings an {1} with it.', '{0} sets bring an {1} with them.', n, scale)
// Shows what a parsed export would actually do before anything is written. An import is
// the one action where "just try it" is expensive — it's someone's entire training
// history — so the numbers, the unit conversion and the exercises we couldn't recognise
// are all on screen before the confirm button.
function ImportSummary({ parsed, close }) {
const st = useStore(s => s.S)
const isBW = parsed.kind === 'bodyweight'
// Our own body CSV brings measurement check-ins along (#718); a day that has one keeps it.
const checkIns = isBW ? parsed.measurements || [] : []
const haveM = checkIns.filter(m => (st.measurements || []).some(x => x.d === m.d)).length
const have = (isBW
? parsed.bodyweight.filter(b => st.bodyweight.some(x => x.d === b.d)).length
: parsed.workouts.filter(w => st.workouts.some(x => x.d === w.d)).length) + haveM
const fresh = (isBW ? parsed.bodyweight.length + checkIns.length : parsed.workouts.length) - have
const doImport = () => {
let res
update(s => { res = mergeImport(s, parsed) })
close()
const measured = res.measurements ? tn('{0} measurement check-in imported', '{0} measurement check-ins imported', res.measurements) : ''
toast(isBW
? (res.added || !measured ? tn('{0} weigh-in imported', '{0} weigh-ins imported', res.added) + (measured ? ' · ' + measured : '') : measured)
: tn('{0} workout imported', '{0} workouts imported', res.added))
}
return <>
<h3>{parsed.source ? t('Import from {0}', parsed.source) : t('Import history')}</h3>
<div className="muted small" style={{ marginBottom: 12 }}>
{fmtDateRange(parsed.from, parsed.to, true)}
</div>
<div className="tiles" style={{ textAlign: 'start' }}>
{isBW ? <>
<div className="tile"><div className="l">{t('Weigh-ins')}</div><div className="v" style={{ fontSize: '1.1rem' }}>{parsed.bodyweight.length}</div></div>
{checkIns.length > 0 && <div className="tile"><div className="l">{t('Body measurements')}</div><div className="v" style={{ fontSize: '1.1rem' }}>{checkIns.length}</div></div>}
<div className="tile"><div className="l">{t('New')}</div><div className="v" style={{ fontSize: '1.1rem' }}>{fresh}</div></div>
</> : <>
<div className="tile"><div className="l">{t('Workouts')}</div><div className="v" style={{ fontSize: '1.1rem' }}>{parsed.workouts.length}</div></div>
<div className="tile"><div className="l">{t('Sets')}</div><div className="v" style={{ fontSize: '1.1rem' }}>{parsed.sets}</div></div>
<div className="tile"><div className="l">{t('Exercises matched')}</div><div className="v" style={{ fontSize: '1.1rem' }}>{parsed.matched}</div></div>
<div className="tile"><div className="l">{t('Added as your own')}</div><div className="v" style={{ fontSize: '1.1rem' }}>{parsed.created}</div></div>
</>}
</div>
{parsed.mixedUnits ? <div className="small" style={{ color: 'var(--yellow)', marginBottom: 10 }}>
{t('The file mixes kg and lb, so each set is converted to {0}.', st.unit)}
</div> : parsed.converted ? <div className="small" style={{ color: 'var(--yellow)', marginBottom: 10 }}>
{t('The file is in {0} and your profile is in {1}, so weights get converted.', parsed.fileUnit, st.unit)}
</div> : null}
{!isBW && !parsed.fileUnit && !parsed.mixedUnits && <div className="small dim" style={{ marginBottom: 10 }}>
{t('The file doesn’t say which unit it uses, so numbers are imported as they are.')}
</div>}
{have > 0 && <div className="small dim" style={{ marginBottom: 10 }}>
{t('{0} days already have data here and will be left alone.', have)}
</div>}
{/* The file rated its sets. Say so: the column is off by default, so the ratings would
otherwise arrive invisibly and look like they had been dropped. */}
{!isBW && (parsed.rirSets + parsed.rpeSets) > 0 && <div className="small dim" style={{ marginBottom: 10 }}>
{effortLine(effortOf(st), parsed.rirSets || parsed.rpeSets, parsed.rirSets ? 'RIR' : 'RPE')}
</div>}
{/* Rows the parser could not use (no date or name, nothing measured, impossible numbers)
are left out; say how many, so a short history does not look like a lost one. */}
{!isBW && parsed.skipped > 0 && <div className="small dim" style={{ marginBottom: 10 }}>
{tn('{0} row skipped (couldn’t read it)', '{0} rows skipped (couldn’t read them)', parsed.skipped)}
</div>}
{!isBW && parsed.unmatchedNames.length > 0 && <>
<h4 className="sec">{t('Not in the library, so they’re added as your own exercises')}</h4>
<div className="mchips" style={{ marginBottom: 12 }}>
{parsed.unmatchedNames.slice(0, 12).map(n => <span key={n} className="mchip capitalize">{n}</span>)}
{parsed.unmatchedNames.length > 12 && <span className="mchip">+{parsed.unmatchedNames.length - 12}</span>}
</div>
</>}
<Button variant="primary" onClick={doImport} disabled={!fresh}>
{fresh ? t('Import') : t('Nothing new to import')}
</Button>
<div style={{ height: 8 }} />
<Button variant="ghost" className="dim" onClick={close}>{t('Cancel')}</Button>
</>
}
/** Read a CSV/XML export, then show what it would do. */
export function importFromApp(file, onDone) {
const rd = new FileReader()
rd.onload = () => {
let parsed
try { parsed = parseImport(String(rd.result), { unit: S().unit, customEx: S().customEx }) }
catch (e) { toast(t('Could not read that file')); return }
if (parsed.error === 'empty') { toast(t('That file is empty')); return }
if (parsed.error) { toast(t("We don't recognise that file's columns. Check the docs for supported apps.")); return }
if (parsed.kind === 'bodyweight' ? !parsed.bodyweight.length && !parsed.measurements?.length : !parsed.workouts.length) {
toast(t('Nothing to import from that file')); return
}
ui().openSheet(close => <ImportSummary parsed={parsed} close={close} />)
onDone && onDone()
}
rd.onerror = () => toast(t('Could not read that file'))
rd.readAsText(file)
}
/* ============================ import from Hevy API ============================ */
// The key lives in React state for this sheet only — dismissed with the sheet, never
// written to the store / localStorage / the server. After a successful fetch the user
// picks workouts and/or weigh-ins before anything is merged.
export function importFromHevy() {
ui().openSheet(close => <HevyImportSheet close={close} />)
}
function hevyProgressLabel(p) {
if (!p) return t('Fetching from Hevy…')
if (p.stage === 'templates') return t('Fetching exercises… ({0}/{1})', p.page, p.pageCount)
if (p.stage === 'workouts') return t('Fetching workouts… ({0}/{1})', p.page, p.pageCount)
if (p.stage === 'routines') return t('Fetching routines… ({0}/{1})', p.page, p.pageCount)
if (p.stage === 'body') return t('Fetching weigh-ins… ({0}/{1})', p.page, p.pageCount)
if (p.stage === 'parse') return t('Matching exercises…')
return t('Fetching from Hevy…')
}
function HevyImportSheet({ close }) {
const st = useStore(s => s.S)
const [apiKey, setApiKey] = useState('')
const [busy, setBusy] = useState(false)
const [progress, setProgress] = useState(null)
const [payload, setPayload] = useState(null) // { workouts, routines, bodyweight }
const [wantWorkouts, setWantWorkouts] = useState(true)
const [wantRoutines, setWantRoutines] = useState(true)
const [wantBody, setWantBody] = useState(true)
const keyRef = useRef(null)
// Drop the key from memory when the sheet goes away (unmount or successful import).
useEffect(() => () => { setApiKey('') }, [])
const wipeKey = () => { setApiKey(''); if (keyRef.current) keyRef.current.value = '' }
const fetchAccount = async () => {
const key = apiKey.trim()
if (!key) { toast(t('Paste your Hevy API key first')); return }
setBusy(true)
setProgress({ stage: 'templates', page: 1, pageCount: 1 })
setPayload(null)
try {
const data = await importHevyData(key, { unit: st.unit, customEx: st.customEx, onProgress: setProgress })
wipeKey()
const empty = !data.workouts.workouts.length && !data.routines.routines.length && !data.bodyweight.bodyweight.length
if (empty) {
toast(t('Nothing to import from Hevy'))
return
}
setWantWorkouts(!!data.workouts.workouts.length)
setWantRoutines(!!data.routines.routines.length)
setWantBody(!!data.bodyweight.bodyweight.length)
setPayload(data)
} catch (e) {
if (e instanceof HevyApiError && e.message === 'auth') toast(t('That Hevy API key was refused'))
else if (e instanceof HevyApiError && e.message === 'rate-limit') toast(t('Hevy is rate-limiting requests. Wait a minute and try again'))
else if (e instanceof HevyApiError && e.message === 'empty') toast(t('Paste your Hevy API key first'))
else toast(t('Couldn’t reach Hevy. Check the key and try again'))
} finally {
setBusy(false)
setProgress(null)
}
}
const doImport = () => {
if (!payload) return
const parts = []
let addedW = 0, addedR = 0, addedB = 0
update(s => {
if (wantWorkouts && payload.workouts.workouts.length) {
const res = mergeImport(s, payload.workouts)
addedW = res.added
parts.push(tn('{0} workout imported', '{0} workouts imported', res.added))
}
if (wantRoutines && payload.routines.routines.length) {
const res = mergeHevyRoutines(s, payload.routines)
addedR = res.added
parts.push(tn('{0} routine imported', '{0} routines imported', res.added))
}
if (wantBody && payload.bodyweight.bodyweight.length) {
const res = mergeImport(s, payload.bodyweight)
addedB = res.added
parts.push(tn('{0} weigh-in imported', '{0} weigh-ins imported', res.added))
}
})
close()
if (!addedW && !addedR && !addedB) toast(t('Nothing new to import'))
else toast(parts.join(' · '))
}
if (!payload) {
return <>
<h3>{t('Import from Hevy')}</h3>
<div className="muted small" style={{ marginBottom: 14, lineHeight: 1.5 }}>
{t('Pull your history with a Hevy Pro API key. The key is only used for this import and is not saved.')}
</div>
<label className="small dim" style={{ display: 'block', marginBottom: 6 }}>{t('Hevy API key')}</label>
<TextField
ref={keyRef}
type="password"
autoComplete="off"
spellCheck={false}
placeholder="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
value={apiKey}
disabled={busy}
onChange={e => setApiKey(e.target.value)}
onKeyDown={e => { if (e.key === 'Enter' && !busy) fetchAccount() }}
/>
<div className="small" style={{ margin: '10px 0 16px', lineHeight: 1.45 }}>
<a href={HEVY_DEV_SETTINGS} target="_blank" rel="noopener noreferrer">{t('Get your API key')}</a>
<span className="dim"> · {t('Hevy → Settings → Developer')}</span>
</div>
{busy && <div className="small dim" style={{ marginBottom: 12 }}>{hevyProgressLabel(progress)}</div>}
<Button variant="primary" onClick={fetchAccount} disabled={busy || !apiKey.trim()}>
{busy ? t('Fetching from Hevy…') : t('Fetch from Hevy')}
</Button>
<div style={{ height: 8 }} />
<Button variant="ghost" className="dim" onClick={close} disabled={busy}>{t('Cancel')}</Button>
</>
}
const w = payload.workouts
const r = payload.routines
const b = payload.bodyweight
const haveW = w.workouts.filter(x => st.workouts.some(y => y.d === x.d)).length
const freshW = w.workouts.length - haveW
const haveB = b.bodyweight.filter(x => st.bodyweight.some(y => y.d === x.d)).length
const freshB = b.bodyweight.length - haveB
// Routines are always added as new copies (same as plan import).
const freshR = r.routines.length
const canImport = (wantWorkouts && freshW > 0) || (wantRoutines && freshR > 0) || (wantBody && freshB > 0)
const unmatched = [...new Set([
...(wantWorkouts ? w.unmatchedNames : []),
...(wantRoutines ? r.unmatchedNames : []),
])].sort()
const counts = hevySummaryCounts(w, r, { workouts: wantWorkouts, routines: wantRoutines })
return <>
<h3>{t('Import from Hevy')}</h3>
<div className="muted small" style={{ marginBottom: 12 }}>
{w.from && (w.from === w.to ? fmtDate(w.from, true) : fmtDate(w.from, true) + ' – ' + fmtDate(w.to, true))}
{!w.from && b.from && (b.from === b.to ? fmtDate(b.from, true) : fmtDate(b.from, true) + ' – ' + fmtDate(b.to, true))}
</div>
<div className="tiles" style={{ textAlign: 'start' }}>
<div className="tile"><div className="l">{t('Workouts')}</div><div className="v" style={{ fontSize: '1.1rem' }}>{w.workouts.length}</div></div>
<div className="tile"><div className="l">{t('Routines')}</div><div className="v" style={{ fontSize: '1.1rem' }}>{r.routines.length}</div></div>
<div className="tile"><div className="l">{t('Exercises matched')}</div><div className="v" style={{ fontSize: '1.1rem' }}>{counts.matched}</div></div>
<div className="tile"><div className="l">{t('Added as your own')}</div><div className="v" style={{ fontSize: '1.1rem' }}>{counts.created}</div></div>
</div>
{w.workouts.length > 0 && <div className="row between" style={{ padding: '10px 2px', borderTop: '1px solid var(--sep)', gap: 12 }}>
<div>
<div className="tt" style={{ fontSize: 15 }}>{t('Import workouts')}</div>
<div className="small dim">{t('{0} new · {1} days already here', freshW, haveW)}</div>
</div>
<Switch checked={wantWorkouts} onChange={setWantWorkouts} />
</div>}
{r.routines.length > 0 && <div className="row between" style={{ padding: '10px 2px', borderTop: '1px solid var(--sep)', gap: 12 }}>
<div>
<div className="tt" style={{ fontSize: 15 }}>{t('Import routines')}</div>
<div className="small dim">{t('{0} routines · {1} exercises, added as new plans', freshR, r.exerciseCount)}</div>
</div>
<Switch checked={wantRoutines} onChange={setWantRoutines} />
</div>}
{b.bodyweight.length > 0 && <div className="row between" style={{ padding: '10px 2px', borderTop: '1px solid var(--sep)', borderBottom: '1px solid var(--sep)', gap: 12, marginBottom: 8 }}>
<div>
<div className="tt" style={{ fontSize: 15 }}>{t('Import weigh-ins')}</div>
<div className="small dim">{t('{0} new · {1} days already here', freshB, haveB)}</div>
</div>
<Switch checked={wantBody} onChange={setWantBody} />
</div>}
{!b.bodyweight.length && <div style={{ borderBottom: '1px solid var(--sep)', marginBottom: 8 }} />}
{(w.converted || r.converted) && <div className="small" style={{ color: 'var(--yellow)', marginBottom: 10 }}>
{t('Hevy stores weights in kg, so they’ll be converted to {0}.', st.unit)}
</div>}
{wantWorkouts && (w.rirSets + w.rpeSets) > 0 && <div className="small dim" style={{ marginBottom: 10 }}>
{effortLine(effortOf(st), w.rirSets || w.rpeSets, w.rirSets ? 'RIR' : 'RPE')}
</div>}
{unmatched.length > 0 && <>
<h4 className="sec">{t('Not in the library, so they’re added as your own exercises')}</h4>
<div className="mchips" style={{ marginBottom: 12 }}>
{unmatched.slice(0, 12).map(n => <span key={n} className="mchip capitalize">{n}</span>)}
{unmatched.length > 12 && <span className="mchip">+{unmatched.length - 12}</span>}
</div>
</>}
<Button variant="primary" onClick={doImport} disabled={!canImport}>
{canImport ? t('Import') : t('Nothing new to import')}
</Button>
<div style={{ height: 8 }} />
<Button variant="ghost" className="dim" onClick={() => { setPayload(null); wipeKey() }}>{t('Back')}</Button>
</>
}
/* ============================ target weight ============================ */
export function bwDeltaColor(delta, currentW) {
if (!delta) return 'var(--label-2)'
if (!S().targetW) return 'var(--label)'
const up = S().targetW > currentW
return (delta > 0) === up ? 'var(--acc)' : 'var(--red)'
}
function GoalSheet({ close }) {
const st = S()
const bw = lastBW(st)
const [v, setV] = useState(st.targetW || (bw ? bw.w : 70))
return <>
<h3>{t('Target weight')}</h3>
<div className="muted small">{t('Your goal is drawn as a line through the weight charts, and gains/losses are colored by whether they move toward it.')}</div>
<WeightInput value={v} setValue={setV} unit={st.unit} />
<div style={{ height: 14 }} />
<Button variant="primary" onClick={() => {
const n = Math.round((v || 0) * 10) / 10
if (!n || n <= 0) { toast(t('Enter a valid weight')); return }
if (bwTooHeavy(n, st.unit)) return
update(s => { s.targetW = n }); close()
const b = lastBW(S()); toast(t('Goal set: {0}', fmtNum(n) + ' ' + st.unit) + (b ? ' (' + t('{0} to go', fmtNum(Math.abs(n - b.w))) + ')' : ''))
}}>{t('Save goal')}</Button>
{st.targetW && <><div style={{ height: 8 }} /><Button variant="danger" onClick={() => { update(s => { s.targetW = null }); close(); toast(t('Goal removed')) }}>{t('Remove goal')}</Button></>}
</>
}
export const goalSheet = () => ui().openSheet(close => <GoalSheet close={close} />)
/* ============================ plate loading ============================ */
// One editor for every place an exercise's loading shows up (exercise detail, exercise config,
// mid-workout sheet). Three things, all per exercise and display-only (lib/plates.js):
// • how the weight is loaded — per side of a bar, one stack, or not plate-loaded at all
// (S.loadKind; absent = derived from the equipment, so most exercises never store it);
// • the base weight before any plate goes on — the bar (S.barWeights, profile unit, lib/bar.js;
// stepping down to 0 clears the override and falls back to the bar type's default), or a
// sled's / machine's own weight for anything else;
// • "No bar" for a bar exercise: an explicit 0 (issue #138 — a counterbalanced Smith machine),
// so the whole total is plates. Distinct from the stepper's 0, which means "default".
// `cfg` is the routine/session config the caller has (its `bodyweight` flag turns a dumbbell
// exercise into "added weight", i.e. a single stack) — the default kind is derived from the
// same thing the workout screen derives it from, so what the editor shows as chosen is what
// the rows do. Without one, the equipment alone decides.
function BarWeightEditor({ ex, cfg, extra }) {
const st = useStore(s => s.S)
const bar = usesBar(ex)
const ctx = { ...(cfg || {}), id: ex.id }
const kind = loadKindFor(st, ctx)
const explicit = hasBarOverride(st, ex.id)
const noBar = bar && isNoBar(st, ex.id)
// What "default" means for this bar: your bar for the type (Settings > Equipment > Your bars),
// else the built-in one.
const def = barTypeWeight(st, ex.eq)
const mine = bar && ownBarDefault(st, ex.eq) !== null
const base = baseWeightFor(st, ex)
// The kinds a pick would only be restating. With a `cfg` there is one: the same default the
// rows derive. Without one the equipment decides — and for body-weight equipment that answer
// ('single', the load being whatever you hang on) is not the one a routine logging the total
// derives ('none', bodyweight: false), so the segment sits lit on a kind the rows do not use
// and the pick that would fix it is the only way to say so: both readings are consulted, and
// it is stored unless it is the default under either. Everything else keeps the plain rule —
// a dumbbell's cfg-less 'none' is what a dumbbell row derives, and storing it would freeze
// 'none' onto the exercise everywhere, override the 'single' an added-weight entry derives,
// and leave no pick that takes the key off again.
const defaults = cfg ? [loadKindFor(null, ctx)]
: isBodyweightEq(ex.id) ? [true, false].map(b => loadKindFor(null, { id: ex.id, bodyweight: b }))
: [loadKindFor(null, ctx)]
// Picking what the equipment already implies puts the exercise back on it (a stamped null,
// lib/plates.js), so a later change of the equipment's own default still reaches it.
const setKind = k => update(s => {
s.loadKind = withLoadKind(s.loadKind, ex.id, defaults.every(d => d === k) ? null : k)
})
const setBar = v => update(s => {
s.barWeights = s.barWeights || {}
const n = Math.max(0, Math.round((v || 0) * 100) / 100)
if (n > 0) s.barWeights[ex.id] = n; else delete s.barWeights[ex.id]
})
// A quick pick of the type's own weight takes the override off, so the exercise keeps following
// your bars instead of freezing today's number onto it.
const pickBar = v => (v === def ? setBar(0) : setBar(v))
// "No bar" is a stored 0, which is a different thing from no entry at all: a counterbalanced
// Smith carriage weighs nothing in your hands, so the plate math and the drop-set steps must
// start from what you logged (issue #138). Clearing it goes back to the bar type's default.
const setNoBar = on => update(s => {
s.barWeights = s.barWeights || {}
if (on) s.barWeights[ex.id] = 0; else delete s.barWeights[ex.id]
})
return <>
<Segmented className="seg-inline" value={kind} onChange={setKind}
options={[{ value: 'pairs', label: t('Per side') }, { value: 'single', label: t('Single stack') }, { value: 'none', label: t('Off') }]} />
<div className="small dim" style={{ margin: '8px 0 12px' }}>
{kind === 'pairs' ? t('Plates split over both sides of a bar.')
: kind === 'single' ? t('One stack: a belt, a landmine, a plate-loaded machine, a sled.')
: t('No plate line under the sets.')}
</div>
{kind !== 'none' && <>
{bar && <div className="list menu-list" style={{ marginBottom: 6 }}>
<Row icon="barbell" title={t('No bar')} subtitle={t('The weight you log is all plates.')}>
<Switch checked={noBar} onChange={setNoBar} />
</Row>
</div>}
{!noBar && <div className="row cfgrow" style={{ marginBottom: 6 }}>
<Stepper label={bar ? t('Bar ({0})', st.unit) : t('Base weight ({0})', st.unit)} value={base} step={2.5} onChange={setBar} />
</div>}
{bar && !noBar && <div className="chips" style={{ flexWrap: 'wrap', overflow: 'visible', touchAction: 'auto', marginBottom: 6 }}>
{[...new Set([def, ...barPicks(st.unit)])].map(w => <button key={w} className={'chip nocap' + (base === w ? ' on' : '')}
aria-pressed={base === w} onClick={() => pickBar(w)}>{fmtNum(w) + ' ' + st.unit}</button>)}
</div>}
<div className="small dim" style={{ marginBottom: bar ? 10 : 18 }}>
{noBar ? t('Plates are counted from 0. Turn this off for the default ({0}).', fmtNum(def) + ' ' + st.unit)
: bar ? (explicit ? t('Set to 0 to go back to the default ({0}).', fmtNum(def) + ' ' + st.unit) : mine ? t('Your bar for this type, set in Your bars.') : t('Default for this bar type.'))
: t('The machine or sled itself, before any plate goes on. 0 if it is all plates.')}
{extra ? ' ' + extra : ''}
</div>
{/* The profile-wide side of it: one bar weight per type, for every exercise without its own. */}
{bar && <div className="list menu-list" style={{ marginBottom: 18 }}>
<Row icon="barbell" title={t('Your bars')} subtitle={t('Set the bar once for every exercise of a type.')} accessory="chevron" onClick={() => barDefaultsSheet()} />
</div>}
</>}
</>
}
// What a dumbbell or kettlebell weight means (issue #474, lib/dumbbells.js): as entered (the
// default, how every set before this was counted), one bell, or both together. `value` is the
// meaning in force and `onChange` stores a pick; the caller decides where (the routine's slot or
// the exercise's own default). The line under it says what the pick does to the numbers.
function DbLoadPicker({ ex, cfg, value, onChange }) {
const one = isOneArm({ ...(cfg || {}), id: ex.id })
return <>
<Segmented className="seg-inline" value={value} onChange={onChange}
options={[{ value: 'as', label: t('As entered') }, { value: 'each', label: t('Each') }, { value: 'total', label: t('Both') }]} />
<div className="small dim" style={{ margin: '8px 0 18px' }}>
{value === 'each' ? (one ? t('The weight of the one you hold. One arm, so it counts once.') : t('The weight of one, so 20 means 20 in each hand. Volume counts both.'))
: value === 'total' ? t('Both together, so 40 means two 20s.')
: t('Counted as you type it, the way it always was.')}
</div>
</>
}
// A machine's own weight stack (issue #688, lib/dumbbells.js): the weights its pin really
// selects, for this exercise everywhere. With a list, progression, deloads, warm-ups, drops and the
// + and − buttons land on these instead of on the increment, like the dumbbell list (#376). Tap a
// weight to take it off, the field adds one, and the quick fill writes an even stack to edit from.
function StackEditor({ ex }) {
const st = useStore(s => s.S)
const unit = st.unit === 'lb' ? 'lb' : 'kg'
const list = stackOf(st, ex.id)
const [draft, setDraft] = useState(null)
const [first, setFirst] = useState(null)
const [step, setStep] = useState(null)
const [count, setCount] = useState(null)
const save = ws => update(s => { s.stackWeights = withStack(s, ex.id, ws) })
const add = () => {
const w = cleanWeights([draft])[0]
if (!w) { toast(t('Enter a valid weight')); return }
save([...list, w])
setDraft(null)
}
const fill = () => {
const ws = stackFromStep(first, step, count || 10)
if (!ws.length) { toast(t('Enter a valid weight')); return }
save(ws)
}
return <>
<div className="small dim" style={{ marginBottom: 10 }}>
{t('The weights this machine really has, odd steps and all. Progression, deloads, warm-ups and the + and − buttons land only on these.')}
</div>
{list.length > 0
? <div className="chips" style={{ flexWrap: 'wrap', overflow: 'visible', touchAction: 'auto', marginBottom: 12 }}>
{list.map(w => <button key={w} className="chip on nocap" aria-label={t('Remove {0}', fmtPlate(w) + ' ' + unit)}
onClick={() => save(list.filter(x => x !== w))}>{fmtPlate(w)} <Icon name="xmark" /></button>)}
</div>
: <div className="small dim" style={{ marginBottom: 12 }}>{t('No list yet, so this exercise steps by its increment as usual.')}</div>}
<div className="row" style={{ gap: 8, marginBottom: 8 }}>
<NumberField className="input" decimal value={draft ?? ''} onChange={v => setDraft(v)}
aria-label={t('Weight ({0})', unit)} placeholder={t('Weight ({0})', unit)} onKeyDown={e => { if (e.key === 'Enter') add() }} />
<Button icon="plus" onClick={add} style={{ flex: '0 0 auto', width: 'auto' }}>{t('Add')}</Button>
</div>
{/* An evenly spaced stack in three numbers, to fix the odd ones on after. */}
{list.length === 0 && <>
<div className="row" style={{ gap: 8, marginBottom: 8 }}>
<NumberField className="input" decimal value={first ?? ''} onChange={setFirst} aria-label={t('First ({0})', unit)} placeholder={t('First ({0})', unit)} />
<NumberField className="input" decimal value={step ?? ''} onChange={setStep} aria-label={t('Step ({0})', unit)} placeholder={t('Step ({0})', unit)} />
</div>
<div className="row" style={{ gap: 8, marginBottom: 8 }}>
<NumberField className="input" value={count ?? ''} onChange={setCount} aria-label={t('How many')} placeholder={t('How many')} />
<Button onClick={fill} style={{ flex: '0 0 auto', width: 'auto' }}>{t('Fill in')}</Button>
</div>
</>}
{list.length > 0 && <Button variant="danger" onClick={() => save([])} style={{ marginBottom: 8 }}>{t('Clear the list')}</Button>}
<div style={{ height: 10 }} />
</>
}
// Mid-workout sheet behind the ⋯ menu's "Plate loading" — same values, same editor.
function BarWeightSheet({ exId, cfg, close }) {
const ex = exOr(exId)
return <>
<h3>{t('Plate loading')}</h3>
<div className={`muted small ${exerciseNameClass(ex)}`} style={{ marginBottom: 12 }}>{exerciseNameFor(ex)}</div>
<BarWeightEditor ex={ex} cfg={cfg} extra={t('Applies to this exercise everywhere, not just this plan.')} />
{canHaveStack(ex) && <><h4 className="sec">{t('Weight stack')}</h4><StackEditor ex={ex} /></>}
<Button variant="primary" onClick={close}>{t('Done')}</Button>
</>
}
export const barWeightSheet = (exId, cfg) => ui().openSheet(close => <BarWeightSheet exId={exId} cfg={cfg} close={close} />)
// The plates you own, as pairs per size, for the profile's unit (Settings → Equipment → Plates).
// The first edit copies the standard set into S.plates[unit] and changes one count in it, so the
// list you see is always the list the set rows load from. "Back to the standard set" empties the
// unit's list again. Both are stamped (lib/plates.js), so the last change wins a sync.
function PlateInventorySheet({ close }) {
const st = useStore(s => s.S)
const unit = st.unit === 'lb' ? 'lb' : 'kg'
const own = ownsPlates(st)
const setPairs = (w, n) => update(s => { s.plates = withPlatePairs(s, w, n) })
const reset = () => update(s => { s.plates = withStandardPlates(s) })
return <>
<h3>{t('Plates')}</h3>
<div className="muted small" style={{ marginBottom: 14 }}>
{t('Pairs of each size you own. Every set row loads from this list, so a home gym with one pair of 45s is never told to use two.')}
</div>
{PLATE_SIZES[unit].map(w => (
<div className="row cfgrow" key={w} style={{ marginBottom: 6 }}>
<Stepper label={fmtPlate(w) + ' ' + unit} value={pairsOf(st, w)} step={1} decimal={false} unit={t('pairs')} onChange={n => setPairs(w, n)} />
</div>
))}
<div className="small dim" style={{ margin: '4px 0 14px' }}>
{own ? t('Your own list for {0}.', unit) : t('The standard set, plenty of each. Change any count to make it yours.')}
</div>
{own && <Button onClick={reset} style={{ marginBottom: 8 }}>{t('Back to the standard set')}</Button>}
<Button variant="primary" onClick={close}>{t('Done')}</Button>
</>
}
export const plateInventorySheet = () => ui().openSheet(close => <PlateInventorySheet close={close} />)
// The dumbbells you own, for the profile's unit (Settings → Equipment → Dumbbells, issue #376):
// a list of single bells, kept per unit like the plates (lib/dumbbells.js). With one, progression,
// deloads, warm-ups, drops and the stepper of every dumbbell lift land on these; an empty list
// puts them back on the increment. Tapping a weight takes it off, the field adds one, and the
// quick fill writes the usual rack for the unit to edit from. Every change is stamped.
function DumbbellInventorySheet({ close }) {
const st = useStore(s => s.S)
const unit = st.unit === 'lb' ? 'lb' : 'kg'
const list = dumbbellsOf(st)
const [draft, setDraft] = useState(null)
const save = ws => update(s => { s.dumbbells = withDumbbells(s, ws) })
const none = ownsNoDumbbells(st)
const setNone = on => update(s => { s.dumbbells = withNoDumbbells(s, on) })
const preset = presetWeights(unit)
const add = () => {
const w = cleanWeights([draft])[0]
if (!w) { toast(t('Enter a valid weight')); return }
save([...list, w])
setDraft(null)
}
return <>
<h3>{t('Dumbbells')}</h3>
<div className="muted small" style={{ marginBottom: 14 }}>
{t('The weights you own, one bell each. Progression, deloads, warm-ups and the + and − buttons of dumbbell lifts land only on these.')}
</div>
{/* An empty list means "any weight", not "none", so owning none is its own switch (Discord,
deamondo). On, the list has nothing to say and steps aside; it is kept for the day you buy a pair. */}
<div className="list menu-list" style={{ marginBottom: 12 }}>
<Row icon="dumbbell" title={t('I own no dumbbells')} subtitle={t('Dumbbell lifts stay out of the library, the picker and the Coach’s plans.')}>
<Switch checked={none} onChange={setNone} />
</Row>
</div>
{!none && <>
{list.length > 0
? <div className="chips" style={{ flexWrap: 'wrap', overflow: 'visible', touchAction: 'auto', marginBottom: 12 }}>
{list.map(w => <button key={w} className="chip on nocap" aria-label={t('Remove {0}', fmtPlate(w) + ' ' + unit)}
onClick={() => save(list.filter(x => x !== w))}>{fmtPlate(w)} <Icon name="xmark" /></button>)}
</div>
: <div className="small dim" style={{ marginBottom: 12 }}>{t('No list yet, so dumbbell lifts step by their increment as usual.')}</div>}
<div className="row" style={{ gap: 8, marginBottom: 12 }}>
<NumberField className="input" decimal value={draft ?? ''} onChange={v => setDraft(v)}
aria-label={t('Weight ({0})', unit)} placeholder={t('Weight ({0})', unit)} onKeyDown={e => { if (e.key === 'Enter') add() }} />
<Button icon="plus" onClick={add} style={{ flex: '0 0 auto', width: 'auto' }}>{t('Add')}</Button>
</div>
<Button onClick={() => save(preset)} style={{ marginBottom: 8 }}>
{t('Fill in {0} to {1} {2}, every {3}', fmtPlate(preset[0]), fmtPlate(preset.at(-1)), unit, fmtPlate(preset[1] - preset[0]))}
</Button>
{list.length > 0 && <Button variant="danger" onClick={() => save([])} style={{ marginBottom: 8 }}>{t('Clear the list')}</Button>}
</>}
<Button variant="primary" onClick={close}>{t('Done')}</Button>
</>
}
export const dumbbellInventorySheet = () => ui().openSheet(close => <DumbbellInventorySheet close={close} />)
// Your bars (Settings > Equipment, and the plate-loading editor): one weight per bar type for the
// profile's unit (S.barDefaults, lib/bar.js), for every exercise of that type without a bar of its
// own. A short 15 kg olympic bar is set here once instead of on every lift. Logged weights stay
// totals, so history keeps its meaning; only the plate math and the drop steps move.
function BarDefaultsSheet({ close }) {
const st = useStore(s => s.S)
const unit = st.unit === 'lb' ? 'lb' : 'kg'
const set = (eq, w) => update(s => { s.barDefaults = withBarDefault(s, eq, w) })
const any = BAR_TYPES.some(eq => ownBarDefault(st, eq) !== null)
const name = eq => { const n = t(eq); return n.charAt(0).toLocaleUpperCase() + n.slice(1) }
return <>
<h3>{t('Your bars')}</h3>
<div className="muted small" style={{ marginBottom: 14 }}>
{t('What the bars at your gym weigh. Every exercise with that bar counts plates from it, unless you gave the exercise its own. Logged weights stay totals.')}
</div>
{BAR_TYPES.map(eq => {
const w = barTypeWeight(st, eq)
const builtIn = defaultBarWeight(eq, unit)
return <div key={eq} style={{ marginBottom: 12 }}>
<div className="row cfgrow" style={{ marginBottom: 6 }}>
<Stepper label={name(eq) + ' (' + unit + ')'} value={w} step={unit === 'lb' ? 5 : 2.5} onChange={v => set(eq, v)} />
</div>
<div className="chips" style={{ flexWrap: 'wrap', overflow: 'visible', touchAction: 'auto' }}>
{[...new Set([builtIn, ...barPicks(unit)])].map(p => <button key={p} className={'chip nocap' + (w === p ? ' on' : '')}
aria-pressed={w === p} onClick={() => set(eq, p)}>{fmtNum(p) + ' ' + unit}</button>)}
</div>
</div>
})}
<div className="small dim" style={{ margin: '4px 0 14px' }}>
{t('Most lifts in the library use “{0}”, a few use “{1}”. A short bar for all of them means setting both.', name('barbell'), name('olympic barbell'))}
</div>
{any && <Button onClick={() => update(s => { s.barDefaults = withStandardBars(s); if (!Object.keys(s.barDefaults).length) delete s.barDefaults })} style={{ marginBottom: 8 }}>{t('Back to the standard bars')}</Button>}
<Button variant="primary" onClick={close}>{t('Done')}</Button>
</>
}
export const barDefaultsSheet = () => ui().openSheet(close => <BarDefaultsSheet close={close} />)
/* ============================ exercise detail ============================ */
// Estimated 1RM for one exercise (issue #18): what the log already implies, plus a calculator
// for a set you have not done — so the number is reachable before there is any history.
function OneRM({ ex }) {
const st = useStore(s => s.S)
const best = best1RM(st, ex.id)
const [w, setW] = useState(best ? best.w : (st.exWeights[ex.id] || {}).w || 20)
const [r, setR] = useState(best ? best.r : 5)
const est = estimate1RM(w, r, formulaOf(st))
return <>
<h4 className="sec">{t('Estimated 1RM')}</h4>
{best && <div className="small" style={{ marginBottom: 8 }}>
{t('From your log:')} <b className="accent">{fmtNum(best.est)} {st.unit}</b>
<span className="dim"> · {t('{0} × {1} on {2}', fmtNum(best.w) + ' ' + st.unit, best.r, fmtDate(best.d, true))}</span>
</div>}
<div className="row cfgrow" style={{ marginBottom: 10 }}>
<Stepper label={t('Weight ({0})', st.unit)} value={w} step={2.5} onChange={setW} />
<Stepper label={t('Reps')} value={r} step={1} decimal={false} onChange={setR} />
</div>
<div className="row between" style={{ marginBottom: 4 }}>
<span className="muted small">{t('Estimate')}</span>
<b className="accent" style={{ fontSize: 20 }}>{est === null ? '–' : fmtNum(est) + ' ' + st.unit}</b>
</div>
<div className="small dim">{est === null
? t('Enter a weight and 1–{0} reps. Beyond that it’s basically guesswork.', REP_CAP)
: formulaOf(st) === 'weighted'
? t('A blend of seven formulas: calculated from one set, not a tested max.')
: t('{0} formula: calculated from one set, not a tested max.', FORMULA_NAMES[formulaOf(st)])}</div>
</>
}
// A muscle the catalogue credits with nothing (a bench press's biceps, #359) is not one it trains,
// so it is not shown as a tag either; "By muscle" already leaves the exercise out.
const trainedBy = ex => { const weights = muscleWeightsOf(ex); return slug => weights[slug] !== 0 }
// The detail page's tags: body part, main muscles, equipment, then up to three helpers. A word
// says once what it says: the body part "Chest" and the main muscle "Chest" of a bench press
// were two tags (QA 10-06), and a helper never repeats a main muscle.
export function detailTags(ex) {
const seen = new Set()
const out = []
const add = (label, extra) => {
const k = String(label || '').trim().toLocaleLowerCase()
if (!k || seen.has(k)) return false
seen.add(k)
out.push({ key: out.length, label, ...extra })
return true
}
add(t(ex.bp), { acc: true })
if (ex.bp === 'cardio') add(t(MUSCLE_NAME['cardiovascular system']), { icon: 'figureStrength' })
else for (const s of (ex.primaries?.length ? ex.primaries : (ex.tg ? [ex.tg] : [])).filter(trainedBy(ex))) add(t(MUSCLE_NAME[s] || s), { icon: 'figureStrength' })
add(t(ex.eq), { icon: 'kettlebell' })
let helpers = 0
for (const s of (ex.secondaries?.length ? ex.secondaries : smOf(ex)).filter(trainedBy(ex))) {
if (helpers >= 3) break
if (add(t(MUSCLE_NAME[s] || s))) helpers++
}
return out
}
function ExerciseDetail({ ex, close }) {
useLang() // the description and steps arrive in their own chunk the first time a sheet asks
const st = useStore(s => s.S)
const last = lastEntryFor(st, ex.id)
const best = bestWeightFor(st, ex.id, currentDbLoad(st, ex.id))
const fav = isFav(st, ex.id)
const flipFav = () => {
let on = false
update(s => { on = toggleFav(s, ex.id) })
toast(on ? t('Added to favourites') : t('Removed from favourites'))
}
return <>
<div className="row between" style={{ gap: 8, alignItems: 'flex-start' }}>
<h3 className={'exdetail-name ' + exerciseNameClass(ex)}>{exerciseNameFor(ex)}</h3>
<button className={'iconbtn fav-btn' + (fav ? ' on' : '')} aria-pressed={fav}
aria-label={fav ? t('Remove from favourites') : t('Add to favourites')} onClick={flipFav}>
<Icon name={fav ? 'starFill' : 'star'} />
</button>
</div>
<Media ex={ex} />
<div className="row" style={{ gap: 6, flexWrap: 'wrap', margin: '10px 0' }}>
{detailTags(ex).map(tg => <span key={tg.key} className={'tag' + (tg.acc ? ' acc' : '')}>{tg.icon && <Icon name={tg.icon} />}{tg.label}</span>)}
</div>
<ExerciseAliasesEditor key={ex.id} ex={ex} />
{descFor(ex) && <div className="exnote">{descFor(ex)}</div>}
{best > 0 && <div className="small row" style={{ marginBottom: 6, gap: 5 }}><Icon name="trophy" style={{ fontSize: 14, color: 'var(--yellow)' }} />{t('Best:')} <b className="accent" style={{ whiteSpace: 'nowrap' }}>{fmtNum(best)} {st.unit}</b>{last ? ` · ${t('last')} ${fmtDate(last.d)}: ${last.sets.map(s => setLabel(ex.id, s, last.target, speedUnitOf(st))).join(', ')}` : ''}</div>}
<Button variant="primary" icon="plus" style={{ margin: '10px 0 4px' }} onClick={() => addToRoutineSheet(ex)}>{t('Add to my plan')}</Button>
{last && <Button icon="history" style={{ marginTop: 4 }} onClick={() => exerciseHistorySheet(ex.id)}>{t('History')}</Button>}
{isCustomEx(ex) && <div className="row" style={{ gap: 8, marginTop: 8 }}>
<Button icon="pencil" style={{ flex: 1 }} onClick={() => { close(); customExSheet(ex) }}>{t('Edit')}</Button>
<Button variant="danger" icon="trash" style={{ flex: 1 }} onClick={() => deleteCustomEx(ex, close)}>{t('Delete')}</Button>
</div>}
{modeOf({ id: ex.id }) === 'reps' && <>
<h4 className="sec">{t('Plate loading')}</h4>
<BarWeightEditor ex={ex} extra={t('You still log the total weight. This only feeds the plate line under each set.')} />
{canHaveStack(ex) && <><h4 className="sec">{t('Weight stack')}</h4><StackEditor ex={ex} /></>}
</>}
{/* The exercise's own default meaning of its weight (S.dbLoad), stamped like the loading
above. A routine can still pick its own on the exercise's settings. */}
{isBellEx(ex) && <>
<h4 className="sec">{t('Weight means')}</h4>
<DbLoadPicker ex={ex} value={dbLoadFor(st, ex.id)}
onChange={v => update(s => { s.dbLoad = withDbLoad(s.dbLoad, ex.id, v) })} />
</>}
{/* No one-rep max on an assistance machine: the load is the help you were given, so the
calculator would answer "your 1RM is 23 kg" about a number that gets smaller as you get
stronger (issue #232). Cardio has none for the same kind of reason. */}
{!isCardio(ex) && !isAssisted(ex) && <OneRM ex={ex} />}
{instrFor(ex).length > 0 &&<><h4 className="sec">{t('How to')}{getLang() !== 'en' && !instrTranslated(ex) && <span className="dim" style={{ textTransform: 'none', letterSpacing: 0 }}> · {t('instructions in English')}</span>}</h4><ol className="steps-list">{instrFor(ex).map((s, i) => <li key={i}>{s}</li>)}</ol></>}
{/* The catalogue is community-edited (catalogue/README.md): a wrong muscle, a clumsy step or a
missing translation is one tap from an issue that already names the exercise. */}
{!isCustomEx(ex) && !ex.missing && <a className="small dim exfix-link" target="_blank" rel="noopener"
href={'https://github.com/DuarteSantos8/openGym/issues/new?template=exercise.yml&exercise=' + encodeURIComponent(ex.id + ', ' + ex.n)}>
<Icon name="pencil" /> {t('Suggest a fix for this exercise')}</a>}
</>
}
export const exerciseDetailSheet = ex => ui().openSheet(close => <ExerciseDetail ex={ex} close={close} />)
/* ============================ exercise history ============================ */
// What you did on this exercise before, reachable mid-workout (issue #43): the curve first,
// then the last sessions set by set, so the question "what did I do last month" is answered
// without leaving the workout for Stats. Derived once per log change — the sheet re-renders on
// every store tick while a session runs, and LineChart drops its hover whenever `points`
// changes identity, so a series rebuilt per render would lose the tooltip under your finger.
function ExerciseHistory({ exId }) {
const st = useStore(s => s.S)
const ex = exOr(exId)
const h = useMemo(() => exerciseHistory(st, exId), [st.workouts, exId, st.volumeWarmups])
const [curve, setCurve] = useState('top')
// Against body weight, as in Stats: the estimated 1RM (body weight included for bodyweight and
// assisted work) as a % of the body weight then. Only for rep work, and only once two sessions
// have a body weight to compare with.
const formula = formulaOf(st)
// The 1RM estimate refuses sets above ~12 reps (lib/onerm.js REP_CAP), so a calf raise or a leg
// press done for 15-20 has none; its curve then follows the heaviest set instead (as Stats does),
// when that one varies. An assistance machine has no 1RM either, and is always the heaviest set.
const { pts: rel, top: relTop } = useMemo(() => {
if (h.metric !== 'weight' && h.metric !== 'reps') return { pts: [], top: false }
const toPct = p => ({ t: p.t, d: p.d, y: p.y * 100, assisted: p.assisted })
const est = relativeSeries(st, exId, { basis: 'e1rm', formula })
if (est.length >= 2) return { pts: est.map(toPct), top: est.some(p => p.assisted) }
const top = relativeSeries(st, exId, { basis: 'top', formula })
const varies = top.length >= 2 && top.some(p => Math.abs(p.y - top[0].y) > 1e-9)
return varies ? { pts: top.map(toPct), top: true } : { pts: [], top: false }
}, [st.workouts, st.bodyweight, st.dbLoad, exId, h.metric, formula])
const canRel = rel.length >= 2
const onRel = curve === 'bodyweight' && canRel
const onE1 = !onRel && curve === 'e1rm' && h.e1rmPoints.length > 0
const unit = h.metric === 'weight' ? st.unit : h.metric === 'reps' ? t('reps') : h.metric === 'sec' ? 's' : t('min')
const e1Best = useMemo(() => Math.max(0, ...h.e1rmPoints.map(p => p.y)), [h])
const relBest = useMemo(() => Math.max(0, ...rel.map(p => p.y)), [rel])
const curveOpts = []
if (h.e1rmPoints.length > 0 && h.metric === 'weight') curveOpts.push({ value: 'top', label: t('Top set') }, { value: 'e1rm', label: t('Est. 1RM') })
if (canRel) {
if (!curveOpts.length) curveOpts.push({ value: 'top', label: h.metric === 'reps' ? t('Max reps') : t('Top set') })
curveOpts.push({ value: 'bodyweight', label: t('Bodyweight') })
}
if (!h.total) return <>
<h3 className={exerciseNameClass(ex)}>{exerciseNameFor(ex)}</h3>
<div className="empty"><div className="ico"><Icon name="history" /></div>{t('No sessions logged yet')}</div>
</>
const tail = s => [
s.volume > 0 && t('Volume') + ' ' + fmtVol(s.volume, st.unit),
s.e1rm != null && t('Est. 1RM') + ' ' + fmtNum(s.e1rm) + ' ' + st.unit,
].filter(Boolean).join(' · ')
return <>
<h3 className={exerciseNameClass(ex)} style={{ marginBottom: 2 }}>{exerciseNameFor(ex)}</h3>
<div className="muted small" style={{ marginBottom: 10 }}>{t('Exercise history')} · {tn('{0} session', '{0} sessions', h.total)}</div>
{/* Only reps work with a load produces an estimate, so the toggle is absent for the rest. */}
{curveOpts.length > 1 && <Segmented className="seg-range" value={onRel ? 'bodyweight' : onE1 ? 'e1rm' : 'top'} onChange={setCurve} options={curveOpts} />}
<div className="chart" style={{ marginTop: 8 }}>
<LineChart points={onRel ? rel : onE1 ? h.e1rmPoints : h.points} h={140} unit={onRel ? '% BW' : onE1 ? st.unit : unit} color="var(--blue)" />
</div>
<div className="small row" style={{ margin: '6px 0 4px', gap: 5 }}>
<Icon name="trophy" style={{ fontSize: 14, color: 'var(--yellow)' }} />
{t('Best:')} <b className="accent">{onRel ? fmtNum(relBest) + ' % BW' : fmtNum(onE1 ? e1Best : h.best) + ' ' + (onE1 ? st.unit : unit)}</b>
</div>
{onRel && <div className="small dim">{relTop ? t('Heaviest set as a share of body weight, per workout') : t('Estimated 1RM as a share of body weight, per workout')}</div>}
<h4 className="sec">{h.sessions.length < h.total ? t('Last {0} sessions', h.sessions.length) : t('Sessions')}</h4>
<div className="list">
{h.sessions.map(s => <div key={s.id} className="item" style={{ alignItems: 'flex-start' }}>
<div className="grow">
<div className="tt">{fmtDate(s.d, true)} {s.pr && <span className="pr"><Icon name="trophy" />PR</span>}</div>
<div className="ss">{s.sets.map(x => setLabel(exId, x, s.target, speedUnitOf(st))).join(' · ')}</div>
{tail(s) && <div className="small dim" style={{ marginTop: 3 }}>{tail(s)}</div>}
</div>
{s.value != null && s.value > 0 && <b className="accent nocap" style={{ whiteSpace: 'nowrap' }}>{fmtNum(s.value)} {unit}</b>}
</div>)}
</div>
</>
}
export const exerciseHistorySheet = exId => ui().openSheet(close => <ExerciseHistory exId={exId} close={close} />)
/* ============================ add to routine ============================ */
function AddToRoutine({ ex, close }) {
const st = useStore(s => s.S)
const pick = rid => {
close()
const isNew = rid === '_new'
exConfigSheet(ex, null, cfg => {
update(s => {
let r = isNew ? { id: uid(), name: t('New routine'), emoji: DEFAULT_GLYPH, ex: [] } : s.routines.find(x => x.id === rid)
if (isNew) s.routines.push(r)
if (r) r.ex.push({ id: ex.id, ...cfg })
})
const r = isNew ? S().routines[S().routines.length - 1] : st.routines.find(x => x.id === rid)
toast(t('“{0}” added to {1}', exerciseNameText(ex), r ? r.name : t('routine')))
if (isNew && r) nav('/plan/r/' + r.id)
}, null, isNew ? null : st.routines.find(x => x.id === rid))
}
return <>
<h3 className={exerciseNameClass(ex)}>{t('Add “{0}”', exerciseNameFor(ex))}</h3>
<div className="muted small" style={{ marginBottom: 12 }}>{t('Pick a routine. Sets, reps and weight come next.')}</div>
<div className="list">
{st.routines.map(r => <div key={r.id} className="item" {...tappable(() => pick(r.id))}>
<span className="lrow-i"><Icon name={glyphOf(r.emoji)} /></span>
<div className="grow"><div className="tt">{r.name}</div><div className="ss">{exCount(r.ex.length)}</div></div>
{r.ex.some(e => e.id === ex.id) && <span className="tag">{t('already in')}</span>}<Icon name="plus" className="chev" />
</div>)}
<div className="item" {...tappable(() => pick('_new'))}><span className="lrow-i" style={{ background: 'var(--surface-3)' }}><Icon name="plusCircle" /></span>
<div className="grow"><div className="tt">{t('New routine')}</div><div className="ss">{t('Create one and start with this exercise')}</div></div><Icon name="plus" className="chev" /></div>
</div>
</>
}
export const addToRoutineSheet = ex => ui().openSheet(close => <AddToRoutine ex={ex} close={close} />)
/* ============================ custom exercises (issue #11) ============================ */
// Name + body part is all it takes — the exercise then behaves like any built-in one
// (planning, logging, PRs, stats). A photo, GIF or video of your own, and a link to a video or
// guide, are optional (components/CustomMediaField.jsx): the state keeps a small reference to the
// file, never the file itself, and the link is cleaned on save and again whenever it is opened.
function CustomExForm({ existing, prefill, onDone, close }) {
const nameRef = useRef(null)
const onNameFocus = useSheetKeyboard(nameRef)
const [n, setN] = useState(existing ? existing.n : (prefill || ''))
const [aliases, setAliases] = useState(() => normalizeAliases(existing?.aliases).join(', '))
const [aliasesOpen, setAliasesOpen] = useState(false)
const [bp, setBp] = useState(existing ? existing.bp : '')
const [eq, setEq] = useState(existing ? (existing.eq || '') : '')
const storedEq = existing && typeof existing.eq === 'string' && existing.eq && !ALL_EQUIPMENT.includes(existing.eq) ? existing.eq : null
const [desc, setDesc] = useState(existing ? (existing.desc || '') : '')
const [media, setMedia] = useState(() => mediaOf(existing))
// Whether this form changed the media at all. An edit that did not touch it writes back what the
// exercise had, byte for byte: a ref from a newer version of the app (a codec or type this one
// does not know) reads as nothing here, and re-normalizing it on a rename would delete it on
// every device.
const [mediaTouched, setMediaTouched] = useState(false)
const [url, setUrl] = useState(existing && typeof existing.url === 'string' ? existing.url : '')
const onMedia = patch => {
if ('media' in patch) { setMedia(patch.media); setMediaTouched(true) }
if ('url' in patch) setUrl(patch.url)
}
const [primaries, setPrimaries] = useState(() => {
if (existing && Array.isArray(existing.primaries) && existing.primaries.length) return [...existing.primaries]
if (existing?.bp === 'cardio') return ['cardiovascular system']
const norm = hasExplicitMuscleMetadata(existing || {}) ? muscleGroupsOf(existing || {}) : []
return norm.length ? [norm[0]] : []
})
const [secondaries, setSecondaries] = useState(() => {
if (existing && Array.isArray(existing.primaries) && existing.primaries.length) return [...(existing.secondaries || [])]
const norm = hasExplicitMuscleMetadata(existing || {}) ? muscleGroupsOf(existing || {}) : []
return norm.slice(1)
})
// Every primary chip this sheet saw a tap on, in that order. `primaries` alone cannot say what the
// user reached for first: an existing exercise seeds it in the map's order, because that is how the
// muscles are stored. The taps are the only record of the user's own order, and they decide the
// target when the one the exercise had is un-ticked.
const [primaryTaps, setPrimaryTaps] = useState([])
const togglePrimary = value => {
setPrimaryTaps(current => [...current, value])
setPrimaries(current => current.includes(value) ? current.filter(m => m !== value) : [...current, value])
}
const toggleSecondary = value => setSecondaries(current => current.includes(value) ? current.filter(m => m !== value) : [...current, value])
const save = () => {
const name = n.trim()
if (!name) { toast(t('Give it a name')); return }
if (!bp) { toast(t('Pick a body part')); return }
if (!eq) { toast(t('Pick equipment')); return }
// An exercise kept under its own name is not a duplicate, even when the catalogue has one
// spelled the same (an imported "trap bar deadlift"): only a new or changed name is checked.
const renamed = !existing || name.toLowerCase() !== String(existing.n || '').trim().toLowerCase()
const dup = renamed && allExercises(S()).find(e => e.n.toLowerCase() === name.toLowerCase() && e.id !== (existing || {}).id)
if (dup) { toast(t('“{0}” already exists', dup.n)); return }
const d = desc.trim().slice(0, 1000)
// An empty field removes the link; anything else has to be a web address.
const link = url.trim() ? cleanUrl(url) : null
if (url.trim() && !link) { toast(t('That link is not a web address')); return }
const keepMedia = !!existing && !mediaTouched
const ref = keepMedia ? null : normalizeMediaRef(media)
// Stored in the map's order, not the order the chips were tapped in — the tags on the exercise
// used to shuffle with every edit.
const prim = bp === 'cardio' ? ['cardiovascular system'] : inMuscleOrder(primaries)
const sm = inMuscleOrder(secondaries.filter(m => !prim.includes(m)))
const groups = [...prim, ...sm]
// The one-word target (the library row, the picker, the Muscles view) is not the sorted list's
// head — a hip thrust with Traps as an extra primary is not a Traps exercise. It is the primary
// tapped first, and an edit keeps the exercise's target as long as that muscle is still a primary.
// Once that muscle is gone the next answer is the first one tapped here that survived — a tap that
// only turned a chip off says nothing and is skipped with it. The sorted list is the last resort,
// for the user who drops the target and adds nothing in its place.
const tg = (existing && prim.includes(existing.tg)) ? existing.tg : (primaryTaps.find(m => prim.includes(m)) || prim[0] || '')
let id = existing && existing.id
const extra = c => {
c.aliases = normalizeAliases(aliases)
if (!keepMedia) { if (ref) c.media = ref; else delete c.media }
if (link) c.url = link; else delete c.url
}
if (existing) update(s => { const c = (s.customEx || []).find(x => x.id === id); if (c) {
c.n = name; c.bp = bp; c.desc = d; c.tg = tg; c.sm = sm; c.muscleGroups = groups; c.primaries = prim; c.secondaries = sm; c.eq = eq
extra(c)
} })
else {
id = 'c' + uid()
update(s => { const c = { id, n: name, bp, desc: d, tg, sm, muscleGroups: groups, primaries: prim, secondaries: sm, eq, custom: true }; extra(c); (s.customEx = s.customEx || []).push(c) })
}
// The file goes to the server now rather than after the state's own debounce: another device
// that sees the reference first shows a tile until it arrives.
if (ref) syncMedia({ force: true })
close()
toast(existing ? t('Saved') : t('“{0}” created', name))
onDone && onDone(EXIDX[id])
}
return <>
<h3>{existing ? t('Edit custom exercise') : t('Create your own exercise')}</h3>
<div className="muted small" style={{ marginBottom: 12 }}>{t('Name it and pick a body part. It works just like any other exercise.')}</div>
<input ref={nameRef} className="input" placeholder={t('Exercise name')} maxLength={80} value={n} onFocus={onNameFocus} onChange={e => setN(e.target.value)} />
{aliasesOpen ? <ExerciseAliasesField value={aliases} onChange={setAliases} autoFocus />
: <div><ExerciseAliasesLink names={normalizeAliases(aliases)} onOpen={() => setAliasesOpen(true)} /></div>}
<div className="chips" style={{ margin: '12px 0' }}>
{BODYPARTS.map(b => <button key={b} className={'chip' + (bp === b ? ' on' : '')} onClick={() => setBp(b)}>{t(b)}</button>)}
</div>
<div className="chips" style={{ margin: '12px 0' }}>
{ALL_EQUIPMENT.map(k => (<button key={k} className={'chip' + (eq === k ? ' on' : '')} onClick={() => setEq(k)}>{t(k)}</button>))}
{/* An imported exercise can carry equipment the list does not have (`custom` from a CSV or
Hevy import). It stays a chip of its own, picked, so Save does not stop on "Pick equipment". */}
{storedEq && <button key={storedEq} className={'chip' + (eq === storedEq ? ' on' : '')} onClick={() => setEq(storedEq)}>{t(storedEq)}</button>}
</div>
{bp && <>
{bp !== 'cardio' && <MultiSelectRow title={t('Primary muscle groups')} sheetTitle={t('Primary muscle groups')}
values={primaries}
options={MUSCLES.map(m => ({ value: m, label: t(MUSCLE_NAME[m]) }))}
onToggle={togglePrimary} noneLabel={t('No explicit muscle group')} doneLabel={t('Done')} />
}
<MultiSelectRow title={t('Additional muscle groups')} sheetTitle={t('Additional muscle groups')}
values={secondaries}
options={MUSCLES.filter(m => !primaries.includes(m)).map(m => ({ value: m, label: t(MUSCLE_NAME[m]) }))}
onToggle={toggleSecondary} noneLabel={t('No explicit muscle group')} doneLabel={t('Done')} />
</>}
{bp === 'cardio' && <div className="small dim row" style={{ marginBottom: 10, gap: 5 }}><Icon name="figureRun" style={{ fontSize: 13 }} />{t('Cardio exercises log time + speed instead of weight × reps.')}</div>}
<textarea className="input" rows={4} maxLength={1000} placeholder={t('Description (optional): setup, cues, anything you want to remember')}
value={desc} onChange={e => setDesc(e.target.value)} />
<CustomMediaField media={media} url={url} onChange={onMedia} />
<div style={{ height: 14 }} />
<Button variant="primary" onClick={save}>{existing ? t('Save') : t('Create exercise')}</Button>
{existing && <><div style={{ height: 8 }} /><Button variant="danger" icon="trash" onClick={() => { close(); deleteCustomEx(existing) }}>{t('Delete exercise')}</Button></>}
</>
}
export const customExSheet = (existing, onDone, prefill) => ui().openSheet(close => <CustomExForm existing={existing} prefill={prefill} onDone={onDone} close={close} />)
export function deleteCustomEx(ex, afterDelete) {
if (S().active?.entries.some(e => e.id === ex.id)) { toast(t('Finish your current workout first')); return }
confirmSheet({
title: t('Delete “{0}”?', ex.n),
message: t('It will be removed from your routines. Already-logged workouts keep their sets.'),
confirmText: t('Delete'), danger: true,
onConfirm: () => {
update(s => {
// Keep display and muscle metadata in history before the custom catalogue row disappears.
const snapshot = exerciseMuscleSnapshot(ex)
s.workouts.forEach(w => w.entries.forEach(e => {
if (e.id !== ex.id) return
e.n = ex.n
if (!e.muscleSnapshot || !Object.keys(e.muscleSnapshot).length) e.muscleSnapshot = snapshot
}))
s.customEx = (s.customEx || []).filter(x => x.id !== ex.id)
s.routines.forEach(r => { r.ex = r.ex.filter(e => e.id !== ex.id); cleanupSg(r.ex) })
delete s.exWeights[ex.id]
s.favEx = (s.favEx || []).filter(id => id !== ex.id)
})
toast(t('Exercise deleted'))
afterDelete && afterDelete()
}
})
}
/* ============================ exercise picker ============================ */
// Exercises already used in your routines or past workouts (for the "Chosen" filter + a marker).
function usageMap(st) {
const u = {}
st.routines.forEach(r => r.ex.forEach(e => { u[e.id] = (u[e.id] || 0) + 1 }))
st.workouts.forEach(w => w.entries.forEach(e => { u[e.id] = (u[e.id] || 0) + 1 }))
return u
}
// The swap pickers' own scope (#473): exercises sharing the swapped exercise's target muscle.
const SAME = '≈'
function ExercisePicker({ onPick, title, close, like, actionIcon, backups }) {
const st = useStore(s => s.S)
const isSwap = !!like || title === t('Swap exercise') || title === t('Replace exercise') || title === 'Swap exercise' || title === 'Replace exercise'
const quickIcon = actionIcon || (isSwap ? 'swap' : 'plus')
const quickLabel = e => isSwap
? (title ? `${title}: ${exerciseNameText(e)}` : `${t('Swap exercise')}: ${exerciseNameText(e)}`)
: t('Add “{0}”', exerciseNameText(e))
const usage = usageMap(st)
const [q, setQ] = useState('')
const backupList = (Array.isArray(backups) ? backups : []).map(id => EXIDX[id]).filter(Boolean)
// What a swap is away from: the picker opens on what trains the same muscle, if anything you
// can use does, and "All" is one tap back to everything.
const likeEx = like ? (EXIDX[like] || allExercises(st).find(e => e.id === like) || null) : null
// A muscle nothing else trains gets no chip at all rather than one that lists nothing.
const likeTg = swapMuscleOf(likeEx) && sameMuscleFirst(allExercises(st), likeEx).length ? swapMuscleOf(likeEx) : ''
const [bp, setBp] = useState(() => {
if (!likeTg) return ''
const prof = activeProfile(st)
return sameMuscleFirst(allExercises(st), likeEx).some(e => !prof || exAvailable(st, e)) ? SAME : ''
}) // '' = all, '★' = chosen, '☆' = favourites, '≈' = same muscle, else a body part
const [eq, setEq] = useState('') // '' = any equipment
const [cat, setCat] = useState('') // '' = any type
const [showAll, setShowAll] = useState(false)
const [shown, setShown] = useState(50)
const [byMuscle, setByMuscle] = useState(false)
const searchRef = useRef(null)
const bpStrip = useRef(null), eqStrip = useRef(null), catStrip = useRef(null)
const onSearchFocus = useSheetKeyboard(searchRef)
const moreRef = useAutoMore(() => setShown(s => s + 50))
const all = allExercises(st)
const profile = activeProfile(st)
const inScope = e => bp === '★' ? usage[e.id] : bp === '☆' ? isFav(st, e.id) : (!bp || e.bp === bp)
let inPart = searchExercises(bp === SAME ? sameMuscleFirst(all, likeEx) : all.filter(inScope), q)
if (bp === '★') inPart = [...inPart].sort((a, b) => (usage[b.id] - usage[a.id]) || exerciseNameFor(a).localeCompare(exerciseNameFor(b)))
// The Library's Type row (issue #521), with its fallback: a type the search or muscle group
// narrowed away is ignored for this view, not forgotten.
const { opts: catOpts, on: catOn, list: base } = byCategory(inPart, cat)
const eqFiltered = (profile && !showAll) ? base.filter(e => exAvailable(st, e)) : base
const eqOpts = equipmentOf(eqFiltered)
// Drop the equipment filter if the search narrowed it away, so you never hit a dead end.
const eqOn = eqOpts.includes(eq) ? eq : ''
// Favourites float to the top of whatever the filters left (issue #6), the rest keeps its order.
// On the same-muscle list the equipment order wins: what is on the same kit comes first,
// favourites leading within each half.
// While searching, what you have logged or planned follows right after them.
const narrowedList = eqOn ? eqFiltered.filter(e => e.eq === eqOn) : eqFiltered
const ranked = q.trim() ? sortYoursFirst(narrowedList, st, usage) : sortFavouritesFirst(narrowedList, st)
const f = bp === SAME ? sameMuscleFirst(ranked, likeEx) : ranked
const chosenCount = Object.keys(usage).length
const favCount = (st.favEx || []).length
const special = bp === '★' || bp === '☆'
// The Library's live result count (GitLab !31), for the same reason: how many are left.
const narrowed = !!(q.trim() || bp || eqOn || catOn)
useRevealActiveChip(bpStrip, bp)
useRevealActiveChip(eqStrip, eqOn)
useRevealActiveChip(catStrip, catOn)
// Close but not exact (a typo too many, most of the words, a similar name), listed under the
// results once you have scrolled to their end, so a search almost never ends on nothing.
// Worked out a beat behind the typing (useDeferredValue): the list itself never waits for it.
const qLate = useDeferredValue(q)
const similar = q.trim() && qLate === q && !special && f.length <= shown
? similarExercises((profile && !showAll ? all.filter(e => exAvailable(st, e)) : all).filter(e => bp === SAME || !bp || e.bp === bp), q, f, f.length ? 12 : 30)
: []
const pickRow = e => <div key={e.id} className="item" {...tappable(() => onPick(e))}>
<Thumb ex={e} /><div className="grow"><div className={`tt ${exerciseNameClass(e)}`}>{isFav(st, e.id) && <Icon name="starFill" className="fav-star" />}{exerciseNameFor(e)}</div><div className="ss capitalize">{t(MUSCLE_NAME[e.tg] || e.tg || e.bp)} · {t(e.eq)}</div></div>
{/* Accent tag = already in a routine/log ("Chosen"); the yellow star by the name = favourite. */}
{usage[e.id] && <span className="tag acc"><Icon name="starFill" /></span>}
{/* A "+" or "swap" glyph reads as "commit this now" — it used to just open the same detail sheet as
tapping the row, so it added nothing until you'd scrolled past the sets/reps config
and found the real button. Now it does what it looks like: commits with the default
config right away. In a swap or replace flow (#561), the opposed-arrows glyph makes it clear
this replaces the chosen exercise rather than adding a new one. Tapping the row itself still
opens the detail/config sheet, for when you want to set sets/reps before committing. */}
<button className="iconbtn chev" aria-label={quickLabel(e)} style={{ padding: 8, margin: -8 }}
onClick={ev => { ev.stopPropagation(); onPick(e, true) }}><Icon name={quickIcon} /></button>
</div>
if (byMuscle) return <>
<div className="row between" style={{ marginBottom: 10 }}><h3>{title || t('Add exercise')}</h3>
<Button size="sm" variant="ghost" onClick={() => setByMuscle(false)}>{t('All')}</Button>
</div>
<MuscleExplorer onPick={onPick} pickIcon={quickIcon} />
</>
return <>
<div className="row between" style={{ marginBottom: 10 }}><h3>{title || t('Add exercise')}</h3>
<Button size="sm" variant="tinted" icon="figureStrength" onClick={() => setByMuscle(true)}>{t('By muscle')}</Button>
</div>
{/* .picker-search is what index.css keys the keyboard-aware sheet layout on: the sheet
lifts above the keys and the search stays put while the list scrolls under it. */}
<div className="picker-search"><div className="search-row"><div className={'search' + (narrowed ? ' has-count' : '') + (q ? ' has-clear' : '')}><svg viewBox="0 0 24 24"><circle cx="11" cy="11" r="7" /><path d="m21 21-4.3-4.3" /></svg>
<input ref={searchRef} className="input" placeholder={t('Search {0} exercises…', all.length)} value={q} onFocus={onSearchFocus} onChange={e => { setQ(e.target.value); setShown(50) }} />
{narrowed && <span className="search-count" role="status" aria-label={exCount(f.length)}>{fmtNum(f.length)}</span>}
{q && <button className="clear" onClick={() => { setQ(''); setShown(50) }} aria-label={t('Clear')}><Icon name="xmark" /></button>}</div>
<ExerciseViewToggle /></div></div>
{profile && <div className="small dim row" style={{ margin: '8px 0 2px', gap: 6, alignItems: 'center' }}>
<Icon name="kettlebell" style={{ fontSize: 13 }} />
{showAll ? t('Showing all equipment') : t('Showing what you have in "{0}"', profile.name)}
<button className="chip nocap" style={{ marginInlineStart: 'auto', padding: '3px 10px', fontSize: 12 }} onClick={() => setShowAll(v => !v)}>
{showAll ? t('Filter by "{0}"', profile.name) : t('Show all equipment')}
</button>
</div>}
{/* Changing muscle group keeps the equipment filter: if it still has exercises under the new
group the filter stays applied, and if not the eqOn fallback above drops it for this view
without forgetting the choice (issue #71). The favourites/chosen chips still clear it —
those are cross-body-part views where a stale equipment filter would be confusing. */}
<div className="chips" ref={bpStrip} style={{ margin: eqOpts.length > 1 || catOpts.length > 1 ? '10px 0 6px' : '10px 0' }}>
{likeTg && <button className={'chip nocap' + (bp === SAME ? ' on' : '')} onClick={() => { setBp(SAME); setShown(50) }}><Icon name="figureStrength" style={{ fontSize: 12, display: 'inline-block', marginInlineEnd: 4, verticalAlign: '-1px' }} />{t('Same muscle: {0}', t(MUSCLE_NAME[likeTg] || likeTg))}</button>}
{favCount > 0 && <button className={'chip' + (bp === '☆' ? ' on' : '')} onClick={() => { setBp('☆'); setEq(''); setShown(50) }}><Icon name="starFill" className="fav-star" />{t('Favourites')} ({favCount})</button>}
{chosenCount > 0 && <button className={'chip' + (bp === '★' ? ' on' : '')} onClick={() => { setBp('★'); setEq(''); setShown(50) }}><Icon name="starFill" style={{ fontSize: 12, display: 'inline-block', marginInlineEnd: 4, verticalAlign: '-1px' }} />{t('Chosen')} ({chosenCount})</button>}
<button className={'chip nocap' + (!bp ? ' on' : '')} onClick={() => { setBp(''); setShown(50) }}>{t('All')}</button>
{BODYPARTS.map(b => <button key={b} className={'chip' + (bp === b ? ' on' : '')} onClick={() => { setBp(b); setShown(50) }}>{t(b)}</button>)}
</div>
{catOpts.length > 1 && <div className="chips" ref={catStrip} style={{ marginBottom: eqOpts.length > 1 ? 6 : 10 }}>
<button className={'chip nocap' + (!catOn ? ' on' : '')} onClick={() => { setCat(''); setShown(50) }}>{t('Any type')}</button>
{catOpts.map(x => <button key={x} className={'chip' + (catOn === x ? ' on' : '')} onClick={() => { setCat(x); setShown(50) }}>{t(x)}</button>)}
</div>}
{eqOpts.length > 1 && <div className="chips" ref={eqStrip} style={{ marginBottom: 10 }}>
<button className={'chip nocap' + (!eqOn ? ' on' : '')} onClick={() => { setEq(''); setShown(50) }}>{t('Any equipment')}</button>
{eqOpts.map(x => <button key={x} className={'chip' + (eqOn === x ? ' on' : '')} onClick={() => { setEq(x); setShown(50) }}>{t(x)}</button>)}
</div>}
<div className={'list' + (st.exerciseView === 'cards' ? ' ex-grid' : '')}>
{!special && <div className="item ex-new" {...tappable(() => customExSheet(null, ex => onPick(ex), q.trim()))}>
<div className="thumb thumb-x"><Icon name="plusCircle" /></div>
<div className="grow"><div className="tt">{t('Create your own exercise')}</div><div className="ss">{t('name + body part, and a photo or video if you like')}</div></div><Icon name="plus" className="chev" />
</div>}
{/* The slot's planned substitutes, first, while nothing is typed: the plan already knows. */}
{backupList.length > 0 && !q.trim() && <>
<h4 className="sec">{t('Your backups')}</h4>
{backupList.map(pickRow)}
<h4 className="sec">{t('Everything else')}</h4>
</>}
{f.slice(0, shown).map(pickRow)}
{f.length === 0 && bp === '★' && <div className="empty">{t('Nothing picked yet. Add some exercises and they’ll show up here.')}</div>}
{f.length === 0 && bp === '☆' && <div className="empty">{t('No favourites yet. Tap the star on an exercise to add one.')}</div>}
{similar.length > 0 && <h4 className="sec similar-label">{f.length ? t('Similar exercises') : t('No exact match. These come close:')}</h4>}
{similar.map(pickRow)}
</div>
{f.length > shown && <><div style={{ height: 8 }} /><Button ref={moreRef} onClick={() => setShown(s => s + 50)}>{t('Show more')}</Button></>}
</>
}
// `title` names what the pick is for when it is not an add — the routine editor's Replace (#110).
// `like` is the exercise a swap or a replace is away from: the picker then opens on the ones that
// train the same muscle, same equipment first (#473).
// `backups`: the slot's planned substitutes (lib/routine-backups.js), listed first in a swap.
export const exercisePicker = (onPick, { title, like, actionIcon, backups } = {}) => ui().openSheet(close => <ExercisePicker onPick={onPick} title={title} like={like} actionIcon={actionIcon} backups={backups} close={close} />)
/** Start a safe swap for one exact active-workout occurrence. */
export function swapActiveWorkoutExercise(index) {
const active = S().active
if (!active?.entries?.[index]) return
// The "+" on a picker row commits with the default config, exactly as it does in the add
// flows; tapping the row still opens the config sheet first.
// Worded for the session, not the routine: a swap changes today's workout only.
const picker = exercisePicker((ex, quick) => quick ? swapTo(ex, pickedExerciseConfig(ex.id)) : exConfigSheet(ex, null, cfg => swapTo(ex, cfg), null, null, null, null, t('Use in this workout')), { title: t('Swap exercise'), like: active.entries[index].id, backups: active.entries[index].target?.subs })
function swapTo(ex, cfg) {
// The picker is a chooser here, not a stack you keep adding from: one swap, then back to
// the workout. (The add flow deliberately leaves it open.)
picker.close()
commitActiveSwap(index, ex, cfg)
}
}
/**
* One tap from the exercise's ⋯ menu (#649): a planned backup takes the slot with the slot's own
* plan, and the exercise it replaces becomes its first backup (backupSlotConfig).
*/
export function swapActiveWorkoutToBackup(index, backupId) {
const st = S()
const current = st.active?.entries?.[index]
const ex = EXIDX[backupId]
if (!current || !ex || backupId === current.id) return
const cfg = backupSlotConfig({ ...plannedConfigOf(current), id: current.id }, backupId, st, current.rid, id => !!EXIDX[id])
commitActiveSwap(index, ex, cfg)
}
function commitActiveSwap(index, ex, cfg) {
const st = S()
const current = st.active?.entries?.[index]
if (!current) return
// A swap is an in-place substitution — routine identity is unchanged, so the replacement
// keeps the slot's own `rid` and reads its prescription from that routine (not a
// session-wide one). A slot with no `rid` is freestyle.
const slotRoutine = current.rid ? st.routines.find(r => r.id === current.rid) : null
// Same rows the add flow builds (buildSessionEntry).
const built = buildSessionEntry(st, ex.id, cfg, slotRoutine, current)
const replacement = {
id: ex.id,
...built,
...(current.rid ? { rid: current.rid } : {}),
// A slot kept out of progression stays out once swapped. Replaced in place the entry keeps
// it anyway; inserted beside logged sets, the replacement would otherwise count as a
// regular session of the new exercise. Only a deload or rehab slot is built without a
// prescription (above): one kept out by hand keeps the marker and its Undo on the card,
// and an Undo must leave numbers that are right to count.
...(current.noProg === true ? { noProg: true } : {}),
}
const apply = options => {
// A timed callback closes over entry/set indexes. Invalidate it, and the current rest,
// before the selected occurrence can be replaced or a new entry shifts those indexes.
ui().stopWork()
ui().stopRest()
update(state => { swapActiveExercise(state.active, index, replacement, options) }, true)
}
const logged = (current.sets || []).some(set => set.done === true)
if (!logged) { apply(); return }
if (current.sg) {
swapGroupSheet(groupDisposition => apply({ loggedConfirmed: true, groupDisposition }))
return
}
confirmSheet({
title: t('Swap exercise?'),
message: t('Logged sets stay with the original exercise. The replacement will be inserted afterward.'),
confirmText: t('Continue'),
onConfirm: () => apply({ loggedConfirmed: true })
})
}
/** A logged superset member being swapped: the replacement stays in the group or goes after it. */
export function swapGroupSheet(choose) {
ui().openSheet(close => <>
<h3>{t('Swap exercise?')}</h3>
<div className="muted small" style={{ marginBottom: 12 }}>
{t('Logged sets stay with the original exercise. Choose where the replacement belongs.')}
</div>
<Button variant="primary" onClick={() => { close(); choose('keep') }}>
{t('Keep replacement in this group')}
</Button>
<div style={{ height: 8 }} />
<Button variant="ghost" onClick={() => { close(); choose('detach') }}>
{t('Insert after this group')}
</Button>
</>)
}
/* ============================ equipment profiles ============================ */
// Create or edit one profile ("Home", "Gym", ...): a name plus a checklist of what you have.
function EquipmentProfileSheet({ profile, close }) {
const update = useStore(s => s.update)
const nameRef = useRef(null)
const [checked, setChecked] = useState(new Set(profile ? profileEquipment(profile) : []))
const toggle = k => setChecked(s => { const n = new Set(s); n.has(k) ? n.delete(k) : n.add(k); return n })
const save = () => {
const name = (nameRef.current.value || '').trim()
if (!name) { return }
update(s => {
s.equipProfiles = s.equipProfiles || []
const equipment = [...checked]
if (profile) {
const p = s.equipProfiles.find(x => x.id === profile.id)
if (p) { p.name = name; p.equipment = equipment; p.accV = ACC_V }
} else {
const p = newProfile(name); p.equipment = equipment
s.equipProfiles.push(p)
if (!s.activeEquipId) s.activeEquipId = p.id
}
})
close()
}
return <>
<h3>{profile ? t('Edit profile') : t('New equipment profile')}</h3>
<div className="muted small" style={{ marginBottom: 14 }}>
{t('Name it after where you train (like "Home" or "Gym"), then tick what you’ve got there.')}
</div>
<TextField ref={nameRef} defaultValue={profile?.name || ''} placeholder={t('Profile name')} maxLength={40} />
<div style={{ height: 12 }} />
<div className="chips">
{ALL_EQUIPMENT.map(k => (
<button key={k} className={'chip' + (checked.has(k) ? ' on' : '')} onClick={() => toggle(k)}>{t(k)}</button>
))}
</div>
<div className="dim small" style={{ marginTop: 10 }}>
{t('Body-weight exercises are always available, unless they need a pull-up bar or a bench.')}
</div>
<div style={{ height: 14 }} /><Button variant="primary" onClick={save}>{t('Save')}</Button>
</>
}
export const equipmentProfileSheet = profile => ui().openSheet(close => <EquipmentProfileSheet profile={profile} close={close} />)
/* ============================ exercise config ============================ */
// Progression settings for one exercise (issue #17). Shown inside the config sheet because
// "how does this lift go up" belongs next to sets and reps, not in a separate screen. Left
// on "follow the routine" it inherits, so most people never touch it.
const progressionStepOf = (c, mode, ex, unit) =>
c.inc >= 0 ? c.inc : (mode === 'time' ? 5 : defaultIncrement(ex.id, unit))
// Back-off sets step down by the same Step, so a plan using them needs one even with no rule.
// Bodyweight reps with nothing added (#583) have no load to step: they climb in reps, then sets
// (lib/progression.js climbsReps), so no step is asked for there.
const progressionStepIsValid = (step, policy, backoff = false, weightless = false) =>
weightless || (policy === 'off' && !backoff) || (Number.isFinite(step) && step > 0)
const weightlessOf = (c, mode, bw) => mode === 'reps' && bw && !(Number(c.weight) > 0)
// Back-off sets (lib/backoff.js) are offered where there is a load to step down: rep work on a
// loaded exercise, not a pyramid (it owns each set), not a rest-pause (one work set), not an
// assistance machine (less load is harder there).
const backoffOffered = (c, mode, ex, bw) => mode === 'reps' && !bw && !isAssisted({ ...c, id: ex.id })
&& !(Array.isArray(c.pyramid) && c.pyramid.length) && c.intensifier?.type !== 'restpause'
// Double and triple progression both climb a rep range (`repsMin`..`reps`), so the sheet keeps it
// normalised for either.
const climbsRange = policy => policy === 'double' || policy === 'triple'
// Triple progression's set ceiling while it is being edited: never below the starting sets.
const setsMaxOf = c => Math.max(Math.max(1, Math.round(c.sets) || 1), Math.min(MAX_TRIPLE_SETS, Math.round(c.setsMax) || 0))
function ProgressionFields({ ex, mode, c, setC, routine, unit, perSide, bw }) {
// Triple progression adds sets and then load. Bodyweight work with nothing added already climbs
// reps and then sets on its own (the rep ceiling below), and has no load to finish the cycle
// with, so it is not offered there.
const options = (POLICIES_FOR[mode] || ['off']).filter(p => p !== 'triple' || !bw || c.weight > 0 || c.prog === 'triple')
if (options.length < 2) return null
const inherited = policyFor({ id: ex.id }, routine, mode)
const active = policyFor({ ...c, id: ex.id }, routine, mode)
const inc = progressionStepOf(c, mode, ex, unit)
const canBackoff = backoffOffered(c, mode, ex, bw)
const backoff = canBackoff && c.backoff === true
const weightless = weightlessOf(c, mode, bw)
const invalid = !progressionStepIsValid(inc, active, backoff, weightless)
// "26 → 24 → 22 kg": what the sets will open at, from the weight and step typed above.
const preview = backoff && c.weight > 0 && inc > 0
? backoffWeights(c.weight, Math.max(1, Math.round(c.sets) || 1), inc).map(fmtNum).join(' → ') + ' ' + unit
: ''
const stride = mode === 'reps' && perSide ? 2 : 1
const range = climbsRange(active) ? normalizeRepRange(c.reps, c.repsMin, stride) : null
const epleyEligible = mode === 'reps' && !isBw({ ...c, id: ex.id }) && (active === 'linear' || climbsRange(active))
// 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 }
// Greyskull's final set is taken to failure by design, so choosing it here switches "Last set
// to failure" on too, unless it was already decided. Only the sheet's draft: nothing is
// written until Save, and a plan saved before stays what it was.
if (v === 'greyskull' && next.lastToFailure == null) next.lastToFailure = true
const picked = policyFor({ ...next, id: ex.id }, routine, mode)
// Triple progression opens with room for two more sets, the usual 3 to 5.
if (picked === 'triple' && !(next.setsMax > 0)) next.setsMax = Math.min(MAX_TRIPLE_SETS, Math.max(1, Math.round(next.sets) || 1) + 2)
return climbsRange(picked)
? { ...next, ...normalizeRepRange(next.reps, next.repsMin, stride) }
: next
})
return <>
<h4 className="sec">{t('Progression')}</h4>
<div className="sect-b" style={{ marginBottom: 8 }}>
<SelectRow title={t('Rule')} sheetTitle={t('Progression')} value={c.prog || ''} onChange={setRule}
options={[{ value: '', label: t('Follow the routine ({0})', t(POLICY_NAME[inherited])) },
...options.map(p => ({ value: p, label: t(POLICY_NAME[p]) }))]} />
</div>
<div className="small dim" style={{ marginBottom: active === 'off' ? 18 : 10 }}>{t(POLICY_DESC[active])}</div>
{/* Double progression on a weighted exercise fills this row with four steppers; `cfgrow-4`
lets it wrap into two pairs on phones, where four abreast left the inputs a few px wide. */}
{weightless && active !== 'off' && <div className="small dim" style={{ marginBottom: 12 }}>{t('Nothing to add in weight here: it goes up in reps, then sets.')}</div>}
{(active !== 'off' || backoff) && (!weightless || climbsRange(active) || active === 'triple') && <div className={'row cfgrow' + ((active === 'double' && epleyEligible) || active === 'triple' ? ' cfgrow-4' : '')} style={{ marginBottom: 18 }}>
{!weightless && <Stepper label={mode === 'time' ? t('Step (seconds)') : t('Step ({0})', unit)} value={inc}
step={mode === 'time' ? 5 : 1.25} decimal={mode !== 'time'} invalid={invalid} className={invalid ? 'invalid' : ''}
onChange={v => setC(x => ({ ...x, inc: v }))} />}
{climbsRange(active) && <>
{/* The draft stays as typed: normalising on every keystroke turned "12" into 92 (the
"1" was pulled above the lower bound first). Save and the engine normalise anyway. */}
<Stepper label={t('Reps from')} value={c.repsMin ?? range.repsMin} step={stride} decimal={false}
onChange={v => setC(x => ({ ...x, repsMin: v }))} />
<Stepper label={t('Reps up to')} value={c.reps ?? range.reps} step={stride} decimal={false}
onChange={v => setC(x => ({ ...x, reps: v }))} />
</>}
{/* Where triple progression stops adding sets; the Sets above are where it starts. */}
{active === 'triple' && <Stepper label={t('Sets up to')} value={c.setsMax ?? setsMaxOf(c)} step={1} decimal={false}
onChange={v => setC(x => ({ ...x, setsMax: v }))} />}
{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.')}
</div>}
{/* Off by default and only written when on, so every plan saved before it stays the shape
it was. The step is the one above: the gear's own increment, set once per exercise. */}
{canBackoff && <>
<div className="sect-b" style={{ marginBottom: 8 }}>
<Row icon="steps" iconTint="var(--acc)" title={t('Back-off sets')}
subtitle={t('Each set one step lighter than the one before. The weight above is the first set.')}>
<Switch checked={backoff} onChange={v => setC(x => ({ ...x, backoff: v || undefined }))} />
</Row>
</div>
<div className="small dim" style={{ marginBottom: 18 }}>
{backoff
? (preview ? t('Sets open at {0}. The first set goes up when every set reaches its reps, and the rest follow it.', preview) : t('The first set goes up when every set reaches its reps, and the rest follow it.'))
: t('All sets at the same weight.')}
</div>
</>}
</>
}
// 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)
// The slot's planned substitutes: listed, each removable, and added from the library (the same
// picker a swap uses, opened on the same muscle).
function BackupsSheet({ ex, subs: initial, onChange, close }) {
const [subs, setSubs] = useState(() => cleanBackups(initial, ex.id, id => !!EXIDX[id]))
const set = next => { setSubs(next); onChange(next) }
const add = () => {
const picker = exercisePicker((pick) => {
picker.close()
set(cleanBackups([...subs, pick.id], ex.id, id => !!EXIDX[id]))
}, { title: t('Add a backup'), like: ex.id })
}
return <>
<h3>{t('Backups for {0}', exerciseNameText(ex))}</h3>
<div className="muted small" style={{ marginBottom: 12 }}>{t('Machine taken? Pick what you’d do instead, and a swap mid-workout offers these first.')}</div>
{subs.length > 0 && <div className="sect-b" style={{ marginBottom: 12 }}>
{subs.map(id => EXIDX[id]).filter(Boolean).map(b => <Row key={b.id} title={exerciseNameText(b)} subtitle={t(b.eq)}>
<button type="button" className="iconbtn" aria-label={t('Remove “{0}”', exerciseNameText(b))} onClick={() => set(subs.filter(x => x !== b.id))}><Icon name="xmark" /></button>
</Row>)}
</div>}
{subs.length < MAX_BACKUPS
? <Button icon="plus" onClick={add}>{t('Add a backup')}</Button>
: <div className="small dim">{t('That’s plenty of backups.')}</div>}
<div style={{ height: 8 }} />
<Button variant="ghost" className="dim" onClick={close}>{t('Done')}</Button>
</>
}
function ExConfig({ ex, existing, onSave, onDelete, onReplace, close, routine, initial, saveLabel, backupsEditable }) {
const st = useStore(s => s.S)
// No warm-up rest of its own: the profile's (Settings > Workout) when it has one, else the work rest.
const warmAll = typeof st.warmupRestSec === 'number' ? st.warmupRestSec : null
const warmLikeAll = warmAll === null ? t('Like the work sets') : t('Default ({0})', warmAll > 0 ? fmtRest(warmAll) : t('No timer'))
// A hold's switch-sides pause when it has none of its own (Workout.jsx SWITCH_SIDES_SEC).
const sideDefault = t('Default ({0})', fmtRest(10))
// No rest of its own: the default rest timer's, which may itself be off.
const restLikeAll = t('Default ({0})', fmtRest(st.restSec))
// A catalogue cardio exercise logs intervals by default, but it can be counted in sets x reps
// or timed like any other (#678). A saved duration/speed plan keeps its cardio form even after
// the catalogue moved its exercise out of cardio (jumping jacks, #717).
const speedUnit = speedUnitOf(st)
const seed = existing || initial || pickedExerciseConfig(ex.id)
const cardioEx = isCardio(ex.id) || modeOf({ ...seed, id: ex.id }) === 'cardio'
const [c, setC] = useState(() => {
const cfg = { ...seed }
// A session's target carries the meaning of its dumbbell weight as a stamp (lib/dumbbells.js);
// one that only repeats the exercise's own default is not the slot's choice, so it is not kept.
if (cfg.dbLoad != null && dbLoadOf(cfg.dbLoad) === dbLoadFor(st, ex.id)) delete cfg.dbLoad
return climbsRange(policyFor({ ...cfg, id: ex.id }, routine, modeOf({ ...cfg, id: ex.id })))
? { ...cfg, ...normalizeRepRange(cfg.reps, cfg.repsMin, isPerSide(cfg) ? 2 : 1) }
: cfg
})
// Cardio keeps its own duration+speed form; the reps/time choice (issue #16) is offered for
// everything else, which is where the gap was — planks, hangs, wall sits, loaded carries.
// A cardio exercise gets Cardio as a third choice, and a slot that picked Reps or Time says so
// in its `mode` (modeOf reads it before the body part).
const mode = modeOf({ ...c, id: ex.id })
const cardio = cardioEx && mode === 'cardio'
// Both default from the dataset and are then whatever the config says — see isBw.
const bw = !cardio && isBw({ ...c, id: ex.id })
const perSide = isPerSide(c)
// What a warm-up rests without one of its own, for its wheel to open on (#735): the profile's,
// else the work sets' (lib/supersetFlow.js warmupRestSecFor).
const warmApplies = warmAll !== null ? warmAll : ownExerciseRest(c) ?? st.restSec
// Pyramid sets: one rep target per set, in place of the flat Sets × Reps (CONTEXT.md).
const pyramid = mode === 'reps' && isPyramid({ ...c, mode })
const setPyramidAt = (i, v) => setC(x => ({ ...x, pyramid: x.pyramid.map((p, j) => (j === i ? v : p)) }))
const progressionPolicy =policyFor({ ...c, id: ex.id }, routine, mode)
const progressionStepInvalid = !progressionStepIsValid(progressionStepOf(c, mode, ex, st.unit), progressionPolicy, c.backoff === true && backoffOffered(c, mode, ex, bw), weightlessOf(c, mode, bw))
const activePolicy = policyFor({ ...c, id: ex.id }, routine, mode)
// Both climb a range: the flat Reps stepper gives way to "Reps from" / "Reps up to".
const ranged = mode === 'reps' && climbsRange(activePolicy)
const triple = mode === 'reps' && activePolicy === 'triple'
// Keep whatever the other mode already had (sets, weight) and fill only what is missing.
const setMode = m => setC(x => {
// Intervals and sets count different things: crossing into or out of cardio starts from the
// new mode's own count instead of 1 set of 10 jumping jacks.
const { sets: _sets, ...rest } = x
const crossing = (m === 'cardio') !== (modeOf({ ...x, id: ex.id }) === 'cardio')
const next = { ...defaultConfig(ex.id, m), ...(crossing ? rest : x), mode: m }
return m === 'reps' && climbsRange(policyFor({ ...next, id: ex.id }, routine, 'reps'))
? { ...next, ...normalizeRepRange(next.reps, next.repsMin, isPerSide(next) ? 2 : 1) }
: next
})
const save = () => {
if (progressionStepInvalid) return
close()
const sets = Math.max(1, Math.round(c.sets) || (cardio ? 1 : 3))
// Only carry progression settings that differ from the inherited default, so a plan file
// stays readable and "follow the routine" keeps meaning exactly that.
const prog = {}
if (c.prog) prog.prog = c.prog
if (c.inc > 0) prog.inc = c.inc
// Epley deloading is configurable per occurrence, but the default stays omitted so older
// plans retain their compact shape and keep the existing 90% behaviour.
if (mode === 'reps' && !bw && (activePolicy === 'linear' || climbsRange(activePolicy))) {
const deloadFactor = Math.max(0.5, Math.min(0.95, Number(c.deloadFactor) || 0.9))
if (deloadFactor !== 0.9) prog.deloadFactor = deloadFactor
}
// Written only when it differs from what the dataset already says, so a barbell config
// stays exactly the shape it was before these flags existed.
// `bodyweight` is true of a hold as much as of a set of reps. `side` used to be dropped on
// switching to Time too — a timed hold has no rep count to split — but "per side" does not
// have to mean splitting: for a hold it means doing the whole thing once per side, so the
// flag now survives the switch and buildWorkSets doubles the planned sets instead.
const flags = {}
if (bw !== isBodyweightEq(ex.id)) flags.bodyweight = bw
// Free text, e.g. a pyramid's per-set loading ("bar only, +1 plate/side each set") — the
// sets/reps/weight fields are one flat target and have no room for that on their own.
// Mode-independent, so it is spread in below rather than folded into `flags`.
const note = (c.note || '').trim().slice(0, 500)
const withNote = note ? { note } : {}
// Only written when there are any, so a plan that never asked for warm-ups keeps the exact
// shape it had — and reads back as 0 either way (buildSets).
const warmupSets = Math.max(0, Math.min(MAX_PLANNED_WARMUPS, Math.round(c.warmupSets) || 0))
const withWarmups = warmupSets ? { warmupSets } : {}
// Per-exercise rest (issue #10): written only when a positive value was set, so 0 keeps
// inheriting the global rest timer and a config that never touched it stays the shape it
// was. Mode-independent — a heavy triple, a plank and a cardio interval all rest.
// "No timer" is its own `restOff` rather than a 0, which has always meant the default.
const restSec = Math.max(0, Math.round(c.restSec) || 0)
const withRest = restSec ? { restSec } : c.restOff === true ? { restOff: true } : {}
// What a dumbbell weight means in this slot (lib/dumbbells.js): written only when the slot
// picked its own, so every other config stays the shape it was.
const dbLoad = isBellEx(ex) && !bw ? dbLoadOf(c.dbLoad) : null
const withMeant = dbLoad ? { dbLoad } : {}
// "Last set to failure": written only when on, so a plan that never asked keeps its shape.
const withFailure = c.lastToFailure === true ? { lastToFailure: true } : {}
// The warm-ups' own rest and the break between sides (Discord "set-specific rest timers"):
// optional fields, written only when chosen, so a config that never touched them keeps its
// exact shape and rests as it always did (lib/supersetFlow.js warmupRestSecFor, sideRestFor).
// Planned substitutes (lib/routine-backups.js): kept whenever the slot has them, whichever
// sheet saves it, so an edit here never drops them; only real exercises, never itself.
const withSubs = withBackups(c.subs, ex.id, id => !!EXIDX[id])
const withSetRests = {
...(c.warmupRestOff === true ? { warmupRestOff: true } : c.warmupRestSec > 0 ? { warmupRestSec: Math.round(c.warmupRestSec) } : {}),
...(perSide && mode === 'time' && c.sideRestOff === true ? { sideRestOff: true }
: perSide && c.sideRestSec > 0 ? { sideRestSec: Math.round(c.sideRestSec) } : {}),
}
// The treadmill's grade, only when it has one, so a flat slot keeps the shape it had.
const incline = clampIncline(c.incline)
if (cardio) onSave({ sets, min: Math.max(1, Math.round(c.min) || 20), speed: Math.max(0, c.speed || 8), ...(incline > 0 ? { incline } : {}), ...withNote, ...withRest, ...withSubs })
else if (mode === 'time') onSave({ sets, mode: 'time', sec: Math.max(1, Math.round(c.sec) || 45), weight: Math.max(0, c.weight || 0), ...flags, ...(perSide ? { side: true } : {}), ...prog, ...withNote, ...withWarmups, ...withRest, ...withSetRests, ...withMeant, ...withFailure, ...withSubs })
else {
// A unilateral target is stored even: the split has to divide, and a typed 15 would
// otherwise plan seven reps on one side and eight on the other, every session.
const typed = Math.max(1, Math.round(c.reps) || 10)
const stride = perSide ? 2 : 1
let reps = perSide ? Math.ceil(typed / stride) * stride : typed
let range = null
if (ranged) {
range = normalizeRepRange(reps, c.repsMin, stride)
reps = range.reps
}
const out = { sets, mode: 'reps', reps, weight: Math.max(0, c.weight || 0), ...flags, ...(perSide ? { side: true } : {}), ...prog, ...withNote, ...withWarmups, ...withRest, ...withSetRests, ...withMeant, ...withSubs }
// The list is the prescription; sets and reps follow it, so everything that reads the
// flat target (a config sheet reopened with the toggle off, older builds) still makes sense.
const list = pyramid ? normalizePyramid(c.pyramid, stride) : []
if (list.length) Object.assign(out, flatFromPyramid(list), { pyramid: list })
const pyramidRest = list.length ? normalizePyramidRest(c.pyramidRest, list.length) : []
if (pyramidRest.length) out.pyramidRest = pyramidRest
const pyramidWeight = list.length && !bw ? normalizePyramidWeight(c.pyramidWeight, list.length) : []
if (pyramidWeight.length) out.pyramidWeight = pyramidWeight
if (ranged) out.repsMin = range.repsMin
// Triple progression's set ceiling, written only when it is above the starting sets: at the
// same number it would add nothing, and the plan keeps the shape it had.
if (triple && !list.length && setsMaxOf(c) > sets) out.setsMax = setsMaxOf(c)
// A ceiling below the working reps would tell you to add a set on day one.
if (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 (!list.length && c.intensifier && c.intensifier.type) out.intensifier = intensifierToSave(c.intensifier)
// Back-off sets: written only when on, and only where the sheet offered them.
if (c.backoff === true && backoffOffered(out, 'reps', ex, bw) && isBackoff({ ...out, backoff: true })) out.backoff = true
// A pyramid has its own Max set for this, so the flag is not offered (or kept) there.
if (!list.length) Object.assign(out, withFailure)
onSave(out)
}
}
return <>
<h3 className={exerciseNameClass(ex)}>{exerciseNameFor(ex)}</h3>
<Media ex={ex} />
{/* The same tags the exercise detail sheet shows, secondaries included: choosing what goes
into a plan is exactly when "what else does this hit" matters, and until now that was
only visible from the Exercises tab, after the fact. Custom exercises and the newer
metadata name muscles by the map's ids ("forearm", "gluteal"), which only MUSCLE_NAME
turns into a translatable label. */}
<div className="row" style={{ gap: 6, flexWrap: 'wrap', margin: '10px 0 14px' }}>
{cardio && <span className="tag acc"><Icon name="figureRun" />{t('Cardio')}</span>}
<span className="tag">{t(MUSCLE_NAME[ex.tg] || ex.tg || ex.bp)}</span><span className="tag">{t(ex.eq)}</span>
{!cardio && (ex.secondaries?.length ? ex.secondaries : smOf(ex)).filter(trainedBy(ex)).slice(0, 3)
.map((s, i) => <span key={i} className="tag dim">{t(MUSCLE_NAME[s] || s)}</span>)}
</div>
{descFor(ex) && <div className="exnote">{descFor(ex)}</div>}
<div style={{ marginBottom: 14 }}>
<Segmented className="seg-range" value={mode} onChange={setMode}
options={[...(cardioEx ? [{ value: 'cardio', label: t('Cardio') }] : []), { value: 'reps', label: t('Reps') }, { value: 'time', label: t('Time') }]} />
</div>
<div className="row cfgrow" style={{ marginBottom: mode === 'time' ? 8 : 18 }}>
{cardio ? <>
<Stepper label={t('Intervals')} value={c.sets} step={1} decimal={false} onChange={v => setC(x => ({ ...x, sets: v }))} />
<Stepper label={t('Minutes')} value={c.min} step={1} decimal={false} onChange={v => setC(x => ({ ...x, min: v }))} />
{/* Typed and stepped in the profile's unit, kept in km/h (lib/speed.js). */}
<Stepper label={speedUnit === 'mph' ? t('Speed (mph)') : t('Speed (km/h)')} value={toSpeed(c.speed, speedUnit)} step={0.5}
onChange={v => setC(x => ({ ...x, speed: fromSpeed(v, speedUnit) }))} />
{/* The grade it starts at, where the exercise has one (lib/incline.js); the workout rows
take it from here when last time had none. */}
{(inclineFits(ex) || c.incline > 0) && <Stepper label={t('Incline (%)')} value={c.incline || 0} step={INCLINE_STEP} max={INCLINE_MAX}
onChange={v => setC(x => ({ ...x, incline: clampIncline(v) }))} />}
</> : mode === 'time' ? <>
<Stepper label={t('Sets')} value={c.sets} step={1} decimal={false} onChange={v => setC(x => ({ ...x, sets: v }))} />
<Stepper label={t('Seconds')} value={c.sec} step={5} decimal={false} onChange={v => setC(x => ({ ...x, sec: v }))} />
{/* A bodyweight hold's load is the "Added" row below — showing both put two fields on
one value. */}
{!bw && <Stepper label={t('Weight ({0})', st.unit)} value={c.weight} step={2.5} onChange={v => setC(x => ({ ...x, weight: v }))} />}
</> : <>
{/* Rest-pause always trains as exactly two rows — a warm-up at this rep count, then one
rest-pause work set — so "Sets" has nothing left to mean and only invites a mismatch. */}
{c.intensifier?.type !== 'restpause' && !pyramid &&
<Stepper label={triple ? t('Sets from') : t('Sets')} value={c.sets} step={1} decimal={false} onChange={v => setC(x => ({ ...x, sets: v }))} />}
{!ranged && !pyramid && <Stepper label={t('Reps')} value={c.reps} step={perSide ? 2 : 1} decimal={false} onChange={v => setC(x => ({ ...x, reps: v }))} />}
{/* On bodyweight work the weight stepper is the click #32 is about, so it is not here
until there is a belt to describe — see the added-weight row below. */}
{!bw && !pyramid && <Stepper label={t('Weight ({0})', st.unit)} value={c.weight} step={2.5} onChange={v => setC(x => ({ ...x, weight: v }))} />}
</>}
</div>
{/* Pyramid sets: a row per set, each with its own reps or Max, rest and weight. A set left
at 0 weight starts from that same set last time (buildSets). */}
{pyramid && <div style={{ marginTop: -10, marginBottom: 18 }}>
{/* Presets replace the list (and its rests and weights) in one tap; the sheet is not saved
until Save. */}
<div className="chips" style={{ marginBottom: 10 }}>
{PYRAMID_PRESETS.map((preset, k) => <button key={k} type="button" className="chip"
onClick={() => setC(x => ({ ...x, pyramid: [...preset], pyramidRest: undefined, pyramidWeight: undefined }))}>{pyramidLabel(preset)}</button>)}
</div>
{c.pyramid.map((p, i) => <div key={i} style={{ marginBottom: 10 }}>
<div className="row cfgrow" style={{ marginBottom: 4 }}>
{p === PYRAMID_MAX
? <div className="small"><strong>{t('Set {0}', i + 1)}</strong> · {t('Max: as many reps as you can')}</div>
: <Stepper label={t('Set {0}', i + 1)} value={p} step={perSide ? 2 : 1} min={1} decimal={false} onChange={v => setPyramidAt(i, v)} />}
<Stepper label={t('Rest (s)')} value={(c.pyramidRest || [])[i] || 0} step={15} decimal={false}
onChange={v => setC(x => {
const rest = Array.from({ length: x.pyramid.length }, (_, j) => (x.pyramidRest || [])[j] || 0)
rest[i] = v
return { ...x, pyramidRest: rest }
})} />
</div>
{/* Bodyweight work has its one "Added" load below instead. */}
{!bw && <div className="row cfgrow" style={{ marginBottom: 4 }}>
<Stepper label={t('Weight ({0})', st.unit)} value={(c.pyramidWeight || [])[i] || 0} step={2.5}
onChange={v => setC(x => {
const weight = Array.from({ length: x.pyramid.length }, (_, j) => (x.pyramidWeight || [])[j] || 0)
weight[i] = v
return { ...x, pyramidWeight: weight }
})} />
</div>}
<div className="row" style={{ justifyContent: 'flex-end', gap: 8 }}>
<Button size="sm" variant={p === PYRAMID_MAX ? 'primary' : 'plain'} aria-pressed={p === PYRAMID_MAX}
onClick={() => setPyramidAt(i, p === PYRAMID_MAX ? (c.reps || 10) : PYRAMID_MAX)}>{t('Max')}</Button>
{c.pyramid.length > 1 && <Button size="sm" icon="trash" aria-label={t('Remove set')} title={t('Remove set')}
onClick={() => setC(x => ({ ...x, pyramid: x.pyramid.filter((_, j) => j !== i), pyramidRest: x.pyramidRest?.filter((_, j) => j !== i), pyramidWeight: x.pyramidWeight?.filter((_, j) => j !== i) }))} />}
</div>
</div>)}
<Button size="sm" icon="plus" disabled={c.pyramid.length >= MAX_PYRAMID_SETS}
onClick={() => setC(x => ({
...x,
pyramid: [...x.pyramid, x.pyramid[x.pyramid.length - 1]],
// The new set copies the last one's rest too, like its target.
pyramidRest: x.pyramidRest?.length ? [...x.pyramid.map((_, j) => x.pyramidRest[j] || 0), x.pyramidRest[x.pyramid.length - 1] || 0] : x.pyramidRest,
pyramidWeight: x.pyramidWeight?.length ? [...x.pyramid.map((_, j) => x.pyramidWeight[j] || 0), x.pyramidWeight[x.pyramid.length - 1] || 0] : x.pyramidWeight,
}))}>{t('Add set')}</Button>
<div className="small dim" style={{ marginTop: 8 }}>{t('A set left at 0 rest uses the exercise’s rest.')}</div>
{!bw && <div className="small dim" style={{ marginTop: 4 }}>{t('A set left at 0 weight starts from that set’s weight last time.')}</div>}
</div>}
{c.intensifier?.type === 'restpause' && <div className="small dim" style={{ marginTop: -10, marginBottom: 18 }}>
{t('Rest-pause always trains as one warm-up set at this rep count, then one rest-pause work set. "Sets" isn’t used here.')}
</div>}
{/* Planned warm-ups: the session used to start at the work weight and you added every
warm-up by hand, every time. Rest-pause is excluded because it builds its own warm-up
row, and cardio because an interval plan has no load to ramp. */}
{!cardio && c.intensifier?.type !== 'restpause' && <>
<div className="row cfgrow" style={{ marginBottom: 6 }}>
<Stepper label={t('Warm-up sets')} value={c.warmupSets || 0} step={1} decimal={false}
onChange={v => setC(x => ({ ...x, warmupSets: Math.max(0, Math.min(MAX_PLANNED_WARMUPS, Math.round(v) || 0)) }))} />
</div>
<div className="small dim" style={{ marginBottom: 18 }}>
{/* Settings > Units can count warm-ups in volume (#719); records and progression never do. */}
{(c.warmupSets || 0) > 0
? st.volumeWarmups === true
? t('Added before your work sets. They count in your volume (Settings), but stay out of records and progression. Each one closes half the gap to the work weight, never below the bar on a barbell lift, and you can still change any of them mid-session.')
: t('Added before your work sets and left out of volume, records and progression. Each one closes half the gap to the work weight, never below the bar on a barbell lift, and you can still change any of them mid-session.')
: t('Ramp-up sets added before the work sets, so you do not have to add them by hand each session.')}
</div>
</>}
{mode === 'time' && !bw && <div className="small dim" style={{ marginBottom: 18 }}>
{t('A timer runs while you hold the set. Leave the weight at 0 for bodyweight holds.')}
</div>}
{/* Per-exercise rest (issue #10). Its own full-width row, like the other steppers with an
explanation under them, and outside every mode branch because a heavy triple, a plank
and a cardio interval all rest — they just do not all want the same break. */}
{/* The default rest, no timer, or a time of its own: the warm-ups' three choices. The wheel
opens on the rest that applies now (#735), and 0:00 on it is no timer. */}
<div className="sect-b cfg-rest" style={{ marginBottom: 6 }}>
<Row icon="timer" iconTint="var(--orange)" title={t('Rest for this exercise')} accessory="chevron"
value={c.restSec > 0 ? fmtRest(c.restSec) : c.restOff === true ? t('No timer') : restLikeAll}
onClick={() => menuSheet({
title: t('Rest for this exercise'),
items: [
{ icon: 'timer', label: restLikeAll, on: !(c.restSec > 0) && c.restOff !== true, onClick: () => setC(x => { const { restOff, restSec, ...rest } = x; return rest }) },
{ icon: 'pause', label: t('No timer'), on: !(c.restSec > 0) && c.restOff === true, onClick: () => setC(x => { const { restSec, ...rest } = x; return { ...rest, restOff: true } }) },
{ icon: 'clock', label: t('Pick a time'), on: c.restSec > 0, onClick: () => durationSheet({
title: t('Rest for this exercise'), value: restWheelValue(c.restSec, c.restOff === true ? 0 : st.restSec), max: REST_MAX, off: t('No timer'),
onDone: v => setC(x => { const { restOff, restSec, ...rest } = x; return v > 0 ? { ...rest, restSec: v } : { ...rest, restOff: true } }),
}) },
],
})} />
</div>
<div className="small dim" style={{ marginBottom: 18 }}>
{t('Rest after each set of this exercise. No timer means no countdown, you go when you’re ready.')}
</div>
{/* The warm-ups' own rest: the profile's or like the work sets (no field, as always), no timer,
or a time of their own. The last warm-up still rests the full work rest before the first work set. */}
{!cardio && <>
<div className="sect-b cfg-rest" style={{ marginBottom: 6 }}>
<Row icon="timer" iconTint="var(--yellow)" title={t('Rest between warm-ups')} accessory="chevron"
value={c.warmupRestOff === true ? t('No timer') : c.warmupRestSec > 0 ? fmtRest(c.warmupRestSec) : warmLikeAll}
onClick={() => menuSheet({
title: t('Rest between warm-ups'),
items: [
{ icon: 'timer', label: warmLikeAll, on: !(c.warmupRestOff === true) && !(c.warmupRestSec > 0), onClick: () => setC(x => { const { warmupRestOff, warmupRestSec, ...rest } = x; return rest }) },
{ icon: 'pause', label: t('No timer'), on: c.warmupRestOff === true, onClick: () => setC(x => { const { warmupRestSec, ...rest } = x; return { ...rest, warmupRestOff: true } }) },
{ icon: 'clock', label: t('Pick a time'), on: c.warmupRestOff !== true && c.warmupRestSec > 0, onClick: () => durationSheet({
title: t('Rest between warm-ups'), value: restWheelValue(c.warmupRestSec, warmApplies), max: REST_MAX, off: t('No timer'),
onDone: v => setC(x => { const { warmupRestOff, warmupRestSec, ...rest } = x; return v > 0 ? { ...rest, warmupRestSec: v } : { ...rest, warmupRestOff: true } }),
}) },
],
})} />
</div>
<div className="small dim" style={{ marginBottom: 18 }}>
{t('Warm-ups are light, so they can rest less, or not at all. The last one still gets the full rest before your first work set.')}
</div>
</>}
{/* The break between the two sides of a per-side set. Without one: none for reps, the short
switch pause for a hold, as before. */}
{!cardio && perSide && <>
<div className="sect-b cfg-rest" style={{ marginBottom: 6 }}>
{/* A reps set has no break by default, so 0:00 on the wheel is its "no timer". A hold's
default is the 10 s switch, so it gets the warm-ups' three choices to turn it off (#636). */}
<Row icon="sides" iconTint="var(--blue)" title={t('Rest between sides')} accessory="chevron"
value={mode === 'time' && c.sideRestOff === true ? t('No timer') : c.sideRestSec > 0 ? fmtRest(c.sideRestSec) : mode === 'time' ? sideDefault : t('No timer')}
onClick={() => mode === 'time' ? menuSheet({
title: t('Rest between sides'),
items: [
{ icon: 'timer', label: sideDefault, on: c.sideRestOff !== true && !(c.sideRestSec > 0), onClick: () => setC(x => { const { sideRestOff, sideRestSec, ...rest } = x; return rest }) },
{ icon: 'pause', label: t('No timer'), on: c.sideRestOff === true, onClick: () => setC(x => { const { sideRestSec, ...rest } = x; return { ...rest, sideRestOff: true } }) },
{ icon: 'clock', label: t('Pick a time'), on: c.sideRestOff !== true && c.sideRestSec > 0, onClick: () => durationSheet({
title: t('Rest between sides'), value: c.sideRestSec > 0 ? c.sideRestSec : 10, max: REST_MAX, off: t('No timer'),
onDone: v => setC(x => { const { sideRestOff, sideRestSec, ...rest } = x; return v > 0 ? { ...rest, sideRestSec: v } : { ...rest, sideRestOff: true } }),
}) },
],
}) : durationSheet({
title: t('Rest between sides'), value: c.sideRestSec > 0 ? c.sideRestSec : 0, max: REST_MAX,
off: t('No timer'),
onDone: v => setC(x => { const { sideRestSec, sideRestOff, ...rest } = x; return v > 0 ? { ...rest, sideRestSec: v } : rest }),
})} />
</div>
<div className="small dim" style={{ marginBottom: 18 }}>
{t('A breather after one side, before the other. The full rest comes once both sides are done.')}
</div>
</>}
{/* Planned substitutes for this slot, in the routine editor: what the swap sheet offers first
when the machine is taken. */}
{backupsEditable && <>
<div className="sect-b" style={{ marginBottom: 6 }}>
<Row icon="swap" iconTint="var(--teal)" title={t('Backups')} accessory="chevron"
value={(c.subs || []).length ? String((c.subs || []).filter(id => EXIDX[id]).length) : t('None')}
subtitle={(c.subs || []).map(id => EXIDX[id]).filter(Boolean).map(exerciseNameFor).join(', ') || undefined}
onClick={() => ui().openSheet(done => <BackupsSheet ex={ex} subs={c.subs || []} onChange={subs => setC(x => ({ ...x, subs }))} close={done} />)} />
</div>
<div className="small dim" style={{ marginBottom: 18 }}>
{t('Machine taken? Pick what you’d do instead, and a swap mid-workout offers these first.')}
</div>
</>}
{/* ---------- bodyweight + per side (issues #31/#32/#33) ---------- */}
{!cardio && <div className="sect-b" style={{ marginBottom: 8 }}>
<Row icon="figureStrength" iconTint="var(--acc)" title={t('Bodyweight')}
subtitle={bw ? (mode === 'time' ? t('No weight to enter. Just time the hold.') : t('No weight to enter. Just log the reps.')) : t('Ask for a weight on every set.')}>
<Switch checked={bw} onChange={v => setC(x => ({ ...x, bodyweight: v, weight: v ? 0 : x.weight }))} />
</Row>
{mode !== 'cardio' && <Row icon="sides" iconTint="var(--blue)" title={t('Per side')}
subtitle={mode === 'time'
? (perSide
? tn('{0} set becomes {1}: one on each side, {2}s held every time.', '{0} sets become {1}: one on each side, {2}s held every time.', c.sets || 0, (c.sets || 0) * 2, c.sec || 0)
: t('For side planks, single-arm holds and the like. Trains each side on its own.'))
: (perSide ? t('You still log the total: {0} is {1} per side.', c.reps || 0, fmtNum(sideReps(c.reps))) : t('For lunges, single-arm rows and the like.'))}>
{/* Reps: turning it on rounds the target up to an even number, since half of an odd total
is a rep one side does not get. Time: nothing to round — a hold's whole duration
happens twice, so only the flag changes (buildWorkSets doubles the planned sets). */}
<Switch checked={perSide} onChange={v => setC(x => {
if (mode === 'time') return { ...x, side: v || undefined }
const next = { ...x, side: v || undefined, reps: v ? Math.ceil((x.reps || 0) / 2) * 2 : x.reps }
return climbsRange(policyFor({ ...next, id: ex.id }, routine, 'reps'))
? { ...next, ...normalizeRepRange(next.reps, next.repsMin, v ? 2 : 1) }
: next
})} />
</Row>}
{/* Off keeps the list as `pyramidDraft`, so a stray tap does not lose it before Save
(save writes neither field unless the toggle is on). */}
{/* The last work set of every session is marked as taken to failure, like Greyskull's
final AMRAP set (workout-model isFailureSet, history.js applyFailurePlan). A pyramid
already has its Max set for that. */}
{!pyramid && <Row icon="gauge" iconTint="var(--purple)" title={t('Last set to failure')}
subtitle={c.lastToFailure ? t('The last set goes until nothing is left, and counts as all-out effort unless you rate it.') : t('Plan the final set as an all-out one.')}>
<Switch checked={c.lastToFailure === true} onChange={v => setC(x => ({ ...x, lastToFailure: v || undefined }))} />
</Row>}
{mode === 'reps' && <Row icon="steps" iconTint="var(--acc)" title={t('Pyramid sets')}
subtitle={t('A different rep target for each set, e.g. 12 · 8 · 6 · Max · 12.')}>
<Switch checked={pyramid} onChange={v => setC(x => (v
? { ...x, pyramid: x.pyramidDraft || pyramidFromFlat(x), intensifier: undefined }
: { ...x, ...flatFromPyramid(x.pyramid), pyramidDraft: x.pyramid, pyramid: undefined }))} />
</Row>}
</div>}
{/* A stepper is too wide to sit in a list row next to a label — it squeezes the text to
one word per line — so added weight gets the same full-width treatment as sets and
reps, with its explanation underneath. */}
{bw && <>
<div className="row cfgrow" style={{ marginBottom: 8 }}>
<Stepper label={t('Added ({0})', st.unit)} value={c.weight || 0} step={2.5}
onChange={v => setC(x => ({ ...x, weight: v }))} />
</div>
<div className="small dim" style={{ marginBottom: 18 }}>
{t('For dips or pull-ups with a belt. Progression then follows the weight.')}
</div>
</>}
{/* The rep ceiling only means something when there is no load to add instead. */}
{mode === 'reps' && bw && !(c.weight > 0) && <div className="row cfgrow" style={{ marginBottom: 18 }}>
<Stepper label={t('Top of the range')} value={c.repsMax || 0} step={1} decimal={false}
onChange={v => setC(x => ({ ...x, repsMax: v }))} />
</div>}
{mode === 'reps' && bw && !(c.weight > 0) && <div className="small dim" style={{ marginTop: -10, marginBottom: 18 }}>
{c.repsMax > 0
? t('Reps climb to {0}, then a set is added and the reps start over. At {1} sets it asks you to add weight instead.', c.repsMax, MAX_BW_SETS)
: t('Reps climb by one whenever every set was clean. Set a ceiling to add sets instead of reps forever.')}
</div>}
{/* A pyramid already shapes every set; a drop-set or rest-pause would reshape them again. */}
{mode === 'reps' && !pyramid && <>
<h4 className="sec">{t('Drop-set / rest-pause')}</h4>
<div className="sect-b" style={{ marginBottom: 8 }}>
<SelectRow title={t('Intensifier')} sheetTitle={t('Intensifier')} value={c.intensifier?.type || ''}
onChange={v => setC(x => ({
...x,
intensifier: !v ? undefined : v === 'dropset'
? { type: 'dropset', count: x.intensifier?.count || 1, pct: x.intensifier?.pct || 20 }
// The activation set's own reps are whatever "Reps" above already says — a
// rest-pause plan only adds two new numbers: the total extra reps wanted past
// it, and the rest between the bursts that total gets split into.
: { type: 'restpause', totalReps: x.intensifier?.totalReps || x.reps || 8, restSec: x.intensifier?.restSec || st.restPauseSec || 15 },
}))}
options={[
{ value: '', label: t('None') },
{ value: 'dropset', label: t('Drop-set') },
{ value: 'restpause', label: t('Rest-pause') },
]} />
</div>
{c.intensifier?.type === 'dropset' && <div className="row cfgrow" style={{ marginBottom: 8 }}>
<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} 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'
? t('Every set becomes a drop-set: after the main set, {0} drop(s) with no rest, each about {1}% lighter.', c.intensifier.count, c.intensifier.pct)
: t('Every set becomes rest-pause: {0} reps to start, then {1} more split into short bursts, {2}s rest before each, roughly halving each time.', c.reps || 0, c.intensifier.totalReps, c.intensifier.restSec)}
</div>}
</>}
{/* What a dumbbell weight means, for this routine's slot (cfg.dbLoad). Picking what the
exercise already means drops the key, so the slot follows the exercise's default again. */}
{isBellEx(ex) && !bw && mode !== 'cardio' && <>
<h4 className="sec">{t('Weight means')}</h4>
<DbLoadPicker ex={ex} cfg={c} value={dbLoadFor(st, { ...c, id: ex.id })}
onChange={v => setC(x => ({ ...x, dbLoad: v === dbLoadFor(st, ex.id) ? undefined : v }))} />
</>}
{/* How the weight is plate-loaded and what the bar weighs — per exercise, not per plan, so
it sits apart from the config fields above and writes straight to S.barWeights/S.loadKind. */}
{mode === 'reps' && <>
<h4 className="sec">{t('Plate loading')}</h4>
<BarWeightEditor ex={ex} cfg={c} extra={t('Applies to this exercise everywhere, not just this plan.')} />
{canHaveStack(ex) && !bw && <><h4 className="sec">{t('Weight stack')}</h4><StackEditor ex={ex} /></>}
</>}
{pyramid
? <div className="small dim" style={{ marginBottom: 18 }}>{t('Weight is up to you: pyramid sets are not progressed automatically.')}</div>
: <ProgressionFields ex={ex} mode={mode} c={c} setC={setC} routine={routine} unit={st.unit} perSide={perSide} bw={bw} />}
<textarea className="input" rows={3} maxLength={500} style={{ marginBottom: 18 }}
placeholder={t('Note (optional): loading cues, "bar only, then +1 plate/side each set", anything worth remembering')}
value={c.note || ''} onChange={e => setC(x => ({ ...x, note: e.target.value }))} />
{/* What saving does, when it is neither of the two usual things: the routine editor's
Replace (#110) seeds this sheet with the slot it replaces, and saving puts the exercise
into that slot. */}
<Button variant="primary" disabled={progressionStepInvalid} onClick={save}>{saveLabel || (existing ? t('Save') : t('Add to routine'))}</Button>
{isCustomEx(ex) && <><div style={{ height: 8 }} /><Button icon="pencil" onClick={() => { close(); customExSheet(ex) }}>{t('Edit or delete this exercise')}</Button></>}
{/* The routine editor's counterpart to a workout's Swap (#110): another exercise in this
slot, with the slot's sets, reps, weight, rule and note kept (lib/routines.js). What was
changed on this sheet and not saved is left behind, as closing it would. */}
{onReplace && <><div style={{ height: 8 }} /><Button icon="swap" onClick={() => { close(); onReplace() }}>{t('Replace exercise')}</Button></>}
{onDelete && <><div style={{ height: 8 }} /><Button variant="danger" onClick={() => { close(); onDelete() }}>{t('Remove from routine')}</Button></>}
</>
}
// `opts.backups`: the routine editor's sheet, where the slot's planned substitutes are picked.
export const exConfigSheet = (ex, existing, onSave, onDelete, routine, initial, onReplace, saveLabel, opts = {}) => ui().openSheet(close => <ExConfig ex={ex} existing={existing} initial={initial} onSave={onSave} onDelete={onDelete} onReplace={onReplace} routine={routine} saveLabel={saveLabel} backupsEditable={!!opts.backups} close={close} />)
/* ============================ glyph picker ============================ */
// Grouped by what the glyph means for a training day, so picking one is a scan
// of four short rows rather than a hunt through twenty loose icons.
export const glyphPicker = (current, onPick) => {
const cur = glyphOf(current)
return ui().openSheet(close => <>
<h3>{t('Pick an icon')}</h3>
{GLYPH_GROUPS.map(g => (
<div key={g.key} style={{ marginBottom: 14 }}>
<div className="sect-t" style={{ padding: '0 2px 7px' }}>{t(g.key)}</div>
<div className="glyph-grid">
{g.items.map(n => (
<button key={n} className={'glyph-cell' + (n === cur ? ' on' : '')}
onClick={() => { close(); onPick(n) }} aria-label={n}>
<Icon name={n} />
</button>
))}
</div>
</div>
))}
<div style={{ height: 4 }} />
</>)
}
/* ============================ effort quick picker (RIR / RPE) ============================ */
// Rating a set used to mean walking a +/- stepper up the scale — eleven taps to log "5 reps
// left". This is the one-tap replacement: a colour-coded button per preset, plus a free field
// for the value between two presets. Presets are stored in RIR internally; a profile that logs
// RPE sees the same buttons labelled on its own scale (toScale), coloured identically — the
// colour is the effort, not the number, so 0 RIR and 10 RPE are both the "went to failure" end.
function EffortPicker({ kind, value, onPick, close, cardio = false }) {
// Local mirror so the ticked preset and the exact field track typing live.
const [v, setV] = useState(value ?? null)
// The exact field used to write through on every change, and onPick concludes the set (issue
// #64): it ticks the set, beeps and starts the rest. Since stepEffort(kind, null, 1) returns
// the band's minimum, one tap of + — or typing the `1` of `10` — was a committed rating, fired
// under the sheet you were still typing into, and on the last set of the last exercise it
// stacked workoutCompleteSheet on top of the picker. So typing moves the mirror only, and the
// rating is written once, when the sheet goes away: Done, the backdrop, a swipe or Escape all
// unmount this. A preset row still commits on the spot, which is what makes it one tap.
const typed = useRef(value ?? null)
const done = useRef(false)
const set = nv => { typed.current = nv; setV(nv) }
useEffect(() => () => {
if (done.current) return
if (typed.current !== (value ?? null)) onPick(typed.current)
}, [])
// `v` is in the profile's own scale (whatever sits on the set: s.rir or s.rpe). Compare in
// RIR so the tick lands on the right preset on either scale, and so a typed RPE colours the
// same as the RIR it equals.
const curRir = rirOf(kind === 'rpe' ? { rpe: v } : { rir: v })
const curColor = effortColor(curRir)
const commit = nv => { done.current = true; close(); onPick(nv) }
const pick = rir => commit(toScale(kind, rir))
const hd = EFFORT[kind].hd
// Same list the ⋯ menus use: a tinted square with the value where the icon goes, the sentence
// as the row title, a tick on the current one. The exact field is the app's own stepper,
// tinted like the logged cell in the set row, so the sheet and the row read as one thing.
return <>
<h3 style={{ marginBottom: 2 }}>{cardio ? t('How hard did that feel?') : t('How hard was that set?')}</h3>
<div className="muted small" style={{ marginBottom: 10 }}>{cardio ? t('Tap how it felt, or type an exact {0}.', hd) : t('Tap how many reps you had left, or type an exact {0}.', hd)}</div>
<div className="list menu-list effpick">
{EFFORT_PRESETS.map(p => {
const label = fmtNum(toScale(kind, p.rir)) + (p.tail ? '+' : '')
const on = curRir != null && curRir === p.rir
return <div key={p.rir} className={'item menu-item' + (on ? ' on' : '')} style={{ '--bc': p.color }}
{...tappable(() => pick(p.rir))}>
<span className="lrow-i effpick-n">{label}</span>
<div className="grow"><div className="tt">{t(cardio ? p.cardioFeel : p.feel)}</div></div>
<span className={'menu-on' + (on ? ' is-on' : '')}><Icon name="check" /></span>
</div>
})}
<div className="item menu-item effpick-free">
<div className="grow"><div className="tt">{t('Exact {0}', hd)}</div></div>
<div className="stp effcell-stp"
style={curColor ? { color: curColor, background: `color-mix(in srgb, ${curColor} 20%, var(--surface-2))` } : undefined}>
<button aria-label={t('Decrease')} onClick={() => set(stepEffort(kind, v, -1))}><Icon name="minus" /></button>
<span className="val"><NumberField decimal nullable value={v ?? ''} placeholder="–"
onChange={nv => set(capEffort(kind, nv))} /></span>
<button aria-label={t('Increase')} onClick={() => set(stepEffort(kind, v, 1))}><Icon name="plus" /></button>
</div>
</div>
</div>
{v != null && <>
<div style={{ height: 10 }} />
{/* Only after the exact field has moved the value off what is stored: the preset rows
close themselves, so this row exists for the one path that does not. */}
{v !== (value ?? null) && <Button icon="check" onClick={() => commit(v)}>{t('Done')}</Button>}
<Button variant="ghost" className="dim" icon="xmark" onClick={() => commit(null)}>{t('Clear rating')}</Button>
</>}
<div style={{ height: 4 }} />
</>
}
// kind is 'rir' | 'rpe'; value is the set's current rating on that scale (or null); onPick
// receives the new value on that same scale (null to clear). The caller stores it exactly as
// weight/reps are stored — a null drops the key rather than writing a zero. `cardio` words the
// presets for a run or a ride (the caller passes 'rpe' then: reps in reserve mean nothing there).
export const effortPickerSheet = (kind, value, onPick, { cardio = false } = {}) =>
ui().openSheet(close => <EffortPicker kind={kind} value={value} onPick={onPick} close={close} cardio={cardio} />)
/* ============================ share / print / import a plan ============================ */
// What the Plan menu does with a plan: export it, print it, import a friend's.
export const planHasRoutines = st => (st.routines || []).some(r => r.ex && r.ex.length)
export async function exportPlanFile() {
const st = S(), user = useStore.getState().user
const bundle = buildPlanBundle(st, user?.name ? t('{0}’s plan', user.name) : '')
const json = JSON.stringify(bundle, null, 2)
const name = 'opengym-plan-' + todayISO() + '.json'
if (MOBILE) { try { await shareExport(json, name) } catch (e) { /* dismissed */ } return }
const blob = new Blob([json], { type: 'application/json' })
const a = document.createElement('a'); a.href = URL.createObjectURL(blob); a.download = name; a.click(); URL.revokeObjectURL(a.href)
toast(t('Plan file saved. Send it to a friend!'))
}
// Web: the browser's print dialog (→ Save as PDF). Mobile: the OS print flow via the native
// Print plugin — Android WebView has no window.print(). Same printable HTML both ways.
export function printWholePlan() {
const st = S(), owner = useStore.getState().user?.name || ''
if (MOBILE) printHtml(planPrintHTML(st, owner), t('Weekly Training Plan'), printOptions()).catch(() => { /* dismissed */ })
else printPlan(st, owner)
}
// A picked plan file → the import sheet, or a toast saying why not.
export function importPlanFile(f, onRead) {
if (!f) return
const rd = new FileReader()
rd.onload = () => {
try { const bundle = parsePlan(rd.result, S().unit || 'kg'); onRead?.(); planImportSheet(bundle) }
// parsePlan's own refusal is written for people; anything else is not shown raw.
catch (e) { toast(e?.code === 'not-plan' ? t('Import failed: {0}', e.message) : t('Couldn’t read that file.')) }
}
rd.readAsText(f)
}
export const planImportSheet = bundle => ui().openSheet(close => <PlanImport bundle={bundle} close={close} />)
function PlanImport({ bundle, close }) {
const [schedule, setSchedule] = useState(false)
const apply = () => {
update(s => mergePlan(s, bundle, { schedule }))
close()
toast(tn('Added {0} routine to your plan', 'Added {0} routines to your plan', bundle.routineCount))
nav('/plan')
}
return <>
<h3>{bundle.name ? t('Import “{0}”', bundle.name) : t('Import this plan')}</h3>
<div className="muted small" style={{ marginBottom: 14 }}>
{routineCount(bundle.routineCount)}
{' · ' + exCount(bundle.exerciseCount)}
{bundle.scheduledDays > 0
? ' · ' + tn('scheduled on {0} day', 'scheduled on {0} days', bundle.scheduledDays)
: ''}
</div>
<div className="dim small" style={{ marginBottom: 14, lineHeight: 1.4 }}>{t('These are added as new routines. Nothing you already have is touched.')}</div>
{bundle.dropped > 0 && <div className="small" style={{ color: 'var(--yellow)', marginBottom: 14, lineHeight: 1.4 }}>
{t(bundle.dropped === 1
? '{0} exercise in the file isn’t in your library and was left out.'
: '{0} exercises in the file aren’t in your library and were left out.', bundle.dropped)}
</div>}
{bundle.scheduledDays > 0 && <div className="row between" style={{ padding: '10px 2px', borderTop: '1px solid var(--sep)', borderBottom: '1px solid var(--sep)', marginBottom: 16, gap: 12 }}>
<div><div className="tt" style={{ fontSize: 15 }}>{t('Use this weekly schedule')}</div><div className="small dim">{t('Replaces your current Mon–Sun assignments.')}</div></div>
<Switch checked={schedule} onChange={setSchedule} />
</div>}
<Button variant="primary" onClick={apply}>{t('Add to my plan')}</Button>
<div style={{ height: 8 }} />
<Button variant="ghost" className="dim" onClick={close}>{t('Cancel')}</Button>
</>
}
/* ============================ day override / assign ============================ */
function DayOverride({ iso, close }) {
const st = useStore(s => s.S)
const hasOvr = st.dayPlan[iso] !== undefined
// A fulfilled pin (a coach session pinned here and since done) reads as no override, as it does
// everywhere else — the row to clear the leftover entry stays.
const changed = hasOvr && pinState(st, st.dayPlan[iso]) !== 'done'
// The plan the day would have without its override — the weekday's routines, and in a coach
// week the queue's session for the day in front of them, same as the Home row shows.
const { [iso]: _ovr, ...noOvr } = st.dayPlan
const weeklyNames = effectiveRoutineIds({ ...st, dayPlan: noOvr }, iso).map(id => st.routines.find(r => r.id === id)?.name).filter(Boolean)
// A weekday can hold several routines; the per-date override stays single-pick, so picking
// one here collapses a combined day to it (docs/dev/COMBINE_ROUTINES.md §8). The check marks
// show everything currently planned for the day.
const effIds = effectiveRoutineIds(st, iso)
// A planned day in the past with nothing logged was missed — or trained and never logged,
// like a run you forgot to start the app for (#284). Logging it opens "Log a past workout" on
// that date with the day's routines picked, where the time and the duration can still change.
const missed = iso < todayISO() && effIds.length > 0 && !workoutsOn(st, iso).length
const logIt = () => { close(); logPastWorkoutSheet({ iso, routineIds: effIds }) }
// A note on a day off (#261, lib/day-notes.js): offered on today and past days with nothing
// logged, kept in view on any day that already has one. Quiet on purpose: a row, not a button.
const note = dayNoteOf(st, iso)
const canNote = !!note || (canNoteDay(iso) && !workoutsOn(st, iso).length)
// The coach week's sessions still to do come first, in slot order: picking one here pins it
// to this day — the same per-date override, read back by queueNext (lib/queue.js). A session
// already pinned to another day says so in place of its exercise count. Done sessions and
// your own routines follow in the plain list.
const remaining = queueRemaining(st)
// Your own rotation's pass (its rotationId matches the saved rotation) is your loop, not a coach's week.
const ownLoop = !!st.queue && !!st.rotation && st.queue.rotationId === st.rotation.id
const coach = remaining.map(id => st.routines.find(r => r.id === id)).filter(Boolean)
// Active pins straight from the plan (earliest day per session, today or later) — not the
// row's 'pinned' items, which leave out the session pinned to today itself.
const pinnedOn = Object.entries(st.dayPlan).filter(([d, id]) => d >= todayISO() && remaining.includes(id)).sort()
.reduce((m, [d, id]) => (m[id] ? m : { ...m, [id]: d }), {})
const set = v => {
update(s => { if (!v) delete s.dayPlan[iso]; else s.dayPlan[iso] = v })
close()
toast(v === '' ? t('Back to weekly plan') : v === 'rest' ? t('{0} set to rest', fmtDate(iso)) : t('{0} planned for {1}', (st.routines.find(r => r.id === v) || {}).name, fmtDate(iso)))
}
const row = r => <div key={r.id} className="item" {...tappable(() => set(r.id))}>
<span className="lrow-i"><Icon name={glyphOf(r.emoji)} /></span>
<div className="grow"><div className="tt">{r.name}</div><div className="ss">{pinnedOn[r.id] && pinnedOn[r.id] !== iso ? fmtDate(pinnedOn[r.id], true) : exCount(r.ex.length)}</div></div>
{effIds.includes(r.id) && <Icon name="check" className="accent" />}</div>
return <>
<h3>{fmtDate(iso, true)}</h3>
<div className="muted small" style={{ marginBottom: 12 }}>{t('Weekly plan:')} {weeklyNames.length ? deriveSessionName(weeklyNames) : t('Rest')}{changed && <span style={{ color: 'var(--orange)' }}> · {t('changed for this day')}</span>}<br />{t('Sick, missed a day or want a different session? Pick what to train instead.')}</div>
{missed && <div style={{ marginBottom: 14 }}><Button variant="primary" icon="checkCircle" onClick={logIt}>{t('Log this workout')}</Button></div>}
{canNote && <div className="day-note" style={{ marginBottom: 14 }}>
<div className="item day-note-row" {...tappable(() => dayNoteSheet(iso))}>
<span className="lrow-i" style={{ background: 'var(--surface-3)' }}><Icon name={note?.tag ? DAY_NOTE_ICON[note.tag] : 'note'} /></span>
<div className="grow">{note
? <><div className="tt">{note.tag ? t(DAY_NOTE_LABEL[note.tag]) : t('Day note')}</div>{note.text && <div className="ss" style={{ whiteSpace: 'pre-wrap' }}>{note.text}</div>}</>
: <div className="tt">{t('Add a note for this day')}</div>}</div>
<Icon name={note ? 'pencil' : 'plus'} className="chev" />
</div>
</div>}
{coach.length > 0 && <>
<h4 className="sec" style={{ marginTop: 0 }}>{ownLoop ? t('Your loop') : t('Coach week')}</h4>
<div className="list" style={{ marginBottom: 8 }}>{coach.map(row)}</div>
</>}
<div className="list">
{st.routines.filter(r => !remaining.includes(r.id)).map(row)}
<div className="item" {...tappable(() => set('rest'))}><span className="lrow-i" style={{ background: 'var(--surface-3)' }}><Icon name="moon" /></span><div className="grow"><div className="tt">{t('Rest / skip this day')}</div></div>{effIds.length === 0 && <Icon name="check" className="accent" />}</div>
{hasOvr && <div className="item" {...tappable(() => set(''))}><span className="lrow-i" style={{ background: 'var(--surface-3)' }}><Icon name="reset" /></span><div className="grow"><div className="tt">{t('Back to weekly plan')}</div></div></div>}
</div>
</>
}
export const dayOverrideSheet = iso => ui().openSheet(close => <DayOverride iso={iso} close={close} />)
// Why a day went without training (#261): one quick pick, a line of text, or both. Saving an
// empty note takes it away (a stamped clear, lib/day-notes.js withDayNote), and so does Remove.
function DayNote({ iso, close }) {
const noteRef = useRef(null)
const onNoteFocus = useSheetKeyboard(noteRef)
const st = useStore(s => s.S)
const had = dayNoteOf(st, iso)
const [tag, setTag] = useState(had?.tag || null)
const [text, setText] = useState(had?.text || '')
const write = v => {
update(s => { s.dayNotes = withDayNote(s.dayNotes, iso, v) })
close()
}
const save = () => {
const clean = text.trim().slice(0, NOTE_MAX)
if (!tag && !clean && !had) { close(); return }
write({ tag, text: clean })
toast(tag || clean ? t('Noted. See you next session!') : t('Note removed'))
}
const remove = () => { write(null); toast(t('Note removed')) }
return <>
<h3>{fmtDate(iso, true)}</h3>
<div className="muted small" style={{ marginBottom: 12 }}>{t('Life happens. Leave yourself a note, and the missed-day nudge lets this one go.')}</div>
<div className="chips" style={{ margin: '0 0 12px' }}>
{DAY_NOTE_TAGS.map(k => <button key={k} className={'chip nocap ichip' + (tag === k ? ' on' : '')} aria-pressed={tag === k}
onClick={() => setTag(tag === k ? null : k)}><Icon name={DAY_NOTE_ICON[k]} />{t(DAY_NOTE_LABEL[k])}</button>)}
</div>
<textarea ref={noteRef} className="input" rows={3} maxLength={NOTE_MAX} value={text}
placeholder={t('Anything else? (optional)')}
onFocus={onNoteFocus} onChange={e => setText(e.target.value)} />
<div style={{ height: 18 }} />
<Button variant="primary" onClick={save}>{t('Save')}</Button>
{had && <><div style={{ height: 10 }} /><Button variant="ghost" icon="trash" onClick={remove}>{t('Remove note')}</Button></>}
</>
}
export const dayNoteSheet = iso => ui().openSheet(close => <DayNote iso={iso} close={close} />)
// Plan → Schedule → a weekday. One sheet for the whole day: tap routines to put them on it or
// take them off (two or more make a combined day, docs/dev/COMBINE_ROUTINES.md), in the order
// tapped, which is the order the session runs them; "Rest day" clears it. The sheet stays open
// for a second pick, so "Done" closes it. The weekday never stores [] (the key goes instead).
function DayAssign({ day, close }) {
const st = useStore(s => s.S)
const cur = [].concat(st.week[day] || [])
const toggle = id => update(s => {
const ids = [].concat(s.week[day] || [])
const next = ids.includes(id) ? ids.filter(x => x !== id) : [...ids, id]
if (next.length) s.week[day] = next; else delete s.week[day]
})
const rest = () => { update(s => { delete s.week[day] }); close() }
return <>
<h3 style={{ marginBottom: 2 }}>{t(DAYN[day])}</h3>
<div className="muted small" style={{ marginBottom: 10 }}>{t('Pick one routine, or more to train them together.')}</div>
<div className="list menu-list">
<div className="item menu-item" {...tappable(rest)}><span className="lrow-i" style={{ background: 'var(--surface-3)' }}><Icon name="moon" /></span><div className="grow"><div className="tt">{t('Rest day')}</div></div><span className={'menu-on' + (!cur.length ? ' is-on' : '')}><Icon name="check" /></span></div>
{st.routines.map(r => {
const on = cur.includes(r.id)
return <div key={r.id} className="item menu-item" aria-pressed={on} {...tappable(() => toggle(r.id))}>
<span className="lrow-i"><Icon name={glyphOf(r.emoji)} /></span>
<div className="grow"><div className="tt">{r.name}</div><div className="ss">{exCount(r.ex.length)}</div></div>
<span className={'menu-on' + (on ? ' is-on' : '')}><Icon name="check" /></span>
</div>
})}
</div>
<div style={{ height: 12 }} />
<Button variant="primary" onClick={close}>{t('Done')}</Button>
</>
}
export const dayAssignSheet = day => ui().openSheet(close => <DayAssign day={day} close={close} />)
/* ============================ workout detail ============================ */
// Correcting when a saved session happened: typed in from another app, or logged on the wrong
// day. Only its place in history moves — the sets, the volume and the notes are the record, and
// the session keeps the length it had. The PR badges of the exercises it trained are worked out
// again, because "this was a record" is a claim about the sessions before it.
function WorkoutDateEdit({ w, onDone, close }) {
const [date, setDate] = useState(w.d)
const [time, setTime] = useState(startTimeOf(w))
const today = todayISO()
const save = () => {
if (!date || date > today) { toast(t('Pick a day up to today')); return }
// Nothing to do, and nothing to push: a no-op save and a workout deleted from another
// sheet both just close.
if (date === w.d && time === startTimeOf(w)) { close(); return }
if (!(S().workouts || []).some(x => sameWorkout(x, w))) { close(); return }
// It keeps its length, so the end it would have is what has to be in the past.
if (endsInFuture(date, time, durationMinOf(w))) { toast(t('That’s still in the future. Try an earlier start.')); return }
update(s => {
const next = moveWorkout(s.workouts, w, date, time)
if (next) s.workouts = next
})
close()
onDone && onDone()
toast(t('Workout moved'))
}
return <>
<h3>{t('Change date & time')}</h3>
<div className="muted small" style={{ marginBottom: 12 }}>{t('The session keeps its length. Personal records are worked out again from the new order.')}</div>
<Row icon="calendar" title={t('Date')}>
<input type="date" className="timef" value={date} max={today} onChange={e => setDate(e.target.value)} /></Row>
<Row icon="clock" title={t('Start time')}>
<input type="time" className="timef" value={time} onChange={e => setTime(e.target.value)} /></Row>
<div style={{ height: 18 }} />
<Button variant="primary" onClick={save}>{t('Save')}</Button>
</>
}
export const workoutDateSheet = (w, onDone) => ui().openSheet(close => <WorkoutDateEdit w={w} onDone={onDone} close={close} />)
// Correcting how long a saved session ran — mostly the workout nobody ended until they got home
// (Discord). The start stays, the end follows it; the sets, the order of history and the badges
// do not depend on the length and are left alone.
function WorkoutDurationEdit({ w, onDone, close }) {
const [dur, setDur] = useState(durationMinOf(w))
// A cleared field reads as 0, and saving that made the session one minute long without a word
// (QA 1.3.9): it is refused with the reason under the field, and the saved length stays.
const durInvalid = !(dur >= 1)
const durTooLong = dur > MAX_DURATION_MIN
const save = () => {
if (durInvalid) { toast(t('Enter how long it took (at least 1 minute).')); return }
if (durTooLong) { toast(t('That’s more than a day. Keep it to 24 hours or less.')); return }
let changed = false
update(s => {
const next = setWorkoutDuration(s.workouts, w, dur) // at least a minute, however the field was left
if (next) { s.workouts = next; changed = true }
})
close()
if (!changed) return
onDone && onDone()
toast(t('Duration changed'))
}
return <>
<h3>{t('Change duration')}</h3>
<div className="muted small" style={{ marginBottom: 12 }}>{t('Forgot to finish on time? Set how long the session really took. It keeps its start time and its sets.')}</div>
{/* min 0, not 1: the stepper would put a cleared field back to 1 as it lost focus to Save. */}
{/* No max either: one would cut a typo down to a day on blur and save that without a word. */}
<Stepper label={t('Duration')} unit={t('min')} value={dur} step={5} min={0} decimal={false} invalid={durInvalid || durTooLong} onChange={v => setDur(Math.round(v))} />
{durInvalid && <div className="small" style={{ color: 'var(--red)', marginTop: 6 }}>{t('Enter how long it took (at least 1 minute).')}</div>}
{durTooLong && <div className="small" style={{ color: 'var(--red)', marginTop: 6 }}>{t('That’s more than a day. Keep it to 24 hours or less.')}</div>}
<div style={{ height: 18 }} />
<Button variant="primary" onClick={save}>{t('Save')}</Button>
</>
}
export const workoutDurationSheet = (w, onDone) => ui().openSheet(close => <WorkoutDurationEdit w={w} onDone={onDone} close={close} />)
function WorkoutDetail({ w, close }) {
const noteRef = useRef(null)
const onNoteFocus = useSheetKeyboard(noteRef)
const st = useStore(s => s.S)
const update = useStore(s => s.update)
// The session note is editable here rather than only at the finish sheet: what you want to
// record about a session is often clearer once you have looked at what you actually did.
const [note, setNote] = useState(w.note || '')
// A note written here is an edit of the saved workout, stamped for the sync to keep it over an
// older copy of the same workout (stampWorkout) — and only when it changed, since a stamp
// outranks what another device wrote since.
const saveNote = () => update(s => {
const rec = s.workouts.find(x => sameWorkout(x, w))
if (!rec) return
const text = note.trim().slice(0, NOTE_MAX)
if (text === (rec.note || '')) return
if (text) rec.note = text; else delete rec.note
stampWorkout(rec)
})
// onBlur alone loses the note: Escape, the Android back gesture and swipe-to-dismiss all
// close the sheet without ever moving focus out of the textarea. Flush on unmount too. The
// ref is what makes that work — a cleanup closes over the note from its own render, which
// is the empty string this started with.
const latest = useRef(note)
latest.current = note
const initial = useRef(w.note || '')
useEffect(() => () => {
const text = latest.current.trim().slice(0, NOTE_MAX)
if (text === initial.current) return
update(s => {
const rec = s.workouts.find(x => sameWorkout(x, w))
if (!rec || text === (rec.note || '')) return // deleted from this very sheet, or already saved
if (text) rec.note = text; else delete rec.note
stampWorkout(rec)
})
}, [])
const nameOf = e => (EXIDX[e.id] ? exerciseNameFor(EXIDX[e.id]) : (e.n || e.id))
// Tapping an exercise opens its history (Discord 'Improvement ideas'): from one session to the
// curve it sits on, which is the question a past workout raises most often.
const entryRow = (e, i) => {
const ex = EXIDX[e.id]
return <div key={i} className="row wd-ex" style={{ alignItems: 'flex-start' }} {...tappable(() => exerciseHistorySheet(e.id))}>
{ex && <Thumb ex={ex} />}
<div className="grow"><div className={`tt ${exerciseNameClass(ex)}`} style={{ fontWeight: 600 }}>{nameOf(e)} {w.prs && w.prs.includes(e.id) && <span className="pr"><Icon name="trophy" />PR</span>}</div>
<div className="ss">{e.sets.filter(hasCompletedWork).map(s => setLabel(e.id, s, e.target, speedUnitOf(st)) + hrLabel(s)).join(' · ') || t('no sets')}</div>
{e.note && <div className="small dim" style={{ marginTop: 3 }}>
{e.notePin && <Icon name="pin" style={{ fontSize: 12, marginInlineEnd: 4, verticalAlign: '-1px', color: 'var(--yellow)' }} />}{e.note}
</div>}</div>
<Icon name="chevronRight" className="chev" style={{ alignSelf: 'center' }} />
</div>
}
// Exercises done as a superset stay together under a "Superset" label and one bar, the way the
// routine editor shows them (Discord: "supersets aren't shown at all"). Adjacent entries with
// the same tag, as in the workout itself; a workout finished before the tag was kept has none.
const entryRows = units => units.map(unit => {
if (unit.length < 2) return entryRow(w.entries[unit[0]], unit[0])
return <div key={'ss' + unit[0]} className="wd-ss">
<div className="ss-label"><Icon name="link" />{t('Superset')}</div>
{unit.map(i => entryRow(w.entries[i], i))}
</div>
})
// Copied with the note as it stands in the box, which may not be saved yet.
const copyAsText = async () => {
const rec = { ...(st.workouts.find(x => sameWorkout(x, w)) || w), note: note.trim() }
toast(await copyText(workoutText(rec, { unit: st.unit, nameOf, speedUnit: speedUnitOf(st), warmups: countsWarmups(st) })) ? t('Copied') : t('Could not copy'))
}
// A Garmin .fit activity (#447) of the saved record, for Garmin Connect's Import Data.
const exportFit = () => {
const rec = st.workouts.find(x => sameWorkout(x, w)) || w
let bytes
try { bytes = workoutFit(rec, { unit: st.unit, version: __APP_VERSION__ }) }
catch (e) { toast(t('Could not create the file')); return }
saveFitFile(new Blob([bytes], { type: 'application/vnd.ant.fit' }), fitFileName(rec))
}
const saveAsPdf = () => {
saveNote()
const rec = { ...(st.workouts.find(x => sameWorkout(x, w)) || w), note: note.trim() }
printWorkout(st, rec, { owner: useStore.getState().user?.name || '' })
}
const shareAsImage = () => {
const rec = st.workouts.find(x => sameWorkout(x, w)) || w
ui().openSheet(c2 => <ShareWorkoutImage w={rec} nameOf={nameOf} close={c2} />)
}
// A combined session's entries carry a `rid`; group them into per-routine sections in merge
// order, with each section's supersets paired inside it (sessionSections, which "Copy as text"
// reads the workout through too). A legacy single-routine workout (one routineIds, or no rid
// anywhere) renders flat.
const groups = sessionSections(w.entries)
const grouped = groups.length > 1 || (groups[0] && groups[0].rid && (w.routineIds || []).length > 1)
return <>
<h3>{w.name}</h3>
<div className="muted small" style={{ marginBottom: 12 }}>{[fmtDate(w.d, true), ...durPart(w.end - w.start), fmtVol(sessionVolume(w, st), st.unit), ...(w.bw ? [fmtNum(w.bw) + ' ' + st.unit] : [])].join(' · ')}</div>
{grouped ? groups.map(g => {
const r = g.rid ? st.routines.find(x => x.id === g.rid) : null
const items = g.items.map(i => w.entries[i])
const setN = items.reduce((n, e) => n + e.sets.filter(s => s.done && !isWarmupRow(s)).length, 0)
const vol = workoutVolume({ entries: items }, { warmups: countsWarmups(st) })
return <div key={g.rid || '__none'}>
<div className="row between" style={{ margin: '2px 0 8px', paddingBottom: 6, borderBottom: '1px solid var(--sep)' }}>
<div className="row" style={{ gap: 7, fontWeight: 600 }}>
{r && <Icon name={glyphOf(r.emoji)} />}{r ? r.name : t('Freestyle')}
</div>
<div className="small dim">{t('{0} sets', setN)} · {fmtVol(vol, st.unit)}</div>
</div>
{entryRows(g.units)}
</div>
}) : entryRows(groups[0]?.units || [])}
{/* Progress photos and form-check videos: added and removed right here, on the saved record. */}
<WorkoutMediaSection w={w} />
<div className="small muted" style={{ margin: '4px 0 6px' }}>{t('Session note')}</div>
<textarea ref={noteRef} className="input" rows={2} maxLength={NOTE_MAX} value={note}
placeholder={t('How the session went as a whole.')}
onFocus={onNoteFocus} onChange={e => setNote(e.target.value)} onBlur={saveNote} />
<div style={{ height: 14 }} />
{st.active && <p className="small muted">{t('Finish the current workout first.')}</p>}
{/* The editor starts from the record as it is, so a note typed here goes in first — the same
flush the date row does, and the unmount hook then has nothing left to write over it. */}
<Button icon="pencil" disabled={!!st.active} onClick={() => {
saveNote()
initial.current = latest.current.trim().slice(0, NOTE_MAX)
try { update(state => { editCompletedSession(state, w) }); closeThenNav(close, '/workout') }
catch (error) { toast(t(error.message)) }
}}>{t('Edit workout')}</Button>
<div style={{ height: 8 }} />
{/* The same exercises again, today, as a new session (#58): the rows read what was logged
in this workout. The record itself stays as it is. */}
<Button icon="reset" disabled={!!st.active} style={{ marginBottom: 8 }} onClick={() => {
saveNote()
initial.current = latest.current.trim().slice(0, NOTE_MAX)
repeatWorkout(w, close)
}}>{t('Repeat today')}</Button>
{/* The note lives in a textarea that only writes on blur, and moving the workout re-keys a
legacy record — so flush it first and stop the unmount hook writing it a second time. */}
<Button icon="calendar" style={{ marginBottom: 8 }} onClick={() => {
saveNote()
initial.current = latest.current.trim().slice(0, NOTE_MAX)
workoutDateSheet(w, close)
}}>{t('Change date & time')}</Button>
<Button icon="stopwatch" style={{ marginBottom: 8 }} onClick={() => {
saveNote()
initial.current = latest.current.trim().slice(0, NOTE_MAX)
workoutDurationSheet(w, close)
}}>{t('Change duration')}</Button>
<Button icon="plus" onClick={() => confirmSheet({
title: t('Save as routine?'),
message: t('Create an independent routine from these exercise targets. Your workout history is kept.'),
confirmText: t('Save'),
onConfirm: () => {
let id
try { update(s => { id = saveSessionAsRoutine(s, w, w.name) }) }
catch (e) { toast(t(e.message)); return }
close()
nav('/plan/r/' + id)
}
})}>{t('Save as routine')}</Button>
<div style={{ height: 8 }} />
<Button icon="copy" onClick={copyAsText}>{t('Copy as text')}</Button>
<div style={{ height: 8 }} />
<Button icon="share" onClick={shareAsImage}>{t('Share as image')}</Button>
<div style={{ height: 8 }} />
{/* On paper: every exercise and every set, the browser's print dialog or the phone's print sheet. */}
<Button icon="note" onClick={saveAsPdf}>{t('Save as PDF')}</Button>
<div style={{ height: 8 }} />
<Button icon="download" onClick={exportFit}>{t('Export as .fit file')}</Button>
<div style={{ height: 10 }} />
{/* Matched the way the edits above are, not by id: a workout from before ids has none, and
filtering on `x.id !== undefined` took every other one of them with it. */}
<Button variant="danger" onClick={() => confirmSheet({ title: t('Delete workout?'), message: t('This removes it from your history for good.') + mediaGoesToo(S().workouts.find(x => sameWorkout(x, w))), confirmText: t('Delete'), danger: true, onConfirm: () => { update(s => { s.workouts = s.workouts.filter(x => !sameWorkout(x, w)) }); close(); toast(t('Workout deleted')) } })}>{t('Delete workout')}</Button>
</>
}
// "Share as image" (#453): the card lib/workout-card.js lays out, painted in the theme and accent
// the app is showing right now, previewed before it goes anywhere. The muscle map is the finish
// summary's, for this one session; whether the card carries it is remembered with the profile.
function ShareWorkoutImage({ w, nameOf, close }) {
const st = useStore(s => s.S)
const update = useStore(s => s.update)
const withMap = st.shareMap !== false
const warmups = countsWarmups(st)
const model = useMemo(() => workoutCardModel(w, { unit: st.unit, nameOf, warmups }), [w, st.unit, warmups])
// The last picture stays up while the next one is drawn, so the toggle never blanks the sheet.
const [img, setImg] = useState(null)
const [busy, setBusy] = useState(false)
const shown = useRef(null)
useEffect(() => {
let alive = true
workoutCardImage(model, { map: withMap, body: st.body })
.then(blob => {
if (!alive) return
if (!blob) { setImg(prev => ({ ...prev, failed: true })); return }
if (shown.current) URL.revokeObjectURL(shown.current)
shown.current = URL.createObjectURL(blob)
setImg({ blob, url: shown.current })
})
.catch(() => { if (alive) setImg(prev => ({ ...prev, failed: true })) })
return () => { alive = false }
}, [model, withMap, st.body])
useEffect(() => () => { if (shown.current) URL.revokeObjectURL(shown.current) }, [])
const name = 'opengym-workout-' + (workoutDay(w) || todayISO()) + '.png'
const share = async () => {
setBusy(true)
await shareImage(img.blob, name, w.name || 'openGym')
setBusy(false)
close()
}
return <>
<h3>{t('Share workout')}</h3>
<div className="share-card">
{img?.url
? <img src={img.url} alt={t('Preview of the image to share')} />
: img?.failed ? <div className="share-card-ph small muted">{t('Could not create the image')}</div>
: <div className="share-card-ph" aria-hidden="true" />}
</div>
{model.levels && <div className="sect-b" style={{ marginBottom: 12 }}>
<Row icon="figureStrength" iconTint="var(--acc)" title={t('Muscle map')} subtitle={t('Show which muscles this workout trained.')}>
<Switch checked={withMap} aria-label={t('Muscle map')} onChange={v => update(s => { s.shareMap = v })} />
</Row>
</div>}
<Button variant="primary" icon="share" disabled={!img?.blob || busy} onClick={share}>{t('Share')}</Button>
</>
}
async function workoutCardImage(model, { map, body }) {
const root = getComputedStyle(document.documentElement)
const css = (name, fallback) => root.getPropertyValue(name).trim() || fallback
const colors = { bg: css('--bg', '#000'), surface: css('--surface', '#1c1c1e'), tile: css('--surface-2', '#2c2c2e'), label: css('--label', '#fff'), label2: css('--label-2', '#999'), acc: css('--acc', '#30d158') }
// The body geometry is ~90 KB and lazy everywhere else too (components/BodyMap.jsx).
const geometry = map && model.levels ? (await import('./lib/body-paths.js')).default : null
const shape = geometry && (geometry[body] || geometry.male)
const canvas = document.createElement('canvas')
const ctx = canvas.getContext('2d')
if (!ctx) return null
const measure = (text, weight, size) => { ctx.font = `${weight} ${size}px ${CARD_FONT}`; return ctx.measureText(text).width }
const layout = layoutWorkoutCard(model, { measure, rtl: document.documentElement.dir === 'rtl', map: shape ? { aspect: viewAspect(shape.front) } : null })
canvas.width = layout.width
canvas.height = layout.height
drawWorkoutCard(ctx, layout, colors, shape ? { body: shape, levels: model.levels } : null)
return new Promise(resolve => canvas.toBlob(resolve, 'image/png'))
}
// The .fit file goes out the way a backup does: the share sheet in the phone app (Files, Drive,
// Garmin Connect itself), a download in the browser.
async function saveFitFile(blob, name) {
if (MOBILE) { try { await shareExportBlob(blob, name) } catch (e) { /* share sheet dismissed */ } return }
const a = document.createElement('a'); a.href = URL.createObjectURL(blob); a.download = name; a.click(); URL.revokeObjectURL(a.href)
toast(t('Saved. Garmin Connect can take it from here.'))
}
// The phone app goes through the OS share sheet the way a backup does; a browser that can share
// files (most phones) opens its own; anything else saves the picture as a download.
async function shareImage(blob, name, title) {
if (MOBILE) { try { await shareExportBlob(blob, name) } catch (e) { /* share sheet dismissed */ } return }
const file = typeof File === 'function' ? new File([blob], name, { type: 'image/png' }) : null
if (file && navigator.canShare?.({ files: [file] })) {
try { await navigator.share({ files: [file], title }) } catch (e) { /* dismissed */ }
return
}
const a = document.createElement('a'); a.href = URL.createObjectURL(blob); a.download = name; a.click(); URL.revokeObjectURL(a.href)
toast(t('Image saved'))
}
// The sentence a workout's Delete adds when its photos and videos go with it — every file the
// record lists, shown or not. Empty when it has none.
function mediaGoesToo(rec) {
const n = workoutMediaOf(rec, Infinity).length
return n ? ' ' + tn('Its photo or video is deleted with it.', 'Its {0} photos or videos are deleted with it.', n) : ''
}
export const workoutDetailSheet = w => ui().openSheet(close => <WorkoutDetail w={w} close={close} />)
/* ============================ calendar ============================ */
function Calendar({ start, close }) {
const st = useStore(s => s.S)
// A day as 'YYYY-MM-DD' is read at local noon: new Date('2026-09-01') is UTC midnight, which is
// still August west of UTC, so a tap on the 1st in Stats opened the month before (found by the
// weekly run in America/Los_Angeles, .github/workflows/audit.yml).
const [cur, setCur] = useState(() => { const d = start ? new Date(/^\d{4}-\d{2}-\d{2}$/.test(start) ? start + 'T12:00:00' : start) : new Date(); d.setDate(1); return d })
const y = cur.getFullYear(), mo = cur.getMonth()
const byDay = {}
st.workouts.forEach(w => {
const day = workoutDay(w)
if (day) (byDay[day] = byDay[day] || []).push(w)
})
// Which column the 1st sits in, and therefore how many blanks come before it.
const ws = weekStartOf(st)
const startOffset = weekDayOffset(new Date(y, mo, 1).getDay(), ws)
const daysIn = new Date(y, mo + 1, 0).getDate()
const monthWs = st.workouts.filter(w => workoutDay(w)?.startsWith(y + '-' + String(mo + 1).padStart(2, '0')))
const monthVol = monthWs.reduce((a, w) => a + sessionVolume(w, st), 0)
const monthMs = monthWs.reduce((a, w) => a + Math.max(0, (w.end || w.start) - w.start), 0)
const cells = []
for (let i = 0; i < startOffset; i++) cells.push(<div key={'e' + i} />)
for (let d = 1; d <= daysIn; d++) {
const iso = y + '-' + String(mo + 1).padStart(2, '0') + '-' + String(d).padStart(2, '0')
// A fulfilled pin (a coach session pinned here and since done) reads as no override, as it does everywhere else.
const ws = byDay[iso], planned = effectiveRoutineIds(st, iso).length > 0, ovr = st.dayPlan[iso] !== undefined && pinState(st, st.dayPlan[iso]) !== 'done'
// A day off with a note on it (#261) shows the note's dot instead of the plan's, and its
// note as the tooltip; tapping it opens the day sheet, where the note is.
const note = !ws && dayNoteOf(st, iso)
const dotCls = ws ? 'done' : note ? 'noted' : ovr && planned ? 'ovr' : planned ? 'plan' : ''
cells.push(<button key={d} className={'cal-d' + (ws ? ' has' : '') + (note ? ' noted' : '') + (iso === todayISO() ? ' today' : '')}
title={note ? dayNoteLine(note, t) : undefined} onClick={() => {
if (!ws) { close(); dayOverrideSheet(iso); return }
if (ws.length === 1) { close(); workoutDetailSheet(ws[0]); return }
close(); ui().openSheet(c2 => <><h3>{fmtDate(iso, true)}</h3><div className="list">{ws.map(w => <WorkoutRow key={w.id} w={w} onClick={() => { c2(); workoutDetailSheet(w) }} />)}</div></>)
}}><span>{d}</span><i className={dotCls} /></button>)
}
return <>
<div className="row between" style={{ marginBottom: 2 }}>
<button className="iconbtn" onClick={() => setCur(new Date(y, mo - 1, 1))} aria-label={t('Previous month')}><Icon name="chevronLeft" /></button>
<h3 style={{ margin: 0 }}>{t(MONTHS_LONG[mo])} {y}</h3>
<button className="iconbtn" onClick={() => setCur(new Date(y, mo + 1, 1))} aria-label={t('Next month')}><Icon name="chevronRight" /></button>
</div>
<div className="small muted" style={{ textAlign: 'center' }}>{monthWs.length ? `${tn('{0} workout', '{0} workouts', monthWs.length)} · ${fmtDur(monthMs)} · ${fmtVol(monthVol, st.unit)}` : t('No workouts this month')}</div>
<div className="cal-grid">{weekOrder(ws).map(d => <div key={d} className="cal-h">{t(DAYS[d])}</div>)}{cells}</div>
<div className="cal-legend">
<span><i style={{ background: 'var(--acc)' }} />{t('Trained')}</span>
<span><i style={{ background: 'var(--label-3)' }} />{t('Planned')}</span>
<span><i style={{ background: 'var(--orange)' }} />{t('Rescheduled')}</span>
{Object.keys(st.dayNotes || {}).some(k => k.startsWith(y + '-' + String(mo + 1).padStart(2, '0')) && dayNoteOf(st, k)) &&
<span><i style={{ background: 'var(--indigo)' }} />{t('Day note')}</span>}
</div>
<div className="small dim" style={{ textAlign: 'center', marginTop: 10 }}>{t('Tap a trained day for details · tap any other day to plan a session')}</div>
</>
}
export const calendarSheet = start => ui().openSheet(close => <Calendar start={start} close={close} />)
/* shared small workout row (used in lists) */
export function WorkoutRow({ w, onClick }) {
const st = useStore(s => s.S)
const glyph = glyphOf((st.routines.find(r => r.id === w.routineId) || {}).emoji)
const mediaN = workoutMediaCount(w)
return <div className="item" {...tappable(onClick)}>
<span className="lrow-i" style={{ width: 34, height: 34, borderRadius: 8, fontSize: 19 }}><Icon name={glyph} /></span>
<div className="grow"><div className="tt">{w.name}</div>
<div className="ss">{[fmtDate(w.d, true), ...durPart(w.end - w.start), tn('{0} set', '{0} sets', setsDone(w)), fmtVol(sessionVolume(w, st), st.unit)].join(' · ')}</div></div>
{mediaN > 0 && <span className="wrow-media" title={tn('{0} photo or video', '{0} photos or videos', mediaN)} aria-label={tn('{0} photo or video', '{0} photos or videos', mediaN)}><Icon name="image" />{mediaN}</span>}
{w.prs && w.prs.length > 0 && <span className="pr"><Icon name="trophy" />{w.prs.length} PR</span>}
<Icon name="chevronRight" className="chev" />
</div>
}
/* a day off with a note (#261), as History lists it between the workouts */
export function DayNoteRow({ iso, note }) {
return <div className="item day-note-row" {...tappable(() => dayNoteSheet(iso))}>
<span className="lrow-i" style={{ width: 34, height: 34, borderRadius: 8, fontSize: 19, background: 'var(--surface-3)' }}><Icon name={note.tag ? DAY_NOTE_ICON[note.tag] : 'note'} /></span>
<div className="grow"><div className="tt">{note.tag ? t(DAY_NOTE_LABEL[note.tag]) : t('Day note')}</div>
<div className="ss">{[fmtDate(iso, true), note.text].filter(Boolean).join(' · ')}</div></div>
<Icon name="chevronRight" className="chev" />
</div>
}
/* ============================ workout lifecycle ============================ */
// `routineIds` accepts `string | string[] | null` — `[r.id]` for one routine,
// `effectiveRoutineIds(...)` for today's planned session, `[]` / null for explicit freestyle.
export function startFlow(routineIds) {
// The weigh-in is a setting (Settings → During a workout, issue #137): off goes straight
// into the session with no body weight on it, same as "Start without weighing in".
if (S().weighIn === false) { beginWorkout(routineIds, null); return }
bwSheet({ required: true, onDone: bw => beginWorkout(routineIds, bw) })
}
// `opts` from the Apple Watch (lib/watch-sync.js): a session started there keeps the watch's id,
// so the sets ticked on it find it, and its start time, however late the phone hears of it.
export function beginWorkout(routineIds, bw, { id = null, start = null } = {}) {
const st = S()
const { entries, routineIds: rids, routines } = buildCombinedEntries(st, routineIds)
const startAt = Number.isFinite(start) && start > 0 ? start : Date.now()
update(s => {
s.active = {
id: id || uid(), d: id && start ? isoOf(new Date(startAt)) : todayISO(), start: startAt,
// A session tracks its routines as a list; per-entry `rid` carries which one each
// exercise came from. No top-level `excludeFromProgression` — per-entry `noProg` does it.
routineIds: rids,
name: routines.length ? deriveSessionName(routines.map(r => r.name)) : t('Freestyle'),
bw: bw || null, cur: 0, entries,
// Snapshot the layout at start so the header ⋮ can change it for this session only —
// changing the saved default (Settings → Workout view) mid-session leaves it alone.
workoutView: st.workoutView || 'cards',
}
})
useUI.getState().stopRest()
navToWorkout()
}
// "Repeat today" (#58): a saved workout's exercises as a new freestyle session dated today, each
// seeded from what that workout logged (lib/session-repeat.js). The weigh-in asks first, the
// way starting any workout does.
export function repeatWorkout(w, close) {
const st = S()
if (st.active) { toast(t('Finish the current workout first.')); return }
const { entries, skipped } = repeatSessionEntries(st, w)
if (!entries.length) { toast(t('Nothing to repeat. Its exercises no longer exist.')); return }
const go = bw => {
if (S().active) { toast(t('Finish the current workout first.')); return }
update(s => {
s.active = {
id: uid(), d: todayISO(), start: Date.now(), routineIds: [],
name: w.name || t('Freestyle'), bw: bw || null, cur: 0, entries,
workoutView: s.workoutView || 'cards',
}
})
useUI.getState().stopRest()
if (close) closeThenNav(close, '/workout'); else navToWorkout()
if (skipped) toast(tn('{0} exercise no longer exists and was left out.', '{0} exercises no longer exist and were left out.', skipped))
}
if (st.weighIn === false) { go(null); return }
if (close) close()
bwSheet({ required: true, onDone: go })
}
/* ============================ log a past workout ============================ */
// The same screen as a live session, pointed at another day. `backfill` on the active
// session is what tells the workout screen to drop the clock and the rest timers, and tells
// the finish path to file the workout where its date belongs instead of at the end.
// `initial` is a missed day of the plan (#284): its date, and the routines planned for it. A
// combined day is offered as the one session it plans, next to each routine on its own.
const PLANNED_DAY = '__planned-day'
function LogPastWorkout({ initial, close }) {
const st = useStore(s => s.S)
const yesterday = new Date(); yesterday.setDate(yesterday.getDate() - 1)
const planned = [].concat(initial?.routineIds || []).map(id => st.routines.find(r => r.id === id)).filter(Boolean)
const [date, setDate] = useState(initial?.iso || isoOf(yesterday))
const [time, setTime] = useState('18:00')
const [dur, setDur] = useState(60)
const [routineId, setRoutineId] = useState(planned.length > 1 ? PLANNED_DAY : planned[0]?.id || '')
const today = todayISO()
const options = [
{ value: '', label: t('Freestyle') },
...(planned.length > 1 ? [{ value: PLANNED_DAY, label: deriveSessionName(planned.map(r => r.name)) }] : []),
...st.routines.map(r => ({ value: r.id, label: r.name })),
]
const go = replaceId => {
close()
const routineIds = routineId === PLANNED_DAY ? planned.map(r => r.id) : routineId ? [routineId] : []
beginBackfill({ iso: date, time, durationMin: Math.max(1, dur), routineIds, replaceId })
}
const submit = () => {
if (!date || date > today) { toast(t('Pick a day up to today')); return }
if (!(dur >= 1)) { toast(t('Enter how long it took (at least 1 minute).')); return }
if (dur > MAX_DURATION_MIN) { toast(t('That’s more than a day. Keep it to 24 hours or less.')); return }
if (endsInFuture(date, time, dur)) { toast(t('That’s still in the future. Try an earlier start.')); return }
const existing = workoutsOn(st, date)
if (!existing.length) { go(null); return }
ui().openSheet(c => <SameDayChoice iso={date} existing={existing} close={c}
onReplace={id => { c(); go(id) }} onAdd={() => { c(); go(null) }} />, { kind: 'center' })
}
return <>
<h3>{t('Log a past workout')}</h3>
<div className="muted small" style={{ marginBottom: 12 }}>{t('Logged on the usual workout screen, without timers.')}</div>
<Row icon="calendar" title={t('Date')}>
<input type="date" className="timef" value={date} max={today} onChange={e => setDate(e.target.value)} /></Row>
<Row icon="clock" title={t('Start time')}>
<input type="time" className="timef" value={time} onChange={e => setTime(e.target.value)} /></Row>
<Stepper label={t('Duration')} unit={t('min')} value={dur} step={5} min={0} decimal={false} invalid={!(dur >= 1) || dur > MAX_DURATION_MIN} onChange={v => setDur(Math.round(v))} />
{!(dur >= 1) && <div className="small" style={{ color: 'var(--red)', marginTop: 6 }}>{t('Enter how long it took (at least 1 minute).')}</div>}
{dur > MAX_DURATION_MIN && <div className="small" style={{ color: 'var(--red)', marginTop: 6 }}>{t('That’s more than a day. Keep it to 24 hours or less.')}</div>}
<div style={{ height: 8 }} />
<SelectRow icon="dumbbell" title={t('Routine')} value={routineId} options={options} onChange={setRoutineId} />
<div style={{ height: 18 }} />
<Button variant="primary" onClick={submit}>{t('Continue')}</Button>
</>
}
// Three ways out when the day already has a workout. Replacing with several on that day means
// picking which one; the rest of the day is left alone. A workout with photos or videos says
// they come along.
function SameDayChoice({ iso, existing, onReplace, onAdd, close }) {
return <div style={{ textAlign: 'center', padding: '4px 0' }}>
<h3 style={{ marginBottom: 8 }}>{fmtDate(iso, true)}</h3>
<div className="muted" style={{ marginBottom: 18, lineHeight: 1.5 }}>{t('There is already a workout on that day.')}</div>
{existing.map(w => {
// Replacing re-logs the sets; the photos and videos move to the new record (completeBackfill).
const n = workoutMediaOf(w, Infinity).length
return <div key={w.id} style={{ marginBottom: 8 }}>
<button className="btn danger" onClick={() => onReplace(w.id)}>{existing.length > 1 ? t('Replace') + ' · ' + w.name : t('Replace')}</button>
{n > 0 && <div className="small dim samed-media" style={{ marginTop: 4 }}>{tn('Its photo or video moves to the new workout.', 'Its {0} photos or videos move to the new workout.', n)}</div>}
</div>
})}
<button className="btn primary" onClick={onAdd}>{t('Add as second workout')}</button>
<div style={{ height: 8 }} />
<Button variant="ghost" className="dim" onClick={close}>{t('Cancel')}</Button>
</div>
}
// History's button hands this its click event; only a day from the plan counts as `initial`.
export function logPastWorkoutSheet(initial) {
if (S().active) { toast(t('Finish the current workout first.')); return }
const from = typeof initial?.iso === 'string' ? initial : null
ui().openSheet(close => <LogPastWorkout initial={from} close={close} />)
}
// A backfilled session is built the way a live one is (buildCombinedEntries → buildPlannedEntry),
// so its entries carry the same stamps: the routine list, per-entry rid and the plan, no
// top-level routineId. One routine from the picker, or every routine of a missed combined day.
// Only from the history before that day (historyAsOf): a session logged later must not hand its
// progression back to the day it skipped.
function beginBackfill({ iso, time, durationMin, routineIds, replaceId }) {
const st = S()
const start = backfillStart(iso, time)
const past = historyAsOf(st, { d: iso, start, replaceId })
const { entries, routineIds: rids, routines } = buildCombinedEntries(past, routineIds || [])
update(s => {
s.active = {
id: uid(), d: iso, start,
routineIds: rids,
name: routines.length ? deriveSessionName(routines.map(r => r.name)) : t('Freestyle'),
bw: null, cur: 0, entries,
backfill: { durationMin, replaceId: replaceId || null },
// Same layout snapshot as a live session (see beginWorkout).
workoutView: st.workoutView || 'cards',
}
})
useUI.getState().stopRest()
navToWorkout()
}
/* ============================ add a routine mid-session ============================ */
// The workout header ⋮ → Add routine. Single-pick: a routine already in the session, or one
// with no exercises, is shown disabled and tagged. Picking one appends its entries (each
// stamped with its `rid`), extends `s.active.routineIds`, and re-derives the session name.
// `s.active.cur` is left where it is — the appended block is reached by scrolling / Next.
function AddRoutineToSession({ close }) {
const st = useStore(s => s.S)
const active = st.active
if (!active) return null
const inSession = new Set([].concat(active.routineIds || []))
const add = r => {
const entries = buildSessionEntries(sessionHistory(st), r).map(e => ({ ...e, rid: r.id }))
update(s => {
if (!s.active) return
// A session kept out of progression as a whole (the header ⋮) keeps the routine's
// exercises out too, the same as an exercise added on its own.
s.active.entries.push(...entries.map(e => joinSessionNoProg(s.active, e)))
s.active.routineIds = [...[].concat(s.active.routineIds || []), r.id]
if (!s.active.customName) {
s.active.name = deriveSessionName(s.active.routineIds.map(id => s.routines.find(x => x.id === id)?.name).filter(Boolean))
}
})
close()
toast(t('{0} added ({1})', r.name, exCount(r.ex.length)))
}
return <>
<h3>{t('Add routine')}</h3>
<div className="list">
{st.routines.map(r => {
const already = inSession.has(r.id)
const empty = !(r.ex || []).length
const disabled = already || empty
return <div key={r.id} className={'item' + (disabled ? ' disabled' : '')} aria-disabled={disabled || undefined}
{...tappable(disabled ? null : () => add(r))}>
<span className="lrow-i"><Icon name={glyphOf(r.emoji)} /></span>
<div className="grow"><div className="tt">{r.name}</div><div className="ss">{exCount(r.ex.length)}</div></div>
{already ? <span className="tag">{t('already added')}</span> : empty ? <span className="tag">{t('no exercises')}</span> : <Icon name="chevronRight" className="chev" />}
</div>
})}
</div>
</>
}
export function addRoutineToSessionSheet() {
if (!S().active) return
ui().openSheet(close => <AddRoutineToSession close={close} />)
}
/* ============================ exercise notes ============================
Two notes, one sheet, because from the user's side it is one question — "what do I want to
remember about this exercise?" — with two different lifetimes:
· today's note belongs to this session and is stored on the workout entry. It is history:
what happened, how it felt. The PIN is the user saying "this one is for next time", which
only they can know at the moment of writing — see pinnedNoteFor.
· the standing note belongs to the exercise itself and lives in S.exNotes. Seat height, pin
position, a form cue. True every session, so it is shown every session and never expires.
A routine's own `note` (a plan's instruction for this exercise) is edited in the config sheet
and is deliberately not here: it belongs to the plan, not to the day or to the movement. */
function ExerciseNote({ entryIdx, close }) {
const noteRef = useRef(null)
const onNoteFocus = useSheetKeyboard(noteRef)
const st = useStore(s => s.S)
const update = useStore(s => s.update)
const A = st.active
const editing = !!A?.editingWorkoutId
const entry = A ? A.entries[entryIdx] : null
const ex = entry ? exOr(entry.id) : null
const [note, setNote] = useState(entry?.note || '')
const [pin, setPin] = useState(!!entry?.notePin)
const [standing, setStanding] = useState(entry ? (st.exNotes?.[entry.id] || '') : '')
useEffect(() => { if (!entry) close() }, [!entry])
if (!entry) return null
const save = () => {
const today = note.trim().slice(0, NOTE_MAX)
const always = standing.trim().slice(0, NOTE_MAX)
update(s => {
const e = s.active?.entries?.[entryIdx]
if (e) {
if (today) { e.note = today; if (pin) e.notePin = true; else delete e.notePin }
else { delete e.note; delete e.notePin }
}
if (!editing) {
s.exNotes = s.exNotes || {}
if (always) s.exNotes[entry.id] = always
else delete s.exNotes[entry.id]
}
})
close()
}
return <>
<h3 className={exerciseNameClass(ex)}>{exerciseNameFor(ex)}</h3>
<div className="small muted" style={{ marginBottom: 6 }}>{t('This session')}</div>
<textarea ref={noteRef} className="input" rows={3} maxLength={NOTE_MAX} value={note}
placeholder={t('How it went, what to change. Saved with today’s workout.')}
onFocus={onNoteFocus} onChange={e => setNote(e.target.value)} />
<div style={{ height: 10 }} />
<div className="sect-b">
<Row icon="pin" iconTint="var(--yellow)" title={t('Show this next time')}
subtitle={t('Brings it up again the next time you train this exercise.')}>
<Switch checked={pin} onChange={setPin} disabled={!note.trim()} />
</Row>
</div>
{!editing && <>
<div style={{ height: 18 }} />
<div className="small muted" style={{ marginBottom: 6 }}>{t('Always for this exercise')}</div>
<textarea className="input" rows={2} maxLength={NOTE_MAX} value={standing}
placeholder={t('Seat height, pin position, a form cue. Shown every session.')}
onChange={e => setStanding(e.target.value)} />
</>}
<div style={{ height: 18 }} />
<Button variant="primary" onClick={save}>{t('Save')}</Button>
</>
}
export const exerciseNoteSheet = entryIdx => ui().openSheet(close => <ExerciseNote entryIdx={entryIdx} close={close} />)
function SetNote({ entryIdx, setIdx, close }) {
const noteRef = useRef(null)
const onNoteFocus = useSheetKeyboard(noteRef)
const row = useStore(s => s.S.active?.entries?.[entryIdx]?.sets?.[setIdx])
const update = useStore(s => s.update)
const [note, setNote] = useState(row?.note || '')
useEffect(() => { if (!row) close() }, [!row])
if (!row) return null
const save = () => {
const text = note.trim().slice(0, NOTE_MAX)
update(s => {
const set = s.active?.entries?.[entryIdx]?.sets?.[setIdx]
if (!set) return
if (text) set.note = text
else delete set.note
}, true)
close()
}
return <>
<h3>{t('Set note')}</h3>
<textarea ref={noteRef} className="input" rows={4} maxLength={NOTE_MAX} value={note}
placeholder={t('Anything specific to remember about this set.')}
onFocus={onNoteFocus} onChange={event => setNote(event.target.value)} />
<div style={{ height: 18 }} />
<Button variant="primary" onClick={save}>{t('Save')}</Button>
</>
}
export const setNoteSheet = (entryIdx, setIdx) =>
ui().openSheet(close => <SetNote entryIdx={entryIdx} setIdx={setIdx} close={close} />)
/* The session note: how the whole workout went, as opposed to how one exercise went. It lives
on the active session, so buildCompletedWorkout carries it onto the finished workout and it
shows up again in history — where it stays editable. Written here rather than only after the
fact because "notes you can write during a workout" is the point; a note you can only add
once the session is filed is a different, smaller feature. */
function SessionNote({ close }) {
const noteRef = useRef(null)
const onNoteFocus = useSheetKeyboard(noteRef)
const st = useStore(s => s.S)
const update = useStore(s => s.update)
const A = st.active
const [note, setNote] = useState(A?.note || '')
useEffect(() => { if (!A) close() }, [!A])
if (!A) return null
const save = () => {
const text = note.trim().slice(0, NOTE_MAX)
update(s => { if (!s.active) return; if (text) s.active.note = text; else delete s.active.note })
close()
}
return <>
<h3>{t('Session note')}</h3>
<textarea ref={noteRef} className="input" rows={4} maxLength={NOTE_MAX} value={note}
placeholder={t('How the session went as a whole.')}
onFocus={onNoteFocus} onChange={e => setNote(e.target.value)} />
<div style={{ height: 18 }} />
<Button variant="primary" onClick={save}>{t('Save')}</Button>
</>
}
export const sessionNoteSheet = () => ui().openSheet(close => <SessionNote close={close} />)
function RenameWorkout({ close }) {
const inputRef = useRef(null)
const onFocus = useSheetKeyboard(inputRef)
const st = useStore(s => s.S)
const update = useStore(s => s.update)
const A = st.active
const [name, setName] = useState(A?.name || '')
useEffect(() => { if (!A) close() }, [!A])
if (!A) return null
const trimmed = name.trim().slice(0, 60)
const canSave = trimmed.length > 0
const save = () => {
if (!canSave) return
update(s => {
if (!s.active) return
s.active.name = trimmed
s.active.customName = true
})
close()
}
return <>
<h3>{t('Rename workout')}</h3>
<input ref={inputRef} className="input" type="text" autoFocus maxLength={60} value={name}
placeholder={t('Workout title')}
onFocus={onFocus} onChange={e => setName(e.target.value)}
onKeyDown={e => { if (e.key === 'Enter' && canSave) save() }} />
<div style={{ height: 18 }} />
<Button variant="primary" disabled={!canSave} onClick={save}>{t('Save')}</Button>
</>
}
export const renameWorkoutSheet = () => ui().openSheet(close => <RenameWorkout close={close} />)
// A superset's own name and rest between rounds (#292), from the routine editor.
function SupersetSettings({ meta, onSave, close }) {
const inputRef = useRef(null)
const onFocus = useSheetKeyboard(inputRef)
const [name, setName] = useState(meta.name || '')
const [rest, setRest] = useState(meta.rest || 0)
const save = () => { onSave({ name: name.trim().slice(0, SG_NAME_MAX), rest }); close() }
return <>
<h3>{t('Superset')}</h3>
<input ref={inputRef} className="input" type="text" maxLength={SG_NAME_MAX} value={name}
placeholder={t('Name, e.g. Arm finisher')} onFocus={onFocus} onChange={e => setName(e.target.value)}
onKeyDown={e => { if (e.key === 'Enter') save() }} />
<div style={{ height: 12 }} />
<div className="list">
<Row icon="timer" iconTint="var(--orange)" title={t('Rest after each round')}
value={rest > 0 ? fmtRest(rest) : t('Longest exercise rest')} accessory="chevron"
onClick={() => durationSheet({ title: t('Rest after each round'), value: rest, max: REST_MAX, off: t('Longest exercise rest'),
footer: t('Without one, the superset rests as long as its longest exercise rest.'), onDone: setRest })} />
</div>
<div style={{ height: 18 }} />
<Button variant="primary" onClick={save}>{t('Save')}</Button>
</>
}
export const supersetSheet = (meta, onSave) => ui().openSheet(close => <SupersetSettings meta={meta} onSave={onSave} close={close} />)
/* Drop-set drops and rest-pause bursts are edited inline on the set row itself (Workout.jsx) —
no sheet, no timer. A planned exercise (see the "Intensifier" config below) arrives with them
already computed via applyIntensifierPlan; an unplanned straight set can still grow one live
by tapping "+ Drop"/"+ Burst", which appends with the same suggested-next-value math. */
// Shown when the last exercise's last set is checked — finish, or keep going.
function WorkoutComplete({ close }) {
return <div style={{ textAlign: 'center', padding: '8px 0' }}>
<div style={{ fontSize: 44, display: 'flex', justifyContent: 'center', color: 'var(--acc)' }}><Icon name="checkCircle" /></div>
<h3 style={{ margin: '8px 0' }}>{t("That's the whole workout!")}</h3>
<div className="muted small" style={{ marginBottom: 16 }}>{t('All exercises done. Nice work! Finish up, or add another one if you’ve got more in you.')}</div>
<Button variant="primary" icon="flag" onClick={() => { close(); finishWorkout() }}>{t('Finish workout')}</Button>
<div style={{ height: 8 }} />
<Button onClick={() => { close(); useUI.getState().toast(t('Keep going: tap “+ Add exercise” below')) }}>{t('Continue workout')}</Button>
</div>
}
export const workoutCompleteSheet = () => ui().openSheet(close => <WorkoutComplete close={close} />, { kind: 'center' })
export function saveWorkoutEdits(onExit = () => nav('/history')) {
// A save decision left open after the editor already closed (a second prompt from a route
// exit that raced the first save) has nothing to save or delete: it only finishes leaving.
if (!S().active?.editingWorkoutId) { onExit(); return }
// An edit that unticked or removed every set would save a workout with nothing in it, which
// the history would still list and count as a training day. Deleting it is what that edit
// means; Keep editing goes back to the sets.
if (editLeftEmpty(S().active)) {
confirmSheet({
title: t('Delete workout?'),
message: t('No sets are left in this workout, so there is nothing to save. Delete it from your history?') + mediaGoesToo(editedRecord(S())),
confirmText: t('Delete workout'), cancelText: t('Keep editing'), danger: true,
onConfirm: () => {
useStore.getState().deleteHistoryEdit()
useUI.getState().stopRest()
useUI.getState().stopWork()
useStore.getState().autoBackupNow()
toast(t('Workout deleted'))
onExit()
},
})
return
}
try {
useStore.getState().saveHistoryEdit()
useUI.getState().stopRest()
useUI.getState().stopWork()
useStore.getState().autoBackupNow()
toast(t('Workout updated'))
onExit()
} catch (error) { toast(t(error.message)) }
}
// The one save prompt that may be open: { id, onExit }. Closing the editor and a route exit
// can both ask in the same moment (the X, then the route change its tap set off); a second
// prompt stacked under the first came back after Save changes and asked all over again
// (QA 10-06). The second ask now joins the open prompt and only says where to go after it.
let savePrompt = null
const promptOpen = () => !!savePrompt && useUI.getState().sheets.some(x => x.id === savePrompt.id)
export function exitWorkoutEdit(onExit = () => nav('/history')) {
if (!S().active?.editingWorkoutId) { onExit(); return }
if (promptOpen()) { savePrompt.onExit = onExit; return }
const prompt = { id: null, onExit }
const done = () => prompt.onExit()
const leave = () => {
// A stale prompt's Don't save must not throw away whatever is in S.active by now.
if (!S().active?.editingWorkoutId) { done(); return }
useStore.getState().discardHistoryEdit()
useUI.getState().stopRest()
useUI.getState().stopWork()
done()
}
// Nothing to save: closing just closes, as it does for a workout only looked at.
if (editChangesNothing(S())) { leave(); return }
prompt.id = ui().openSheet(close => <>
<h3>{t('Save workout changes?')}</h3>
<p className="muted">{t('Save your edits to this workout, or keep the original record.')}</p>
<Button variant="primary" onClick={() => { close(); saveWorkoutEdits(done) }}>{t('Save changes')}</Button>
<div style={{ height: 8 }} />
<Button onClick={() => { close(); leave() }}>{t("Don't save")}</Button>
<div style={{ height: 8 }} />
<Button variant="ghost" className="dim" onClick={close}>{t('Keep editing')}</Button>
</>, { kind: 'center' })?.id
savePrompt = prompt
}
// "60 kg × 5, 5, 4", or "60×5 · 62.5×5" when the weight changed between sets; a bodyweight lift
// without load is just its reps. A set taken to failure keeps its "F", as it does in the history.
export function setsLine(sets, unit) {
const ws = new Set(sets.map(s => s.w))
const reps = s => s.r + (s.f ? ' ' + t('F') : '')
if (ws.size === 1) {
const w0 = sets[0].w
return (w0 > 0 ? fmtNum(w0) + ' ' + unit + ' × ' : '') + sets.map(reps).join(', ')
}
return sets.map(s => (s.w > 0 ? fmtNum(s.w) + '×' : '') + reps(s)).join(' · ')
}
const TREND = { 1: ['arrowUp', 'var(--green)'], 0: ['minus', 'var(--text-2)'], [-1]: ['arrowDown', 'var(--orange)'] }
function LastAndNext({ st, w }) {
const rows = finishCompare(st, w)
if (!rows.length) return null
const nextWord = { up: t('step up'), hold: t('same again'), down: t('lighter'), deload: t('deload') }
return <div style={{ textAlign: 'start', marginBottom: 14 }}>
<h4 className="sec">{t('Last time and next time')}</h4>
<div className="list">{rows.map(row => {
const ex = EXIDX[row.id]
const tr = row.trend == null ? null : TREND[row.trend]
return <div key={row.id} className="item" style={{ display: 'block' }}>
<div className="row between" style={{ gap: 8 }}>
<span className={'tt ' + exerciseNameClass(ex)} style={{ fontWeight: 600 }}>{ex ? exerciseNameFor(ex) : row.id}</span>
{tr ? <span style={{ color: tr[1], display: 'inline-flex' }} aria-label={row.trend > 0 ? t('Up on last time') : row.trend < 0 ? t('Down on last time') : t('Same as last time')}><Icon name={tr[0]} /></span>
: <span className="tag nocap">{t('First time')}</span>}
</div>
<div className="small">{t('Today')}: {setsLine(row.today, st.unit)}</div>
{row.last && <div className="small dim">{t('Last time')}: {setsLine(row.last, st.unit)}</div>}
{row.next && <div className="small accent">{t('Next time')}: {row.next.w != null ? fmtNum(row.next.w) + ' ' + st.unit : ''}{row.next.w != null && row.next.r != null ? ' × ' : ''}{row.next.r != null ? row.next.r : ''} · {nextWord[row.next.kind]}</div>}
</div>
})}</div>
</div>
}
// The cardio of the session, which "Last time and next time" leaves out: minutes, speed and the
// treadmill's incline as they were logged.
function CardioDone({ st, w }) {
const rows = finishCardio(w)
if (!rows.length) return null
return <div style={{ textAlign: 'start', marginBottom: 14 }}>
<h4 className="sec">{t('Cardio')}</h4>
<div className="list">{rows.map((row, i) => {
const ex = EXIDX[row.id]
return <div key={i} className="item" style={{ display: 'block' }}>
<span className={'tt ' + exerciseNameClass(ex)} style={{ fontWeight: 600 }}>{ex ? exerciseNameFor(ex) : row.id}</span>
<div className="small">{row.sets.map(x => setLabel(row.id, x, row.target, speedUnitOf(st)) + hrLabel(x)).join(' · ')}</div>
</div>
})}</div>
</div>
}
// Exercises added during a routine's session, offered to that routine for next time (#592). One
// tap adds them with today's setup; leaving the summary without it changes nothing.
function KeepInRoutine({ additions }) {
const [added, setAdded] = useState({})
if (!additions?.length) return null
const keep = a => {
let n = 0
update(s => { n = addToRoutine(s.routines.find(r => r.id === a.routineId), a.ex) })
setAdded(x => ({ ...x, [a.routineId]: true }))
if (n) ui().toast(t('Routine updated'))
}
return <div style={{ textAlign: 'start', marginBottom: 14 }} data-keep-in-routine>
<h4 className="sec">{t('New in this workout')}</h4>
{additions.map(a => <div key={a.routineId} style={{ marginBottom: 8 }}>
<div className="small dim" style={{ marginBottom: 6 }}>{a.ex.map(cfg => EXIDX[cfg.id] ? exerciseNameText(EXIDX[cfg.id]) : cfg.id).join(', ')}</div>
{added[a.routineId]
? <div className="small accent row" style={{ gap: 6 }}><Icon name="checkCircle" /><span>{t('Added to “{0}”', a.name)}</span></div>
: <Button icon="plusCircle" onClick={() => keep(a)}>{tn('Add it to “{1}”', 'Add them to “{1}”', a.ex.length, a.name)}</Button>}
</div>)}
</div>
}
function FinishSummary({ w, prs, e1prs = [], additions = [], close }) {
const st = useStore(s => s.S)
return <div style={{ textAlign: 'center', padding: '8px 0' }}>
{/* Sticky, so the summary can be left from the top however far it has been scrolled. */}
<div className="fin-x"><button type="button" className="iconbtn" aria-label={t('Close')} onClick={() => closeThenNav(close, '/home')}><Icon name="xmark" /></button></div>
<div style={{ fontSize: 44, display: 'flex', justifyContent: 'center', color: 'var(--acc)', position: 'relative' }}><Icon name="trophy" /><SeasonDeco spot="finish" workout /></div>
<h3 style={{ margin: '8px 0' }}>{t('Workout complete!')}</h3>
<div className="tiles fin" style={{ textAlign: 'start' }}>
<div className="tile"><div className="l">{t('Duration')}</div><div className="v" style={{ fontSize: '1.1rem' }}>{fmtDur(w.end - w.start)}</div></div>
<div className="tile"><div className="l">{t('Volume')}</div><div className="v" style={{ fontSize: '1.1rem' }}>{fmtVol(sessionVolume(w, st), st.unit)}</div></div>
<div className="tile wide"><div className="l">{t('Sets')}</div><div className="v" style={{ fontSize: '1.1rem' }}>{setsWorkCount(setsDone(w), workSetsDone(w))}</div></div>
<div className="tile narrow"><div className="l">{t('PRs')}</div><div className="v" style={{ fontSize: '1.1rem' }}>{prs.length || '–'}</div></div>
</div>
{(prs.length > 0 || e1prs.length > 0) && <div style={{ textAlign: 'start', marginBottom: 12 }}>
{prs.map(id => <div key={id} className="small accent row" style={{ gap: 6, alignItems: 'flex-start' }}><Icon name="trophy" style={{ fontSize: 13, marginTop: 2, flex: 'none' }} /><span>{t('New PR:')} <span className={exerciseNameClass(EXIDX[id])}>{EXIDX[id] ? exerciseNameFor(EXIDX[id]) : id}</span></span></div>)}
{e1prs.map(p => <div key={p.id} className="small accent row" style={{ gap: 6, alignItems: 'flex-start' }}><Icon name="chartLine" style={{ fontSize: 13, marginTop: 2, flex: 'none' }} /><span>{t('Best estimated 1RM:')} <span className={exerciseNameClass(EXIDX[p.id])}>{EXIDX[p.id] ? exerciseNameFor(EXIDX[p.id]) : p.id}</span> · {fmtNum(p.est)} {st.unit}</span></div>)}
</div>}
<h4 className="sec" style={{ textAlign: 'start' }}>{t('What you just trained')}</h4>
<BodyMap load={loadOfWorkouts([w])} body={st.body} />
<div style={{ height: 14 }} />
<LastAndNext st={st} w={w} />
<CardioDone st={st} w={w} />
<KeepInRoutine additions={additions} />
{/* The moment for a progress photo or the clip of a set: the workout is already saved, so
what is added here goes straight onto its record. */}
<div style={{ textAlign: 'start' }}><WorkoutMediaSection w={w} hint /></div>
<Button variant="primary" onClick={() => closeThenNav(close, '/home')}>{t('Nice!')}</Button>
</div>
}
export function finishWorkout() {
if (S().active?.editingWorkoutId) { saveWorkoutEdits(); return }
const A = S().active
if (!A) return
const done = setsDoneActive(A)
const total = setUnitsTotal(A.entries)
if (!done) { confirmSheet({ title: t('Nothing logged yet'), message: t('You haven’t checked off any sets. Finish the workout anyway?'), confirmText: t('Finish anyway'), onConfirm: doFinishWorkout }); return }
if (done < total) { confirmSheet({ title: t('Finish early?'), message: tn('{0} set still unchecked. Finish the workout now?', '{0} sets still unchecked. Finish the workout now?', total - done), confirmText: t('Finish workout'), onConfirm: doFinishWorkout }); return }
doFinishWorkout()
}
// The workout screen's Finish pill (v1.3.11): one sheet with both ways out, Finish and save or
// Discard. It says what finishWorkout's confirm would have said about sets still unchecked, so
// the save needs no second question. `onDiscard` is the screen's own discard, with its confirm.
// The editor of a saved workout still saves at once.
export function finishWorkoutSheet({ onDiscard } = {}) {
if (S().active?.editingWorkoutId) { saveWorkoutEdits(); return }
const A = S().active
if (!A) return
const done = setsDoneActive(A)
const total = setUnitsTotal(A.entries)
menuSheet({
title: !done ? t('Nothing logged yet') : done < total ? t('Finish early?') : t('Finish workout?'),
subtitle: !done ? t('You haven’t checked off any sets. Finish the workout anyway?')
: done < total ? tn('{0} set still unchecked. Finish the workout now?', '{0} sets still unchecked. Finish the workout now?', total - done) : null,
sections: [
{ items: [{ icon: 'flag', accent: true, label: t('Finish and save'), onClick: doFinishWorkout }] },
{ items: [onDiscard && { icon: 'trash', danger: true, label: t('Discard workout'), onClick: onDiscard }] },
],
})
}
// Finish and save without a question: the Apple Watch's Finish (lib/watch-sync.js) runs exactly
// what the phone's Finish and save runs.
export const finishWorkoutNow = () => doFinishWorkout()
function doFinishWorkout() {
const st = S()
const A = st.active
if (!A) return
const past = !!A.backfill
const prs = []
const e1prs = []
// A workout logged into the past is held against the history before it, the way an edit of a
// saved one is (rebuildPrHistory, below): it gains a badge it leads with, and a later session
// it outdoes loses its own. It used to report none at all while the editor's Save awarded them
// (QA 1.3.9). The confirmed weights are left alone either way.
if (!past) A.entries.forEach(e => {
const loads = e.sets.filter(s => s.done && !isWarmupRow(s)).map(s => s.w).filter(w => w > 0)
const mx = loads.length ? loads.reduce((a, b) => betterWeight(e.id, a, b)) : 0
// Held against history read the way this session logged a dumbbell weight (lib/dumbbells.js).
if (beatsWeight(e.id, mx, bestWeightFor(st, e.id, entryDbLoad(e)))) prs.push(e.id)
// A heavier estimate without a heavier top set is its own kind of progress —
// same weight for more reps. Reported separately so it can't be read as a load PR.
const rec = is1RMRecord(st, e.id, e)
if (rec && !prs.includes(e.id)) e1prs.push({ id: e.id, ...rec })
})
const w = buildCompletedWorkout(A, {
end: past ? backfillEnd(A) : sessionEnd(A),
prs,
snapshotFor: e => isCustomEx(EXIDX[e.id]) ? exerciseMuscleSnapshot(EXIDX[e.id]) : null,
})
w.vol = workoutVolume(w)
// Read before the session is gone: the summary offers these to their routines (#592).
const additions = past ? [] : routineAdditions(A, st.routines)
let shown = w
update(s => {
if (past) {
// Its start and end are in the past; the stamp says when it was logged, which is what a
// merge after a reset elsewhere asks of it (lib/sync-merge.js sinceReset).
stampWorkout(w)
const replaced = A.backfill.replaceId ? s.workouts.find(x => x.id === A.backfill.replaceId) : null
const touched = [...new Set([...(replaced?.entries || []), ...w.entries].map(e => e?.id).filter(id => id != null))]
s.workouts = rebuildPrHistory(completeBackfill(s.workouts, A, w), touched, w)
shown = s.workouts.find(x => x === w || (w.id != null && x.id === w.id)) || w
prs.push(...[...(shown.prs || [])])
} else {
w.entries.forEach(e => {
const mx = bestWeightForEntry(e)
if (mx > 0 && beatsWeight(e.id, mx, (s.exWeights[e.id] || {}).w || 0)) s.exWeights[e.id] = { w: mx, d: w.d }
})
s.workouts.push(w)
}
s.active = null
// A rotation pass this app manages starts its next pass the moment the last session lands
// (lib/rotation.js). A planner's queue is left alone: it completes and waits for its writer.
refillAfter(s, w)
})
useStore.getState().autoBackupNow()
useUI.getState().stopRest()
// A hold still counting (a plank left unchecked, then Finish early) goes with the workout, as
// on Discard: its bar is app-wide and would chime over the summary, and its callback writes
// into s.active, which is gone. What it held is not logged, like any set left unchecked.
useUI.getState().stopWork()
beep(snd(), 880, 0.15); beep(snd(), 1100, 0.15, 0.18); beep(snd(), 1320, 0.3, 0.36)
ui().openSheet(close => <FinishSummary w={shown} prs={prs} e1prs={e1prs} additions={additions} close={close} />, { kind: 'center', locked: true, wide: true })
}