feat: add Stats exercise progress picker search
This commit is contained in:
1 parent
5314b342ff
commit
283d897762
3 files changed
+90
-19
No files matched your search
@@ -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 => (
|
||||
<>
|
||||
<h3>{sheetTitle || title}</h3>
|
||||
<div className="sect-b">
|
||||
{options.map(o => (
|
||||
<button key={o.value} className="lrow tap" onClick={() => { close(); onChange(o.value) }}>
|
||||
<span className="lrow-m"><span className="lrow-t">{o.label}</span>
|
||||
{o.subtitle && <span className="lrow-s">{o.subtitle}</span>}</span>
|
||||
{o.value === value && <Icon name="check" className="lrow-k" />}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
<div style={{ height: 8 }} />
|
||||
</>
|
||||
))
|
||||
const h = openSheet(close => <SelectSheet title={sheetTitle || title} value={value} options={options}
|
||||
onChange={onChange} search={search} close={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 (
|
||||
<>
|
||||
<h3>{title}</h3>
|
||||
{search && <div style={{ marginBottom: 10 }}>
|
||||
<SearchField value={query} onChange={e => setQuery(e.target.value)} onInput={e => setQuery(e.target.value)}
|
||||
onClear={() => setQuery('')} placeholder={search.placeholder} aria-label={search.label || search.placeholder} />
|
||||
</div>}
|
||||
<div className="sect-b">
|
||||
{visible.map(o => (
|
||||
<button key={o.value} className="lrow tap" onClick={() => { close(); onChange(o.value) }}>
|
||||
<span className="lrow-m"><span className="lrow-t">{o.label}</span>
|
||||
{o.subtitle && <span className="lrow-s">{o.subtitle}</span>}</span>
|
||||
{o.value === value && <Icon name="check" className="lrow-k" />}
|
||||
</button>
|
||||
))}
|
||||
{!visible.length && search && <div className="empty">
|
||||
<div className="ico"><Icon name="magnifier" /></div>{search.emptyLabel}
|
||||
</div>}
|
||||
</div>
|
||||
<div style={{ height: 8 }} />
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
/** 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 }) {
|
||||
|
||||
@@ -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 ? <>
|
||||
<div className="sect-b" style={{ marginBottom: 10 }}>
|
||||
<SelectRow title={t('Exercise')} sheetTitle={t('Exercise progress')} value={curEx} onChange={setExId} stackedValue
|
||||
options={exHist.map(id => ({ 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),
|
||||
}} />
|
||||
</div>
|
||||
{exOpts.length > 1 && <Segmented className="seg-range" value={onEff ? 'effort' : onE1 ? 'e1rm' : 'top'} onChange={setExMetric} options={exOpts} />}
|
||||
<div className="chart">
|
||||
|
||||
@@ -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')
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user