@devries-cameron20 / t3code Mirror · read-only / commits / a5539a6b03
Fork 0

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 23, 08:00 UTC

refactor(web): move agent-opened browser and device surfacing out of ChatView (#20312)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 16, 2026 at 17:39 UTC a5539a6b0375968ba8f743b8cc364ef75971d089
3 files changed +165 -105
apps/web/src/components/ChatView.tsx
+10 -105
@@ -462,6 +462,7 @@ import {
462 import { QueuedRunsControl, type QueuedRunsControlHandle } from "./chat/QueuedRunsControl";
463 import { useThreadLinkedPullRequest } from "./pullRequest/useThreadLinkedPullRequest";
464 import { useProactivePanels } from "../hooks/useProactivePanels";
465 +import { useAgentOpenedPreviews } from "./preview/useAgentOpenedPreviews";
466 import type { ComposerBannerStackItem } from "./chat/ComposerBannerStack";
467 import { ThreadStatusLine } from "./chat/ThreadStatusLine";
468 import { formatRelativeTimeLabel, formatRelativeTimeUntilLabel } from "../timestampFormat";
@@ -670,8 +671,6 @@ const PreviewPanel = lazy(() =>
671 import("./preview/PreviewPanel").then((module) => ({ default: module.PreviewPanel })),
672 );
673 const DiffPanel = lazy(() => import("./DiffPanel"));
673 -const selectAutoShowFloatingPreview = (settings: { browserAutoShowFloatingPreview: boolean }) =>
674 - settings.browserAutoShowFloatingPreview;
674 const DevicePanel = lazy(() =>
675 import("./device/DevicePanel").then((module) => ({ default: module.DevicePanel })),
676 );
@@ -4129,110 +4128,16 @@ export default function ChatView(props: ChatViewProps) {
4128 useRightPanelStore.getState().open(activeThreadRef, "device");
4129 };
4130 const addDeviceSurfaceStable = useStableCallback(addDeviceSurface);
4132 - const autoShowFloatingPreview = useClientSettings(selectAutoShowFloatingPreview);
4133 - const previousDeviceSessions = useRef(new Map<string, Set<string>>());
4134 - useEffect(() => {
4135 - if (!activeThreadRef || !deviceStateLoaded) return;
4136 - const threadKey = `${activeThreadRef.environmentId}:${activeThreadRef.threadId}`;
4137 - const sessions = deviceState.sessions.filter(
4138 - (session) => session.threadId === activeThreadRef.threadId,
4139 - );
4140 - const key = (session: (typeof sessions)[number]) => `${session.hostId}:${session.deviceId}`;
4141 - const deviceFor = (session: (typeof sessions)[number]) =>
4142 - deviceState.devices.find(
4143 - (entry) => entry.hostId === session.hostId && entry.id === session.deviceId,
4144 - );
4145 - const previous = previousDeviceSessions.current.get(threadKey);
4146 - previousDeviceSessions.current.set(
4147 - threadKey,
4148 - new Set(sessions.filter((session) => deviceFor(session) !== undefined).map(key)),
4149 - );
4150 - if (!previous || shouldUsePlanSidebarSheet) return;
4151 - for (const session of sessions) {
4152 - if (previous.has(key(session))) continue;
4153 - const device = deviceFor(session);
4154 - if (!device) continue;
4155 - const target = {
4156 - hostId: session.hostId,
4157 - deviceId: session.deviceId,
4158 - platform: device.platform,
4159 - name: device.name,
4160 - };
4161 - if (autoShowFloatingPreview) {
4162 - usePreviewMiniPlayerStore.getState().open(activeThreadRef, { kind: "device", ...target });
4163 - continue;
4164 - }
4165 - const existing = useRightPanelStore
4166 - .getState()
4167 - .byThreadKey[scopedThreadKey(activeThreadRef)]?.surfaces.some(
4168 - (surface) =>
4169 - surface.kind === "device" &&
4170 - surface.target?.hostId === session.hostId &&
4171 - surface.target.deviceId === session.deviceId,
4172 - );
4173 - if (existing) continue;
4174 - useRightPanelStore.getState().openDevice(activeThreadRef, target, true);
4175 - }
4176 - }, [
4177 - activeThreadRef,
4178 - autoShowFloatingPreview,
4131 + useAgentOpenedPreviews({
4132 + threadRef: activeThreadRef,
4133 + deviceState,
4134 deviceStateLoaded,
4180 - shouldUsePlanSidebarSheet,
4181 - deviceState.sessions,
4182 - deviceState.devices,
4183 - ]);
4184 - const previousServerPreviewTabs = useRef(new Map<string, Map<string, string | undefined>>());
4185 - useEffect(() => {
4186 - if (!activeThreadRef || !activeEnvironmentServerBrowser || !activePreviewState.listLoaded)
4187 - return;
4188 - const threadKey = scopedThreadKey(activeThreadRef);
4189 - const serverSessions = Object.values(activePreviewState.sessions).filter(
4190 - (session) => session.runtime === "server",
4191 - );
4192 - const previous = previousServerPreviewTabs.current.get(threadKey);
4193 - previousServerPreviewTabs.current.set(
4194 - threadKey,
4195 - new Map(serverSessions.map((session) => [session.tabId, session.revealRequest?.id])),
4196 - );
4197 - if (!previous) return;
4198 - for (const session of serverSessions) {
4199 - const requested = session.revealRequest;
4200 - const fresh = requested
4201 - ? previous.get(session.tabId) !== requested.id
4202 - : !previous.has(session.tabId);
4203 - if (!fresh || session.reveal !== true) continue;
4204 - if (!autoShowFloatingPreview && requested?.force !== true) continue;
4205 - const surface = rightPanelState.surfaces.find(
4206 - (surface) => surface.kind === "preview" && surface.resourceId === session.tabId,
4207 - );
4208 - if (surface && requested?.force === true) {
4209 - useRightPanelStore.getState().activateSurface(activeThreadRef, surface.id);
4210 - } else if (!surface) {
4211 - usePreviewMiniPlayerStore
4212 - .getState()
4213 - .open(activeThreadRef, browserMiniPlayerSource(session.tabId));
4214 - }
4215 - }
4216 - }, [
4217 - activeEnvironmentServerBrowser,
4218 - activePreviewState.listLoaded,
4219 - activePreviewState.sessions,
4220 - activeThreadRef,
4221 - autoShowFloatingPreview,
4222 - rightPanelState.surfaces,
4223 - ]);
4224 - useEffect(() => {
4225 - if (!activeThreadRef || !deviceStateLoaded) return;
4226 - const source = activePreviewMiniPlayer?.source;
4227 - if (source?.kind !== "device") return;
4228 - const sessionStillExists = deviceState.sessions.some(
4229 - (session) =>
4230 - session.threadId === activeThreadRef.threadId &&
4231 - session.hostId === source.hostId &&
4232 - session.deviceId === source.deviceId,
4233 - );
4234 - if (!sessionStillExists) usePreviewMiniPlayerStore.getState().close(activeThreadRef);
4235 - }, [activePreviewMiniPlayer, activeThreadRef, deviceState.sessions, deviceStateLoaded]);
4135 + previewState: activePreviewState,
4136 + serverBrowserAvailable: activeEnvironmentServerBrowser,
4137 + panelSurfaces: rightPanelState.surfaces,
4138 + miniPlayerSource: activePreviewMiniPlayer?.source ?? null,
4139 + sheetLayout: shouldUsePlanSidebarSheet,
4140 + });
4141 const openFileSurface = (relativePath: string) => {
4142 if (!activeThreadRef || !activeProject) return;
4143 useRightPanelStore.getState().openFile(activeThreadRef, relativePath);
apps/web/src/components/preview/useAgentOpenedPreviews.ts new
+154
@@ -0,0 +1,154 @@
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 + browserMiniPlayerSource,
7 + usePreviewMiniPlayerStore,
8 + type PreviewMiniPlayerState,
9 +} from "../../previewMiniPlayerStore";
10 +import type { ThreadPreviewState } from "../../previewStateStore";
11 +import { useRightPanelStore, type RightPanelSurface } from "../../rightPanelStore";
12 +
13 +const selectAutoShowFloatingPreview = (settings: { browserAutoShowFloatingPreview: boolean }) =>
14 + settings.browserAutoShowFloatingPreview;
15 +
16 +/**
17 + * Shows browsers and devices an agent or another client opened for the thread,
18 + * and closes a floating device once its session ends.
19 + */
20 +export function useAgentOpenedPreviews(input: {
21 + readonly threadRef: ScopedThreadRef | null;
22 + readonly deviceState: DeviceServiceState;
23 + readonly deviceStateLoaded: boolean;
24 + readonly previewState: ThreadPreviewState;
25 + // Whether the environment hosts browser tabs for this client.
26 + readonly serverBrowserAvailable: boolean;
27 + readonly panelSurfaces: ReadonlyArray<RightPanelSurface>;
28 + readonly miniPlayerSource: PreviewMiniPlayerState["source"] | null;
29 + // Sheet layouts show neither floating previews nor auto-opened device tabs.
30 + readonly sheetLayout: boolean;
31 +}) {
32 + const {
33 + threadRef,
34 + deviceState,
35 + deviceStateLoaded,
36 + previewState,
37 + serverBrowserAvailable,
38 + panelSurfaces,
39 + miniPlayerSource,
40 + sheetLayout,
41 + } = input;
42 + const autoShowFloatingPreview = useClientSettings(selectAutoShowFloatingPreview);
43 + // A device the agent opens floats over chat like an agent-driven browser,
44 + // or becomes a panel tab when floating previews are off. Sessions opened by
45 + // another client arrive the same way; sheet layouts get neither. The first
46 + // snapshot is a baseline: persisted tabs restore themselves, and existing
47 + // sessions must not resurrect closed tabs. A session whose device summary
48 + // has not arrived yet stays out of the baseline so a later snapshot opens it.
49 + const previousDeviceSessions = useRef(new Map<string, Set<string>>());
50 + useEffect(() => {
51 + if (!threadRef || !deviceStateLoaded) return;
52 + const threadKey = `${threadRef.environmentId}:${threadRef.threadId}`;
53 + const sessions = deviceState.sessions.filter(
54 + (session) => session.threadId === threadRef.threadId,
55 + );
56 + const key = (session: (typeof sessions)[number]) => `${session.hostId}:${session.deviceId}`;
57 + const deviceFor = (session: (typeof sessions)[number]) =>
58 + deviceState.devices.find(
59 + (entry) => entry.hostId === session.hostId && entry.id === session.deviceId,
60 + );
61 + const previous = previousDeviceSessions.current.get(threadKey);
62 + previousDeviceSessions.current.set(
63 + threadKey,
64 + new Set(sessions.filter((session) => deviceFor(session) !== undefined).map(key)),
65 + );
66 + if (!previous || sheetLayout) return;
67 + for (const session of sessions) {
68 + if (previous.has(key(session))) continue;
69 + const device = deviceFor(session);
70 + if (!device) continue;
71 + const target = {
72 + hostId: session.hostId,
73 + deviceId: session.deviceId,
74 + platform: device.platform,
75 + name: device.name,
76 + };
77 + if (autoShowFloatingPreview) {
78 + usePreviewMiniPlayerStore.getState().open(threadRef, { kind: "device", ...target });
79 + continue;
80 + }
81 + const existing = useRightPanelStore
82 + .getState()
83 + .byThreadKey[scopedThreadKey(threadRef)]?.surfaces.some(
84 + (surface) =>
85 + surface.kind === "device" &&
86 + surface.target?.hostId === session.hostId &&
87 + surface.target.deviceId === session.deviceId,
88 + );
89 + if (existing) continue;
90 + useRightPanelStore.getState().openDevice(threadRef, target, true);
91 + }
92 + }, [
93 + threadRef,
94 + autoShowFloatingPreview,
95 + deviceStateLoaded,
96 + sheetLayout,
97 + deviceState.sessions,
98 + deviceState.devices,
99 + ]);
100 + // Baseline loaded tabs so reloads never reopen previews the user dismissed.
101 + const previousServerPreviewTabs = useRef(new Map<string, Map<string, string | undefined>>());
102 + useEffect(() => {
103 + if (!threadRef || !serverBrowserAvailable || !previewState.listLoaded) return;
104 + const threadKey = scopedThreadKey(threadRef);
105 + const serverSessions = Object.values(previewState.sessions).filter(
106 + (session) => session.runtime === "server",
107 + );
108 + const previous = previousServerPreviewTabs.current.get(threadKey);
109 + previousServerPreviewTabs.current.set(
110 + threadKey,
111 + new Map(serverSessions.map((session) => [session.tabId, session.revealRequest?.id])),
112 + );
113 + if (!previous) return;
114 + for (const session of serverSessions) {
115 + const requested = session.revealRequest;
116 + const fresh = requested
117 + ? previous.get(session.tabId) !== requested.id
118 + : !previous.has(session.tabId);
119 + if (!fresh || session.reveal !== true) continue;
120 + if (!autoShowFloatingPreview && requested?.force !== true) continue;
121 + const surface = panelSurfaces.find(
122 + (surface) => surface.kind === "preview" && surface.resourceId === session.tabId,
123 + );
124 + if (surface && requested?.force === true) {
125 + useRightPanelStore.getState().activateSurface(threadRef, surface.id);
126 + } else if (!surface) {
127 + usePreviewMiniPlayerStore
128 + .getState()
129 + .open(threadRef, browserMiniPlayerSource(session.tabId));
130 + }
131 + }
132 + }, [
133 + serverBrowserAvailable,
134 + previewState.listLoaded,
135 + previewState.sessions,
136 + threadRef,
137 + autoShowFloatingPreview,
138 + panelSurfaces,
139 + ]);
140 + // A floating device follows its session: once the agent or another client
141 + // closes the device there is nothing left to stream.
142 + useEffect(() => {
143 + if (!threadRef || !deviceStateLoaded) return;
144 + const source = miniPlayerSource;
145 + if (source?.kind !== "device") return;
146 + const sessionStillExists = deviceState.sessions.some(
147 + (session) =>
148 + session.threadId === threadRef.threadId &&
149 + session.hostId === source.hostId &&
150 + session.deviceId === source.deviceId,
151 + );
152 + if (!sessionStillExists) usePreviewMiniPlayerStore.getState().close(threadRef);
153 + }, [miniPlayerSource, threadRef, deviceState.sessions, deviceStateLoaded]);
154 +}
vite.config.ts
+1
@@ -331,6 +331,7 @@ export default defineConfig({
331 "apps/web/src/components/chat/useQueuedRunEdit.ts",
332 "apps/web/src/components/chat/useUsageLimitsPanel.ts",
333 "apps/web/src/components/pullRequest/useThreadLinkedPullRequest.ts",
334 + "apps/web/src/components/preview/useAgentOpenedPreviews.ts",
335 "apps/web/src/components/terminal/**",
336 "apps/web/src/hooks/useProactivePanels.ts",
337 ],