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.

curated2: backfill helpers — a past workout is filed by date, the session entries are built by one shared function

Duarte Santos346e1110c74e
diff --git a/frontend/src/lib/backfill.js b/frontend/src/lib/backfill.js
new file mode 100644
index 000000000..be8a6cb1a
--- /dev/null
+++ b/frontend/src/lib/backfill.js
@@ -0,0 +1,39 @@
+// Logging a workout after the fact. The session itself is the ordinary active workout with a
+// `backfill` field on it; these helpers are the parts that differ from a live session, kept
+// pure so the date arithmetic and the history surgery can be tested without the UI.
+
+export const workoutsOn = (S, iso) => (S.workouts || []).filter(w => w.d === iso)
+
+// Epoch of `iso` at the given wall-clock time, in the browser's zone — the same zone
+// todayISO() and isoOf() use, so `d` and `start` agree the way they do for a live session.
+export const backfillStart = (iso, time = '18:00') => {
+ const [h, m] = String(time || '18:00').split(':').map(Number)
+ const d = new Date(iso + 'T12:00:00')
+ d.setHours(h || 0, m || 0, 0, 0)
+ return d.getTime()
+}
+
+// A live session ends when you tap finish; a logged one ends when you said it did.
+export const backfillEnd = active => active.start + Math.max(1, active.backfill?.durationMin || 60) * 60000
+
+// The workouts array is chronological (History reverses it), so a past workout cannot just be
+// pushed — it goes where its date and start time put it, after anything from the same moment.
+export function insertChronological(workouts, w) {
+ const key = x => [x.d || '', x.start || 0]
+ const [d, s] = key(w)
+ let i = workouts.length
+ while (i > 0) {
+ const [pd, ps] = key(workouts[i - 1])
+ if (pd < d || (pd === d && ps <= s)) break
+ i--
+ }
+ return [...workouts.slice(0, i), w, ...workouts.slice(i)]
+}
+
+// The history after a backfilled session is filed: the workout it replaces (if any) is gone
+// and the new one sits in date order. Returns a new array; the caller stores it.
+export function completeBackfill(workouts, active, w) {
+ const replaceId = active.backfill?.replaceId
+ const kept = replaceId ? workouts.filter(x => x.id !== replaceId) : workouts
+ return insertChronological(kept, w)
+}
diff --git a/frontend/src/lib/backfill.test.js b/frontend/src/lib/backfill.test.js
new file mode 100644
index 000000000..4d54698e1
--- /dev/null
+++ b/frontend/src/lib/backfill.test.js
@@ -0,0 +1,59 @@
+import { describe, expect, it } from 'vitest'
+import { workoutsOn, backfillStart, backfillEnd, insertChronological, completeBackfill } from './backfill.js'
+
+const w = (id, d, start = 0) => ({ id, d, start })
+
+describe('workoutsOn', () => {
+ it('returns every workout of that day and nothing else', () => {
+ const S = { workouts: [w('a', '2026-01-01'), w('b', '2026-01-02'), w('c', '2026-01-02')] }
+ expect(workoutsOn(S, '2026-01-02').map(x => x.id)).toEqual(['b', 'c'])
+ expect(workoutsOn(S, '2026-01-03')).toEqual([])
+ expect(workoutsOn({}, '2026-01-03')).toEqual([])
+ })
+})
+
+describe('backfillStart / backfillEnd', () => {
+ it('lands on the chosen day at the chosen time in local zone', () => {
+ const t = new Date(backfillStart('2026-03-10', '07:45'))
+ expect([t.getFullYear(), t.getMonth() + 1, t.getDate(), t.getHours(), t.getMinutes()]).toEqual([2026, 3, 10, 7, 45])
+ })
+ it('defaults to 18:00 and ends after the given duration', () => {
+ const start = backfillStart('2026-03-10')
+ expect(new Date(start).getHours()).toBe(18)
+ expect(backfillEnd({ start, backfill: { durationMin: 45 } })).toBe(start + 45 * 60000)
+ expect(backfillEnd({ start, backfill: {} })).toBe(start + 60 * 60000)
+ expect(backfillEnd({ start, backfill: { durationMin: 0 } })).toBe(start + 60 * 60000)
+ })
+})
+
+describe('insertChronological', () => {
+ const list = [w('a', '2026-01-01', 10), w('b', '2026-01-05', 10), w('c', '2026-01-05', 20), w('d', '2026-01-09', 10)]
+ it('places by date, then by start time, after equal keys', () => {
+ expect(insertChronological(list, w('x', '2026-01-03')).map(x => x.id)).toEqual(['a', 'x', 'b', 'c', 'd'])
+ expect(insertChronological(list, w('x', '2026-01-05', 15)).map(x => x.id)).toEqual(['a', 'b', 'x', 'c', 'd'])
+ expect(insertChronological(list, w('x', '2026-01-05', 20)).map(x => x.id)).toEqual(['a', 'b', 'c', 'x', 'd'])
+ })
+ it('appends at the end and inserts at the front', () => {
+ expect(insertChronological(list, w('x', '2026-02-01')).at(-1).id).toBe('x')
+ expect(insertChronological(list, w('x', '2025-12-31'))[0].id).toBe('x')
+ expect(insertChronological([], w('x', '2026-01-01')).map(x => x.id)).toEqual(['x'])
+ })
+ it('does not mutate the input', () => {
+ const copy = [...list]
+ insertChronological(list, w('x', '2026-01-03'))
+ expect(list).toEqual(copy)
+ })
+})
+
+describe('completeBackfill', () => {
+ const list = [w('a', '2026-01-01', 10), w('b', '2026-01-05', 10), w('d', '2026-01-09', 10)]
+ it('adds a second workout on a day in order', () => {
+ const out = completeBackfill(list, { backfill: { durationMin: 60, replaceId: null } }, w('x', '2026-01-05', 5))
+ expect(out.map(x => x.id)).toEqual(['a', 'x', 'b', 'd'])
+ })
+ it('replaces the chosen workout', () => {
+ const out = completeBackfill(list, { backfill: { durationMin: 60, replaceId: 'b' } }, w('x', '2026-01-05', 30))
+ expect(out.map(x => x.id)).toEqual(['a', 'x', 'd'])
+ expect(list).toHaveLength(3)
+ })
+})
diff --git a/frontend/src/lib/session-start.js b/frontend/src/lib/session-start.js
new file mode 100644
index 000000000..5ca9f50c2
--- /dev/null
+++ b/frontend/src/lib/session-start.js
@@ -0,0 +1,21 @@
+// How a session's exercise entries are built from a routine. Shared by the live start and by
+// "log a past workout", which is the same screen pointed at another day — both must walk up
+// to identical entries, or the two paths drift apart the first time a prescription rule changes.
+// Imports both history.js and progression.js (which itself imports history.js); nothing in
+// either imports this file, so there is no cycle.
+import { buildSets, applyIntensifierPlan } from './history.js'
+import { nextPrescription, applyPrescription, defaultIncrement } from './progression.js'
+
+export function buildSessionEntries(st, r) {
+ // The prescription is applied as the session is built, so you walk up to the bar with the
+ // right weight already on the screen instead of being told about it afterwards. `plan` is
+ // kept on the entry purely so the workout can explain the number it chose.
+ const excluded = r?.excludeFromProgression === true
+ const entries = (r ? r.ex : []).map(cfg => {
+ const plan = excluded ? { policy: 'off', kind: 'off' } : nextPrescription(st, cfg, r)
+ const step = defaultIncrement(cfg.id, st.unit)
+ const sets = applyIntensifierPlan(applyPrescription(buildSets(st, cfg, { step, useTarget: excluded }), plan, step), cfg)
+ return { id: cfg.id, sg: cfg.sg, target: { ...cfg }, plan, sets }
+ })
+ return { entries, excluded }
+}