From 43d54950071449f44870ba5745d619f0bdfd55ae Mon Sep 17 00:00:00 2001 From: Duarte Santos Date: Fri, 24 Jul 2026 23:45:17 +0200 Subject: [PATCH] Import from other apps: convert weights per row, not per file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The unit was decided once for the whole file: if any row said lb and none said kg it converted, otherwise it did nothing. FitNotes writes the unit next to every set, so a history recorded partly in pounds and partly in kilos hit the 'otherwise' branch — the file was taken over unconverted and "185 lb" was logged as 185 kg, while the summary claimed the file does not say which unit it uses. Each row now carries its own unit through to the conversion (falling back to the file's, then to the profile's), and a mixed file says so in the preview. Also: the "exercises matched" tile counted matched rows, so three exercises across five sets read as five. It counts distinct exercises now, which is what the label says. --- frontend/src/lib/import-csv.js | 36 ++++++++++++++++++++++++---------- frontend/src/sheets.jsx | 8 +++++--- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/frontend/src/lib/import-csv.js b/frontend/src/lib/import-csv.js index 0b80865..4ee04ec 100644 --- a/frontend/src/lib/import-csv.js +++ b/frontend/src/lib/import-csv.js @@ -338,9 +338,11 @@ export function parseWorkoutCSV(text, { unit = 'kg' } = {}) { } const isCardio = (km > 0 || mins > 0) && !reps + // `u` carries the row's own unit into the conversion pass below and is dropped there — + // it never reaches the stored set. const set = isCardio ? { min: mins || 0, speed: mins > 0 ? Math.round(km / (mins / 60) * 10) / 10 : 0, done: true } - : { w, r: reps || 0, done: true } + : { w, r: reps || 0, done: true, u: rowUnit } let day = byDate.get(when.d) if (!day) { @@ -355,19 +357,29 @@ export function parseWorkoutCSV(text, { unit = 'kg' } = {}) { sets++ } - // lb -> kg only when the file disagrees with the profile. The app never converts units - // on its own, so importing unconverted would silently rewrite someone's numbers. + // lb -> kg only where a row disagrees with the profile. The app never converts units on + // its own, so importing unconverted would silently rewrite someone's numbers. + // Converting PER ROW matters: apps like FitNotes write the unit next to every set, and a + // history recorded partly in lb and partly in kg used to be taken over as-is, turning + // "185 lb" into 185 kg. const fileUnit = sawLb && !sawKg ? 'lb' : sawKg && !sawLb ? 'kg' : '' - const converted = !!fileUnit && fileUnit !== unit - const conv = converted - ? (fileUnit === 'lb' ? x => Math.round(x * LB_TO_KG * 10) / 10 : x => Math.round(x / LB_TO_KG * 10) / 10) - : x => x + const mixedUnits = sawLb && sawKg + const toKg = x => Math.round(x * LB_TO_KG * 10) / 10 + const toLb = x => Math.round(x / LB_TO_KG * 10) / 10 + // A row without its own unit follows the file's, and a file that says nothing is taken + // to already be in the profile's unit. + const convRow = s => { + const u = s.u || fileUnit + if (!u || u === unit) return s.w + return u === 'lb' ? toKg(s.w) : toLb(s.w) + } + const converted = (!!fileUnit && fileUnit !== unit) || mixedUnits const dates = [...byDate.keys()].sort() const workouts = dates.map(d => { const day = byDate.get(d) const entries = [...day.ex.entries()].map(([id, ss]) => { - const conv2 = ss.map(s => (s.w !== undefined ? { ...s, w: conv(s.w) } : s)) + const conv2 = ss.map(({ u, ...s }) => (s.w !== undefined ? { ...s, w: convRow({ ...s, u }) } : s)) const mx = Math.max(0, ...conv2.map(s => s.w || 0)) return { id, sets: conv2, topW: mx || null } }) @@ -384,8 +396,12 @@ export function parseWorkoutCSV(text, { unit = 'kg' } = {}) { return { kind: 'workouts', source, workouts, customEx: [...created.values()], - matched, created: created.size, unmatchedNames: [...unmatched].sort(), - sets, skipped, warmups, fileUnit, converted, + // distinct library exercises behind the matched rows — the summary calls this + // "exercises matched", and counting rows there made three exercises read as five + matched: new Set([...resolved.values()].filter(Boolean)).size, + matchedSets: matched, + created: created.size, unmatchedNames: [...unmatched].sort(), + sets, skipped, warmups, fileUnit, mixedUnits, converted, from: dates[0] || null, to: dates[dates.length - 1] || null, } } diff --git a/frontend/src/sheets.jsx b/frontend/src/sheets.jsx index fd520d6..ae86efa 100644 --- a/frontend/src/sheets.jsx +++ b/frontend/src/sheets.jsx @@ -168,10 +168,12 @@ function ImportSummary({ parsed, close }) { } - {parsed.converted &&
+ {parsed.mixedUnits ?
+ {t('The file mixes kg and lb — each set is converted to {0}.', st.unit)} +
: parsed.converted ?
{t('The file is in {0} and your profile is in {1} — weights will be converted.', parsed.fileUnit, st.unit)} -
} - {!isBW && !parsed.fileUnit &&
+
: null} + {!isBW && !parsed.fileUnit && !parsed.mixedUnits &&
{t('The file does not say which unit it uses — numbers are imported as they are.')}
} {have > 0 &&