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.

Coach: a reload on the intake no longer sends you home

Duarte Santosca5fa388a2fa
diff --git a/frontend/src/lib/use-coach-gate.js b/frontend/src/lib/use-coach-gate.js
new file mode 100644
index 000000000..76143ec1a
--- /dev/null
+++ b/frontend/src/lib/use-coach-gate.js
@@ -0,0 +1,28 @@
+// Whether the Coach is here, once that is known. A cold start straight on #/coach or
+// #/coach/intake (a reload, a bookmark) reads the store before it has the answer: /api/config is
+// fetched at boot without a session, so it carries no `coach` key, and setUser asks again only
+// afterwards (useStore setUser). Deciding then sent everyone home (QA v1.4.2). Here the screens
+// wait for an answer made for this session, asking for one if it is missing, and decide after.
+import { useEffect, useState } from 'react'
+import { useStore } from '../store/useStore.js'
+import { coachAvailable } from './coach.js'
+import { DEMO } from './demo.js'
+import { MOBILE } from './mobile.js'
+
+/** `{ known, ok }`: `ok` the Coach is available; `known` the answer is final for this session. */
+export function useCoachGate() {
+ const config = useStore(s => s.config)
+ const user = useStore(s => s.user)
+ const coachMode = useStore(s => s.coachLocal?.mode)
+ const ready = useStore(s => s.ready)
+ const ok = coachAvailable(config, user, { demo: DEMO, mobile: MOBILE, coachMode })
+ const fresh = !user || !!(config && 'coach' in config)
+ const [asked, setAsked] = useState(false)
+ useEffect(() => {
+ if (!ready || ok || fresh || asked) return
+ let gone = false
+ Promise.resolve(useStore.getState().refreshConfig?.()).catch(() => {}).finally(() => { if (!gone) setAsked(true) })
+ return () => { gone = true }
+ }, [ready, ok, fresh, asked])
+ return { known: ok || (ready && (fresh || asked)), ok }
+}
diff --git a/frontend/src/views/CoachChat.jsx b/frontend/src/views/CoachChat.jsx
index 5c7466321..20d32ca6a 100644
--- a/frontend/src/views/CoachChat.jsx
+++ b/frontend/src/views/CoachChat.jsx
@@ -32,6 +32,7 @@ import {
import { insightsFor, sessionInsights } from '../lib/coach-insights.js'
import { useCoachStatus, requestReview, requestDebrief, requestPlan, refinePlan, resolvePending, cohortStats, setCohortShare, jobErrorText, awaitedJob, settleAwaited, JOB_ERRORS, ensureAiConsent, isDeclined } from '../lib/coach-api.js'
import { AiConsentSection } from '../components/CoachConsentSheet.jsx'
+import { useCoachGate } from '../lib/use-coach-gate.js'
import { confirmSheet } from '../sheets.jsx'
import Icon from '../components/Icon.jsx'
import { glyphOf } from '../lib/glyphs.js'
@@ -58,15 +59,17 @@ export default function CoachChat() {
const prevJob = useRef(null)
const coachMode = coachLocal?.mode
- const ok = coachAvailable(config, user, { demo: DEMO, mobile: MOBILE, coachMode })
+ const gate = useCoachGate()
+ const ok = gate.ok
const ready = ok && hasConsent(S) && !!S.coach?.profile
// Not before the store has loaded: a cold start straight on #/coach would otherwise read an
- // empty state, decide there is no consent, and bounce a consenting user into the intake.
+ // empty state, decide there is no consent, and bounce a consenting user into the intake. Nor
+ // before the server said for this session whether it has a Coach (lib/use-coach-gate.js).
useEffect(() => {
- if (!storeReady) return
+ if (!storeReady || !gate.known) return
if (!ok) nav('/home', { replace: true })
else if (!ready) nav('/coach/intake', { replace: true })
- }, [storeReady, ok, ready])
+ }, [storeReady, gate.known, ok, ready])
// A job that ends is either a proposal, "nothing to change", or a failure. The server tells
// the client none of that directly — the job simply stops appearing — so the transition is
diff --git a/frontend/src/views/CoachIntake.jsx b/frontend/src/views/CoachIntake.jsx
index f5a429536..7954b9bf7 100644
--- a/frontend/src/views/CoachIntake.jsx
+++ b/frontend/src/views/CoachIntake.jsx
@@ -19,6 +19,7 @@ import { emptyCoach, coachAvailable, hasConsent, CONSENT_VERSION, CATEGORY_TEXT,
import { requestPlan, disclosure, JOB_ERRORS, isDeclined, coachVia } from '../lib/coach-api.js'
import { consentTarget, allowAi } from '../lib/coach-consent.js'
import { DEMO } from '../lib/demo.js'
+import { useCoachGate } from '../lib/use-coach-gate.js'
import { MOBILE } from '../lib/mobile.js'
import { activeProfile } from '../lib/equipment.js'
import Icon from '../components/Icon.jsx'
@@ -80,7 +81,9 @@ export default function CoachIntake() {
}))
const set = patch => setP(v => ({ ...v, ...patch }))
- if (!coachAvailable(config, user, { demo: DEMO, mobile: MOBILE, coachMode })) { nav('/home', { replace: true }); return null }
+ const gate = useCoachGate()
+ useEffect(() => { if (gate.known && !gate.ok) nav('/home', { replace: true }) }, [gate.known, gate.ok])
+ if (!gate.ok) return null
const key = STEPS[step]
const last = step === STEPS.length - 1
diff --git a/frontend/src/views/CoachIntake.test.jsx b/frontend/src/views/CoachIntake.test.jsx
index cdf202bfa..f4fdc533d 100644
--- a/frontend/src/views/CoachIntake.test.jsx
+++ b/frontend/src/views/CoachIntake.test.jsx
@@ -28,6 +28,8 @@ const mocks = vi.hoisted(() => {
user: state.user,
config: state.config,
coachLocal: null,
+ ready: true,
+ refreshConfig: async () => state.config,
update: mut => mut(state.S),
})
return state
@@ -128,6 +130,17 @@ describe('CoachIntake — who gets in', () => {
})
})
+// A cold start (a reload on #/coach/intake): the boot's /api/config was read before the session
+// and has no `coach` key yet. The screen waits for the answer for this session, not home.
+describe('CoachIntake — a cold start', () => {
+ it('asks again and stays while the config is the one from before the session', async () => {
+ mocks.config = { allow_guest: true }
+ mount()
+ expect(mocks.nav).not.toHaveBeenCalledWith('/home', { replace: true })
+ expect(host.textContent).toBe('')
+ })
+})
+
describe('CoachIntake — the consent screen', () => {
it('is the first screen on a fresh profile, and is not one of the progress dots', () => {
mount()