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.

Follow-up to #194: converting the numbers to the other unit keeps each unit's plate list, so the rows load from the new unit's plates and switching back finds the old list as it was

Duarte Santoscfee3751d129
diff --git a/frontend/src/lib/units.js b/frontend/src/lib/units.js
index 22c250543..62780d298 100644
--- a/frontend/src/lib/units.js
+++ b/frontend/src/lib/units.js
@@ -95,6 +95,10 @@ export function convertStateUnit(S, to) {
if (S.targetW != null) out.targetW = bw(S.targetW)
if (S.exWeights) out.exWeights = Object.fromEntries(Object.entries(S.exWeights).map(([k, v]) => [k, v && typeof v === 'object' ? { ...v, w: c(v.w) } : c(v)]))
if (S.barWeights) out.barWeights = convBarWeights(S.barWeights, from, to)
+ // The plate inventory (S.plates) is carried over as it is, not converted: it is kept per unit
+ // (lib/plates.js), because a 45 lb plate does not become a 20.4 kg one. After the switch the
+ // rows load from the new unit's own list, or the standard set until you count yours, and
+ // switching back finds the old list as you left it. The load kinds (S.loadKind) hold no weight.
if (Array.isArray(S.routines)) out.routines = S.routines.map(r => ({ ...r, ex: (r.ex || []).map(cfg => convTarget(cfg, from, to)) }))
if (Array.isArray(S.workouts)) out.workouts = S.workouts.map(convSession)
if (S.active) out.active = convSession(S.active)
diff --git a/frontend/src/lib/units.test.js b/frontend/src/lib/units.test.js
index 6fb9c808c..a5381596d 100644
--- a/frontend/src/lib/units.test.js
+++ b/frontend/src/lib/units.test.js
@@ -1,6 +1,7 @@
import { describe, it, expect } from 'vitest'
import { convertWeight, convertStateUnit } from './units.js'
import { workoutVolume } from './history.js'
+import { inventoryFor, withPlatePairs } from './plates.js'
describe('convertWeight', () => {
it('rounds lb to a half and kg to a quarter', () => {
@@ -113,3 +114,29 @@ describe('convertStateUnit: bar weights', () => {
})
})
+describe('convertStateUnit: plate inventories', () => {
+ // Plates are kept per unit (lib/plates.js): the switch hands the rows the new unit's list and
+ // leaves the old one for the way back, rather than turning 45 lb plates into 20.5 kg ones.
+ const home = { 45: 1, 35: 1, 25: 1, 10: 2, _ts: 50 }
+ const S = { unit: 'lb', plates: { lb: home }, loadKind: { '0025': { kind: 'none', _ts: 40 } }, barWeights: { 1383: 100 } }
+
+ it('each unit keeps its own list; the kg side starts from the standard kg set', () => {
+ const kg = convertStateUnit(S, 'kg')
+ expect(kg.plates).toEqual({ lb: home })
+ expect(kg.loadKind).toEqual(S.loadKind)
+ expect(inventoryFor(kg)).toEqual(inventoryFor({ unit: 'kg' }))
+ expect(kg.barWeights).toEqual({ 1383: 45.25 }) // a sled's own weight is a weight and converts
+ const back = convertStateUnit(kg, 'lb')
+ expect(back.plates).toEqual({ lb: home })
+ expect(inventoryFor(back)).toEqual([{ w: 45, n: 1 }, { w: 35, n: 1 }, { w: 25, n: 1 }, { w: 10, n: 2 }])
+ })
+
+ it('a kg list counted after the switch is the one the kg rows use, and the lb list survives it', () => {
+ const kg = convertStateUnit(S, 'kg')
+ kg.plates = withPlatePairs(kg, 20, 1, 60)
+ expect(inventoryFor(kg).find(p => p.w === 20).n).toBe(1)
+ const back = convertStateUnit(kg, 'lb')
+ expect(back.plates.lb).toEqual(home)
+ expect(back.plates.kg).toMatchObject({ 20: 1, _ts: 60 })
+ })
+})