From 3d5b00cecd3a75dbce33c1da40e74af560607164 Mon Sep 17 00:00:00 2001 From: Duarte Santos Date: Fri, 24 Jul 2026 23:45:04 +0200 Subject: [PATCH] A shared plan file can no longer take the app down MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Importing a plan whose exercise ids this build doesn't know — a hand-edited file, an export from a build with a different dataset — put those ids straight into a routine. The routine editor skipped them silently (you could neither see nor delete them), and starting the routine reached for ex.n on undefined: white screen, no tab bar, no way back. The running workout is persisted, so it came back on every reload. Three layers, so this class of bug can't reach the screen again: · parsePlan() drops ids that resolve to neither the library nor a custom exercise carried in the same file, and the import sheet says how many were left out. · exOr() renders an unresolvable id as a placeholder, so the routine editor shows it (and lets you remove it) instead of hiding it, and the workout view survives one. · an ErrorBoundary around the routes: it keeps the tab bar alive, offers a reload, and can discard a running workout that can't be drawn. It is keyed on the route, so switching tabs clears it. Turning the schedule switch on now really replaces Mon–Sun, as the switch has always claimed. It only wrote the days the shared plan filled, so days it left empty kept your own routine and you silently ended up training a mix of two plans. Also here, because they sit in the same lines: the tags that carry a unit or a whole sentence opt out of the capitalise rule ("Best: 85 Kg", "Last Time You Hit All Reps"), and the workout header's "best" now includes the working weight you confirmed, so it agrees with the sheet that asked for it. --- frontend/src/App.jsx | 33 ++++++++++-------- frontend/src/components/ErrorBoundary.jsx | 41 +++++++++++++++++++++++ frontend/src/index.css | 8 +++-- frontend/src/lib/exercises.js | 8 +++++ frontend/src/lib/plan-share.js | 33 ++++++++++++++---- frontend/src/sheets.jsx | 12 +++++-- frontend/src/views/RoutineEdit.jsx | 6 ++-- frontend/src/views/Workout.jsx | 21 +++++++----- 8 files changed, 126 insertions(+), 36 deletions(-) create mode 100644 frontend/src/components/ErrorBoundary.jsx diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index ae9e303..48cba0c 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -9,6 +9,7 @@ import { setNav } from './lib/nav.js' import { startFlow } from './sheets.jsx' import Icon from './components/Icon.jsx' import TabBar from './components/TabBar.jsx' +import ErrorBoundary from './components/ErrorBoundary.jsx' import Modals from './components/Modals.jsx' import Toast from './components/Toast.jsx' import RestTimer from './components/RestTimer.jsx' @@ -57,21 +58,25 @@ function Shell() { return ( <> + {/* keyed on the route: a view that throws is contained, and switching tabs + re-mounts the boundary, so the tab bar is always a way out */}
- {!authed ? : ( - - } /> - } /> - } /> - } /> - } /> - } /> - } /> - } /> - : } /> - } /> - - )} + + {!authed ? : ( + + } /> + } /> + } /> + } /> + } /> + } /> + } /> + } /> + : } /> + } /> + + )} +
diff --git a/frontend/src/components/ErrorBoundary.jsx b/frontend/src/components/ErrorBoundary.jsx new file mode 100644 index 0000000..c2c0e9d --- /dev/null +++ b/frontend/src/components/ErrorBoundary.jsx @@ -0,0 +1,41 @@ +import { Component } from 'react' +import { useStore } from '../store/useStore.js' +import { t } from '../lib/i18n.js' +import Icon from './Icon.jsx' +import { Button } from './ui.jsx' + +/** + * Last line of defence: one bad render used to blank the whole app, with no way back — + * a workout referencing an exercise the build doesn't know would white-screen and, since + * the running workout is persisted, do it again on every reload. + * + * Sits inside #app so the tab bar stays usable; the shell keys this subtree on the route, + * so switching tabs re-mounts it and clears the error by itself. + */ +export default class ErrorBoundary extends Component { + constructor(props) { super(props); this.state = { failed: false } } + static getDerivedStateFromError() { return { failed: true } } + componentDidCatch(err) { console.error('openGym render error:', err) } + + render() { + if (!this.state.failed) return this.props.children + const active = useStore.getState().S.active + return ( +
+
+
+
{t('Something went wrong')}
+ {t('This screen could not be drawn. Your data is safe on this device.')} +
+ + {active && <> +
+ + } +
+ ) + } +} diff --git a/frontend/src/index.css b/frontend/src/index.css index 4c36ef1..b216a4a 100644 --- a/frontend/src/index.css +++ b/frontend/src/index.css @@ -153,10 +153,12 @@ button,.chip,.tile,.item,.wday,.cal-d,.swatch,.hm-c,.lrow,.seg button,.stp butto .small{font-size:13px;line-height:1.38} .accent{color:var(--acc)} .capitalize{text-transform:capitalize} -.nocap{text-transform:none} /* exercise names and body-part/equipment terms arrive lowercase from the dataset, - so capitalising is opt-in per element — blanket-capitalising a row also turned - "60 kg" into "60 Kg" and whole sentences into Title Case. */ + so tags and chips capitalise by default. Anything that is a unit or a sentence opts + back out — blanket-capitalising turned "60 kg" into "60 Kg" and a whole hint into + Title Case. The compound selectors are needed: .tag/.chip come later in this file + and would otherwise win on source order. */ +.nocap,.tag.nocap,.chip.nocap{text-transform:none} /* ---------------------------------------------------------------- layout -- */ #app{ diff --git a/frontend/src/lib/exercises.js b/frontend/src/lib/exercises.js index ba39e78..20d82d2 100644 --- a/frontend/src/lib/exercises.js +++ b/frontend/src/lib/exercises.js @@ -1,4 +1,5 @@ import { EXDB } from './exercises-data.js' +import { t } from './i18n.js' export { EXDB } export const EXIDX = {} @@ -35,3 +36,10 @@ export const gifSrc = ex => GIF_BASE + ex.gif // Cardio exercises log time + speed instead of weight × reps. export const isCardio = idOrEx => (typeof idOrEx === 'string' ? EXIDX[idOrEx] : idOrEx)?.bp === 'cardio' + +// An id that resolves to nothing — a plan file built against a different exercise dataset, +// a custom exercise deleted on another device before the sync arrived — still has to +// render. A placeholder keeps it visible (and removable) instead of taking the whole view +// down on the first `ex.n`. +export const exOr = id => EXIDX[id] || + { id, n: t('Unknown exercise'), bp: '', tg: '', eq: '', sm: [], st: [], missing: true } diff --git a/frontend/src/lib/plan-share.js b/frontend/src/lib/plan-share.js index ef8b46b..4fd2fcf 100644 --- a/frontend/src/lib/plan-share.js +++ b/frontend/src/lib/plan-share.js @@ -9,7 +9,7 @@ // a page break — each exercise, and each routine that fits, stays in one place. import { EXIDX, isCardio } from './exercises.js' -import { uid, todayISO, DAYN, fmtNum } from './format.js' +import { uid, todayISO, DAYN, fmtNum, exCount } from './format.js' import { t } from './i18n.js' const PLAN_FMT = 1 @@ -43,18 +43,37 @@ export function buildPlanBundle(S, name) { return { opengym_plan: PLAN_FMT, exported: todayISO(), name: name || '', week, routines, customEx } } -/** Validate + normalise an imported file. Throws with a friendly message if it isn't one. */ +/** + * Validate + normalise an imported file. Throws with a friendly message if it isn't one. + * + * Every exercise id has to resolve — either to the built-in library or to a custom + * exercise carried in the same file. An id that resolves to neither (a hand-edited file, + * an export from a build with a different exercise dataset) is dropped here: kept, it + * would sit invisibly in the routine and only surface as a blank screen when the routine + * is trained. + */ export function parsePlan(raw) { const data = typeof raw === 'string' ? JSON.parse(raw) : raw if (!data || !data.opengym_plan || !Array.isArray(data.routines)) { throw new Error(t('this isn’t an openGym plan file')) } - const routines = data.routines.filter(r => r && Array.isArray(r.ex)) + const customEx = (Array.isArray(data.customEx) ? data.customEx : []).filter(c => c && c.id) + const known = new Set(customEx.map(c => c.id)) + let dropped = 0 + const routines = data.routines.filter(r => r && Array.isArray(r.ex)).map(r => ({ + ...r, + ex: r.ex.filter(e => { + const ok = !!e && (known.has(e.id) || !!EXIDX[e.id]) + if (!ok) dropped++ + return ok + }) + })) return { name: (data.name || '').trim(), routines, week: data.week || {}, - customEx: Array.isArray(data.customEx) ? data.customEx : [], + customEx, + dropped, routineCount: routines.length, exerciseCount: routines.reduce((n, r) => n + r.ex.length, 0), scheduledDays: WEEK_ORDER.filter(d => data.week?.[d]).length @@ -65,7 +84,8 @@ export function parsePlan(raw) { * Merge a parsed bundle into a draft state `s` (call inside store.update). * - customs: reuse one you already have with the same name + body part, else add it fresh * - routines: always added as NEW routines (fresh ids) — never overwrites yours - * - schedule: optional; when on, points your week at the freshly-added routines + * - schedule: optional; when on, the shared week REPLACES yours (days the shared plan + * leaves empty become rest days — a half-overwritten week would silently mix two plans) */ export function mergePlan(s, bundle, { schedule } = {}) { s.customEx = s.customEx || [] @@ -89,6 +109,7 @@ export function mergePlan(s, bundle, { schedule } = {}) { }) }) if (schedule) { + WEEK_ORDER.forEach(d => { delete s.week[d] }) Object.entries(bundle.week || {}).forEach(([d, oldId]) => { if (ridMap[oldId]) s.week[d] = ridMap[oldId] }) @@ -136,7 +157,7 @@ function routineHTML(r, unit) { ? `
${esc(t('Superset'))}
${items}
` : items }).join('') - const count = t('{0} exercises', r.ex.length).replace('{0}', r.ex.length) + const count = exCount(r.ex.length) return `

