diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index 28ff0eb..60e0f29 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -3,7 +3,7 @@ import { HashRouter, Routes, Route, Navigate, useNavigate, useLocation, useNavig import { useStore } from './store/useStore.js' import { useUI } from './store/useUI.js' import { bindUI } from './components/ui.jsx' -import { ACCENTS } from './lib/format.js' +import { ACCENTS, setWeightDecimals } from './lib/format.js' import { setLang, useLang } from './lib/i18n.js' import { setPlayOnSilent } from './lib/sound.js' import { setNav } from './lib/nav.js' @@ -81,6 +81,8 @@ function Shell() { return () => mql.removeEventListener('change', onChange) }, [S.theme, S.accent]) useEffect(() => { setLang(S.lang || 'en') }, [S.lang]) + // Same shape as the language: a module-level display setting, pushed when it changes (#139). + useEffect(() => { setWeightDecimals(S.wdec) }, [S.wdec]) useEffect(() => { document.documentElement.lang = S.lang || 'en' }, [langV, S.lang]) // Forward navigation starts at the top; going back lands where you left off. // The position is recorded from scroll events rather than read at route diff --git a/frontend/src/lib/format.js b/frontend/src/lib/format.js index 3817119..fe8b040 100644 --- a/frontend/src/lib/format.js +++ b/frontend/src/lib/format.js @@ -29,7 +29,22 @@ export const durPart = ms => (ms >= 60000 ? [fmtDur(ms)] : []) // Exercise names are stored lower-case and shown through CSS `capitalize`; text that has no // element of its own (a toast) capitalises here instead. export const capWords = s => String(s || '').replace(/(^|[\s(\-\/])(\p{Ll})/gu, (m, pre, ch) => pre + ch.toUpperCase()) -export const fmtNum = n => (Math.round(n * 10) / 10).toLocaleString(dateLocale()) +/* How many decimals a weight is shown with. One is enough for plate-loadable numbers, but a + * per-side figure from kg plates lands on .25 and .75 and reading those as .3 and .8 is the + * complaint in issue #139 — as is microplate work. The setting is read here rather than passed + * through sixty-odd call sites, the same way the date locale is; App.jsx pushes it whenever + * `S.wdec` changes. + * + * It only ever adds precision that is really there: a whole number still prints whole, and + * 62.5 is 62.5 either way. Nothing rounds differently in storage — this is display only. + */ +let decimals = 1 +export const setWeightDecimals = n => { decimals = n === 2 ? 2 : 1 } +export const weightDecimals = () => decimals +export const fmtNum = n => { + const p = decimals === 2 ? 100 : 10 + return (Math.round(n * p) / p).toLocaleString(dateLocale(), { maximumFractionDigits: decimals }) +} // Volume stays in the profile's unit throughout: the old shorthand turned anything over // 10 000 into "t", which is wrong for a pound profile and made one list mix "18.8t" with // "7'535 kg" — two numbers you can't compare at a glance. diff --git a/frontend/src/lib/weight-decimals.test.js b/frontend/src/lib/weight-decimals.test.js new file mode 100644 index 0000000..a675619 --- /dev/null +++ b/frontend/src/lib/weight-decimals.test.js @@ -0,0 +1,34 @@ +// Issue #139: a per-side figure off kg plates lands on .25 and .75, and one decimal reported +// those as .3 and .8. The setting adds precision that is already in the number — it changes +// nothing about what is stored. +import { describe, it, expect, afterEach } from 'vitest' +import { fmtNum, setWeightDecimals, weightDecimals } from './format.js' + +afterEach(() => setWeightDecimals(1)) + +describe('weight decimals', () => { + it('rounds to one decimal by default, exactly as before', () => { + expect(weightDecimals()).toBe(1) + expect(fmtNum(62.75)).toBe('62.8') + expect(fmtNum(20.25)).toBe('20.3') + }) + + it('keeps the quarter when asked for two', () => { + setWeightDecimals(2) + expect(fmtNum(62.75)).toBe('62.75') + expect(fmtNum(20.25)).toBe('20.25') + }) + + it('never invents precision that is not there', () => { + setWeightDecimals(2) + expect(fmtNum(100)).toBe('100') + expect(fmtNum(62.5)).toBe('62.5') + expect(fmtNum(0)).toBe('0') + }) + + it('ignores anything that is not 1 or 2', () => { + setWeightDecimals(5); expect(weightDecimals()).toBe(1) + setWeightDecimals(undefined); expect(weightDecimals()).toBe(1) + setWeightDecimals(2); expect(weightDecimals()).toBe(2) + }) +}) diff --git a/frontend/src/store/useStore.js b/frontend/src/store/useStore.js index 79451df..00fc90d 100644 --- a/frontend/src/store/useStore.js +++ b/frontend/src/store/useStore.js @@ -55,6 +55,9 @@ export const DEF = { // every profile written before this setting existed keeps the week it has been looking at. // See lib/format.js: nothing reads this field directly, everything goes through the helpers. weekStart: 1, + // Decimals on displayed weights: 1 by default, 2 for anyone loading quarter plates or + // microplates (issue #139). Display only — nothing is stored or rounded differently. + wdec: 1, // Per-exercise bar weight overrides, keyed by exercise id, in the profile unit (see // lib/bar.js). Personal equipment, so it syncs with the account but never travels in a // shared plan. Logged weights stay the total — this only feeds the plate math. diff --git a/frontend/src/views/Settings.jsx b/frontend/src/views/Settings.jsx index 1682ba1..993c037 100644 --- a/frontend/src/views/Settings.jsx +++ b/frontend/src/views/Settings.jsx @@ -245,6 +245,14 @@ export default function Settings() { options={[{ value: 'kg', label: 'kg' }, { value: 'lb', label: 'lb' }]} value={S.unit} onChange={v => switchUnit(v)} /> + {/* Display only: one decimal reads fine for plate-loadable numbers, two for anyone whose + per-side figure lands on .25 or .75, or who loads microplates (issue #139). Nothing is + stored or rounded differently — lib/format.js fmtNum just prints what is already there. */} + + update(s => { s.wdec = v })} /> + {/* Monday or Sunday — the Plan list, the Home strip, the calendar grid and every "this week" total follow it. Stored as a getDay() index (see lib/format.js). */}