This commit is contained in:
Duarte Santos committed 2026-08-30 13:30:59 +02:00
commit e0241b804e
23 files changed
+2157 -6

No files matched your search

+198
View File
@@ -17,6 +17,7 @@ import { glyphOf, GLYPH_GROUPS, DEFAULT_GLYPH } from './lib/glyphs.js'
import BodyMap from './components/BodyMap.jsx'
import { exerciseMuscleSnapshot, loadOfWorkouts, MUSCLES, MUSCLE_NAME, normalizeMuscleGroups, hasExplicitMuscleMetadata } 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 } from './lib/plan-share.js'
import { estimate1RM, best1RM, is1RMRecord, REP_CAP } from './lib/onerm.js'
import { nextPrescription, applyPrescription, policyFor, defaultIncrement, POLICIES_FOR, POLICY_NAME, POLICY_DESC, MAX_BW_SETS } from './lib/progression.js'
@@ -239,6 +240,203 @@ export function importFromApp(file, onDone) {
rd.readAsText(file)
}
/* ============================ import from Hevy API ============================ */
// The key lives in React state for this sheet only — dismissed with the sheet, never
// written to the store / localStorage / the server. After a successful fetch the user
// picks workouts and/or weigh-ins before anything is merged.
export function importFromHevy() {
ui().openSheet(close => <HevyImportSheet close={close} />)
}
function hevyProgressLabel(p) {
if (!p) return t('Fetching from Hevy…')
if (p.stage === 'templates') return t('Fetching exercises… ({0}/{1})', p.page, p.pageCount)
if (p.stage === 'workouts') return t('Fetching workouts… ({0}/{1})', p.page, p.pageCount)
if (p.stage === 'routines') return t('Fetching routines… ({0}/{1})', p.page, p.pageCount)
if (p.stage === 'body') return t('Fetching weigh-ins… ({0}/{1})', p.page, p.pageCount)
if (p.stage === 'parse') return t('Matching exercises…')
return t('Fetching from Hevy…')
}
function HevyImportSheet({ close }) {
const st = useStore(s => s.S)
const [apiKey, setApiKey] = useState('')
const [busy, setBusy] = useState(false)
const [progress, setProgress] = useState(null)
const [payload, setPayload] = useState(null) // { workouts, routines, bodyweight }
const [wantWorkouts, setWantWorkouts] = useState(true)
const [wantRoutines, setWantRoutines] = useState(true)
const [wantBody, setWantBody] = useState(true)
const keyRef = useRef(null)
// Drop the key from memory when the sheet goes away (unmount or successful import).
useEffect(() => () => { setApiKey('') }, [])
const wipeKey = () => { setApiKey(''); if (keyRef.current) keyRef.current.value = '' }
const fetchAccount = async () => {
const key = apiKey.trim()
if (!key) { toast(t('Paste your Hevy API key first')); return }
setBusy(true)
setProgress({ stage: 'templates', page: 1, pageCount: 1 })
setPayload(null)
try {
const data = await importHevyData(key, { unit: st.unit, onProgress: setProgress })
wipeKey()
const empty = !data.workouts.workouts.length && !data.routines.routines.length && !data.bodyweight.bodyweight.length
if (empty) {
toast(t('Nothing to import from Hevy'))
return
}
setWantWorkouts(!!data.workouts.workouts.length)
setWantRoutines(!!data.routines.routines.length)
setWantBody(!!data.bodyweight.bodyweight.length)
setPayload(data)
} catch (e) {
if (e instanceof HevyApiError && e.message === 'auth') toast(t('That Hevy API key was refused'))
else if (e instanceof HevyApiError && e.message === 'empty') toast(t('Paste your Hevy API key first'))
else toast(t('Could not reach Hevy — check the key and try again'))
} finally {
setBusy(false)
setProgress(null)
}
}
const doImport = () => {
if (!payload) return
const parts = []
let addedW = 0, addedR = 0, addedB = 0
update(s => {
if (wantWorkouts && payload.workouts.workouts.length) {
const res = mergeImport(s, payload.workouts)
addedW = res.added
parts.push(t('{0} workouts imported', res.added))
}
if (wantRoutines && payload.routines.routines.length) {
const res = mergeHevyRoutines(s, payload.routines)
addedR = res.added
parts.push(t('{0} routines imported', res.added))
}
if (wantBody && payload.bodyweight.bodyweight.length) {
const res = mergeImport(s, payload.bodyweight)
addedB = res.added
parts.push(t('{0} weigh-ins imported', res.added))
}
})
close()
if (!addedW && !addedR && !addedB) toast(t('Nothing new to import'))
else toast(parts.join(' · '))
}
if (!payload) {
return <>
<h3>{t('Import from Hevy')}</h3>
<div className="muted small" style={{ marginBottom: 14, lineHeight: 1.5 }}>
{t('Pull your history with a Hevy Pro API key. The key is only used for this import and is not saved.')}
</div>
<label className="small dim" style={{ display: 'block', marginBottom: 6 }}>{t('Hevy API key')}</label>
<TextField
ref={keyRef}
type="password"
autoComplete="off"
spellCheck={false}
placeholder="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
value={apiKey}
disabled={busy}
onChange={e => setApiKey(e.target.value)}
onKeyDown={e => { if (e.key === 'Enter' && !busy) fetchAccount() }}
/>
<div className="small" style={{ margin: '10px 0 16px', lineHeight: 1.45 }}>
<a href={HEVY_DEV_SETTINGS} target="_blank" rel="noopener noreferrer">{t('Get your API key')}</a>
<span className="dim"> — {t('Hevy → Settings → Developer')}</span>
</div>
{busy && <div className="small dim" style={{ marginBottom: 12 }}>{hevyProgressLabel(progress)}</div>}
<Button variant="primary" onClick={fetchAccount} disabled={busy || !apiKey.trim()}>
{busy ? t('Fetching from Hevy…') : t('Fetch from Hevy')}
</Button>
<div style={{ height: 8 }} />
<Button variant="ghost" className="dim" onClick={close} disabled={busy}>{t('Cancel')}</Button>
</>
}
const w = payload.workouts
const r = payload.routines
const b = payload.bodyweight
const haveW = w.workouts.filter(x => st.workouts.some(y => y.d === x.d)).length
const freshW = w.workouts.length - haveW
const haveB = b.bodyweight.filter(x => st.bodyweight.some(y => y.d === x.d)).length
const freshB = b.bodyweight.length - haveB
// Routines are always added as new copies (same as plan import).
const freshR = r.routines.length
const canImport = (wantWorkouts && freshW > 0) || (wantRoutines && freshR > 0) || (wantBody && freshB > 0)
const unmatched = [...new Set([
...(wantWorkouts ? w.unmatchedNames : []),
...(wantRoutines ? r.unmatchedNames : []),
])].sort()
return <>
<h3>{t('Import from Hevy')}</h3>
<div className="muted small" style={{ marginBottom: 12 }}>
{w.from && (w.from === w.to ? fmtDate(w.from, true) : fmtDate(w.from, true) + ' – ' + fmtDate(w.to, true))}
{!w.from && b.from && (b.from === b.to ? fmtDate(b.from, true) : fmtDate(b.from, true) + ' – ' + fmtDate(b.to, true))}
</div>
<div className="tiles" style={{ textAlign: 'left' }}>
<div className="tile"><div className="l">{t('Workouts')}</div><div className="v" style={{ fontSize: '1.1rem' }}>{w.workouts.length}</div></div>
<div className="tile"><div className="l">{t('Routines')}</div><div className="v" style={{ fontSize: '1.1rem' }}>{r.routines.length}</div></div>
<div className="tile"><div className="l">{t('Exercises matched')}</div><div className="v" style={{ fontSize: '1.1rem' }}>{w.matched + r.matched}</div></div>
<div className="tile"><div className="l">{t('Added as your own')}</div><div className="v" style={{ fontSize: '1.1rem' }}>{w.created + r.created}</div></div>
</div>
{w.workouts.length > 0 && <div className="row between" style={{ padding: '10px 2px', borderTop: '1px solid var(--sep)', gap: 12 }}>
<div>
<div className="tt" style={{ fontSize: 15 }}>{t('Import workouts')}</div>
<div className="small dim">{t('{0} new · {1} days already here', freshW, haveW)}</div>
</div>
<Switch checked={wantWorkouts} onChange={setWantWorkouts} />
</div>}
{r.routines.length > 0 && <div className="row between" style={{ padding: '10px 2px', borderTop: '1px solid var(--sep)', gap: 12 }}>
<div>
<div className="tt" style={{ fontSize: 15 }}>{t('Import routines')}</div>
<div className="small dim">{t('{0} routines · {1} exercises — added as new plans', freshR, r.exerciseCount)}</div>
</div>
<Switch checked={wantRoutines} onChange={setWantRoutines} />
</div>}
{b.bodyweight.length > 0 && <div className="row between" style={{ padding: '10px 2px', borderTop: '1px solid var(--sep)', borderBottom: '1px solid var(--sep)', gap: 12, marginBottom: 8 }}>
<div>
<div className="tt" style={{ fontSize: 15 }}>{t('Import weigh-ins')}</div>
<div className="small dim">{t('{0} new · {1} days already here', freshB, haveB)}</div>
</div>
<Switch checked={wantBody} onChange={setWantBody} />
</div>}
{!b.bodyweight.length && <div style={{ borderBottom: '1px solid var(--sep)', marginBottom: 8 }} />}
{(w.converted || r.converted) && <div className="small" style={{ color: 'var(--yellow)', marginBottom: 10 }}>
{t('Hevy stores weights in kg — they will be converted to {0}.', st.unit)}
</div>}
{wantWorkouts && (w.rirSets + w.rpeSets) > 0 && <div className="small dim" style={{ marginBottom: 10 }}>
{t(effortOf(st) === 'none'
? '{0} sets bring an {1} with them — switch on Effort per set in Settings to see it.'
: '{0} sets bring an {1} with them.',
w.rirSets || w.rpeSets, w.rirSets ? 'RIR' : 'RPE')}
</div>}
{unmatched.length > 0 && <>
<h4 className="sec">{t('Not in the library — added as your own exercises')}</h4>
<div className="mchips" style={{ marginBottom: 12 }}>
{unmatched.slice(0, 12).map(n => <span key={n} className="mchip capitalize">{n}</span>)}
{unmatched.length > 12 && <span className="mchip">+{unmatched.length - 12}</span>}
</div>
</>}
<Button variant="primary" onClick={doImport} disabled={!canImport}>
{canImport ? t('Import') : t('Nothing new to import')}
</Button>
<div style={{ height: 8 }} />
<Button variant="ghost" className="dim" onClick={() => { setPayload(null); wipeKey() }}>{t('Back')}</Button>
</>
}
/* ============================ target weight ============================ */
export function bwDeltaColor(delta, currentW) {
if (!delta) return 'var(--label-2)'