Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/browser/hooks/useReviewRefreshController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ describe("useReviewRefreshController design", () => {

test("visibility contract: hidden tab queues refresh for later", () => {
// Contract: When document.hidden is true, refresh is queued.
// When visibilitychange fires and document.hidden becomes false, queued refresh executes.
// When visibilitychange fires or window.focus fires (and document is visible),
// queued refresh executes. Uses both events since visibilitychange alone is
// unreliable in Electron when app is behind other windows or on different desktop.
// This prevents wasted git operations when user isn't looking.
expect(true).toBe(true);
});
Expand Down
14 changes: 10 additions & 4 deletions src/browser/hooks/useReviewRefreshController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,17 +238,23 @@ export function useReviewRefreshController(
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [api, workspaceId, isCreating]);

// Handle visibility changes - flush pending refresh when tab becomes visible
// Handle visibility/focus changes - flush pending refresh when user returns.
// Uses both visibilitychange (for browser tab hidden state) and window focus
// (for Electron app focus) since visibilitychange alone is unreliable in Electron
// when the app is behind other windows or on a different desktop/space.
useEffect(() => {
const handleVisibilityChange = () => {
const handleReturn = () => {
// Only flush if document is actually visible
if (!document.hidden) {
flushPending("hidden");
}
};

document.addEventListener("visibilitychange", handleVisibilityChange);
document.addEventListener("visibilitychange", handleReturn);
window.addEventListener("focus", handleReturn);
return () => {
document.removeEventListener("visibilitychange", handleVisibilityChange);
document.removeEventListener("visibilitychange", handleReturn);
window.removeEventListener("focus", handleReturn);
};
// flushPending is stable (only uses refs internally)
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down