Skip to content

Commit be9bac7

Browse files
fix(web): SSH password prompts wait for their dialog, and the app shell drops its sync effects
SSH password prompts that the desktop sent while the prompt dialog was not mounted (pairing, sign-in, or any page outside the authenticated shell) were dropped, because the dialog only listened to the bridge from its own effect. The queue now subscribes once at app start and the dialog reads it with useSyncExternalStore. The rest of the app shell's lifetime work moves to its real owners: - background thread announcements, the legacy visited-time migration and the main-app location tracker start in startAppServices instead of running from effects (or a null component) inside AppSidebarLayout - the confirm dialog treats a store subscriber as its host, so the host's registration effect goes away - the quit-hold hint and desktop-local bootstraps become external stores/atoms - live regions attach through a ref callback, and Escape-to-go-back registers a dispatcher observer from the page root's ref callback - remote-capable editors are probed once per page by a keepAlive atom Effects removed: 12 (SshPasswordPromptDialog 3, BackgroundThreadAnnouncer, ConfirmDialogHost, QuitHoldOverlay, mainAppLocation, useDesktopLocalBootstraps, useLiveRegion, useNavigateBack, useThreadVisitedMigration, remoteOpen 1 each). The sidebar toggle effect in AppSidebarLayout stays: moving it needs a stable toggle from the sidebar provider, which is a separate change. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 3de45c6 commit be9bac7

16 files changed

Lines changed: 331 additions & 254 deletions

‎apps/web/src/appInit.tsx‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ import { createBrowserHistory, createHashHistory } from "@tanstack/react-router"
33

