Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ The separate Personal health experience (see [DELIVERY_PLAN.md](DELIVERY_PLAN.md
- Regions are keyboard-focusable buttons (Enter/Space); the mirrored half is hidden from assistive tech.
- Colour transitions run only under `prefers-reduced-motion: no-preference`.
- Illustration is decorative anatomy, not medical imagery, and never implies diagnosis or injury.
- The map is interactive (Planned / Logged / Preview, with a 7, 28 or 90 day period): see [P1_MUSCLE_EXPLORER.md](P1_MUSCLE_EXPLORER.md). Segmented controls reuse the Front/Back tab styling with 44px targets; the figure never moves when controls change.

## History: Luxe (superseded)

Expand Down
31 changes: 31 additions & 0 deletions docs/P1_MUSCLE_EXPLORER.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Personal interactive muscle map

**Status:** implemented on `/me/body`. No schema change, no new route, no new dependency.

## What it does
- **Planned** (default): the original map from declared activities plus club visits the user has allowed.
- **Logged**: the same map from the user's own completed workouts (`WorkoutSession` from P1-B), over 7, 28 or 90 days. Each muscle shows weighted set-equivalents, when it was last worked and which exercises contributed most.
- **Preview**: pick any catalogue activity and see which muscles it trains. It is a lens, not history.
- Tap or press Enter on a muscle for its detail; every value is also in the text list.

## How workouts become muscles
- Logged exercise names are free text, so `src/lib/personal/exercise-catalog.ts` maps keywords to muscles. Matching is lower-cased words, a keyword must start at a word boundary, and the longest keyword wins ("romanian deadlift" is not a plain deadlift, "leg curl" is not a biceps curl).
- A set counts 1; a primary muscle gets weight 1 and a secondary 0.4 (the same constants as `muscleLoad`). A timed set with no reps counts one set-equivalent per 10 minutes. Empty sets count nothing.
- A workout with no usable exercises falls back to its declared `activityKey`: one set-equivalent per 10 minutes, at least 3.
- Names the catalogue cannot place are **listed to the user as not counted**, never guessed.
- Load is relative to the most-worked muscle in the chosen window, so 100% means "most", not "a lot".

## Privacy and gating
- Reads only the signed-in user's rows. Nothing flows to clubs; the map never feeds leaderboards or club data.
- Logged data reuses P1-B's own gate (`assertWellnessEnabled`, `assertWellnessAdult` in `wellness-policy.ts`), so it is off unless `PERSONAL_WELLNESS_ENABLED=true` and the user is a clear adult. When off, "Logged" is disabled and the page says why.
- Descriptive only: no recovery score, readiness, injury or health claim.

## Files (all new unless noted)
`src/lib/personal/exercise-catalog.ts`, `muscle-training.ts`, `muscle-explorer-service.ts`; `src/components/personal/body-map/muscle-explorer.tsx`; `body-map.tsx` and `body-map.css` extended; `src/app/me/body/page.tsx` rewired. Tests: `tests/unit/personal-muscle-training.test.mjs`, two cases in `tests/integration/personal-service.test.mjs`.

## Coordination
Built to avoid Codex's in-flight work: it imports P1-B code but edits none of it, and adds no Prisma change, so it cannot conflict with the P1-C Health Connect schema branch.

## Known limits
- The exercise catalogue covers common lifts and cardio only; unusual names go to "not counted". A user-editable mapping is a possible follow-up.
- Set-equivalents are a descriptive volume count, not a validated training-load model.
12 changes: 5 additions & 7 deletions src/app/me/body/page.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { prisma } from "@/lib/prisma";
import { requireSession } from "@/lib/required-session";
import { getOverview } from "@/lib/personal/personal-service";
import { getMuscleExplorer } from "@/lib/personal/muscle-explorer-service";
import { displayUnit, type MeasurementKind } from "@/lib/personal/measurements";
import { MEASUREMENT_LABEL, MEASUREMENT_MUSCLE } from "@/lib/personal/measurement-muscles";
import type { MuscleId } from "@/lib/personal/muscle-map";
import { BodyMap } from "@/components/personal/body-map/body-map";
import { MuscleExplorer } from "@/components/personal/body-map/muscle-explorer";

export default async function BodyPage() {
const session = await requireSession();
const o = await getOverview(prisma, session.user.id);
const { overview: o, logged } = await getMuscleExplorer(prisma, session.user.id);

// Latest logged girth per muscle, across left and right.
const notes: Partial<Record<MuscleId, string>> = {};
Expand All @@ -29,11 +29,9 @@ export default async function BodyPage() {
<>
<header>
<h1 style={{ margin: 0, fontFamily: "var(--font-hm-display, inherit)" }}>My muscle map</h1>
<p className="hm-small">
Shaded by how much your activities train each muscle. Tap a muscle for details.
</p>
<p className="hm-small">Tap a muscle to see what trains it.</p>
</header>
<BodyMap load={o.load} notes={notes} />
<MuscleExplorer planned={o.load} logged={logged} measurementNotes={notes} />
</>
);
}
22 changes: 22 additions & 0 deletions src/components/personal/body-map/body-map.css
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,25 @@
padding: 0.25rem 0;
font-variant-numeric: tabular-nums;
}

/* Interactive explorer controls (source, period, preview). */
.hm-explorer {
display: grid;
gap: 0.75rem;
}
.hm-explorer-controls {
display: grid;
gap: 0.5rem;
justify-items: center;
}
.hm-explorer-controls .hm-me-field {
width: min(100%, 20rem);
}
.hm-explorer-summary {
text-align: center;
margin: 0;
}
.hm-bodymap-tab:disabled {
opacity: 0.45;
cursor: not-allowed;
}
31 changes: 23 additions & 8 deletions src/components/personal/body-map/body-map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
} from "./body-paths";
import "./body-map.css";

