Skip to content

Commit ea4d849

Browse files
fix(devices): keep independent tabs and unify connection loading
1 parent 4ec975f commit ea4d849

7 files changed

Lines changed: 429 additions & 199 deletions

File tree

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

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4143,7 +4143,9 @@ export default function ChatView(props: ChatViewProps) {
41434143
if (!activeThreadRef || !supportsThreadPullRequests) return;
41444144
useRightPanelStore.getState().open(activeThreadRef, "pull-requests");
41454145
}, [activeThreadRef, supportsThreadPullRequests]);
4146-
const { state: deviceState } = useDeviceState(activeThreadRef?.environmentId ?? null);
4146+
const { state: deviceState, loaded: deviceStateLoaded } = useDeviceState(
4147+
activeThreadRef?.environmentId ?? null,
4148+
);
41474149
const [deviceSetupThread, setDeviceSetupThread] = useState<ScopedThreadRef | null>(null);
41484150
const addDeviceSurface = useCallback(() => {
41494151
if (!activeThreadRef) return;
@@ -4153,24 +4155,53 @@ export default function ChatView(props: ChatViewProps) {
41534155
}
41544156
useRightPanelStore.getState().open(activeThreadRef, "device");
41554157
}, [activeThreadRef, deviceState.onboardingCompleted, deviceState.hostStatus]);
4156-
// An agent's `device_open` surfaces in every client the same way a
4157-
// `preview_open` does: the thread starts a device or gains a session and the panel
4158-
// opens on it. Closing the last session leaves the tab in place so the
4159-
// user keeps their picker; only new sessions raise the panel.
4160-
const threadDeviceSessionCount = activeThreadRef
4161-
? deviceState.sessions.filter((session) => session.threadId === activeThreadRef.threadId)
4162-
.length +
4163-
(deviceState.bootingDevices?.filter((device) => device.threadId === activeThreadRef.threadId)
4164-
.length ?? 0)
4165-
: 0;
4166-
const previousDeviceSessionCount = useRef(threadDeviceSessionCount);
4158+
// Reconcile new server sessions into separate tabs, including sessions opened
4159+
// by an agent or another client. The first snapshot is a baseline: persisted
4160+
// tabs restore themselves, and existing sessions must not resurrect closed tabs.
4161+
const previousDeviceSessions = useRef(new Map<string, Set<string>>());
41674162
useEffect(() => {
4168-
const previous = previousDeviceSessionCount.current;
4169-
previousDeviceSessionCount.current = threadDeviceSessionCount;
4170-
if (!activeThreadRef || threadDeviceSessionCount <= previous) return;
4171-
if (shouldUseRightPanelSheet) return;
4172-
useRightPanelStore.getState().open(activeThreadRef, "device");
4173-
}, [activeThreadRef, shouldUseRightPanelSheet, threadDeviceSessionCount]);
4163+
if (!activeThreadRef || !deviceStateLoaded) return;
4164+
const threadKey = `${activeThreadRef.environmentId}:${activeThreadRef.threadId}`;
4165+
const sessions = deviceState.sessions.filter(
4166+
(session) => session.threadId === activeThreadRef.threadId,
4167+
);
4168+
const key = (session: (typeof sessions)[number]) => `${session.hostId}:${session.deviceId}`;
4169+
const previous = previousDeviceSessions.current.get(threadKey);
4170+
previousDeviceSessions.current.set(threadKey, new Set(sessions.map(key)));
4171+
if (!previous || shouldUseRightPanelSheet) return;
4172+
for (const session of sessions) {
4173+
if (previous?.has(key(session))) continue;
4174+
const existing = useRightPanelStore
4175+
.getState()
4176+
.byThreadKey[scopedThreadKey(activeThreadRef)]?.surfaces.some(
4177+
(surface) =>
4178+
surface.kind === "device" &&
4179+
surface.target?.hostId === session.hostId &&
4180+
surface.target.deviceId === session.deviceId,
4181+
);
4182+
if (existing) continue;
4183+
const device = deviceState.devices.find(
4184+
(entry) => entry.hostId === session.hostId && entry.id === session.deviceId,
4185+
);
4186+
if (!device) continue;
4187+
useRightPanelStore.getState().openDevice(
4188+
activeThreadRef,
4189+
{
4190+
hostId: session.hostId,
4191+
deviceId: session.deviceId,
4192+
platform: device.platform,
4193+
name: device.name,
4194+
},
4195+
true,
4196+
);
4197+
}
4198+
}, [
4199+
activeThreadRef,
4200+
deviceStateLoaded,
4201+
shouldUseRightPanelSheet,
4202+
deviceState.sessions,
4203+
deviceState.devices,
4204+
]);
41744205
const openFileSurface = useCallback(
41754206
(relativePath: string) => {
41764207
if (!activeThreadRef || !activeProject) return;
@@ -8142,7 +8173,8 @@ export default function ChatView(props: ChatViewProps) {
81428173
<DevicePanel
81438174
mode="embedded"
81448175
threadRef={activeThreadRef}
8145-
deviceId={null}
8176+
key={renderedRightPanelSurface.id}
8177+
surface={renderedRightPanelSurface}
81468178
visible={rightPanelOpen}
81478179
onDismissSetup={() => {
81488180
closeRightPanelSurface(renderedRightPanelSurface);
@@ -8714,6 +8746,10 @@ export default function ChatView(props: ChatViewProps) {
87148746
terminalLabelsById={activeTerminalLabelsById}
87158747
onActivate={activateRightPanelSurface}
87168748
onCloseSurface={closeRightPanelSurface}
8749+
onRenameDevice={(surfaceId, title) => {
8750+
if (activeThreadRef)
8751+
useRightPanelStore.getState().renameDevice(activeThreadRef, surfaceId, title);
8752+
}}
87178753
onCloseOtherSurfaces={closeOtherRightPanelSurfaces}
87188754
onCloseSurfacesToRight={closeRightPanelSurfacesToRight}
87198755
onCloseAllSurfaces={closeAllRightPanelSurfaces}
@@ -8768,6 +8804,10 @@ export default function ChatView(props: ChatViewProps) {
87688804
terminalLabelsById={activeTerminalLabelsById}
87698805
onActivate={activateRightPanelSurface}
87708806
onCloseSurface={closeRightPanelSurface}
8807+
onRenameDevice={(surfaceId, title) => {
8808+
if (activeThreadRef)
8809+
useRightPanelStore.getState().renameDevice(activeThreadRef, surfaceId, title);
8810+
}}
87718811
onCloseOtherSurfaces={closeOtherRightPanelSurfaces}
87728812
onCloseSurfacesToRight={closeRightPanelSurfacesToRight}
87738813
onCloseAllSurfaces={closeAllRightPanelSurfaces}

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

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import type { RightPanelSurface } from "~/rightPanelStore";
4646
import { cn } from "~/lib/utils";
4747
import { readLocalApi } from "~/localApi";
4848
import { Button } from "~/components/ui/button";
49+
import { AndroidIcon, AppleIcon } from "~/components/Icons";
4950
import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip";
5051
import { Kbd } from "~/components/ui/kbd";
5152
import {
@@ -97,6 +98,7 @@ interface RightPanelTabsProps {
9798
previewRuntimeTabId?: ((tabId: string) => string) | undefined;
9899
terminalLabelsById: ReadonlyMap<string, string>;
99100
onActivate: (surface: RightPanelSurface) => void;
101+
onRenameDevice?: (surfaceId: string, title: string) => void;
100102
onCloseSurface: (surface: RightPanelSurface) => void;
101103
onCloseOtherSurfaces: (surface: RightPanelSurface) => void;
102104
onCloseSurfacesToRight: (surface: RightPanelSurface) => void;
@@ -182,6 +184,7 @@ const SURFACE_UNAVAILABLE_HINTS = {
182184
} as const;
183185

184186
type TabContextMenuAction =
187+
| "rename"
185188
| "copy-path"
186189
| "toggle-mute"
187190
| "close"
@@ -648,7 +651,7 @@ function surfaceTitle(
648651
case "agents":
649652
return "Agents";
650653
case "device":
651-
return "Device";
654+
return surface.title ?? surface.target?.name ?? "Device";
652655
case "preview": {
653656
const snapshot = surface.resourceId ? sessions[surface.resourceId] : null;
654657
if (!snapshot || snapshot.navStatus._tag === "Idle") return "Browser";
@@ -733,7 +736,13 @@ function SurfaceIcon({
733736
case "agents":
734737
return <Bot className="size-3 shrink-0" />;
735738
case "device":
736-
return <Smartphone className="size-3 shrink-0" />;
739+
return surface.target?.platform === "ios" ? (
740+
<AppleIcon className="size-3 shrink-0" />
741+
) : surface.target?.platform === "android" ? (
742+
<AndroidIcon className="size-3 shrink-0" />
743+
) : (
744+
<Smartphone className="size-3 shrink-0" />
745+
);
737746
}
738747
}
739748

@@ -832,6 +841,7 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
832841
const browserProfiles = useBrowserDefaults().profiles;
833842
const { resolvedTheme } = useTheme();
834843
const tabListRef = useRef<HTMLDivElement>(null);
844+
const [renamingDevice, setRenamingDevice] = useState<string | null>(null);
835845
const [addSurfaceMenuOpen, setAddSurfaceMenuOpen] = useState(false);
836846
const [tabScrollState, setTabScrollState] = useState({
837847
hasOverflow: false,
@@ -958,6 +968,8 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
958968
if (surfaceIndex < 0) return;
959969

960970
const items: ContextMenuItem<TabContextMenuAction>[] = [];
971+
if (surface.kind === "device" && props.onRenameDevice)
972+
items.push({ id: "rename", label: "Rename" });
961973
if (surface.kind === "file" && surface.attachment === undefined) {
962974
items.push({ id: "copy-path", label: "Copy path" });
963975
}
@@ -1001,6 +1013,9 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
10011013

10021014
const action = await api.contextMenu.show(items, { x: event.clientX, y: event.clientY });
10031015
switch (action) {
1016+
case "rename":
1017+
setRenamingDevice(surface.id);
1018+
break;
10041019
case "copy-path":
10051020
if (surface.kind === "file" && surface.attachment === undefined) {
10061021
props.onCopyFilePath(surface.relativePath);
@@ -1202,20 +1217,48 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
12021217
<TooltipPopup>{audio === "muted" ? "Unmute tab" : "Mute tab"}</TooltipPopup>
12031218
</Tooltip>
12041219
)}
1205-
<Tooltip>
1206-
<TooltipTrigger
1207-
render={
1208-
<button
1209-
type="button"
1210-
className="cursor-pointer flex min-w-0 items-center"
1211-
onClick={() => props.onActivate(surface)}
1212-
>
1213-
<span className="truncate">{title}</span>
1214-
</button>
1215-
}
1220+
{renamingDevice === surface.id ? (
1221+
<input
1222+
aria-label="Device tab name"
1223+
className="w-24 min-w-0 rounded-sm bg-background px-1 outline-none ring-1 ring-ring"
1224+
defaultValue={title}
1225+
ref={(element) => {
1226+
element?.focus();
1227+
element?.select();
1228+
}}
1229+
onBlur={(event) => {
1230+
props.onRenameDevice?.(surface.id, event.currentTarget.value);
1231+
setRenamingDevice(null);
1232+
}}
1233+
onKeyDown={(event) => {
1234+
event.stopPropagation();
1235+
if (event.key === "Enter") event.currentTarget.blur();
1236+
if (event.key === "Escape") {
1237+
event.currentTarget.value = title;
1238+
event.currentTarget.blur();
1239+
}
1240+
}}
12161241
/>
1217-
<TooltipPopup>{title}</TooltipPopup>
1218-
</Tooltip>
1242+
) : (
1243+
<Tooltip>
1244+
<TooltipTrigger
1245+
render={
1246+
<button
1247+
type="button"
1248+
onDoubleClick={() => {
1249+
if (surface.kind === "device" && props.onRenameDevice)
1250+
setRenamingDevice(surface.id);
1251+
}}
1252+
className="cursor-pointer flex min-w-0 items-center"
1253+
onClick={() => props.onActivate(surface)}
1254+
>
1255+
<span className="truncate">{title}</span>
1256+
</button>
1257+
}
1258+
/>
1259+
<TooltipPopup>{title}</TooltipPopup>
1260+
</Tooltip>
1261+
)}
12191262
</div>
12201263
);
12211264
})}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { Smartphone } from "lucide-react";
2+
3+
import { Spinner } from "~/components/ui/spinner";
4+
5+
export function DeviceLoadingView(props: {
6+
readonly name: string;
7+
readonly description?: string;
8+
readonly stage: "opening" | "stream";
9+
readonly message: string;
10+
readonly error?: boolean;
11+
}) {
12+
return (
13+
<div
14+
role={props.error ? "alert" : "status"}
15+
className="flex size-full items-center justify-center bg-background px-6 py-10"
16+
>
17+
<div className="flex max-w-sm flex-col items-center gap-3 text-center">
18+
<div className="grid size-12 place-items-center rounded-xl border bg-muted/30">
19+
<Smartphone className="size-6 text-muted-foreground" />
20+
</div>
21+
<div className="space-y-1">
22+
<p className="text-sm font-medium">{props.name}</p>
23+
{props.description ? (
24+
<p className="text-xs text-muted-foreground">{props.description}</p>
25+
) : null}
26+
</div>
27+
<div className="flex items-center gap-2 text-xs text-muted-foreground">
28+
{!props.error ? <Spinner className="size-3" /> : null}
29+
<span>{props.message}</span>
30+
</div>
31+
{!props.error ? (
32+
<div
33+
className="flex w-24 gap-1"
34+
aria-label={
35+
props.stage === "opening" ? "Step 1 of 2: open device" : "Step 2 of 2: connect video"
36+
}
37+
>
38+
<span className="h-1 flex-1 rounded-full bg-foreground/60" />
39+
<span
40+
className={`h-1 flex-1 rounded-full ${props.stage === "stream" ? "bg-foreground/60" : "bg-muted"}`}
41+
/>
42+
</div>
43+
) : null}
44+
</div>
45+
</div>
46+
);
47+
}

0 commit comments

Comments
 (0)