From 83b2a140d64a28bfb456427c2833e88bde4e82a5 Mon Sep 17 00:00:00 2001 From: Duarte Santos Date: Thu, 17 Sep 2026 11:37:56 +0200 Subject: [PATCH] Custom exercises: muscles are stored in the map's order, not the order the chips were tapped in --- frontend/src/lib/muscle-order.test.js | 18 ++++++++++++++++++ frontend/src/lib/muscles.js | 7 +++++++ frontend/src/sheets.jsx | 8 +++++--- 3 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 frontend/src/lib/muscle-order.test.js diff --git a/frontend/src/lib/muscle-order.test.js b/frontend/src/lib/muscle-order.test.js new file mode 100644 index 0000000..35855ed --- /dev/null +++ b/frontend/src/lib/muscle-order.test.js @@ -0,0 +1,18 @@ +// A custom exercise used to store its muscles in the order the chips were tapped, so the same +// exercise read differently depending on how it was built (Discord, luckapow). +import { describe, expect, it } from 'vitest' +import { inMuscleOrder, MUSCLES } from './muscles.js' + +describe('muscles in the map order', () => { + it('is the same list whatever order it was picked in', () => { + expect(inMuscleOrder(['triceps', 'chest', 'deltoids'])).toEqual(['deltoids', 'chest', 'triceps']) + expect(inMuscleOrder(['chest', 'deltoids', 'triceps'])).toEqual(['deltoids', 'chest', 'triceps']) + }) + it('follows MUSCLES, leaves unknown names at the end and the input alone', () => { + const picked = ['calves', 'made-up', 'abs'] + expect(inMuscleOrder(picked)).toEqual(['abs', 'calves', 'made-up']) + expect(picked).toEqual(['calves', 'made-up', 'abs']) + expect(inMuscleOrder([...MUSCLES].reverse())).toEqual(MUSCLES) + expect(inMuscleOrder(undefined)).toEqual([]) + }) +}) diff --git a/frontend/src/lib/muscles.js b/frontend/src/lib/muscles.js index 2d7e10b..eb913e1 100644 --- a/frontend/src/lib/muscles.js +++ b/frontend/src/lib/muscles.js @@ -21,6 +21,13 @@ export const MUSCLES = [ 'calves', 'tibialis', ] +// A picked list in the map's own order rather than the order the chips were tapped in — two +// people building the same exercise get the same exercise. Unknown names keep their place at the end. +export const inMuscleOrder = list => { + const at = m => { const i = MUSCLES.indexOf(m); return i < 0 ? MUSCLES.length : i } + return [...(list || [])].sort((a, b) => at(a) - at(b)) +} + // Drawn as the silhouette, never shaded: they carry no training load. export const INERT = ['head', 'hair', 'neck', 'hands', 'feet', 'knees', 'ankles'] diff --git a/frontend/src/sheets.jsx b/frontend/src/sheets.jsx index a99a8c7..019bd88 100644 --- a/frontend/src/sheets.jsx +++ b/frontend/src/sheets.jsx @@ -19,7 +19,7 @@ import { Button, Slider, Switch, Segmented, SelectRow, Row, TextField, NumberFie import { glyphOf, GLYPH_GROUPS, DEFAULT_GLYPH } from './lib/glyphs.js' import BodyMap from './components/BodyMap.jsx' import MuscleExplorer from './components/MuscleExplorer.jsx' -import { exerciseMuscleSnapshot, loadOfWorkouts, MUSCLES, MUSCLE_NAME, normalizeMuscleGroups, hasExplicitMuscleMetadata } from './lib/muscles.js' +import { exerciseMuscleSnapshot, loadOfWorkouts, MUSCLES, MUSCLE_NAME, normalizeMuscleGroups, hasExplicitMuscleMetadata, inMuscleOrder } from './lib/muscles.js' import { parseImport, mergeImport } from './lib/import-csv.js' import { importHevyData, HevyApiError, HEVY_DEV_SETTINGS, mergeHevyRoutines } from './lib/import-hevy.js' import { buildPlanBundle, parsePlan, mergePlan, printPlan, planPrintHTML } from './lib/plan-share.js' @@ -790,8 +790,10 @@ function CustomExForm({ existing, prefill, onDone, close }) { const dup = allExercises(S()).find(e => e.n.toLowerCase() === name.toLowerCase() && e.id !== (existing || {}).id) if (dup) { toast(t('“{0}” already exists', dup.n)); return } const d = desc.trim().slice(0, 1000) - const prim = bp === 'cardio' ? ['cardiovascular system'] : [...primaries] - const sm = secondaries.filter(m => !prim.includes(m)) + // Stored in the map's order, not the order the chips were tapped in — the tags on the exercise + // used to shuffle with every edit. + const prim = bp === 'cardio' ? ['cardiovascular system'] : inMuscleOrder(primaries) + const sm = inMuscleOrder(secondaries.filter(m => !prim.includes(m))) const groups = [...prim, ...sm] let id = existing && existing.id if (existing) update(s => { const c = (s.customEx || []).find(x => x.id === id); if (c) {