Repository navigation
Start Session form keeps walkthrough values after the session ends instead of resetting to defaults #1650
Description
The Start Session page resets its form values to the defaults in an effect that fires when a session ends. That reset never reaches the form if the page was mounted with navigation state (location.state.initialValues) while a session was active. When the session then ends, the form comes back pre-filled with the navigation-state values instead of the defaults. The only producer of that state is the walkthrough (START_SESSION_DATA: subject ID 123, custom ID, in-person). Its history entry keeps the state, so one way to hit this is to go Back in the browser to that entry while a real session is running, then end the session. The user then sees a start-session form pre-filled with the walkthrough's demo subject.
Where
apps/web/src/routes/_app/session/start-session.tsx:27-41,50-61
const [initialValues, setInitialValues] = useState(location.state?.initialValues ?? defaultInitialValues);
useEffect(() => {
if (currentSession === null) {
setInitialValues(defaultInitialValues);
}
}, [currentSession]);
...
{currentSession === null && <StartSessionForm initialValues={initialValues} ... />}When the page mounts during an active session, initialValues holds the navigation-state values and the form is not rendered. When currentSession becomes null, StartSessionForm mounts in that same render with the stale values. The effect sets the defaults only after the mount. libui's Form reads initialValues once, on mount (useState(...) in @douglasneuroinformatics/libui src/components/Form/Form.tsx:90), and StartSessionForm has no key, so the later prop change is ignored. Ending a session from the sidebar or navbar navigates to /session/start-session, which keeps an already-mounted page mounted, so the stale state survives.
Reproduce
- Run the walkthrough past the "Start Session" step, which pushes a history entry for
/session/start-sessioncarryinginitialValues: START_SESSION_DATA(apps/web/src/providers/WalkthroughProvider.tsx:211-216), then close it. - Start a real session.
- Use the browser's Back button to return to that history entry (the page mounts with the walkthrough state while the session is active).
- End the session from the sidebar.
Actual: the Start Session form shows subject ID 123 and the walkthrough's other values.
Expected: the form shows the defaults (sessionType: 'IN_PERSON', the group's default identification method, no subject ID).
Tests
apps/web/src/__tests__/start-session-route.test.tsx, test should mount the form with the defaults once the session ends, not the values the walkthrough supplied (line 194), currently it.fails with a "Known defect" comment. When fixing, change it to a plain it and remove the comment. The test mounts with walkthrough state and an active session, ends the session, and checks the initialValues of the first StartSessionForm render afterwards.
Suggested fix
Reset initialValues while the form is unmounted, that is, when currentSession is non-null rather than when it becomes null, so the next mount reads the defaults. Alternatively, give StartSessionForm a key that changes with the reset.
Found while raising unit test coverage in #1632.
Activity