${esc(r.name)}

${esc(count)}
${rows || `
${esc(t('No exercises yet.'))}
`}
diff --git a/frontend/src/sheets.jsx b/frontend/src/sheets.jsx index 2a1fe11..fd520d6 100644 --- a/frontend/src/sheets.jsx +++ b/frontend/src/sheets.jsx @@ -532,10 +532,18 @@ function PlanImport({ bundle, close }) { return <>

{bundle.name ? t('Import “{0}”', bundle.name) : t('Import this plan')}

- {t('{0} routines · {1} exercises', bundle.routineCount, bundle.exerciseCount)} - {bundle.scheduledDays > 0 ? ' · ' + t('scheduled on {0} days', bundle.scheduledDays) : ''} + {t(bundle.routineCount === 1 ? '{0} routine' : '{0} routines', bundle.routineCount)} + {' · ' + exCount(bundle.exerciseCount)} + {bundle.scheduledDays > 0 + ? ' · ' + t(bundle.scheduledDays === 1 ? 'scheduled on {0} day' : 'scheduled on {0} days', bundle.scheduledDays) + : ''}
{t('These are added as new routines — nothing you already have is changed.')}
+ {bundle.dropped > 0 &&
+ {t(bundle.dropped === 1 + ? '{0} exercise in the file isn’t in your library and was left out.' + : '{0} exercises in the file aren’t in your library and were left out.', bundle.dropped)} +
} {bundle.scheduledDays > 0 &&
{t('Use this weekly schedule')}
{t('Replaces your current Mon–Sun assignments.')}
diff --git a/frontend/src/views/RoutineEdit.jsx b/frontend/src/views/RoutineEdit.jsx index 1efb4a0..3094031 100644 --- a/frontend/src/views/RoutineEdit.jsx +++ b/frontend/src/views/RoutineEdit.jsx @@ -1,7 +1,7 @@ import { useNavigate, useParams } from 'react-router-dom' import { useEffect } from 'react' import { useStore } from '../store/useStore.js' -import { EXIDX, isCardio } from '../lib/exercises.js' +import { exOr, isCardio } from '../lib/exercises.js' import { fmtNum, uid } from '../lib/format.js' import { t } from '../lib/i18n.js' import { supersetUnits, cleanupSg } from '../lib/history.js' @@ -47,7 +47,9 @@ export default function RoutineEdit() {
{r.ex.length ?
{r.ex.map((e, i) => { - const ex = EXIDX[e.id]; if (!ex) return null + // An unresolvable id is shown rather than skipped — hiding it left an entry you + // could neither see nor delete, but that still turned up in the workout. + const ex = exOr(e.id) const linkedPrev = i > 0 && e.sg && r.ex[i - 1].sg === e.sg return
{unitFirst.has(i) &&
{t('Superset')}
} diff --git a/frontend/src/views/Workout.jsx b/frontend/src/views/Workout.jsx index 2c5143b..cdb6b37 100644 --- a/frontend/src/views/Workout.jsx +++ b/frontend/src/views/Workout.jsx @@ -2,9 +2,9 @@ import { useEffect, useState } from 'react' import { useNavigate } from 'react-router-dom' import { useStore } from '../store/useStore.js' import { useUI } from '../store/useUI.js' -import { EXIDX, isCardio } from '../lib/exercises.js' +import { exOr, isCardio } from '../lib/exercises.js' import { effectiveRoutine, lastEntryFor, bestWeightFor, buildSets, setsDoneActive, supersetUnits, unitOf, setLabel } from '../lib/history.js' -import { fmtNum, fmtDate, todayISO, DAYN } from '../lib/format.js' +import { fmtNum, fmtDate, todayISO, exCount, DAYN } from '../lib/format.js' import { beep, vibrate } from '../lib/sound.js' import { t } from '../lib/i18n.js' import { api } from '../lib/api.js' @@ -26,7 +26,7 @@ function StartChooser() { {todayR &&

{t("Today's plan")}{todayOvr ? ' · ' + t('rescheduled') : ''}

-
{todayR.name}
{t('{0} exercises', todayR.ex.length)}
+
{todayR.name}
{exCount(todayR.ex.length)}
@@ -34,7 +34,7 @@ function StartChooser() { {others.length > 0 && <>

{t('Other routines')}

{others.map(r =>
startFlow(r.id)}> -
{r.name}
{t('{0} exercises', r.ex.length)}
+
{r.name}
{exCount(r.ex.length)}
{t('Start')}
)}
}
@@ -56,10 +56,12 @@ function Elapsed({ start }) { function ExerciseBlock({ entryIdx, compact, onToggle, onField, onBumpAll, onAddSet, onRemoveSet }) { const S = useStore(s => s.S) const entry = S.active.entries[entryIdx] - const ex = EXIDX[entry.id] + const ex = exOr(entry.id) const cardio = isCardio(ex) const last = lastEntryFor(S, entry.id) - const best = cardio ? 0 : bestWeightFor(S, entry.id) + // The same number the "confirm your working weight" sheet calls your best, so the two + // never disagree inside one session: heaviest logged set, or the working weight you kept. + const best = cardio ? 0 : Math.max(bestWeightFor(S, entry.id), (S.exWeights[entry.id] || {}).w || 0) const hint = !cardio && last && last.sets.length >= (entry.target.sets || 1) && last.sets.every(s => s.r >= entry.target.reps) && last.sets[0].w > 0 ? Math.max(...last.sets.map(s => s.w)) + 2.5 : null const col1 = cardio ? { f: 'min', step: 1, dec: false, hd: t('Duration (min)') } : { f: 'w', step: 2.5, dec: true, hd: t('Weight ({0})', S.unit) } @@ -81,11 +83,12 @@ function ExerciseBlock({ entryIdx, compact, onToggle, onField, onBumpAll, onAddS
{cardio && {t('Cardio')}} - {t(ex.tg || ex.bp)}{t(ex.eq)} - {best > 0 && {t('Best:')} {fmtNum(best)} {S.unit}} + {(ex.tg || ex.bp) && {t(ex.tg || ex.bp)}} + {ex.eq && {t(ex.eq)}} + {best > 0 && {t('Best:')} {fmtNum(best)} {S.unit}}
{last &&
{t('Last time')} ({fmtDate(last.d)}): {last.sets.map(s => setLabel(entry.id, s)).join(', ')}
} - {hint && } + {hint && }
{col1.hd}{col2.hd}
{entry.sets.map((s, i) =>