Skip to content

Commit a7313c2

Browse files
fix(web): closing the panel no longer floats a device whose session ended
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 55b01b3 commit a7313c2

4 files changed

Lines changed: 58 additions & 5 deletions

File tree

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1839,7 +1839,9 @@ export default function ChatView(props: ChatViewProps) {
18391839
const visiblePullRequestCount = visiblePullRequests.length;
18401840
const pullRequestsSurfaceAvailable =
18411841
isServerThread && supportsThreadPullRequests && visiblePullRequestCount > 0;
1842-
const { state: deviceState } = useDeviceState(activeThreadRef?.environmentId ?? null);
1842+
const { state: deviceState, loaded: deviceStateLoaded } = useDeviceState(
1843+
activeThreadRef?.environmentId ?? null,
1844+
);
18431845
// The thread's own change request, placed against the project it belongs to. Without a
18441846
// project there is nothing to resolve it against, so the caller falls back to the browser.
18451847
const activeProjectRepository = sourceControlClients.repositorySelector(
@@ -2319,6 +2321,7 @@ export default function ChatView(props: ChatViewProps) {
23192321
activeRightPanelSurface,
23202322
activePreviewState,
23212323
deviceState,
2324+
deviceStateLoaded,
23222325
pullRequestPanelTarget,
23232326
threadPanelPresentation,
23242327
isServerThread,

‎apps/web/src/components/chat/useRightPanelActions.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { BrowserSettingsReadError } from "../../browser/openFileInPreview";
2323
import { addBrowserSurface } from "../preview/addBrowserSurface";
2424
import { focusActivePanelTerminal, focusTerminal } from "../../lib/terminalFocus";
2525
import { subscribePreviewAction } from "../preview/previewActionBus";
26+
import { floatDeviceSurface } from "../preview/agentOpenedPreviews";
2627
import { browserMiniPlayerSource, usePreviewMiniPlayerStore } from "../../previewMiniPlayerStore";
2728
import type { threadPullRequestPanelTarget } from "../pullRequest/pullRequestDetail.logic";
2829
import type { useDeviceState } from "../../state/device";
@@ -37,6 +38,7 @@ interface RightPanelActionsInput {
3738
readonly activeRightPanelSurface: RightPanelSurface | null;
3839
readonly activePreviewState: ThreadPreviewState;
3940
readonly deviceState: ReturnType<typeof useDeviceState>["state"];
41+
readonly deviceStateLoaded: boolean;
4042
readonly pullRequestPanelTarget: ReturnType<typeof threadPullRequestPanelTarget> | null;
4143
readonly threadPanelPresentation: ThreadPanelPresentation;
4244
readonly isServerThread: boolean;
@@ -69,6 +71,7 @@ export function useRightPanelActions(input: RightPanelActionsInput) {
6971
activeRightPanelSurface,
7072
activePreviewState,
7173
deviceState,
74+
deviceStateLoaded,
7275
pullRequestPanelTarget,
7376
threadPanelPresentation,
7477
isServerThread,
@@ -164,9 +167,11 @@ export function useRightPanelActions(input: RightPanelActionsInput) {
164167
.getState()
165168
.open(activeThreadRef, browserMiniPlayerSource(activeRightPanelSurface.resourceId));
166169
} else if (activeRightPanelSurface?.kind === "device" && activeRightPanelSurface.target) {
167-
usePreviewMiniPlayerStore
168-
.getState()
169-
.open(activeThreadRef, { kind: "device", ...activeRightPanelSurface.target });
170+
floatDeviceSurface(
171+
activeThreadRef,
172+
{ kind: "device", ...activeRightPanelSurface.target },
173+
deviceStateLoaded ? deviceState : null,
174+
);
170175
}
171176
useRightPanelStore.getState().setMaximized(activeThreadRef, false);
172177
useRightPanelStore.getState().close(activeThreadRef);

‎apps/web/src/components/preview/agentOpenedPreviews.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
import { useRightPanelStore } from "../../rightPanelStore";
2121
import {
2222
applyDeviceSnapshot,
23+
floatDeviceSurface,
2324
followPreviewSessions,
2425
resetDeviceSessionBaselinesForTests,
2526
revealServerTabs,
@@ -156,6 +157,32 @@ describe("applyDeviceSnapshot", () => {
156157
});
157158
});
158159

160+
describe("floatDeviceSurface", () => {
161+
const source = {
162+
kind: "device",
163+
hostId: "local",
164+
deviceId: "pixel",
165+
platform: "android",
166+
name: "Pixel 8",
167+
} as const;
168+
169+
it("floats a device with a live session, or before device state loads", () => {
170+
setup();
171+
floatDeviceSurface(ref, source, deviceState({ sessions: [session] }));
172+
expect(miniPlayerSource()).toMatchObject({ kind: "device", deviceId: "pixel" });
173+
174+
setup();
175+
floatDeviceSurface(ref, source, null);
176+
expect(miniPlayerSource()).toMatchObject({ kind: "device", deviceId: "pixel" });
177+
});
178+
179+
it("does not float a device whose session ended", () => {
180+
setup();
181+
floatDeviceSurface(ref, source, deviceState({ sessions: [] }));
182+
expect(miniPlayerSource()).toBeUndefined();
183+
});
184+
});
185+
159186
describe("revealServerTabs", () => {
160187
const tab = (tabId: string, force?: boolean): PreviewSessionSnapshot => ({
161188
threadId: "thread-1",

‎apps/web/src/components/preview/agentOpenedPreviews.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ import * as Option from "effect/Option";
1414
import { AsyncResult } from "effect/reactivity";
1515

1616
import { getClientSettings } from "../../hooks/useSettings";
17-
import { browserMiniPlayerSource, usePreviewMiniPlayerStore } from "../../previewMiniPlayerStore";
17+
import {
18+
browserMiniPlayerSource,
19+
type PreviewMiniPlayerSource,
20+
usePreviewMiniPlayerStore,
21+
} from "../../previewMiniPlayerStore";
1822
import {
1923
previewStateAtom,
2024
previewThreadRefsAtom,
@@ -197,6 +201,20 @@ export function applyDeviceSnapshot(
197201
}
198202
}
199203

204+
/**
205+
* Floats a device panel tab over chat. A tab whose device session already
206+
* ended has nothing to stream, so it only closes the panel. Before the first
207+
* device snapshot the tab floats, and that snapshot closes it if needed.
208+
*/
209+
export function floatDeviceSurface(
210+
threadRef: ScopedThreadRef,
211+
source: Extract<PreviewMiniPlayerSource, { kind: "device" }>,
212+
state: DeviceServiceState | null,
213+
): void {
214+
if (state !== null && !hasLiveSession(state, threadRef.threadId, source)) return;
215+
usePreviewMiniPlayerStore.getState().open(threadRef, source);
216+
}
217+
200218
export function resetDeviceSessionBaselinesForTests(): void {
201219
deviceSessionBaselines.clear();
202220
}

0 commit comments

Comments
 (0)