Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

Exercise search shows a live result count while a search or filter narrows the list (GitLab !31)

Duarte Santosf4c19302bd1b
diff --git a/frontend/src/index.css b/frontend/src/index.css
index cdf223e75..62f4550ea 100644
--- a/frontend/src/index.css
+++ b/frontend/src/index.css
@@ -564,6 +564,16 @@ body.chips-dragging *{cursor:inherit}
position:absolute;left:11px;top:50%;transform:translateY(-50%);z-index:1;
width:16px;height:16px;stroke:var(--label-3);fill:none;stroke-width:2.1;stroke-linecap:round;
}
+/* The live result count (Library and the exercise picker, from GitLab !31): a pill at the end
+ of the field while a search or filter narrows the list. The field makes room for it only while
+ it is there, so a long placeholder is not cut short for nothing. */
+.search.has-count .input{padding-right:62px}
+.search .search-count{
+ position:absolute;right:9px;top:50%;transform:translateY(-50%);
+ font-size:12px;font-weight:600;line-height:1.4;color:var(--label-2);
+ background:var(--surface-3);padding:2px 8px;border-radius:99px;
+ font-variant-numeric:tabular-nums;pointer-events:none;user-select:none;
+}
/* --- switch --- */
.sw{
diff --git a/frontend/src/search-count.test.jsx b/frontend/src/search-count.test.jsx
new file mode 100644
index 000000000..d52b66222
--- /dev/null
+++ b/frontend/src/search-count.test.jsx
@@ -0,0 +1,83 @@
+// @vitest-environment happy-dom
+// The live result count in the exercise search (idea: GitLab !31, sagnikpatra301): the Library
+// and the exercise picker show how many exercises are left once a search or a filter narrows
+// the list, and say it in words to a screen reader.
+import React, { act } from 'react'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+import { createRoot } from 'react-dom/client'
+import { MemoryRouter } from 'react-router-dom'
+import { DEF, useStore } from './store/useStore.js'
+import { useUI } from './store/useUI.js'
+import { exercisePicker } from './sheets.jsx'
+import Library from './views/Library.jsx'
+import { EXDB, searchExercises } from './lib/exercises.js'
+
+globalThis.IS_REACT_ACT_ENVIRONMENT = true
+const mounted = []
+function mount(node) {
+ const host = document.createElement('div')
+ document.body.appendChild(host)
+ const root = createRoot(host)
+ mounted.push(root)
+ act(() => root.render(node))
+ return host
+}
+const renderTopSheet = () => {
+ const sheet = useUI.getState().sheets.at(-1)
+ return mount(sheet.render(() => useUI.getState().closeSheet(sheet.id)))
+}
+function type(el, value) {
+ Object.getOwnPropertyDescriptor(el.constructor.prototype, 'value').set.call(el, value)
+ el.dispatchEvent(new Event('input', { bubbles: true }))
+}
+const chip = (host, text) => [...host.querySelectorAll('.chips .chip')].find(b => b.textContent.trim() === text)
+const count = host => host.querySelector('.search .search-count')
+
+beforeEach(() => {
+ useUI.setState({ sheets: [], toastMsg: '' })
+ useStore.setState({ S: structuredClone(DEF), user: null })
+ document.body.innerHTML = ''
+})
+afterEach(() => { act(() => { mounted.splice(0).forEach(root => root.unmount()) }) })
+
+// The screens run the same search over the same catalogue, so the expected numbers come from it.
+const matches = q => searchExercises(EXDB, q).length
+
+describe('exercise search result count', () => {
+ for (const [where, open] of [
+ ['Library', () => mount(<MemoryRouter><Library /></MemoryRouter>)],
+ ['exercise picker', () => { exercisePicker(vi.fn()); return renderTopSheet() }],
+ ]) {
+ it(`${where}: appears with a search, follows it, and goes when the search is cleared`, () => {
+ const host = open()
+ const input = host.querySelector('.search input')
+ expect(count(host)).toBeNull()
+ expect(host.querySelector('.search').classList.contains('has-count')).toBe(false)
+
+ act(() => type(input, 'bench press'))
+ const n = matches('bench press')
+ expect(n).toBeGreaterThan(1)
+ expect(count(host).textContent).toBe(String(n))
+ expect(count(host).getAttribute('aria-label')).toBe(`${n} exercises`)
+ expect(host.querySelector('.search').classList.contains('has-count')).toBe(true)
+
+ act(() => type(input, 'bench press dumbbell incline'))
+ expect(Number(count(host).textContent)).toBe(matches('bench press dumbbell incline'))
+
+ act(() => type(input, ''))
+ expect(count(host)).toBeNull()
+ })
+
+ it(`${where}: counts a body-part filter too, and reads "1 exercise" for one`, () => {
+ const host = open()
+ act(() => chip(host, 'neck').click())
+ const neck = EXDB.filter(e => e.bp === 'neck').length
+ expect(Number(count(host).textContent)).toBe(neck)
+ const one = EXDB.find(e => searchExercises(EXDB, e.n).length === 1)
+ act(() => chip(host, 'All').click())
+ act(() => type(host.querySelector('.search input'), one.n))
+ expect(count(host).textContent).toBe('1')
+ expect(count(host).getAttribute('aria-label')).toBe('1 exercise')
+ })
+ }
+})
diff --git a/frontend/src/sheets.jsx b/frontend/src/sheets.jsx
index d2f0333fe..7dc867dd6 100644
--- a/frontend/src/sheets.jsx
+++ b/frontend/src/sheets.jsx
@@ -938,6 +938,8 @@ function ExercisePicker({ onPick, title, close }) {
const chosenCount = Object.keys(usage).length
const favCount = (st.favEx || []).length
const special = bp === '★' || bp === '☆'
+ // The Library's live result count (GitLab !31), for the same reason: how many are left.
+ const narrowed = !!(q.trim() || bp || eqOn)
useRevealActiveChip(bpStrip, bp)
useRevealActiveChip(eqStrip, eqOn)
if (byMuscle) return <>
@@ -953,8 +955,9 @@ function ExercisePicker({ onPick, title, close }) {
</div>
{/* .picker-search is what index.css keys the keyboard-aware sheet layout on: the sheet
lifts above the keys and the search stays put while the list scrolls under it. */}
- <div className="picker-search"><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 ref={searchRef} className="input" placeholder={t('Search {0} exercises…', all.length)} value={q} onFocus={onSearchFocus} onChange={e => { setQ(e.target.value); setShown(50) }} /></div></div>
+ <div className="picker-search"><div className={'search' + (narrowed ? ' has-count' : '')}><svg viewBox="0 0 24 24"><circle cx="11" cy="11" r="7" /><path d="m21 21-4.3-4.3" /></svg>
+ <input ref={searchRef} className="input" placeholder={t('Search {0} exercises…', all.length)} value={q} onFocus={onSearchFocus} onChange={e => { setQ(e.target.value); setShown(50) }} />
+ {narrowed && <span className="search-count" role="status" aria-label={exCount(f.length)}>{fmtNum(f.length)}</span>}</div></div>
{profile && <div className="small dim row" style={{ margin: '8px 0 2px', gap: 6, alignItems: 'center' }}>
<Icon name="dumbbell" style={{ fontSize: 13 }} />
{showAll ? t('Showing all equipment') : t('Showing what you have in "{0}"', profile.name)}
diff --git a/frontend/src/views/Library.jsx b/frontend/src/views/Library.jsx
index 84ffd7dc8..7433aa6d9 100644
--- a/frontend/src/views/Library.jsx
+++ b/frontend/src/views/Library.jsx
@@ -5,7 +5,7 @@ import { EXDB, BODYPARTS, allExercises, equipmentOf, searchExercises } from '../
import { MUSCLE_NAME } from '../lib/muscles.js'
import { activeProfile, exAvailable } from '../lib/equipment.js'
import { bestWeightFor } from '../lib/history.js'
-import { fmtNum } from '../lib/format.js'
+import { fmtNum, exCount } from '../lib/format.js'
import { t, exerciseNameFor } from '../lib/i18n.js'
import { Thumb } from '../components/Media.jsx'
import { exerciseDetailSheet, addToRoutineSheet, customExSheet } from '../sheets.jsx'
@@ -33,13 +33,17 @@ export default function Library() {
const f = sortFavouritesFirst(eqOn ? eqFiltered.filter(e => e.eq === eqOn) : eqFiltered, S)
useRevealActiveChip(bpStrip, bp)
useRevealActiveChip(eqStrip, eqOn)
+ // A live count at the end of the search field while a search or filter narrows the list
+ // (idea and first version: GitLab !31) — how many are left, before scrolling to find out.
+ const narrowed = !!(q.trim() || bp || eqOn)
return <>
<div className="hdr"><div><h1>{t('Exercises')}</h1><div className="sub">{t('{0} exercises with animations', EXDB.length)}</div></div>
<Button size="sm" variant="tinted" icon="target" onClick={() => nav('/muscles')}>{t('By muscle')}</Button>
</div>
- <div className="search" style={{ marginBottom: 10 }}><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={t('Search…')} value={q} onChange={e => { setQ(e.target.value); setShown(40) }} /></div>
+ <div className={'search' + (narrowed ? ' has-count' : '')} style={{ marginBottom: 10 }}><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={t('Search…')} value={q} onChange={e => { setQ(e.target.value); setShown(40) }} />
+ {narrowed && <span className="search-count" role="status" aria-label={exCount(f.length)}>{fmtNum(f.length)}</span>}</div>
{profile && <div className="small dim row" style={{ margin: '-4px 2px 10px', gap: 6, alignItems: 'center' }}>
<Icon name="dumbbell" style={{ fontSize: 13 }} />
{showAll ? t('Showing all equipment') : t('Showing what you have in "{0}"', profile.name)}