Workout-Builder: Suchsuche bleibt offen, "Chosen"-Filter, Supersätze

- Übungs-Picker schließt beim Antippen nicht mehr — Config öffnet darüber,
  Wegwischen führt zurück zur Suche (Suchbegriff bleibt erhalten)
- Neuer "⭐ Chosen"-Filter: Übungen, die schon in Routinen/Workouts genutzt
  werden (nach Häufigkeit sortiert), plus ⭐-Marker in der Liste
- Supersätze: im Routine-Editor per 🔗 mit der Übung darüber verknüpfen
  (visuelle Gruppierung mit Akzent-Rand + Label); im Workout als eine
  Superset-Karte dargestellt (beide Übungen, Pause erst nach der letzten),
  Unit-weise Navigation und weiterführende Gewichts-Abfrage
This commit is contained in:
Duarte Santos committed 2026-07-20 15:24:15 +02:00
1 parent 7e1d27b218
commit 4c554d9149
6 files changed
+185 -104

No files matched your search

+3 -3
View File
@@ -1,11 +1,11 @@
import { useState } from 'react'
import { imgSrc, gifSrc } from '../lib/exercises.js'
// Big autoplaying animation; tap toggles to the still frame.
export default function Media({ ex, id }) {
// Big autoplaying animation; tap toggles to the still frame. `compact` shrinks it (superset cards).
export default function Media({ ex, id, compact }) {
const [playing, setPlaying] = useState(true)
return (
<div className="exmedia" id={id} onClick={() => setPlaying(p => !p)}>
<div className={'exmedia' + (compact ? ' compact' : '')} id={id} onClick={() => setPlaying(p => !p)}>
<img decoding="async" src={playing ? gifSrc(ex) : imgSrc(ex)} alt={ex.n} />
<span className="gifhint">{playing ? '⏸ tap to pause' : '▶ tap to play'}</span>
</div>
+15
View File
@@ -306,3 +306,18 @@ h4.sec{font-size:.8rem;text-transform:uppercase;letter-spacing:.08em;color:var(-
.exmedia img{height:400px}
.hm-wrap{display:flex;flex-direction:column;align-items:center}
}
/* ---------- supersets ---------- */
/* editor: grouped exercises get a left accent + a label */
.ss-label{font-size:.66rem;font-weight:800;letter-spacing:.06em;text-transform:uppercase;
color:var(--acc);margin:6px 0 4px 4px;display:flex;align-items:center;gap:5px}
.item.in-ss{border-left:3px solid var(--acc)}
.iconbtn.on-ss{background:var(--accT);border-color:var(--acc);color:var(--acc)}
/* workout: superset card holds both exercises */
.ss-card{border:1.5px solid var(--acc);border-radius:var(--r);padding:14px;
background:color-mix(in srgb, var(--acc) 6%, var(--card))}
.ss-hd{font-size:.72rem;font-weight:800;letter-spacing:.04em;color:var(--acc);
margin-bottom:12px;text-align:center}
.ss-ex{position:relative}
.ss-amp{text-align:center;font-size:1.3rem;font-weight:800;color:var(--acc);margin:14px 0 8px}
.exmedia.compact img{height:190px}
+13
View File
@@ -57,6 +57,19 @@ export function setsDoneActive(A) {
}
export const lastBW = S => (S.bodyweight.length ? S.bodyweight[S.bodyweight.length - 1] : null)
// Group consecutive items sharing a superset id (sg) into "units" of indices.
// items may be routine exercises ({sg}) or active-workout entries ({sg}).
export function supersetUnits(items) {
const units = []
items.forEach((e, i) => {
const prev = items[i - 1]
if (i > 0 && e.sg && prev && prev.sg && e.sg === prev.sg) units[units.length - 1].push(i)
else units.push([i])
})
return units
}
export function unitOf(units, idx) { return units.find(u => u.includes(idx)) || [idx] }
export function streakWeeks(S) {
if (!S.workouts.length) return 0
const weeks = new Set(S.workouts.map(w => weekKey(w.d)))
+41 -18
View File
@@ -3,7 +3,7 @@ import { useStore } from './store/useStore.js'
import { useUI } from './store/useUI.js'
import { EXDB, EXIDX, BODYPARTS } from './lib/exercises.js'
import { fmtDate, fmtNum, fmtVol, fmtDur, todayISO, uid, DAYN, MONTHS_LONG, ACCENTS } from './lib/format.js'
import { lastEntryFor, bestWeightFor, buildSets, effectiveRoutineId, workoutVolume, setsDone, setsDoneActive, lastBW } from './lib/history.js'
import { lastEntryFor, bestWeightFor, buildSets, effectiveRoutineId, workoutVolume, setsDone, setsDoneActive, lastBW, supersetUnits, unitOf } from './lib/history.js'
import { beep, vibrate } from './lib/sound.js'
import { nav } from './lib/nav.js'
import Media, { Thumb } from './components/Media.jsx'
@@ -145,25 +145,40 @@ function AddToRoutine({ ex, close }) {
export const addToRoutineSheet = ex => ui().openSheet(close => <AddToRoutine ex={ex} close={close} />)
/* ============================ exercise picker ============================ */
// Exercises already used in your routines or past workouts (for the "Chosen" filter + a marker).
function usageMap(st) {
const u = {}
st.routines.forEach(r => r.ex.forEach(e => { u[e.id] = (u[e.id] || 0) + 1 }))
st.workouts.forEach(w => w.entries.forEach(e => { u[e.id] = (u[e.id] || 0) + 1 }))
return u
}
function ExercisePicker({ onPick, close }) {
const st = useStore(s => s.S)
const usage = usageMap(st)
const [q, setQ] = useState('')
const [bp, setBp] = useState('')
const [bp, setBp] = useState('') // '' = all, '★' = chosen, else a body part
const [shown, setShown] = useState(50)
const ql = q.toLowerCase().trim()
const f = EXDB.filter(e => (!bp || e.bp === bp) && (!ql || e.n.includes(ql) || e.tg.includes(ql) || e.eq.includes(ql)))
let f = EXDB.filter(e =>
(bp === '★' ? usage[e.id] : (!bp || e.bp === bp)) &&
(!ql || e.n.includes(ql) || e.tg.includes(ql) || e.eq.includes(ql)))
if (bp === '★') f = [...f].sort((a, b) => (usage[b.id] - usage[a.id]) || (a.n < b.n ? -1 : 1))
const chosenCount = Object.keys(usage).length
return <>
<h3>Add exercise</h3>
<div className="search"><svg viewBox="0 0 24 24"><circle cx="11" cy="11" r="7" /><path d="m21 21-4.3-4.3" /></svg>
<input className="input" placeholder={`Search ${EXDB.length} exercises…`} value={q} onChange={e => { setQ(e.target.value); setShown(50) }} /></div>
<div className="chips" style={{ margin: '10px 0' }}>
{chosenCount > 0 && <button className={'chip' + (bp === '★' ? ' on' : '')} onClick={() => { setBp('★'); setShown(50) }}>⭐ Chosen ({chosenCount})</button>}
<button className={'chip' + (!bp ? ' on' : '')} onClick={() => { setBp(''); setShown(50) }}>All</button>
{BODYPARTS.map(b => <button key={b} className={'chip' + (bp === b ? ' on' : '')} onClick={() => { setBp(b); setShown(50) }}>{b}</button>)}
</div>
<div className="list">
{f.slice(0, shown).map(e => <div key={e.id} className="item" onClick={() => { close(); onPick(e) }}>
<Thumb ex={e} /><div className="grow"><div className="tt">{e.n}</div><div className="ss">{(e.tg || e.bp)} · {e.eq}</div></div><span className="chev">+</span>
{f.slice(0, shown).map(e => <div key={e.id} className="item" onClick={() => onPick(e)}>
<Thumb ex={e} /><div className="grow"><div className="tt">{e.n}</div><div className="ss">{(e.tg || e.bp)} · {e.eq}</div></div>
{usage[e.id] && <span className="tag acc">⭐</span>}<span className="chev">+</span>
</div>)}
{f.length === 0 && <div className="empty">No match 🤷</div>}
{f.length === 0 && <div className="empty">{bp === '★' ? 'Nothing chosen yet — add exercises and they’ll show up here.' : 'No match 🤷'}</div>}
</div>
{f.length > shown && <><div style={{ height: 8 }} /><button className="btn" onClick={() => setShown(s => s + 50)}>Show more</button></>}
</>
@@ -322,48 +337,56 @@ export function startFlow(routineId) {
export function beginWorkout(routineId, bw) {
const st = S()
const r = routineId ? st.routines.find(x => x.id === routineId) : null
const entries = (r ? r.ex : []).map(cfg => ({ id: cfg.id, target: { ...cfg }, sets: buildSets(st, cfg) }))
const entries = (r ? r.ex : []).map(cfg => ({ id: cfg.id, sg: cfg.sg, target: { ...cfg }, sets: buildSets(st, cfg) }))
update(s => {
s.active = { id: uid(), d: todayISO(), start: Date.now(), routineId, name: r ? r.name : 'Freestyle', bw: bw || null, cur: 0, entries }
})
useUI.getState().stopRest()
nav('/workout')
}
function TopWeight({ entry, close }) {
function TopWeight({ entryIdx, close }) {
const st = useStore(s => s.S)
const A = st.active
const entry = A.entries[entryIdx]
const ex = EXIDX[entry.id]
const maxSet = Math.max(0, ...entry.sets.filter(s => s.done).map(s => s.w || 0))
const prevBest = Math.max((st.exWeights[entry.id] || {}).w || 0, bestWeightFor(st, entry.id))
const [v, setV] = useState(String(Math.max(maxSet, prevBest) || entry.target.weight || 0))
const isLastEx = A && A.cur >= A.entries.length - 1
const ref = useRef(null)
useEffect(() => { setTimeout(() => { ref.current?.focus(); ref.current?.select() }, 250) }, [])
const units = supersetUnits(A.entries)
const unit = unitOf(units, entryIdx)
const unitDone = unit.every(i => A.entries[i].sets.every(s => s.done))
const unitIdx = units.findIndex(u => u === unit)
const isLastUnit = unitIdx === units.length - 1
const commit = advance => {
const n = parseFloat(v)
if (!isFinite(n) || n < 0) { toast('Enter a valid weight'); return }
update(s => {
const e = s.active.entries.find(x => x === s.active.entries[A.cur]) || s.active.entries[A.cur]
s.active.entries[A.cur].topW = n
s.active.entries[entryIdx].topW = n
const cur = s.exWeights[entry.id]
s.exWeights[entry.id] = { w: Math.max(n, cur ? cur.w : 0), d: todayISO() }
})
close()
if (advance && A) {
if (isLastEx) finishWorkout()
else update(s => { s.active.cur = Math.min(s.active.entries.length - 1, s.active.cur + 1) })
if (advance && unitDone) {
if (isLastUnit) finishWorkout()
else update(s => { s.active.cur = units[unitIdx + 1][0] })
} else toast('Tracked — next time starts at ' + fmtNum(S().exWeights[entry.id].w) + ' ' + st.unit + ' 📈')
}
return <>
<h3 className="capitalize">✅ {ex.n} done</h3>
<div className="muted small">Confirm the weight you worked with — your highest becomes the default next time.</div>
<div className="muted small">Confirm the weight you worked with — your highest becomes the default next time.{!unitDone && unit.length > 1 ? ' Then finish the superset partner.' : ''}</div>
<div className="bwin"><input ref={ref} type="number" inputMode="decimal" step="0.5" value={v} onChange={e => setV(e.target.value)} /><span>{st.unit}</span></div>
{prevBest > 0 ? <div className="small dim" style={{ textAlign: 'center', marginBottom: 12 }}>Previous best: {fmtNum(prevBest)} {st.unit}{maxSet > prevBest && <span style={{ color: 'var(--gold)' }}> — new record! 🏆</span>}</div> : <div style={{ height: 4 }} />}
<button className="btn primary" onClick={() => commit(true)}>{isLastEx ? 'Save & finish 🏁' : 'Save & next exercise ›'}</button>
<div style={{ height: 8 }} /><button className="btn ghost dim" onClick={() => commit(false)}>Just close</button>
{unitDone ? <>
<button className="btn primary" onClick={() => commit(true)}>{isLastUnit ? 'Save & finish 🏁' : 'Save & next exercise ›'}</button>
<div style={{ height: 8 }} /><button className="btn ghost dim" onClick={() => commit(false)}>Just close</button>
</> : <button className="btn primary" onClick={() => commit(false)}>Save weight</button>}
</>
}
export const topWeightSheet = entry => ui().openSheet(close => <TopWeight entry={entry} close={close} />)
export const topWeightSheet = entryIdx => ui().openSheet(close => <TopWeight entryIdx={entryIdx} close={close} />)
function FinishSummary({ w, prs, close }) {
const st = useStore(s => s.S)
+38 -17
View File
@@ -2,10 +2,18 @@ import { useNavigate, useParams } from 'react-router-dom'
import { useEffect } from 'react'
import { useStore } from '../store/useStore.js'
import { EXIDX } from '../lib/exercises.js'
import { fmtNum } from '../lib/format.js'
import { fmtNum, uid } from '../lib/format.js'
import { supersetUnits } from '../lib/history.js'
import { Thumb } from '../components/Media.jsx'
import { emojiPicker, exercisePicker, exConfigSheet } from '../sheets.jsx'
// drop superset ids that no longer have an adjacent partner (after unlink/reorder)
function cleanupSg(ex) {
ex.forEach((e, i) => {
if (e.sg && !(ex[i - 1]?.sg === e.sg || ex[i + 1]?.sg === e.sg)) delete e.sg
})
}
export default function RoutineEdit() {
const nav = useNavigate()
const { id } = useParams()
@@ -15,13 +23,20 @@ export default function RoutineEdit() {
useEffect(() => { if (!r) nav('/plan') }, [!!r])
if (!r) return null
const move = (i, dir) => update(s => {
const ex = s.routines.find(x => x.id === id).ex
const j = i + dir
if (j < 0 || j >= ex.length) return
;[ex[i], ex[j]] = [ex[j], ex[i]]
const edit = fn => update(s => { fn(s.routines.find(x => x.id === id).ex) })
const move = (i, dir) => edit(ex => { const j = i + dir; if (j < 0 || j >= ex.length) return;[ex[i], ex[j]] = [ex[j], ex[i]]; cleanupSg(ex) })
const toggleLink = i => edit(ex => {
if (i < 1) return
const cur = ex[i], prev = ex[i - 1]
if (cur.sg && prev.sg && cur.sg === prev.sg) delete cur.sg
else { const gid = prev.sg || ('sg' + uid()); prev.sg = gid; cur.sg = gid }
cleanupSg(ex)
})
const units = supersetUnits(r.ex)
const unitFirst = new Set(units.filter(u => u.length > 1).map(u => u[0]))
const inSS = new Set(units.filter(u => u.length > 1).flat())
return <div className="narrow">
<div className="hdr">
<button className="iconbtn" onClick={() => nav('/plan')}>‹</button>
@@ -34,21 +49,27 @@ export default function RoutineEdit() {
{r.ex.length ? <div className="list">{r.ex.map((e, i) => {
const ex = EXIDX[e.id]; if (!ex) return null
return <div key={i} className="item" onClick={() => {
exConfigSheet(ex, e, cfg => update(s => { Object.assign(s.routines.find(x => x.id === id).ex[i], cfg) }),
() => update(s => { s.routines.find(x => x.id === id).ex.splice(i, 1) }))
}}>
<Thumb ex={ex} />
<div className="grow"><div className="tt">{ex.n}</div><div className="ss">{e.sets} × {e.reps}{e.weight ? ' · ' + fmtNum(e.weight) + ' ' + S.unit : ''}</div></div>
<div style={{ display: 'flex', flexDirection: 'column', gap: 2, flex: 'none' }}>
<button className="iconbtn" style={{ width: 34, height: 30, fontSize: '.8rem' }} onClick={ev => { ev.stopPropagation(); move(i, -1) }}>▲</button>
<button className="iconbtn" style={{ width: 34, height: 30, fontSize: '.8rem' }} onClick={ev => { ev.stopPropagation(); move(i, 1) }}>▼</button>
const linkedPrev = i > 0 && e.sg && r.ex[i - 1].sg === e.sg
return <div key={i}>
{unitFirst.has(i) && <div className="ss-label">🔗 Superset</div>}
<div className={'item' + (inSS.has(i) ? ' in-ss' : '')} onClick={() => {
exConfigSheet(ex, e, cfg => edit(x => { Object.assign(x[i], cfg) }), () => edit(x => { x.splice(i, 1); cleanupSg(x) }))
}}>
<Thumb ex={ex} />
<div className="grow"><div className="tt">{ex.n}</div><div className="ss">{e.sets} × {e.reps}{e.weight ? ' · ' + fmtNum(e.weight) + ' ' + S.unit : ''}</div></div>
<div style={{ display: 'flex', flexDirection: 'column', gap: 2, flex: 'none', alignItems: 'center' }}>
{i > 0 && <button className={'iconbtn' + (linkedPrev ? ' on-ss' : '')} title="Superset with exercise above" style={{ width: 34, height: 28, fontSize: '.85rem' }} onClick={ev => { ev.stopPropagation(); toggleLink(i) }}>🔗</button>}
<div style={{ display: 'flex', gap: 2 }}>
<button className="iconbtn" style={{ width: 28, height: 26, fontSize: '.7rem' }} onClick={ev => { ev.stopPropagation(); move(i, -1) }}>▲</button>
<button className="iconbtn" style={{ width: 28, height: 26, fontSize: '.7rem' }} onClick={ev => { ev.stopPropagation(); move(i, 1) }}>▼</button>
</div>
</div>
</div>
</div>
})}</div> : <div className="empty"><div className="ico">🏋️</div>No exercises yet — add your first one.</div>}
<div style={{ height: 10 }} />
<button className="btn primary" onClick={() => exercisePicker(ex => exConfigSheet(ex, null, cfg => update(s => { s.routines.find(x => x.id === id).ex.push({ id: ex.id, ...cfg }) })))}>+ Add exercise</button>
<div className="small dim" style={{ margin: '10px 2px' }}>Tip: tap 🔗 on an exercise to superset it with the one above — you’ll do them back-to-back.</div>
<button className="btn primary" onClick={() => exercisePicker(ex => exConfigSheet(ex, null, cfg => edit(x => { x.push({ id: ex.id, ...cfg }) })))}>+ Add exercise</button>
<div style={{ height: 10 }} />
<button className="btn danger" onClick={() => {
if (!confirm('Delete "' + r.name + '"?')) return
+75 -66
View File
@@ -3,7 +3,7 @@ import { useNavigate } from 'react-router-dom'
import { useStore } from '../store/useStore.js'
import { useUI } from '../store/useUI.js'
import { EXIDX } from '../lib/exercises.js'
import { effectiveRoutine, lastEntryFor, bestWeightFor, buildSets, setsDoneActive } from '../lib/history.js'
import { effectiveRoutine, lastEntryFor, bestWeightFor, buildSets, setsDoneActive, supersetUnits, unitOf } from '../lib/history.js'
import { fmtNum, fmtDate, todayISO, DAYN } from '../lib/format.js'
import { beep, vibrate } from '../lib/sound.js'
import Media from '../components/Media.jsx'
@@ -37,23 +37,47 @@ function StartChooser() {
</div>
}
/* ---------- one set row ---------- */
function SetRow({ entry, i, unit, last, onToggle, onWeight, onReps }) {
const s = entry.sets[i]
return <div className={'setrow' + (s.done ? ' done' : '')}>
<div className="n">{i + 1}</div>
<div className="step w">
<button onClick={() => onWeight(Math.max(0, (s.w || 0) - 2.5))}>−</button>
<input type="number" inputMode="decimal" value={s.w} onFocus={e => e.target.select()} onChange={e => onWeight(e.target.value === '' ? 0 : Math.max(0, parseFloat(e.target.value) || 0))} />
<button onClick={() => onWeight(Math.max(0, (s.w || 0) + 2.5))}>+</button>
/* ---------- one exercise block (used standalone or inside a superset card) ---------- */
function ExerciseBlock({ entryIdx, compact, onToggle, onWeight, onReps, onAddSet }) {
const S = useStore(s => s.S)
const entry = S.active.entries[entryIdx]
const ex = EXIDX[entry.id]
const last = lastEntryFor(S, entry.id)
const best = bestWeightFor(S, entry.id)
const hint = 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
return <>
<Media ex={ex} key={entry.id} compact={compact} />
<div className="row between" style={{ marginBottom: 6 }}>
<div style={{ fontSize: compact ? '1.05rem' : '1.2rem', fontWeight: 800, textTransform: 'capitalize', lineHeight: 1.2 }}>{ex.n}</div>
<button className="iconbtn" onClick={() => exerciseDetailSheet(ex)}>ℹ️</button>
</div>
<div className="step r">
<button onClick={() => onReps(Math.max(0, (s.r || 0) - 1))}>−</button>
<input type="number" inputMode="numeric" value={s.r} onFocus={e => e.target.select()} onChange={e => onReps(e.target.value === '' ? 0 : Math.max(0, Math.round(parseFloat(e.target.value) || 0)))} />
<button onClick={() => onReps(Math.max(0, (s.r || 0) + 1))}>+</button>
<div className="row" style={{ gap: 6, flexWrap: 'wrap', marginBottom: 8 }}>
<span className="tag">{ex.tg || ex.bp}</span><span className="tag">{ex.eq}</span>
{best > 0 && <span className="tag">Best: {fmtNum(best)} {S.unit}</span>}
</div>
<button className={'ck' + (s.done ? ' on' : '')} onClick={onToggle}><svg viewBox="0 0 24 24"><path d="m4.5 12.5 5 5 10-11" /></svg></button>
</div>
{last && <div className="small dim" style={{ marginBottom: 4 }}>Last time ({fmtDate(last.d)}): {last.sets.map(s => fmtNum(s.w) + '×' + s.r).join(', ')}</div>}
{hint && <button className="tag acc" style={{ border: 'none' }} onClick={() => onWeight(-1, hint)}>💡 Last time you hit all reps — try {fmtNum(hint)} {S.unit}</button>}
<div className="card" style={{ marginTop: 10, marginBottom: 0 }}>
<div className="sethead"><span className="n-sp" /><span className="w-sp">Weight ({S.unit})</span><span className="r-sp">Reps</span><span className="ck-sp" /></div>
{entry.sets.map((s, i) => <div key={i} className={'setrow' + (s.done ? ' done' : '')}>
<div className="n">{i + 1}</div>
<div className="step w">
<button onClick={() => onWeight(i, Math.max(0, (s.w || 0) - 2.5))}>−</button>
<input type="number" inputMode="decimal" value={s.w} onFocus={e => e.target.select()} onChange={e => onWeight(i, e.target.value === '' ? 0 : Math.max(0, parseFloat(e.target.value) || 0))} />
<button onClick={() => onWeight(i, Math.max(0, (s.w || 0) + 2.5))}>+</button>
</div>
<div className="step r">
<button onClick={() => onReps(i, Math.max(0, (s.r || 0) - 1))}>−</button>
<input type="number" inputMode="numeric" value={s.r} onFocus={e => e.target.select()} onChange={e => onReps(i, e.target.value === '' ? 0 : Math.max(0, Math.round(parseFloat(e.target.value) || 0)))} />
<button onClick={() => onReps(i, Math.max(0, (s.r || 0) + 1))}>+</button>
</div>
<button className={'ck' + (s.done ? ' on' : '')} onClick={() => onToggle(i)}><svg viewBox="0 0 24 24"><path d="m4.5 12.5 5 5 10-11" /></svg></button>
</div>)}
<div style={{ height: 8 }} />
<button className="btn sm" onClick={onAddSet}>+ Add set</button>
</div>
</>
}
/* ---------- active workout ---------- */
@@ -65,45 +89,39 @@ function ActiveWorkout() {
const A = S.active
const [elapsed, setElapsed] = useState('0:00')
const cur = Math.min(A.cur, Math.max(0, A.entries.length - 1))
const entry = A.entries[cur]
useEffect(() => {
const tick = () => {
const sec = Math.floor((Date.now() - A.start) / 1000)
setElapsed(Math.floor(sec / 60) + ':' + String(sec % 60).padStart(2, '0'))
}
const tick = () => { const sec = Math.floor((Date.now() - A.start) / 1000); setElapsed(Math.floor(sec / 60) + ':' + String(sec % 60).padStart(2, '0')) }
tick(); const iv = setInterval(tick, 1000); return () => clearInterval(iv)
}, [A.start])
const units = supersetUnits(A.entries)
const cur = Math.min(A.cur, Math.max(0, A.entries.length - 1))
const unit = A.entries.length ? unitOf(units, cur) : []
const unitIdx = units.findIndex(u => u === unit)
const isSuperset = unit.length > 1
const total = A.entries.reduce((n, e) => n + e.sets.length, 0)
const done = setsDoneActive(A)
const ex = entry ? EXIDX[entry.id] : null
const last = entry ? lastEntryFor(S, entry.id) : null
const best = entry ? bestWeightFor(S, entry.id) : 0
const hint = last && entry && 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 mutEntry = fn => update(s => { fn(s.active.entries[cur]) }, true)
const mutEntry = (idx, fn) => update(s => { fn(s.active.entries[idx]) }, true)
const setWeight = (idx, i, v) => { if (i === -1) mutEntry(idx, e => e.sets.forEach(s => { if (!s.done) s.w = v })); else mutEntry(idx, e => { e.sets[i].w = v }) }
const setReps = (idx, i, v) => mutEntry(idx, e => { e.sets[i].r = v })
const addSet = idx => mutEntry(idx, e => { const l = e.sets[e.sets.length - 1]; e.sets.push({ w: l ? l.w : 0, r: l ? l.r : e.target.reps, done: false }) })
const toggle = i => {
const toggle = (idx, i) => {
let askTop = false
mutEntry(e => {
mutEntry(idx, e => {
e.sets[i].done = !e.sets[i].done
if (e.sets[i].done) {
beep(S.sound, 1040, 0.12); vibrate(30)
const allDone = e.sets.every(x => x.done)
const isLastEx = cur >= A.entries.length - 1
if (!allDone) startRest(S.restSec)
else { stopRest(); useUI.getState().toast(isLastEx ? 'All sets done — Finish 🏁' : 'Exercise done ✓') }
if (allDone && !e.asked) { e.asked = true; askTop = true }
const isLastExInUnit = idx === unit[unit.length - 1]
const unitDone = unit.every(ui => (ui === idx ? e : A.entries[ui]).sets.every(x => x.done))
if (isLastExInUnit && !unitDone) startRest(S.restSec)
else if (unitDone) stopRest()
if (e.sets.every(x => x.done) && !e.asked) { e.asked = true; askTop = true }
}
})
if (askTop) {
const e2 = S.active.entries[cur]
const maxW = Math.max(0, ...entry.sets.map(x => x.w || 0), entry.target.weight || 0, (S.exWeights[entry.id] || {}).w || 0)
if (maxW > 0) topWeightSheet(entry)
}
if (askTop) topWeightSheet(idx)
}
return <div className="narrow">
@@ -114,35 +132,26 @@ function ActiveWorkout() {
</div>
<div className="wprog"><i style={{ width: (total ? done / total * 100 : 0) + '%' }} /></div>
{entry ? <>
<div className="muted small" style={{ marginBottom: 6 }}>Exercise {cur + 1} / {A.entries.length}</div>
<Media ex={ex} key={entry.id} />
<div className="row between" style={{ marginBottom: 6 }}>
<div style={{ fontSize: '1.2rem', fontWeight: 800, textTransform: 'capitalize', lineHeight: 1.2 }}>{ex.n}</div>
<button className="iconbtn" onClick={() => exerciseDetailSheet(ex)}>ℹ️</button>
</div>
<div className="row" style={{ gap: 6, flexWrap: 'wrap', marginBottom: 8 }}>
<span className="tag">{ex.tg || ex.bp}</span><span className="tag">{ex.eq}</span>
{best > 0 && <span className="tag">Best: {fmtNum(best)} {S.unit}</span>}
</div>
{last && <div className="small dim" style={{ marginBottom: 4 }}>Last time ({fmtDate(last.d)}): {last.sets.map(s => fmtNum(s.w) + '×' + s.r).join(', ')}</div>}
{hint && <button className="tag acc" style={{ border: 'none' }} onClick={() => { mutEntry(e => e.sets.forEach(s => { if (!s.done) s.w = hint })); useUI.getState().toast('Weights bumped to ' + fmtNum(hint) + ' ' + S.unit + ' 💪') }}>💡 Last time you hit all reps — try {fmtNum(hint)} {S.unit}</button>}
<div className="card" style={{ marginTop: 10 }}>
<div className="sethead"><span className="n-sp" /><span className="w-sp">Weight ({S.unit})</span><span className="r-sp">Reps</span><span className="ck-sp" /></div>
{entry.sets.map((s, i) => <SetRow key={i} entry={entry} i={i} unit={S.unit} last={last}
onToggle={() => toggle(i)}
onWeight={v => mutEntry(e => { e.sets[i].w = v })}
onReps={v => mutEntry(e => { e.sets[i].r = v })} />)}
<div style={{ height: 8 }} />
<button className="btn sm" onClick={() => mutEntry(e => { const l = e.sets[e.sets.length - 1]; e.sets.push({ w: l ? l.w : 0, r: l ? l.r : e.target.reps, done: false }) })}>+ Add set</button>
</div>
{A.entries.length ? <>
<div className="muted small" style={{ marginBottom: 6 }}>{isSuperset ? `Superset ${unitIdx + 1} / ${units.length}` : `Exercise ${unitIdx + 1} / ${units.length}`}</div>
{isSuperset ? (
<div className="ss-card">
<div className="ss-hd">🔗 Superset · do these back-to-back, rest after both</div>
{unit.map((idx, k) => <div key={idx} className="ss-ex">
{k > 0 && <div className="ss-amp">+</div>}
<ExerciseBlock entryIdx={idx} compact
onToggle={i => toggle(idx, i)} onWeight={(i, v) => setWeight(idx, i, v)} onReps={(i, v) => setReps(idx, i, v)} onAddSet={() => addSet(idx)} />
</div>)}
</div>
) : (
<ExerciseBlock entryIdx={cur} onToggle={i => toggle(cur, i)} onWeight={(i, v) => setWeight(cur, i, v)} onReps={(i, v) => setReps(cur, i, v)} onAddSet={() => addSet(cur)} />
)}
</> : <div className="empty"><div className="ico">🎯</div>Freestyle workout — add your first exercise.</div>}
<div style={{ height: 12 }} />
<div className="row">
<button className="btn" disabled={cur === 0 || !entry} onClick={() => update(s => { s.active.cur = Math.max(0, cur - 1) })}>‹ Prev</button>
<button className="btn" disabled={cur >= A.entries.length - 1} onClick={() => update(s => { s.active.cur = Math.min(s.active.entries.length - 1, cur + 1) })}>Next ›</button>
<button className="btn" disabled={unitIdx <= 0} onClick={() => update(s => { s.active.cur = units[unitIdx - 1][0] })}>‹ Prev</button>
<button className="btn" disabled={unitIdx < 0 || unitIdx >= units.length - 1} onClick={() => update(s => { s.active.cur = units[unitIdx + 1][0] })}>Next ›</button>
</div>
<div style={{ height: 10 }} />
<button className="btn" onClick={() => exercisePicker(ex => exConfigSheet(ex, null, cfg => update(s => {