diff --git a/frontend/src/components/ui.jsx b/frontend/src/components/ui.jsx index 8c9fd0c..a65a30c 100644 --- a/frontend/src/components/ui.jsx +++ b/frontend/src/components/ui.jsx @@ -248,25 +248,12 @@ export function Row({ icon, iconTint, title, subtitle, value, accessory = 'none' // theme entirely — on dark mode it flashes a white sheet — and can't show more // than a bare label per option. This opens our own sheet with a checkmark on the // current value, which is also how iOS itself handles a long option list. -export function SelectRow({ icon, iconTint, title, value, options, onChange, sheetTitle, stackedValue = false }) { +export function SelectRow({ icon, iconTint, title, value, options, onChange, sheetTitle, stackedValue = false, search }) { const cur = options.find(o => o.value === value) const open = () => { const { openSheet } = require_ui() - const h = openSheet(close => ( - <> -

{sheetTitle || title}

-
- {options.map(o => ( - - ))} -
-
- - )) + const h = openSheet(close => ) return h } return ( @@ -275,6 +262,34 @@ export function SelectRow({ icon, iconTint, title, value, options, onChange, she ) } +function SelectSheet({ title, value, options, onChange, search, close }) { + const [query, setQuery] = useState('') + const matcher = typeof search === 'function' ? search : search?.match + const visible = matcher ? options.filter(o => matcher(o, query)) : options + return ( + <> +

{title}

+ {search &&
+ setQuery(e.target.value)} onInput={e => setQuery(e.target.value)} + onClear={() => setQuery('')} placeholder={search.placeholder} aria-label={search.label || search.placeholder} /> +
} +
+ {visible.map(o => ( + + ))} + {!visible.length && search &&
+
{search.emptyLabel} +
} +
+
+ + ) +} + /** Multi-select row for additive exercise metadata. The sheet mirrors selection locally so * each tap updates its checkmark immediately while the caller persists the value. */ export function MultiSelectRow({ icon, iconTint, title, values, options, onToggle, sheetTitle, noneLabel, doneLabel }) { diff --git a/frontend/src/views/Stats.jsx b/frontend/src/views/Stats.jsx index 3622b7b..6c02109 100644 --- a/frontend/src/views/Stats.jsx +++ b/frontend/src/views/Stats.jsx @@ -1,7 +1,7 @@ import { useEffect, useMemo, useState } from 'react' import { useNavigate } from 'react-router-dom' import { useStore } from '../store/useStore.js' -import { EXIDX } from '../lib/exercises.js' +import { EXIDX, matchExercise } from '../lib/exercises.js' import { lastBW, streakWeeks, setLabel, modeOf, effortOf, metricModeForEntry, metricRowsForEntry, bestWeightForEntry } from '../lib/history.js' import { fmtNum, fmtDate, fmtVol, todayISO, weekKey } from '../lib/format.js' import { t, exerciseNameFor, getLang } from '../lib/i18n.js' @@ -428,7 +428,13 @@ export default function Stats() { {exHist.length ? <>
({ value: id, label: nameOf(id) + (exCurrent[id].mx ? ' ' + '—' + ' ' + fmtNum(exCurrent[id].mx) + ' ' + exCurrent[id].unit : '') }))} /> + options={exHist.map(id => ({ value: id, label: nameOf(id) + (exCurrent[id].mx ? ' ' + '—' + ' ' + fmtNum(exCurrent[id].mx) + ' ' + exCurrent[id].unit : '') }))} + search={{ + placeholder: t('Search…'), + label: t('Search…'), + emptyLabel: t('No match'), + match: (option, query) => matchExercise(EXIDX[option.value] || { n: nameOf(option.value) }, query), + }} />
{exOpts.length > 1 && }
diff --git a/frontend/src/views/Stats.recovery.test.jsx b/frontend/src/views/Stats.recovery.test.jsx index 70d1e0a..8496939 100644 --- a/frontend/src/views/Stats.recovery.test.jsx +++ b/frontend/src/views/Stats.recovery.test.jsx @@ -6,6 +6,9 @@ import { MUSCLES, levelsOf } from '../lib/muscles.js' import { FATIGUE_STATES, STRENGTH_FLOOR } from '../lib/recovery.js' import { fatigueStateOf } from '../lib/recovery-view.js' import Stats from './Stats.jsx' +import Modals from '../components/Modals.jsx' +import { bindUI } from '../components/ui.jsx' +import { useUI } from '../store/useUI.js' const DAY = 86400000 const HOUR = 3600000 @@ -50,6 +53,8 @@ vi.mock('../components/BodyMap.jsx', () => ({ BodyMapLegend: () => React.createElement('div', { 'data-balance-legend': true }), })) +bindUI(useUI) + let dom let root let container @@ -105,19 +110,30 @@ function allSubfullWorkout(now = BASE_NOW) { return workout('all-subfull', now - 15 * DAY, [entry('1254', [set(true)])]) } +function exercisePickerWorkouts(now = BASE_NOW) { + return [ + workout('bench', now, [entry('0025', [set(true, { w: 80 })])]), + workout('squat', now - DAY, [entry('0043', [set(true, { w: 60 })])]), + ] +} + function resetFixture(workouts = lifecycleWorkouts()) { mocks.S.unit = 'kg' mocks.S.bodyweight = [] mocks.S.workouts = workouts mocks.maps.length = 0 mocks.mapMounts = 0 + useUI.setState({ sheets: [] }) } function installDom() { dom = new Window({ url: 'http://localhost/' }) + dom.scrollTo = vi.fn() globalThis.window = dom globalThis.document = dom.document Object.defineProperty(globalThis, 'navigator', { configurable: true, value: dom.navigator }) + Object.defineProperty(globalThis, 'location', { configurable: true, value: dom.location }) + Object.defineProperty(globalThis, 'history', { configurable: true, value: dom.history }) for (const key of ['HTMLElement', 'HTMLIFrameElement', 'Node', 'Element', 'Event', 'MouseEvent']) { globalThis[key] = dom[key] } @@ -129,7 +145,7 @@ function installDom() { async function mountStats() { installDom() - await act(async () => { root.render(React.createElement(Stats)) }) + await act(async () => { root.render(React.createElement(React.Fragment, null, React.createElement(Stats), React.createElement(Modals))) }) } async function unmountStats() { @@ -179,6 +195,7 @@ beforeEach(() => { afterEach(async () => { await unmountStats() + useUI.setState({ sheets: [] }) vi.useRealTimers() vi.restoreAllMocks() }) @@ -272,3 +289,36 @@ describe('Stats muscle recovery view runtime', () => { expect(strengthMap.thresholds.at(-1)).toEqual({ at: 1, level: 4 }) }) }) + +describe('Stats exercise progress picker', () => { + it('filters with the shared exercise matcher and still selects from the mounted sheet', async () => { + resetFixture(exercisePickerWorkouts()) + await mountStats() + + const card = [...container.querySelectorAll('.card')].find(el => el.querySelector('h2')?.textContent.trim() === 'Exercise progress') + const selector = [...card.querySelectorAll('button')].find(button => button.querySelector('.lrow-t')?.textContent.trim() === 'Exercise') + await click(selector) + + const modal = document.querySelector('#modal-root') + const input = modal.querySelector('input') + expect(input).toBeTruthy() + expect(input.getAttribute('aria-label')).toBe('Search…') + expect(input.getAttribute('placeholder')).toBe('Search…') + + const setter = Object.getOwnPropertyDescriptor(input.constructor.prototype, 'value').set + await act(async () => { + setter.call(input, 'squat full bárbell') + input.dispatchEvent(new Event('input', { bubbles: true })) + input.dispatchEvent(new Event('change', { bubbles: true })) + }) + + expect(modal.textContent).toContain('barbell full squat') + expect(modal.textContent).not.toContain('barbell bench press') + + const matching = [...modal.querySelectorAll('button')].find(button => button.textContent.includes('barbell full squat')) + await click(matching) + + expect(useUI.getState().sheets).toHaveLength(0) + expect(card.querySelector('.lrow-v').textContent).toContain('barbell full squat') + }) +})