44
import { applyAppearanceContrast } from "./appearanceContrast";
55
import { applyAppearanceFontVariables } from "./appearanceFonts";
6+
import { startBackgroundThreadAnnouncements } from "./components/BackgroundThreadAnnouncer";
7+
import { startSshPasswordPromptQueue } from "./components/desktop/SshPasswordPromptDialog";
68
import { startAgentOpenedPreviews } from "./components/preview/agentOpenedPreviews";
9+
import { startMainAppLocationTracking } from "./components/sidebar/mainAppLocation";
710
import { APP_BASE_NAME, APP_DISPLAY_NAME, APP_STAGE_LABEL } from "./branding";
811
import { resolveServerBackedAppDisplayName } from "./branding.logic";
912
import { isElectron } from "./env";
1013
import { getClientSettings, subscribeClientSettings } from "./hooks/useSettings";
14+
import { startThreadVisitedMigration } from "./hooks/useThreadVisitedMigration";
1115
import {
1216
syncDocumentElectronPlatformClasses,
1317
syncDocumentWindowControlsOverlayClass,
@@ -103,6 +107,10 @@ export function startAppServices(): AppRouter {
103107
startClientAppearance();
104108
startDocumentTitle();
105109
startAgentOpenedPreviews();
110+
startMainAppLocationTracking(router);
111+
startBackgroundThreadAnnouncements(appAtomRegistry, router);
112+
startThreadVisitedMigration(appAtomRegistry);
113+
startSshPasswordPromptQueue();
106114

107115
return router;
108116
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,9 @@ import {
3838
} from "../panelAnimations";
3939
import { BackgroundThreadAnnouncer } from "./BackgroundThreadAnnouncer";
4040
import LegacyThreadSidebar from "./LegacySidebar";
41-
import { useThreadVisitedMigration } from "../hooks/useThreadVisitedMigration";
4241
import ThreadSidebar from "./Sidebar";
4342
import { SidebarBrandWidthProbe } from "./sidebar/SidebarChrome";
44-
import { isAppModalPage, MainAppLocationTracker } from "./sidebar/mainAppLocation";
43+
import { isAppModalPage } from "./sidebar/mainAppLocation";
4544
import type { SidebarRouteReveal } from "./sidebar/routeReveal";
4645
import { useSidebarStageBackdropVariant } from "./SidebarStageBackdrop";
4746
import {
@@ -359,7 +358,6 @@ export function AppSidebarLayout({ children }: { children: ReactNode }) {
359358
}));
360359
const { active: panelAnimationsActive, durationMs: panelAnimationDurationMs } =
361360
usePanelAnimationSettings();
362-
useThreadVisitedMigration();
363361
const pathname = useLocation({ select: (location) => location.pathname });
364362
const panelAnimationsSuppressed = usePanelNavigationSuppression(pathname);
365363
const routePanelAnimationsActive = panelAnimationsActive && !panelAnimationsSuppressed;
@@ -429,7 +427,6 @@ export function AppSidebarLayout({ children }: { children: ReactNode }) {
429427
{children}
430428
<BackgroundThreadAnnouncer />
431429
<SidebarControl />
432-
<MainAppLocationTracker />
433430
</SidebarProvider>
434431
</PanelAnimationSuppressionProvider>
435432
);
Lines changed: 58 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,44 @@
1-
import { useAtomValue } from "@effect/atom-react";
21
import { scopeThreadRef, scopedThreadKey } from "@t3tools/client-runtime/environment";
32
import { effectiveSnoozed } from "@t3tools/client-runtime/state/thread-settled";
4-
import { useEffect, useRef } from "react";
3+
import type { AnyRouter } from "@tanstack/react-router";
4+
import type { AtomRegistry } from "effect/reactivity";
55

66
import { useComposerDraftStore } from "../composerDraftStore";
7-
import { useLiveRegion } from "../hooks/useLiveRegion";
8-
import { useThreadRouteTarget } from "../hooks/useThreadRouteTarget";
9-
import { useQuietThreadShells } from "../state/entities";
7+
import { createLiveRegion } from "../hooks/useLiveRegion";
108
import { liveShellEnvironmentIdsAtom } from "../state/shell";
11-
import { resolveActiveThreadRouteRef } from "../threadRoutes";
9+
import { environmentThreadShells } from "../state/threads";
10+
import { resolveActiveThreadRouteRef, resolveThreadRouteTarget } from "../threadRoutes";
1211
import {
1312
type BackgroundThreadMemory,
1413
type BackgroundThreadSnapshot,
1514
nextBackgroundThreadAnnouncement,
1615
resolveBackgroundThreadRequest,
1716
} from "./backgroundThreadAnnouncement";
1817

19-
/**
20-
* Speaks status changes for threads other than the open one. It lives outside
21-
* the sidebar because a collapsed sidebar is inert, which silences its rows.
22-
*/
23-
export function BackgroundThreadAnnouncer() {
24-
const threads = useQuietThreadShells();
25-
const liveEnvironmentIds = useAtomValue(liveShellEnvironmentIdsAtom);
26-
const routeTarget = useThreadRouteTarget();
27-
const routeDraftThread = useComposerDraftStore((store) =>
28-
routeTarget?.kind === "draft" ? store.getDraftSession(routeTarget.draftId) : null,
29-
);
30-
const openThreadRef = resolveActiveThreadRouteRef(routeTarget, routeDraftThread);
31-
const openThreadKey = openThreadRef === null ? null : scopedThreadKey(openThreadRef);
32-
const { regionRef, announce } = useLiveRegion();
33-
const memoryRef = useRef<BackgroundThreadMemory>(new Map());
18+
const backgroundThreadLiveRegion = createLiveRegion();
19+
const attachBackgroundThreadRegion = backgroundThreadLiveRegion.attach;
20+
21+
function readOpenThreadKey(router: AnyRouter): string | null {
22+
const target = resolveThreadRouteTarget(router.state.matches.at(-1)?.params ?? {});
23+
const draft =
24+
target?.kind === "draft"
25+
? useComposerDraftStore.getState().getDraftSession(target.draftId)
26+
: null;
27+
const ref = resolveActiveThreadRouteRef(target, draft);
28+
return ref === null ? null : scopedThreadKey(ref);
29+
}
30+
31+
/** Speaks status changes for threads other than the open one, for the life of the app. */
32+
export function startBackgroundThreadAnnouncements(
33+
registry: AtomRegistry.AtomRegistry,
34+
router: AnyRouter,
35+
): () => void {
36+
let memory: BackgroundThreadMemory = new Map();
37+
let openThreadKey = readOpenThreadKey(router);
3438

35-
useEffect(() => {
39+
const update = () => {
40+
const threads = registry.get(environmentThreadShells.quietThreadShellsAtom);
41+
const liveEnvironmentIds = registry.get(liveShellEnvironmentIdsAtom);
3642
const now = new Date().toISOString();
3743
const snapshots: BackgroundThreadSnapshot[] = [];
3844
for (const thread of threads) {
@@ -50,15 +56,36 @@ export function BackgroundThreadAnnouncer() {
5056
snoozed: effectiveSnoozed(thread, { now }),
5157
});
5258
}
53-
const { memory, announcement } = nextBackgroundThreadAnnouncement(
54-
memoryRef.current,
55-
snapshots,
56-
openThreadKey,
57-
);
58-
memoryRef.current = memory;
59-
if (announcement !== null) announce(announcement);
60-
}, [threads, liveEnvironmentIds, openThreadKey, announce]);
59+
const next = nextBackgroundThreadAnnouncement(memory, snapshots, openThreadKey);
60+
memory = next.memory;
61+
if (next.announcement !== null) backgroundThreadLiveRegion.announce(next.announcement);
62+
};
6163

64+
// The draft store changes on every keystroke; only a new open thread matters here.
65+
const updateOpenThread = () => {
66+
const next = readOpenThreadKey(router);
67+
if (next === openThreadKey) return;
68+
openThreadKey = next;
69+
update();
70+
};
71+
72+
const unsubscribes = [
73+
registry.subscribe(environmentThreadShells.quietThreadShellsAtom, update),
74+
registry.subscribe(liveShellEnvironmentIdsAtom, update),
75+
router.subscribe("onResolved", updateOpenThread),
76+
useComposerDraftStore.subscribe(updateOpenThread),
77+
];
78+
update();
79+
return () => {
80+
for (const unsubscribe of unsubscribes) unsubscribe();
81+
};
82+
}
83+
84+
/**
85+
* The region the background announcements are spoken through. It lives outside
86+
* the sidebar because a collapsed sidebar is inert, which silences its rows.
87+
*/
88+
export function BackgroundThreadAnnouncer() {
6289
// No role="status": Orca 50 maps it to a status bar in Chromium and drops its updates.
63-
return <div ref={regionRef} aria-live="polite" className="sr-only" />;
90+
return <div ref={attachBackgroundThreadRegion} aria-live="polite" className="sr-only" />;
6491
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { useEffect, useSyncExternalStore } from "react";
1+
import { useSyncExternalStore } from "react";
22

33
import {
44
completeConfirmDialogClose,
55
readConfirmDialogState,
6-
registerConfirmDialogHost,
76
respondToConfirmDialog,
87
subscribeConfirmDialog,
98
} from "../confirmDialog";
@@ -58,8 +57,6 @@ export function ConfirmDialogHost() {
5857
readConfirmDialogState,
5958
);
6059

61-
useEffect(() => registerConfirmDialogHost(), []);
62-
6360
const copy = resolveConfirmDialogCopy(state.status === "idle" ? "" : state.message);
6461
const confirmVariant = state.status === "idle" ? "default" : state.variant;
6562
const onCancel = () => respondToConfirmDialog(false);

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

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,55 @@
1-
import { useEffect, useState } from "react";
1+
import { useSyncExternalStore } from "react";
22

33
import { isMacPlatform } from "../lib/utils";
44

55
const HOLD_HINT_LINGER_MS = 1200;
66

7+
type QuitHintMode = "hold" | "double-click";
8+
9+
let visibleMode: QuitHintMode | null = null;
10+
let pressedMode: QuitHintMode = "hold";
11+
let hideTimer: number | undefined;
12+
const listeners = new Set<() => void>();
13+
let stopBridge: (() => void) | null = null;
14+
15+
function setVisibleMode(next: QuitHintMode | null) {
16+
if (next === visibleMode) return;
17+
visibleMode = next;
18+
for (const listener of listeners) listener();
19+
}
20+
21+
function subscribeQuitHint(listener: () => void): () => void {
22+
listeners.add(listener);
23+
if (listeners.size === 1) {
24+
stopBridge =
25+
window.desktopBridge?.onQuitShortcut?.((hint) => {
26+
window.clearTimeout(hideTimer);
27+
if (hint.state === "down") {
28+
pressedMode = hint.mode;
29+
setVisibleMode(hint.mode);
30+
return;
31+
}
32+
if (pressedMode === "double-click") {
33+
setVisibleMode(null);
34+
return;
35+
}
36+
hideTimer = window.setTimeout(() => setVisibleMode(null), HOLD_HINT_LINGER_MS);
37+
}) ?? null;
38+
}
39+
return () => {
40+
listeners.delete(listener);
41+
if (listeners.size > 0) return;
42+
window.clearTimeout(hideTimer);
43+
stopBridge?.();
44+
stopBridge = null;
45+
visibleMode = null;
46+
};
47+
}
48+
49+
const readQuitHint = () => visibleMode;
50+
751
export function QuitHoldOverlay() {
8-
const [visibleMode, setVisibleMode] = useState<"hold" | "double-click" | null>(null);
9-
10-
useEffect(() => {
11-
const subscribe = window.desktopBridge?.onQuitShortcut;
12-
if (!subscribe) return;
13-
let hideTimer: number | undefined;
14-
let pressedMode: "hold" | "double-click" = "hold";
15-
const unsubscribe = subscribe((hint) => {
16-
window.clearTimeout(hideTimer);
17-
if (hint.state === "down") {
18-
pressedMode = hint.mode;
19-
setVisibleMode(hint.mode);
20-
return;
21-
}
22-
if (pressedMode === "double-click") {
23-
setVisibleMode(null);
24-
return;
25-
}
26-
hideTimer = window.setTimeout(() => setVisibleMode(null), HOLD_HINT_LINGER_MS);
27-
});
28-
return () => {
29-
window.clearTimeout(hideTimer);
30-
unsubscribe();
31-
};
32-
}, []);
52+
const visibleMode = useSyncExternalStore(subscribeQuitHint, readQuitHint, readQuitHint);
3353

3454
if (!visibleMode) return null;
3555
const shortcut = isMacPlatform(navigator.platform) ? "⌘Q" : "Ctrl+Q";
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type { DesktopSshPasswordPromptRequest } from "@t3tools/contracts";
2+
import { expect, it } from "vite-plus/test";
3+
4+
import { render, stubGlobal } from "../../../test/render";
5+
import { SshPasswordPromptDialog, startSshPasswordPromptQueue } from "./SshPasswordPromptDialog";
6+
7+
it("shows a prompt that arrived before the dialog mounted", async () => {
8+
const listeners: Array<(request: DesktopSshPasswordPromptRequest) => void> = [];
9+
using _bridge = stubGlobal("desktopBridge", {
10+
onSshPasswordPrompt: (listener: (request: DesktopSshPasswordPromptRequest) => void) => {
11+
listeners.push(listener);
12+
return () => listeners.splice(listeners.indexOf(listener), 1);
13+
},
14+
resolveSshPasswordPrompt: async () => undefined,
15+
});
16+
const stopQueue = startSshPasswordPromptQueue();
17+
using _queue = { [Symbol.dispose]: stopQueue };
18+
19+
// The desktop asks while the app is still on a page without the dialog (pairing, sign-in).
20+
for (const listener of listeners) {
21+
listener({
22+
requestId: "ssh-1",
23+
destination: "devbox",
24+
username: "julius",
25+
prompt: "julius@devbox's password:",
26+
expiresAt: "2999-01-01T00:00:00.000Z",
27+
});
28+
}
29+
30+
await using screen = await render(<SshPasswordPromptDialog />);
31+
await expect.element(screen.getByText("SSH verification required")).toBeVisible();
32+
});

0 commit comments

Comments
 (0)