[PR #1602] [MERGED] fix(staffml): add pickRandom to keyboard effect and pickNext dep arrays #9214

Closed
opened 2026-05-03 01:29:00 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/harvard-edge/cs249r_book/pull/1602
Author: @Shashank-Tripathi-07
Created: 4/29/2026
Status: Merged
Merged: 4/30/2026
Merged by: @profvjreddi

Base: devHead: fix/practice-keyboard-stale-closure


📝 Commits (1)

  • bc4cca7 fix(staffml): add pickRandom to keyboard effect and pickNext dep arrays

📊 Changes

1 file changed (+18 additions, -18 deletions)

View changed files

📝 interviews/staffml/src/app/practice/page.tsx (+18 -18)

📄 Description

Summary

Two related stale-closure bugs in practice/page.tsx:

Bug 1 -- keyboard shortcut N picks from wrong pool after filter change

pickRandom is a useCallback with deps [pool, current, showAnswer]. When the user changes a filter (track, level, area, etc.), pool updates and pickRandom gets a new identity. But the keyboard useEffect only listed [showAnswer, current] as deps -- it never re-registered the listener with the new pickRandom. Pressing N after a filter change would pick from the stale pre-filter question pool.

Bug 2 -- pickNext also calls stale pickRandom

Same issue in pickNext: deps were [reviewMode, pool] but it calls pickRandom(), so the same staleness applied after a pool change.

Fix:

  • Move pickRandom declaration before the keyboard useEffect (satisfies declaration-before-use order)
  • Add pickRandom to the keyboard effect dep array: [showAnswer, current, pickRandom]
  • Add pickRandom to pickNext dep array: [reviewMode, pool, pickRandom]

Repro steps

  1. Open /practice, select "Cloud" + "L3"
  2. Answer one question, then switch track to "Edge"
  3. Press N on the keyboard
  4. Before fix: the picked question is often from the "Cloud" pool
  5. After fix: the picked question is always from the current "Edge" pool

Test plan

  • Change filters and immediately press N -- verify picked question matches the new filter
  • Score a question with 1-4 keyboard keys -- verify scoring still works
  • Cmd+Enter in the textarea -- verify reveal shortcut still works
  • Spaced repetition review mode -- verify pickNext correctly transitions to pickRandom when due queue is empty

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/harvard-edge/cs249r_book/pull/1602 **Author:** [@Shashank-Tripathi-07](https://github.com/Shashank-Tripathi-07) **Created:** 4/29/2026 **Status:** ✅ Merged **Merged:** 4/30/2026 **Merged by:** [@profvjreddi](https://github.com/profvjreddi) **Base:** `dev` ← **Head:** `fix/practice-keyboard-stale-closure` --- ### 📝 Commits (1) - [`bc4cca7`](https://github.com/harvard-edge/cs249r_book/commit/bc4cca744452da2b9f2521d37a7e80606a59c9f8) fix(staffml): add pickRandom to keyboard effect and pickNext dep arrays ### 📊 Changes **1 file changed** (+18 additions, -18 deletions) <details> <summary>View changed files</summary> 📝 `interviews/staffml/src/app/practice/page.tsx` (+18 -18) </details> ### 📄 Description ## Summary Two related stale-closure bugs in `practice/page.tsx`: **Bug 1 -- keyboard shortcut `N` picks from wrong pool after filter change** `pickRandom` is a `useCallback` with deps `[pool, current, showAnswer]`. When the user changes a filter (track, level, area, etc.), `pool` updates and `pickRandom` gets a new identity. But the keyboard `useEffect` only listed `[showAnswer, current]` as deps -- it never re-registered the listener with the new `pickRandom`. Pressing `N` after a filter change would pick from the stale pre-filter question pool. **Bug 2 -- `pickNext` also calls stale `pickRandom`** Same issue in `pickNext`: deps were `[reviewMode, pool]` but it calls `pickRandom()`, so the same staleness applied after a pool change. **Fix:** - Move `pickRandom` declaration before the keyboard `useEffect` (satisfies declaration-before-use order) - Add `pickRandom` to the keyboard effect dep array: `[showAnswer, current, pickRandom]` - Add `pickRandom` to `pickNext` dep array: `[reviewMode, pool, pickRandom]` ## Repro steps 1. Open `/practice`, select "Cloud" + "L3" 2. Answer one question, then switch track to "Edge" 3. Press `N` on the keyboard 4. Before fix: the picked question is often from the "Cloud" pool 5. After fix: the picked question is always from the current "Edge" pool ## Test plan - [ ] Change filters and immediately press `N` -- verify picked question matches the new filter - [ ] Score a question with 1-4 keyboard keys -- verify scoring still works - [ ] Cmd+Enter in the textarea -- verify reveal shortcut still works - [ ] Spaced repetition review mode -- verify `pickNext` correctly transitions to `pickRandom` when due queue is empty --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-05-03 01:29:00 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/cs249r_book#9214