Skip to content

Commit 32cd8cb

Browse files
fix(web): every return to the window refreshes git status, palette mount is an explicit effect
Coming back to the window refreshes the checkout's status on each window focus and each switch to a visible tab, as before, not only when the cached focus state flips. The palette's mount tracking is a plain effect instead of a ref callback, and the revalidation atom's comment says when it refreshes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 31ed312 commit 32cd8cb

8 files changed

Lines changed: 92 additions & 34 deletions

File tree

‎apps/web/src/commandPaletteStore.ts‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useEffect } from "react";
12
import { create } from "zustand";
23

34
import {
@@ -27,17 +28,22 @@ export function isCommandPaletteMounted(): boolean {
2728
return mountedPalettes > 0;
2829
}
2930

30-
/** Ref callback for the palette's root; an unmounted palette cannot stay open. */
31-
export function commandPaletteMountRef(node: Element | null): (() => void) | undefined {
32-
if (node === null) return undefined;
33-
mountedPalettes += 1;
34-
return () => {
35-
mountedPalettes -= 1;
36-
if (mountedPalettes === 0) {
37-
useCommandPaletteStore.setState(
38-
(state) => ({ ...INITIAL_STATE, session: state.session + 1 }),
39-
true,
40-
);
41-
}
42-
};
31+
/**
32+
* Marks the palette as mounted while the calling component is. An unmounted
33+
* palette cannot stay open, so the last unmount resets the store.
34+
*/
35+
export function useCommandPaletteMount(): void {
36+
// oxlint-disable-next-line t3code/react-effect-budget -- The palette's mount gates its global shortcuts, and leaving the route while it is open calls no close handler, so only unmount can reset the store.
37+
useEffect(() => {
38+
mountedPalettes += 1;
39+
return () => {
40+
mountedPalettes -= 1;
41+
if (mountedPalettes === 0) {
42+
useCommandPaletteStore.setState(
43+
(state) => ({ ...INITIAL_STATE, session: state.session + 1 }),
44+
true,
45+
);
46+
}
47+
};
48+
}, []);
4349
}

‎apps/web/src/components/CommandPalette.tsx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ import {
128128
resolveProjectPathForDispatch,
129129
} from "../lib/projectPaths";
130130
import {
131-
commandPaletteMountRef,
132131
dispatchCommandPalette,
132+
useCommandPaletteMount,
133133
useCommandPaletteStore,
134134
} from "../commandPaletteStore";
135135
import { PULL_REQUESTS_PANEL_REF, useRightPanelStore } from "../rightPanelStore";
@@ -260,6 +260,7 @@ function openIntentView(intent: CommandPaletteOpenIntent | null): CommandPalette
260260
}
261261

262262
export function CommandPalette({ children }: { children: ReactNode }) {
263+
useCommandPaletteMount();
263264
const state = useCommandPaletteStore();
264265
const setOpen = (open: boolean) => dispatchCommandPalette({ _tag: "SetOpen", open });
265266
const openChildSearch = (mode: "files" | "content", context: CommandPaletteReturnContext) =>
@@ -279,7 +280,7 @@ export function CommandPalette({ children }: { children: ReactNode }) {
279280
setOpen(open);
280281
}}
281282
>
282-
<div ref={commandPaletteMountRef} className="contents" inert={state.open}>
283+
<div className="contents" inert={state.open}>
283284
{children}
284285
</div>
285286
<CommandPaletteDialog

‎apps/web/src/components/UsageToggleShortcut.browser.test.tsx‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,15 @@ vi.mock(import("@tanstack/react-router"), async (original) => ({
3232
useNavigate: (() => state.navigate) as never,
3333
}));
3434

35-
import { commandPaletteMountRef } from "../commandPaletteStore";
35+
import { useCommandPaletteMount } from "../commandPaletteStore";
3636
import { installCommandPaletteCommands } from "../shortcuts/commandPaletteCommands";
3737
import { registerNavigationHistoryCommands } from "./AppSidebarLayout";
3838

39+
function MountedPalette() {
40+
useCommandPaletteMount();
41+
return null;
42+
}
43+
3944
// regression: #21357
4045
describe("usage toggle shortcut on Usage", () => {
4146
it("goes back", async () => {
@@ -54,7 +59,7 @@ describe("usage toggle shortcut on Usage", () => {
5459
it("does not reopen Usage from the command palette", async () => {
5560
vi.clearAllMocks();
5661
using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("Linux x86_64");
57-
await using screen = await render(<div ref={commandPaletteMountRef} />, { router: "/usage" });
62+
await using screen = await render(<MountedPalette />, { router: "/usage" });
5863
using navigate = vi.spyOn(screen.router!, "navigate");
5964
using commands = new DisposableStack();
6065
commands.defer(
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { describe, expect, it, vi } from "vite-plus/test";
2+
3+
import { subscribeDocumentReturn } from "./documentActivity";
4+
5+
describe("subscribeDocumentReturn", () => {
6+
// Focus can come back from the preview iframe, and a quick alt-tab can miss
7+
// the blur, so a return must not depend on the cached activity flipping.
8+
it("fires on every window focus and every return to a visible tab", () => {
9+
using _focused = vi.spyOn(document, "hasFocus").mockReturnValue(true);
10+
let visibility: DocumentVisibilityState = "visible";
11+
using _visibility = vi
12+
.spyOn(document, "visibilityState", "get")
13+
.mockImplementation(() => visibility);
14+
const wake = vi.fn();
15+
using subscription = new DisposableStack();
16+
subscription.defer(subscribeDocumentReturn(wake));
17+
18+
window.dispatchEvent(new Event("focus"));
19+
window.dispatchEvent(new Event("focus"));
20+
expect(wake).toHaveBeenCalledTimes(2);
21+
22+
visibility = "hidden";
23+
document.dispatchEvent(new Event("visibilitychange"));
24+
expect(wake).toHaveBeenCalledTimes(2);
25+
visibility = "visible";
26+
document.dispatchEvent(new Event("visibilitychange"));
27+
expect(wake).toHaveBeenCalledTimes(3);
28+
29+
subscription.dispose();
30+
window.dispatchEvent(new Event("focus"));
31+
expect(wake).toHaveBeenCalledTimes(3);
32+
});
33+
});

‎apps/web/src/lib/documentActivity.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,22 @@ export function subscribeDocumentActivity(listener: () => void): () => void {
4848
window.removeEventListener("blur", handleActivityEvent);
4949
};
5050
}
51+
52+
/**
53+
* Fires on every window focus and every switch to a visible tab. Unlike
54+
* `subscribeDocumentActivity` it does not wait for the cached activity to flip:
55+
* focus coming back from an iframe, or an alt-tab whose blur was missed, still
56+
* counts as a return.
57+
*/
58+
export function subscribeDocumentReturn(listener: () => void): () => void {
59+
if (typeof document === "undefined") return () => {};
60+
const handleVisibilityChange = () => {
61+
if (document.visibilityState === "visible") listener();
62+
};
63+
window.addEventListener("focus", listener);
64+
document.addEventListener("visibilitychange", handleVisibilityChange);
65+
return () => {
66+
window.removeEventListener("focus", listener);
67+
document.removeEventListener("visibilitychange", handleVisibilityChange);
68+
};
69+
}

‎apps/web/src/shortcuts/commandPaletteCommands.browser.test.tsx‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@ import { describe, expect, it, vi } from "vite-plus/test";
33
import { userEvent } from "vite-plus/test/browser";
44

55
import { render } from "../../test/render";
6-
import { commandPaletteMountRef, useCommandPaletteStore } from "../commandPaletteStore";
6+
import { useCommandPaletteMount, useCommandPaletteStore } from "../commandPaletteStore";
77
import { installCommandPaletteCommands } from "./commandPaletteCommands";
88

99
const keybindings = compileResolvedKeybindingsConfig([
1010
{ key: "mod+k", command: "commandPalette.toggle" },
1111
]);
1212

13+
function MountedPalette() {
14+
useCommandPaletteMount();
15+
return null;
16+
}
17+
1318
function setup() {
1419
useCommandPaletteStore.setState(useCommandPaletteStore.getInitialState(), true);
1520
}
@@ -32,7 +37,7 @@ describe("command palette shortcuts", () => {
3237
it("open the palette while it is mounted, and it closes when it unmounts", async () => {
3338
setup();
3439
using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("Linux x86_64");
35-
await using screen = await render(<div ref={commandPaletteMountRef} />, { router: "/" });
40+
await using screen = await render(<MountedPalette />, { router: "/" });
3641
using commands = new DisposableStack();
3742
commands.defer(
3843
installCommandPaletteCommands(screen.router!, { keybindings: () => keybindings }),

‎apps/web/src/state/vcs.ts‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,11 @@ import {
55
} from "@t3tools/client-runtime/state/vcs";
66

77
import { connectionAtomRuntime } from "../connection/runtime";
8-
import { getDocumentActivity, subscribeDocumentActivity } from "../lib/documentActivity";
8+
import { subscribeDocumentReturn } from "../lib/documentActivity";
99

1010
export const vcsEnvironment = createVcsEnvironmentAtoms(connectionAtomRuntime);
1111
export const vcsActionManager = createVcsActionManager(connectionAtomRuntime);
1212

13-
/** Fires when the window regains focus or the tab becomes visible again. */
14-
function subscribeDocumentReturn(wake: () => void): () => void {
15-
let previous = getDocumentActivity();
16-
return subscribeDocumentActivity(() => {
17-
const next = getDocumentActivity();
18-
if ((next.focused && !previous.focused) || (next.visible && !previous.visible)) wake();
19-
previous = next;
20-
});
21-
}
22-
2313
/**
2414
* Refreshes a checkout's status when the user comes back to the window, where
2515
* edits made in another app or terminal may be waiting.

‎packages/client-runtime/src/state/vcsStatusRevalidation.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ export interface VcsStatusRevalidationTarget {
1010
}
1111

1212
/**
13-
* One atom per environment and cwd that refreshes VCS status while mounted, so
14-
* controls showing the same checkout share one cadence. Refreshes on mount, on
15-
* reconnect when `connected` is given, and on `subscribeWake` (debounced); the
16-
* subscription and timer end with the last subscriber.
13+
* One atom per checkout, so controls showing it share one cadence. Refreshes on
14+
* `subscribeWake` (debounced) and whenever `connected`, if given, is true on
15+
* mount or turns true. Listening ends with the last subscriber.
1716
*/
1817
export function createVcsStatusRevalidationAtomFamily<R>(input: {
1918
readonly refreshStatus: AtomCommand<

0 commit comments

Comments
 (0)