/** Primary activities come first, so the cap keeps the most relevant ones. */
const MAX_ACTIVITIES_SHOWN = 5;
const MIRROR = "translate(200 0) scale(-1 1)";
const SIDES: { side: MuscleSide; label: string }[] = [
{ side: "front", label: "Front" },
Expand All @@ -29,10 +31,12 @@ export interface BodyMapProps {
/** Load per muscle on a 0..1 scale (see muscleLoad). Missing muscles count as 0. */
load: Partial<Record<MuscleId, number>>;
/** Optional short text per muscle, e.g. a recent girth measurement, shown in the detail panel. */
notes?: Partial<Record<MuscleId, string>>;
notes?: Partial<Record<MuscleId, string | readonly string[]>>;
/** Wording for the load figure in the detail panel, e.g. "of your most-worked muscle". */
loadPhrase?: string;
}

export function BodyMap({ load, notes }: BodyMapProps) {
export function BodyMap({ load, notes, loadPhrase = "of your most-worked muscle" }: BodyMapProps) {
const [side, setSide] = useState<MuscleSide>("front");
const [selected, setSelected] = useState<MuscleId | null>(null);

Expand Down Expand Up @@ -138,14 +142,25 @@ export function BodyMap({ load, notes }: BodyMapProps) {
{selectedMuscle ? (
<>
<h3>{selectedMuscle.label}</h3>
<p>Training load {percent(selectedMuscle.id)}% of your most-worked muscle.</p>
{notes?.[selectedMuscle.id] ? <p>{notes[selectedMuscle.id]}</p> : null}
<p>
Training load {percent(selectedMuscle.id)}% {loadPhrase}.
</p>
{[notes?.[selectedMuscle.id] ?? []].flat().map((line) => (
<p key={line}>{line}</p>
))}
<ul>
{activitiesForMuscle(selectedMuscle.id).map((a) => (
<li key={a.key}>
{a.label} <span>({a.role})</span>
{activitiesForMuscle(selectedMuscle.id)
.slice(0, MAX_ACTIVITIES_SHOWN)
.map((a) => (
<li key={a.key}>
{a.label} <span>({a.role})</span>
</li>
))}
{activitiesForMuscle(selectedMuscle.id).length > MAX_ACTIVITIES_SHOWN && (
<li>
+{activitiesForMuscle(selectedMuscle.id).length - MAX_ACTIVITIES_SHOWN} more
</li>
))}
)}
</ul>
</>
) : (
Expand Down
156 changes: 156 additions & 0 deletions src/components/personal/body-map/muscle-explorer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
"use client";

import Link from "next/link";
import { useMemo, useState } from "react";
import { ACTIVITIES, muscleLoad, type ActivityKey, type MuscleId } from "@/lib/personal/muscle-map";
import { daysAgoLabel, type MuscleTraining } from "@/lib/personal/muscle-training";
import { BodyMap } from "./body-map";

type Source = "planned" | "logged" | "preview";
type Window = 7 | 28 | 90;
const WINDOWS: Window[] = [7, 28, 90];

export interface MuscleExplorerProps {
/** Declared activities plus consented club visits, 0..1. */
planned: Record<MuscleId, number>;
/** Completed workouts per window; null when workout tracking is not available to this user. */
logged: Record<Window, MuscleTraining> | null;
/** Latest logged girth per muscle, already worded (e.g. "Latest chest: 96 cm (2026-10-01)"). */
measurementNotes: Partial<Record<MuscleId, string>>;
}

export function MuscleExplorer({ planned, logged, measurementNotes }: MuscleExplorerProps) {
const hasLogged = logged !== null && logged[28].workouts + logged[90].workouts > 0;
const [source, setSource] = useState<Source>(hasLogged ? "logged" : "planned");
const [days, setDays] = useState<Window>(28);
const [lens, setLens] = useState<ActivityKey>(ACTIVITIES[0].key);

const training = logged?.[days] ?? null;
const lensDef = ACTIVITIES.find((a) => a.key === lens) ?? ACTIVITIES[0];

const { load, notes, phrase } = useMemo(() => {
const out: Partial<Record<MuscleId, string[]>> = {};
const push = (id: MuscleId, line: string) => (out[id] = [...(out[id] ?? []), line]);
for (const [id, line] of Object.entries(measurementNotes) as [MuscleId, string][])
push(id, line);

if (source === "logged" && training) {
for (const id of Object.keys(training.load) as MuscleId[]) {
const sets = training.setEquivalents[id];
if (sets > 0) {
const ago = training.lastWorkedDaysAgo[id];
push(
id,
`${sets} set-equivalents in the last ${days} days` +
(ago === undefined ? "" : `, last worked ${daysAgoLabel(ago)}.`)
);
const who = training.contributors[id];
if (who?.length) push(id, `Mostly from: ${who.join(", ")}.`);
} else {
push(id, `Not worked in the last ${days} days.`);
}
}
return {
load: training.load,
notes: out,
phrase: "of your most-worked muscle in this period",
};
}
if (source === "preview") {
for (const id of lensDef.primary) push(id, `${lensDef.label} trains this as a main muscle.`);
for (const id of lensDef.secondary) push(id, `${lensDef.label} also works this one.`);
return {
load: muscleLoad([{ key: lensDef.key, sessionsPerWeek: 1 }]),
notes: out,
phrase: `of ${lensDef.label}'s heaviest muscle`,
};
}
return { load: planned, notes: out, phrase: "of your most-worked muscle" };
}, [source, training, days, lensDef, planned, measurementNotes]);

return (
<div className="hm-explorer">
<div className="hm-explorer-controls">
<div className="hm-bodymap-tabs" role="group" aria-label="What to show">
{(
[
["planned", "Planned"],
["logged", "Logged"],
["preview", "Preview"],
] as [Source, string][]
).map(([value, label]) => (
<button
key={value}
type="button"
className="hm-bodymap-tab"
aria-pressed={source === value}
disabled={value === "logged" && logged === null}
onClick={() => setSource(value)}
>
{label}
</button>
))}
</div>

{source === "logged" && training && (
<div className="hm-bodymap-tabs" role="group" aria-label="Period">
{WINDOWS.map((w) => (
<button
key={w}
type="button"
className="hm-bodymap-tab"
aria-pressed={days === w}
onClick={() => setDays(w)}
>
{w} days
</button>
))}
</div>
)}

{source === "preview" && (
<label className="hm-me-field">
Preview an activity
<select value={lens} onChange={(e) => setLens(e.target.value as ActivityKey)}>
{ACTIVITIES.map((a) => (
<option key={a.key} value={a.key}>
{a.label}
</option>
))}
</select>
</label>
)}
</div>

<p className="hm-small hm-explorer-summary" role="status">
{source === "planned" &&
"Your declared activities, plus club visits you have allowed. Tap a muscle for details."}
{source === "preview" &&
`What ${lensDef.label} trains. This is a preview, not your history.`}
{source === "logged" && training && training.workouts > 0 && (
<>
{training.workouts} workout{training.workouts === 1 ? "" : "s"} in the last {days} days.
{training.unmatchedExercises.length > 0 && (
<>
{" "}
Not counted, because we could not place them:{" "}
{training.unmatchedExercises.join(", ")}.
</>
)}
</>
)}
{source === "logged" && training && training.workouts === 0 && (
<>
No workouts logged in the last {days} days.{" "}
<Link href="/me/workouts">Log a workout</Link>
</>
)}
{logged === null &&
source !== "preview" &&
" Workout-based mapping appears here once workout tracking is available for your account."}
</p>

<BodyMap load={load} notes={notes} loadPhrase={phrase} />
</div>
);
}
Loading
Loading