mirror of
https://github.com/harvard-edge/cs249r_book.git
synced 2026-07-16 14:42:29 -05:00
[PR #1834] [MERGED] fix(staffml): pause Nav due-count poll on hidden tabs + sync across tabs #36457
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/harvard-edge/cs249r_book/pull/1834
Author: @farhan523
Created: 6/3/2026
Status: ✅ Merged
Merged: 6/10/2026
Merged by: @profvjreddi
Base:
dev← Head:fix/nav-pause-polling-on-hidden-tab📝 Commits (1)
4f31ebdfix(staffml): pause Nav due-count poll on hidden tabs + sync across tabs📊 Changes
3 files changed (+244 additions, -8 deletions)
View changed files
➕
interviews/staffml/src/__tests__/use-visibility-poll.test.tsx(+144 -0)📝
interviews/staffml/src/components/Nav.tsx(+26 -8)➕
interviews/staffml/src/lib/hooks/useVisibilityPoll.ts(+74 -0)📄 Description
Summary
Nav was polling
getDueCountevery 30s indefinitely via a baresetInterval, even on backgrounded tabs. Cheap per call (it reads localStorage), but the unnecessary wakeups burn battery on mobile and add no value when no one is looking. There was also no cross-tab sync: a card completed in one tab wouldn't update the badge in another tab until that other tab's next 30s tick.Changes
src/lib/hooks/useVisibilityPoll.ts: pauses the interval onvisibilitychange→hidden, re-arms on→visiblewith an immediate catch-up tick. Callback is held in a ref so a fresh closure on every render is picked up without re-arming.try/catchwithconsole.error. Without this, a throwing callback on the mount-time synchronous tick propagates out of the effect body before React receives the cleanup — the already-armedsetIntervalwould orphan and re-throw every interval forever.storageevent listener that fires in other tabs the instant one writes to localStorage.Nav's emptycatch {}withconsole.error(...)so a real failure (corrupted localStorage, JSON parse error) surfaces in devtools instead of presenting as a frozen badge. Refresh logic hoisted into oneuseCallbackshared by both the poll and the storage handler.Test plan
npx vitest run→ 65/65 individual cases (was 57 on dev; +8 new for the hook).npx tsc --noEmit→ unchanged from dev baseline (the 2 pre-existingkatex/js-quantitieserrors are env-local: those packages are declared inpackage.jsonbut missing fromnode_modulesuntilnpm installruns).storageevent fires immediately; the visibility poll would otherwise wait up to 30s).setIntervalwakeups while the tab is hidden.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.