The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 15:45 UTC
fix(web): devices agents open in background threads now show up (#25714)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 26, 2026 at 19:19 UTC
10a82e86ed29e03821056a7a8f19ee49943595ec
5 files changed
+333
-105
apps/web/src/components/ChatView.tsx
+1
-8
@@ -303,7 +303,6 @@ import { useThreadLinkedPullRequest } from "./pullRequest/useThreadLinkedPullReq
303
import { useProactivePanels } from "../hooks/useProactivePanels";
304
import { useIncrementalTimelineEntries } from "../hooks/useIncrementalTimelineEntries";
305
import { useRightPanelSurfaceClose } from "../hooks/useRightPanelSurfaceClose";
306
-import { useAgentOpenedPreviews } from "./preview/useAgentOpenedPreviews";
306
import type { ComposerBannerStackItem } from "./chat/ComposerBannerStack";
307
import { ComposerSurface } from "./chat/ComposerSurface";
308
import { resolveThreadSyncPhase } from "../threadSync";
@@ -1843,13 +1842,6 @@ export default function ChatView(props: ChatViewProps) {
1842
const { state: deviceState, loaded: deviceStateLoaded } = useDeviceState(
1843
activeThreadRef?.environmentId ?? null,
1844
);
1846
- useAgentOpenedPreviews({
1847
- threadRef: activeThreadRef,
1848
- deviceState,
1849
- deviceStateLoaded,
1850
- miniPlayerSource: activePreviewMiniPlayer?.source ?? null,
1851
- sheetLayout: shouldUsePlanSidebarSheet,
1852
- });
1845
// The thread's own change request, placed against the project it belongs to. Without a
1846
// project there is nothing to resolve it against, so the caller falls back to the browser.
1847
const activeProjectRepository = sourceControlClients.repositorySelector(
@@ -2329,6 +2321,7 @@ export default function ChatView(props: ChatViewProps) {
2321
activeRightPanelSurface,
2322
activePreviewState,
2323
deviceState,
2324
+ deviceStateLoaded,
2325
pullRequestPanelTarget,
2326
threadPanelPresentation,
2327
isServerThread,
apps/web/src/components/chat/useRightPanelActions.ts
+8
-3
@@ -23,6 +23,7 @@ import { BrowserSettingsReadError } from "../../browser/openFileInPreview";
23
import { addBrowserSurface } from "../preview/addBrowserSurface";
24
import { focusActivePanelTerminal, focusTerminal } from "../../lib/terminalFocus";
25
import { subscribePreviewAction } from "../preview/previewActionBus";
26
+import { floatDeviceSurface } from "../preview/agentOpenedPreviews";
27
import { browserMiniPlayerSource, usePreviewMiniPlayerStore } from "../../previewMiniPlayerStore";
28
import type { threadPullRequestPanelTarget } from "../pullRequest/pullRequestDetail.logic";
29
import type { useDeviceState } from "../../state/device";
@@ -37,6 +38,7 @@ interface RightPanelActionsInput {
38
readonly activeRightPanelSurface: RightPanelSurface | null;
39
readonly activePreviewState: ThreadPreviewState;
40
readonly deviceState: ReturnType<typeof useDeviceState>["state"];
41
+ readonly deviceStateLoaded: boolean;
42
readonly pullRequestPanelTarget: ReturnType<typeof threadPullRequestPanelTarget> | null;
43
readonly threadPanelPresentation: ThreadPanelPresentation;
44
readonly isServerThread: boolean;
@@ -69,6 +71,7 @@ export function useRightPanelActions(input: RightPanelActionsInput) {
71
activeRightPanelSurface,
72
activePreviewState,
73
deviceState,
74
+ deviceStateLoaded,
75
pullRequestPanelTarget,
76
threadPanelPresentation,
77
isServerThread,
@@ -164,9 +167,11 @@ export function useRightPanelActions(input: RightPanelActionsInput) {
167
.getState()
168
.open(activeThreadRef, browserMiniPlayerSource(activeRightPanelSurface.resourceId));
169
} 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
+ );
175
}
176
useRightPanelStore.getState().setMaximized(activeThreadRef, false);
177
useRightPanelStore.getState().close(activeThreadRef);
apps/web/src/components/preview/agentOpenedPreviews.test.ts
+162
-1
@@ -1,5 +1,10 @@
1
import { scopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime/environment";
2
-import { type EnvironmentId, type PreviewSessionSnapshot, ThreadId } from "@t3tools/contracts";
2
+import {
3
+ type DeviceServiceState,
4
+ type EnvironmentId,
5
+ type PreviewSessionSnapshot,
6
+ ThreadId,
7
+} from "@t3tools/contracts";
8
import { describe, expect, it, vi } from "vite-plus/test";
9
10
import { browserMiniPlayerSource, usePreviewMiniPlayerStore } from "../../previewMiniPlayerStore";
@@ -14,7 +19,10 @@ import {
19
} from "../../previewStateStore";
20
import { useRightPanelStore } from "../../rightPanelStore";
21
import {
22
+ applyDeviceSnapshot,
23
+ floatDeviceSurface,
24
followPreviewSessions,
25
+ resetDeviceSessionBaselinesForTests,
26
revealServerTabs,
27
startAgentOpenedPreviews,
28
} from "./agentOpenedPreviews";
@@ -26,14 +34,167 @@ vi.mock("../../state/entities", () => ({
34
const ref = scopeThreadRef("env-1" as EnvironmentId, ThreadId.make("thread-1"));
35
const threadKey = scopedThreadKey(ref);
36
37
+const device = {
38
+ hostId: "local",
39
+ id: "pixel",
40
+ platform: "android",
41
+ name: "Pixel 8",
42
+ version: "15",
43
+ booted: true,
44
+ physical: false,
45
+} as const;
46
+
47
+const session = {
48
+ threadId: ref.threadId,
49
+ hostId: "local",
50
+ deviceId: "pixel",
51
+ platform: "android",
52
+ openedAt: "2026-01-01T00:00:00.000Z",
53
+} as const;
54
+
55
+const deviceState = (input: {
56
+ readonly sessions?: ReadonlyArray<
57
+ Omit<typeof session, "openedAt"> & { readonly openedAt: string }
58
+ >;
59
+ readonly devices?: ReadonlyArray<typeof device>;
60
+}) =>
61
+ ({
62
+ hosts: [],
63
+ hostStatus: "ready",
64
+ hostStatuses: {},
65
+ devices: input.devices ?? [device],
66
+ sessions: input.sessions ?? [],
67
+ onboardingCompleted: true,
68
+ agentAccessEnabled: true,
69
+ hubBasePath: "/api/device-hub",
70
+ revision: 0,
71
+ }) as unknown as DeviceServiceState;
72
+
73
+const floating = { sheetLayout: false, autoShowFloatingPreview: true };
74
+
75
function setup() {
76
resetPreviewStateForTests();
77
+ resetDeviceSessionBaselinesForTests();
78
usePreviewMiniPlayerStore.setState({ byThreadKey: {} });
79
useRightPanelStore.setState({ byThreadKey: {} });
80
}
81
82
const miniPlayerSource = () => usePreviewMiniPlayerStore.getState().byThreadKey[threadKey]?.source;
83
84
+describe("applyDeviceSnapshot", () => {
85
+ it("treats the first snapshot as a baseline and floats sessions that start later", () => {
86
+ setup();
87
+ applyDeviceSnapshot(ref.environmentId, deviceState({ sessions: [session] }), floating);
88
+ expect(miniPlayerSource()).toBeUndefined();
89
+
90
+ applyDeviceSnapshot(ref.environmentId, deviceState({ sessions: [] }), floating);
91
+ applyDeviceSnapshot(ref.environmentId, deviceState({ sessions: [session] }), floating);
92
+ expect(miniPlayerSource()).toMatchObject({ kind: "device", deviceId: "pixel" });
93
+ });
94
+
95
+ it("opens a session once its device summary arrives", () => {
96
+ setup();
97
+ applyDeviceSnapshot(ref.environmentId, deviceState({}), floating);
98
+ applyDeviceSnapshot(
99
+ ref.environmentId,
100
+ deviceState({ sessions: [session], devices: [] }),
101
+ floating,
102
+ );
103
+ expect(miniPlayerSource()).toBeUndefined();
104
+
105
+ applyDeviceSnapshot(ref.environmentId, deviceState({ sessions: [session] }), floating);
106
+ expect(miniPlayerSource()).toMatchObject({ kind: "device", name: "Pixel 8" });
107
+ });
108
+
109
+ it("opens a panel tab once when floating previews are off, and nothing in sheet layouts", () => {
110
+ setup();
111
+ const panel = { sheetLayout: false, autoShowFloatingPreview: false };
112
+ applyDeviceSnapshot(ref.environmentId, deviceState({}), { ...panel, sheetLayout: true });
113
+ applyDeviceSnapshot(ref.environmentId, deviceState({ sessions: [session] }), {
114
+ ...panel,
115
+ sheetLayout: true,
116
+ });
117
+ expect(useRightPanelStore.getState().byThreadKey[threadKey]).toBeUndefined();
118
+
119
+ applyDeviceSnapshot(ref.environmentId, deviceState({}), panel);
120
+ applyDeviceSnapshot(ref.environmentId, deviceState({ sessions: [session] }), panel);
121
+ applyDeviceSnapshot(ref.environmentId, deviceState({}), panel);
122
+ applyDeviceSnapshot(ref.environmentId, deviceState({ sessions: [session] }), panel);
123
+ const devices = useRightPanelStore
124
+ .getState()
125
+ .byThreadKey[threadKey]?.surfaces.filter((surface) => surface.kind === "device");
126
+ expect(devices).toHaveLength(1);
127
+ expect(miniPlayerSource()).toBeUndefined();
128
+ });
129
+
130
+ it("closes a floating device when its session ends, even before a baseline", () => {
131
+ setup();
132
+ usePreviewMiniPlayerStore.getState().open(ref, {
133
+ kind: "device",
134
+ hostId: "local",
135
+ deviceId: "pixel",
136
+ platform: "android",
137
+ name: "Pixel 8",
138
+ });
139
+ applyDeviceSnapshot(ref.environmentId, deviceState({ sessions: [session] }), floating);
140
+ expect(miniPlayerSource()).toMatchObject({ kind: "device" });
141
+
142
+ applyDeviceSnapshot(ref.environmentId, deviceState({ sessions: [] }), floating);
143
+ expect(miniPlayerSource()).toBeUndefined();
144
+ });
145
+
146
+ it("opens a session that ended and reopened between two snapshots", () => {
147
+ setup();
148
+ applyDeviceSnapshot(ref.environmentId, deviceState({ sessions: [session] }), floating);
149
+ expect(miniPlayerSource()).toBeUndefined();
150
+
151
+ const reopened = { ...session, openedAt: "2026-01-01T00:05:00.000Z" };
152
+ applyDeviceSnapshot(ref.environmentId, deviceState({ sessions: [reopened] }), floating);
153
+ expect(miniPlayerSource()).toMatchObject({ kind: "device", deviceId: "pixel" });
154
+ });
155
+
156
+ it("opens a device for a thread that had no sessions when the baseline was taken", () => {
157
+ setup();
158
+ const otherRef = scopeThreadRef(ref.environmentId, ThreadId.make("thread-2"));
159
+ applyDeviceSnapshot(ref.environmentId, deviceState({ sessions: [session] }), floating);
160
+ applyDeviceSnapshot(
161
+ ref.environmentId,
162
+ deviceState({ sessions: [session, { ...session, threadId: otherRef.threadId }] }),
163
+ floating,
164
+ );
165
+ expect(miniPlayerSource()).toBeUndefined();
166
+ expect(
167
+ usePreviewMiniPlayerStore.getState().byThreadKey[scopedThreadKey(otherRef)]?.source,
168
+ ).toMatchObject({ kind: "device", deviceId: "pixel" });
169
+ });
170
+});
171
+
172
+describe("floatDeviceSurface", () => {
173
+ const source = {
174
+ kind: "device",
175
+ hostId: "local",
176
+ deviceId: "pixel",
177
+ platform: "android",
178
+ name: "Pixel 8",
179
+ } as const;
180
+
181
+ it("floats a device with a live session, or before device state loads", () => {
182
+ setup();
183
+ floatDeviceSurface(ref, source, deviceState({ sessions: [session] }));
184
+ expect(miniPlayerSource()).toMatchObject({ kind: "device", deviceId: "pixel" });
185
+
186
+ setup();
187
+ floatDeviceSurface(ref, source, null);
188
+ expect(miniPlayerSource()).toMatchObject({ kind: "device", deviceId: "pixel" });
189
+ });
190
+
191
+ it("does not float a device whose session ended", () => {
192
+ setup();
193
+ floatDeviceSurface(ref, source, deviceState({ sessions: [] }));
194
+ expect(miniPlayerSource()).toBeUndefined();
195
+ });
196
+});
197
+
198
describe("revealServerTabs", () => {
199
const tab = (tabId: string, force?: boolean): PreviewSessionSnapshot => ({
200
threadId: "thread-1",
apps/web/src/components/preview/agentOpenedPreviews.ts
+162
-4
@@ -1,16 +1,35 @@
1
-import { scopedThreadKey } from "@t3tools/client-runtime/environment";
2
-import type { PreviewSessionSnapshot, ScopedThreadRef } from "@t3tools/contracts";
1
+import {
2
+ parseScopedThreadKey,
3
+ scopedThreadKey,
4
+ scopeThreadRef,
5
+} from "@t3tools/client-runtime/environment";
6
+import type {
7
+ DeviceServiceState,
8
+ EnvironmentId,
9
+ PreviewSessionSnapshot,
10
+ ScopedThreadRef,
11
+ ThreadId,
12
+} from "@t3tools/contracts";
13
+import * as Option from "effect/Option";
14
+import { AsyncResult } from "effect/reactivity";
15
16
import { getClientSettings } from "../../hooks/useSettings";
5
-import { browserMiniPlayerSource, usePreviewMiniPlayerStore } from "../../previewMiniPlayerStore";
17
+import {
18
+ browserMiniPlayerSource,
19
+ type PreviewMiniPlayerSource,
20
+ usePreviewMiniPlayerStore,
21
+} from "../../previewMiniPlayerStore";
22
import {
23
previewStateAtom,
24
previewThreadRefsAtom,
25
type ThreadPreviewState,
26
} from "../../previewStateStore";
27
import { useRightPanelStore } from "../../rightPanelStore";
28
+import { RIGHT_PANEL_INLINE_LAYOUT_MEDIA_QUERY } from "../../rightPanelLayout";
29
import { appAtomRegistry } from "../../rpc/atomRegistry";
30
+import { deviceEnvironment } from "../../state/device";
31
import { readEnvironmentSupportsServerBrowser } from "../../state/entities";
32
+import { environmentSummaries } from "../../state/presentation";
33
34
/**
35
* Shows browsers an agent or another client revealed for a thread: floating
@@ -99,7 +118,126 @@ function followThread(threadKey: string, threadRef: ScopedThreadRef): () => void
118
});
119
}
120
102
-/** Follows every thread's browser previews for the life of the page, not just the open thread's. */
121
+// `openedAt` tells a session apart from one that ended and reopened on the same device while
122
+// this client was disconnected.
123
+const sessionKey = (session: DeviceServiceState["sessions"][number]) =>
124
+ `${session.threadId}:${session.hostId}:${session.deviceId}:${session.openedAt}`;
125
+
126
+/**
127
+ * Per environment: the device sessions seen in the last snapshot. The first
128
+ * snapshot is a baseline, so persisted tabs restore themselves and existing
129
+ * sessions never resurrect tabs the user closed. Baselines outlive a
130
+ * disconnect, so a device an agent opened during the gap still shows.
131
+ */
132
+const deviceSessionBaselines = new Map<EnvironmentId, ReadonlySet<string>>();
133
+
134
+function hasLiveSession(
135
+ state: DeviceServiceState,
136
+ threadId: ThreadId,
137
+ target: { readonly hostId: string; readonly deviceId: string },
138
+): boolean {
139
+ return state.sessions.some(
140
+ (session) =>
141
+ session.threadId === threadId &&
142
+ session.hostId === target.hostId &&
143
+ session.deviceId === target.deviceId,
144
+ );
145
+}
146
+
147
+/**
148
+ * A device the agent opens floats over its thread's chat, or becomes a panel tab
149
+ * when floating previews are off; sheet layouts get neither. A session without a
150
+ * device summary yet stays out of the baseline so a later snapshot opens it.
151
+ * Floating devices whose session ended close here.
152
+ */
153
+export function applyDeviceSnapshot(
154
+ environmentId: EnvironmentId,
155
+ state: DeviceServiceState,
156
+ options: { readonly sheetLayout: boolean; readonly autoShowFloatingPreview: boolean },
157
+): void {
158
+ const deviceFor = (session: DeviceServiceState["sessions"][number]) =>
159
+ state.devices.find((entry) => entry.hostId === session.hostId && entry.id === session.deviceId);
160
+
161
+ const miniPlayer = usePreviewMiniPlayerStore.getState();
162
+ for (const [threadKey, entry] of Object.entries(miniPlayer.byThreadKey)) {
163
+ if (entry.source.kind !== "device") continue;
164
+ const ref = parseScopedThreadKey(threadKey);
165
+ if (ref?.environmentId === environmentId && !hasLiveSession(state, ref.threadId, entry.source))
166
+ miniPlayer.close(ref);
167
+ }
168
+
169
+ const previous = deviceSessionBaselines.get(environmentId);
170
+ deviceSessionBaselines.set(
171
+ environmentId,
172
+ new Set(state.sessions.filter((session) => deviceFor(session) !== undefined).map(sessionKey)),
173
+ );
174
+ if (!previous || options.sheetLayout) return;
175
+ for (const session of state.sessions) {
176
+ if (previous.has(sessionKey(session))) continue;
177
+ const device = deviceFor(session);
178
+ if (!device) continue;
179
+ const threadRef = scopeThreadRef(environmentId, session.threadId);
180
+ const target = {
181
+ hostId: session.hostId,
182
+ deviceId: session.deviceId,
183
+ platform: device.platform,
184
+ name: device.name,
185
+ };
186
+ if (options.autoShowFloatingPreview) {
187
+ usePreviewMiniPlayerStore.getState().open(threadRef, { kind: "device", ...target });
188
+ continue;
189
+ }
190
+ const existing = useRightPanelStore
191
+ .getState()
192
+ .byThreadKey[scopedThreadKey(threadRef)]?.surfaces.some(
193
+ (surface) =>
194
+ surface.kind === "device" &&
195
+ surface.target?.hostId === session.hostId &&
196
+ surface.target.deviceId === session.deviceId,
197
+ );
198
+ if (existing) continue;
199
+ useRightPanelStore.getState().openDevice(threadRef, target, true);
200
+ }
201
+}
202
+
203
+/**
204
+ * Floats a device panel tab over chat. A tab whose device session already
205
+ * ended has nothing to stream, so it only closes the panel. Before the first
206
+ * device snapshot the tab floats, and that snapshot closes it if needed.
207
+ */
208
+export function floatDeviceSurface(
209
+ threadRef: ScopedThreadRef,
210
+ source: Extract<PreviewMiniPlayerSource, { kind: "device" }>,
211
+ state: DeviceServiceState | null,
212
+): void {
213
+ if (state !== null && !hasLiveSession(state, threadRef.threadId, source)) return;
214
+ usePreviewMiniPlayerStore.getState().open(threadRef, source);
215
+}
216
+
217
+export function resetDeviceSessionBaselinesForTests(): void {
218
+ deviceSessionBaselines.clear();
219
+}
220
+
221
+function watchEnvironmentDevices(environmentId: EnvironmentId): () => void {
222
+ return appAtomRegistry.subscribe(
223
+ deviceEnvironment.state({ environmentId, input: {} }),
224
+ (result) => {
225
+ const state = Option.getOrNull(AsyncResult.value(result));
226
+ if (state === null) return;
227
+ applyDeviceSnapshot(environmentId, state, {
228
+ sheetLayout: window.matchMedia(RIGHT_PANEL_INLINE_LAYOUT_MEDIA_QUERY).matches,
229
+ autoShowFloatingPreview: getClientSettings().browserAutoShowFloatingPreview,
230
+ });
231
+ },
232
+ { immediate: true },
233
+ );
234
+}
235
+
236
+/**
237
+ * Follows every thread's browser previews and every connected environment's
238
+ * device sessions for the life of the page, not just the open thread's: an
239
+ * agent in a background thread can open either.
240
+ */
241
export function startAgentOpenedPreviews(): () => void {
242
const followed = new Map<string, () => void>();
243
const followNewThreads = (refs: ReadonlyMap<string, ScopedThreadRef>) => {
@@ -110,8 +248,28 @@ export function startAgentOpenedPreviews(): () => void {
248
const stopIndex = appAtomRegistry.subscribe(previewThreadRefsAtom, followNewThreads, {
249
immediate: true,
250
});
251
+ const watchedEnvironments = new Map<EnvironmentId, () => void>();
252
+ const watchConnectedEnvironments = (environmentIds: ReadonlyArray<EnvironmentId>) => {
253
+ for (const [environmentId, stop] of watchedEnvironments) {
254
+ if (environmentIds.includes(environmentId)) continue;
255
+ stop();
256
+ watchedEnvironments.delete(environmentId);
257
+ }
258
+ for (const environmentId of environmentIds) {
259
+ if (!watchedEnvironments.has(environmentId)) {
260
+ watchedEnvironments.set(environmentId, watchEnvironmentDevices(environmentId));
261
+ }
262
+ }
263
+ };
264
+ const stopEnvironments = appAtomRegistry.subscribe(
265
+ environmentSummaries.connectedEnvironmentIdsAtom,
266
+ watchConnectedEnvironments,
267
+ { immediate: true },
268
+ );
269
return () => {
270
stopIndex();
271
+ stopEnvironments();
272
for (const stop of followed.values()) stop();
273
+ for (const stop of watchedEnvironments.values()) stop();
274
};
275
}
apps/web/src/components/preview/useAgentOpenedPreviews.ts
deleted
-89
@@ -1,89 +0,0 @@
1
-import type { DeviceServiceState, ScopedThreadRef } from "@t3tools/contracts";
2
-import { scopedThreadKey } from "@t3tools/client-runtime/environment";
3
-import { useEffect, useRef } from "react";
4
-import { useClientSettings } from "../../hooks/useSettings";
5
-import {
6
- usePreviewMiniPlayerStore,
7
- type PreviewMiniPlayerState,
8
-} from "../../previewMiniPlayerStore";
9
-import { useRightPanelStore } from "../../rightPanelStore";
10
-
11
-const selectAutoShowFloatingPreview = (settings: { browserAutoShowFloatingPreview: boolean }) =>
12
- settings.browserAutoShowFloatingPreview;
13
-
14
-export function useAgentOpenedPreviews(input: {
15
- readonly threadRef: ScopedThreadRef | null;
16
- readonly deviceState: DeviceServiceState;
17
- readonly deviceStateLoaded: boolean;
18
- readonly miniPlayerSource: PreviewMiniPlayerState["source"] | null;
19
- // Sheet layouts show neither floating previews nor auto-opened device tabs.
20
- readonly sheetLayout: boolean;
21
-}) {
22
- const { threadRef, deviceState, deviceStateLoaded, miniPlayerSource, sheetLayout } = input;
23
- const autoShowFloatingPreview = useClientSettings(selectAutoShowFloatingPreview);
24
- // Baseline sessions to preserve dismissals on reload; missing device summaries must remain eligible to open
25
- // later.
26
- const previousDeviceSessions = useRef(new Map<string, Set<string>>());
27
- useEffect(() => {
28
- if (!threadRef || !deviceStateLoaded) return;
29
- const threadKey = `${threadRef.environmentId}:${threadRef.threadId}`;
30
- const sessions = deviceState.sessions.filter(
31
- (session) => session.threadId === threadRef.threadId,
32
- );
33
- const key = (session: (typeof sessions)[number]) => `${session.hostId}:${session.deviceId}`;
34
- const deviceFor = (session: (typeof sessions)[number]) =>
35
- deviceState.devices.find(
36
- (entry) => entry.hostId === session.hostId && entry.id === session.deviceId,
37
- );
38
- const previous = previousDeviceSessions.current.get(threadKey);
39
- previousDeviceSessions.current.set(
40
- threadKey,
41
- new Set(sessions.filter((session) => deviceFor(session) !== undefined).map(key)),
42
- );
43
- if (!previous || sheetLayout) return;
44
- for (const session of sessions) {
45
- if (previous.has(key(session))) continue;
46
- const device = deviceFor(session);
47
- if (!device) continue;
48
- const target = {
49
- hostId: session.hostId,
50
- deviceId: session.deviceId,
51
- platform: device.platform,
52
- name: device.name,
53
- };
54
- if (autoShowFloatingPreview) {
55
- usePreviewMiniPlayerStore.getState().open(threadRef, { kind: "device", ...target });
56
- continue;
57
- }
58
- const existing = useRightPanelStore
59
- .getState()
60
- .byThreadKey[scopedThreadKey(threadRef)]?.surfaces.some(
61
- (surface) =>
62
- surface.kind === "device" &&
63
- surface.target?.hostId === session.hostId &&
64
- surface.target.deviceId === session.deviceId,
65
- );
66
- if (existing) continue;
67
- useRightPanelStore.getState().openDevice(threadRef, target, true);
68
- }
69
- }, [
70
- threadRef,
71
- autoShowFloatingPreview,
72
- deviceStateLoaded,
73
- sheetLayout,
74
- deviceState.sessions,
75
- deviceState.devices,
76
- ]);
77
- useEffect(() => {
78
- if (!threadRef || !deviceStateLoaded) return;
79
- const source = miniPlayerSource;
80
- if (source?.kind !== "device") return;
81
- const sessionStillExists = deviceState.sessions.some(
82
- (session) =>
83
- session.threadId === threadRef.threadId &&
84
- session.hostId === source.hostId &&
85
- session.deviceId === source.deviceId,
86
- );
87
- if (!sessionStillExists) usePreviewMiniPlayerStore.getState().close(threadRef);
88
- }, [miniPlayerSource, threadRef, deviceState.sessions, deviceStateLoaded]);
89
-}