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.

Settings search: an old query no longer greets you when Settings is opened again

Duarte Santosf00bd3ea4e4e
diff --git a/frontend/src/views/Settings.jsx b/frontend/src/views/Settings.jsx
index c3fb49b93..8638fe490 100644
--- a/frontend/src/views/Settings.jsx
+++ b/frontend/src/views/Settings.jsx
@@ -45,8 +45,10 @@ import { PAGES, ROOT_GROUPS, pageVisible, searchSettings, pageTrail } from './se
reads. `page` is the open page (null for the root), `find` a row a search hit asked for, which
is scrolled to and flashed. */
-// What was typed into the search, kept while you step into a hit and back out again.
-let lastQuery = ''
+// What was typed into the search, kept while you step into a hit and back out again: tied to
+// the history entry it was typed on, so Back (in the app, the browser's or Android's) brings the
+// results back, while Settings opened afresh from anywhere (the gear, a tab) starts at the root.
+let lastQuery = { q: '', key: null }
// The route: /settings and /settings/<page>. An unknown page, or one this device does not have
// (the Coach page off the phone app), goes back to the root.
@@ -834,15 +836,17 @@ export default function Settings({ page = null, find = null }) {
/* ---------------- the root ---------------- */
return <SettingsRoot ctx={ctx} preview={preview} open={open} user={user} sync={sync}
- home={() => { lastQuery = ''; nav('/home') }} go={(hit) => {
+ home={() => nav('/home')} go={(hit) => {
if (hit.page === 'coach') { nav('/coach/setup'); return }
nav('/settings/' + hit.page, hit.isPage ? undefined : { state: { find: hit.title } })
}} />
}
function SettingsRoot({ ctx, preview, open, user, sync, home, go }) {
- const [q, setQ] = useState(lastQuery)
- const set = v => { lastQuery = v; setQ(v) }
+ // The router's key for this history entry (what useLocation().key reads), taken once.
+ const [key] = useState(() => window.history.state?.key || null)
+ const [q, setQ] = useState(() => (key && lastQuery.key === key ? lastQuery.q : ''))
+ const set = v => { lastQuery = { q: v, key }; setQ(v) }
const hits = q.trim() ? searchSettings(q, ctx) : null
// The account card: who this is, and the one line that matters about it.
const acct = DEMO ? { title: t('Demo'), sub: t('Example data, only in this browser.') }
diff --git a/frontend/src/views/settings-pages.test.jsx b/frontend/src/views/settings-pages.test.jsx
index f5cc46f63..5aa249489 100644
--- a/frontend/src/views/settings-pages.test.jsx
+++ b/frontend/src/views/settings-pages.test.jsx
@@ -142,6 +142,29 @@ describe('the root', () => {
})
})
+describe('the search query', () => {
+ const type = v => act(() => {
+ const input = host.querySelector('.sp-search input')
+ Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value').set.call(input, v)
+ input.dispatchEvent(new Event('input', { bubbles: true }))
+ })
+ afterEach(() => window.history.replaceState(null, ''))
+ it('comes back with Back to the same entry, and is gone when Settings is opened afresh', () => {
+ window.history.replaceState({ key: 'root1', idx: 1 }, '')
+ mount()
+ type('vib')
+ act(() => root.render(null))
+ mount() // Back from the hit: same entry
+ expect(host.querySelector('.sp-search input').value).toBe('vib')
+ expect(titles()).toContain('Vibrate')
+ act(() => root.render(null))
+ window.history.replaceState({ key: 'root2', idx: 3 }, '') // the gear, a tab, Android Back then the gear
+ mount()
+ expect(host.querySelector('.sp-search input').value).toBe('')
+ expect(titles()).toContain('Timer alerts')
+ })
+})
+
describe('the pages', () => {
it('each search entry is a row on its page (guest in a browser)', () => {
const byPage = {}