Skip to content

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.

feat(finish): last time and next time for each lift on the summary (#324)

Duarte Santosfbd9b8091adb
diff --git a/frontend/src/lib/finish-compare.js b/frontend/src/lib/finish-compare.js
new file mode 100644
index 000000000..e71c835c2
--- /dev/null
+++ b/frontend/src/lib/finish-compare.js
@@ -0,0 +1,62 @@
+// The finish summary's "last time / next time" (#324): one line per lift, today against the
+// previous session of the same exercise, and what the next session will open at. "Next" has no
+// rule of its own: it is buildPlannedEntry, the same call Start makes, run on the state right
+// after this workout was saved, so the summary cannot promise a number the next session won't.
+import { isWarmupRow } from './workout-model.js'
+import { buildPlannedEntry } from './session-start.js'
+import { workoutAt, modeOf } from './history.js'
+
+const workSets = entry => (Array.isArray(entry?.sets) ? entry.sets : [])
+ .filter(s => s && s.done && !isWarmupRow(s) && Number(s.r) > 0)
+ .map(s => ({ w: Number(s.w) || 0, r: Math.round(Number(s.r)) }))
+
+const top = sets => sets.reduce((m, s) => Math.max(m, s.w), 0)
+const reps = sets => sets.reduce((n, s) => n + s.r, 0)
+
+/** -1, 0 or 1: today against last time, by the heaviest set first, then by total reps. */
+export function trendOf(today, last) {
+ if (!last?.length) return null
+ const dw = top(today) - top(last)
+ if (Math.abs(dw) > 1e-9) return dw > 0 ? 1 : -1
+ const dr = reps(today) - reps(last)
+ return dr ? Math.sign(dr) : 0
+}
+
+/** Rows for the summary of `w`, read from `st` (which already holds `w`). Cardio and timed sets are left out. */
+export function finishCompare(st, w) {
+ const workouts = (st?.workouts || []).filter(x => x && x !== w && x.id !== w?.id && workoutAt(x) < workoutAt(w))
+ const rows = []
+ const seen = new Set()
+ for (const entry of w?.entries || []) {
+ if (!entry?.id || seen.has(entry.id)) continue
+ const mode = modeOf(entry.target || entry)
+ if (mode !== 'reps') continue
+ const today = workSets(entry)
+ if (!today.length) continue
+ seen.add(entry.id)
+ let last = null
+ for (let i = workouts.length - 1; i >= 0 && !last; i--) {
+ const prev = (workouts[i].entries || []).find(e => e?.id === entry.id)
+ const sets = prev ? workSets(prev) : []
+ if (sets.length) last = sets
+ }
+ let next = null
+ const routine = entry.rid ? (st.routines || []).find(r => r.id === entry.rid) : null
+ const cfg = routine && !routine.excludeFromProgression && !entry.noProg && !w.noProg
+ ? (routine.ex || []).find(c => c?.id === entry.id) : null
+ if (cfg) {
+ try {
+ const built = buildPlannedEntry(st, cfg, routine)
+ const nw = built.target?.weight, nr = built.target?.reps
+ if (nw != null || nr != null) {
+ const kind = built.plan?.kind === 'deload' ? 'deload'
+ : nw != null && nw > top(today) + 1e-9 ? 'up'
+ : nw != null && nw < top(today) - 1e-9 ? 'down' : 'hold'
+ next = { w: nw ?? null, r: nr ?? null, sets: built.target?.sets ?? null, kind }
+ }
+ } catch { /* a routine the builder cannot read keeps its row without a "next" */ }
+ }
+ rows.push({ id: entry.id, today, last, trend: trendOf(today, last), next })
+ }
+ return rows
+}
diff --git a/frontend/src/lib/finish-compare.test.js b/frontend/src/lib/finish-compare.test.js
new file mode 100644
index 000000000..bf82b1f97
--- /dev/null
+++ b/frontend/src/lib/finish-compare.test.js
@@ -0,0 +1,33 @@
+import { describe, it, expect } from 'vitest'
+import { finishCompare, trendOf } from './finish-compare.js'
+
+const set = (w, r, extra = {}) => ({ w, r, done: true, ...extra })
+const routine = { id: 'r1', name: 'A', prog: 'linear', ex: [{ id: 'bench', sets: 3, reps: 5, weight: 60, mode: 'reps' }] }
+const workout = (id, d, sets, more = {}) => ({ id, d, start: Date.parse(d + 'T10:00:00'), end: Date.parse(d + 'T11:00:00'),
+ entries: [{ id: 'bench', rid: 'r1', target: { ...routine.ex[0] }, sets }], ...more })
+
+describe('last time and next time on the finish summary (#324)', () => {
+ it('compares by the heaviest set, then by reps', () => {
+ expect(trendOf([set(62.5, 5)], [set(60, 6)])).toBe(1)
+ expect(trendOf([set(60, 5), set(60, 5)], [set(60, 5), set(60, 6)])).toBe(-1)
+ expect(trendOf([set(60, 5)], [set(60, 5)])).toBe(0)
+ expect(trendOf([set(60, 5)], null)).toBeNull()
+ })
+ it('finds the previous session of the exercise and leaves warm-ups out', () => {
+ const prev = workout('w1', '2026-10-01', [set(20, 5, { phase: 'warmup' }), set(60, 5), set(60, 5), set(60, 4)])
+ const now = workout('w2', '2026-10-03', [set(60, 5), set(60, 5), set(60, 5)])
+ const st = { unit: 'kg', routines: [routine], workouts: [prev, now], exWeights: {} }
+ const [row] = finishCompare(st, now)
+ expect(row.last).toEqual([{ w: 60, r: 5 }, { w: 60, r: 5 }, { w: 60, r: 4 }])
+ expect(row.trend).toBe(1)
+ expect(row.next).toBeTruthy()
+ expect(row.next.w).toBeGreaterThanOrEqual(60)
+ })
+ it('a first time has no last, and a workout out of progression has no next', () => {
+ const now = workout('w1', '2026-10-03', [set(60, 5)], { noProg: true })
+ const [row] = finishCompare({ unit: 'kg', routines: [routine], workouts: [now] }, now)
+ expect(row.last).toBeNull()
+ expect(row.trend).toBeNull()
+ expect(row.next).toBeNull()
+ })
+})
diff --git a/frontend/src/sheets.jsx b/frontend/src/sheets.jsx
index c1e8c825b..eaef4de9b 100644
--- a/frontend/src/sheets.jsx
+++ b/frontend/src/sheets.jsx
@@ -49,6 +49,7 @@ import { useSheetKeyboard, useRevealActiveChip, tappable } from './lib/use-sheet
import { useAutoMore } from './lib/use-auto-more.js'
import { isFav, toggleFav, sortFavouritesFirst } from './lib/favourites.js'
import { buildSessionEntries, buildPlannedEntry, builtOutOfProgression } from './lib/session-start.js'
+import { finishCompare } from './lib/finish-compare.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'
@@ -2926,6 +2927,40 @@ export function exitWorkoutEdit(onExit = () => nav('/history')) {
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.
+function setsLine(sets, unit) {
+ const ws = new Set(sets.map(s => s.w))
+ if (ws.size === 1) {
+ const w0 = sets[0].w
+ return (w0 > 0 ? fmtNum(w0) + ' ' + unit + ' × ' : '') + sets.map(s => s.r).join(', ')
+ }
+ return sets.map(s => (s.w > 0 ? fmtNum(s.w) + '×' : '') + s.r).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>
+}
+
function FinishSummary({ w, prs, e1prs = [], close }) {
const st = useStore(s => s.S)
return <div style={{ textAlign: 'center', padding: '8px 0' }}>
@@ -2944,6 +2979,7 @@ function FinishSummary({ w, prs, e1prs = [], close }) {
<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} />
{/* 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>