Skip to content
TypeScript

fix(web): restored Device view waits for device state before setup (#24106)

Victor Vogel9bd5e12242b3
diff --git a/apps/web/src/components/device/DevicePanel.tsx b/apps/web/src/components/device/DevicePanel.tsx
index 90cc7533c0b..cee7a3e4e4b 100644
--- a/apps/web/src/components/device/DevicePanel.tsx
+++ b/apps/web/src/components/device/DevicePanel.tsx
@@ -49,7 +49,7 @@ export function DevicePanel(props: {
readonly onDismissSetup: () => void;
}) {
const { environmentId, threadId } = props.threadRef;
- const { state, loaded } = useDeviceState(environmentId);
+ const { state, loaded, error: stateError, refresh: refreshState } = useDeviceState(environmentId);
const list = useAtomCommand(deviceEnvironment.list, { reportFailure: false });
const open = useAtomCommand(deviceEnvironment.open);
const close = useAtomCommand(deviceEnvironment.close);
@@ -172,6 +172,8 @@ export function DevicePanel(props: {
state.bootingDevices?.filter((device) => device.threadId === threadId) ?? [];
const { ready: hostReady, busy: hostBusy } = deviceHubReadiness(state);
const loading = !activeDevice && (pendingDevice !== null || hostBusy || !loaded);
+ // Without a first snapshot a failed subscription never settles on its own.
+ const loadError = loaded ? null : stateError;
const diagnostics = state.hosts.flatMap((host) => {
const status = state.hostStatuses[host.id];
const detail = status?.detail ?? (host.kind === "local" ? state.hostStatusDetail : undefined);
@@ -257,15 +259,23 @@ export function DevicePanel(props: {
}
stage="opening"
message={
- pendingDevice
+ loadError ??
+ (pendingDevice
? pendingDevice.booted
? "Opening device…"
: "Starting device…"
: state.hostStatus === "installing"
? (state.hostStatusDetail ?? "Installing device support…")
- : "Finding devices…"
+ : "Finding devices…")
}
- />
+ error={loadError !== null}
+ >
+ {loadError !== null ? (
+ <Button size="sm" variant="outline" onClick={refreshState}>
+ Retry
+ </Button>
+ ) : null}
+ </DeviceLoadingView>
) : (
<div className="scrollbar-gutter-both flex size-full flex-col overflow-y-auto px-5 py-8 text-sm text-muted-foreground">
<div
diff --git a/apps/web/src/state/device.browser.test.tsx b/apps/web/src/state/device.browser.test.tsx
new file mode 100644
index 00000000000..1188119271c
--- /dev/null
+++ b/apps/web/src/state/device.browser.test.tsx
@@ -0,0 +1,185 @@
+import { RegistryContext } from "@effect/atom-react";
+import {
+ EnvironmentAuthorizationError,
+ EnvironmentId,
+ ThreadId,
+ type DeviceServiceState,
+} from "@t3tools/contracts";
+import { scopedThreadKey } from "@t3tools/client-runtime/environment";
+import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
+import { expect, it, vi } from "vite-plus/test";
+import { page } from "vite-plus/test/browser";
+import { useEffect } from "react";
+
+import { act, render } from "../../test/render";
+import { DevicePanel } from "../components/device/DevicePanel";
+import {
+ startAgentOpenedPreviews,
+ resetDeviceSessionBaselinesForTests,
+} from "../components/preview/agentOpenedPreviews";
+import { usePreviewMiniPlayerStore } from "../previewMiniPlayerStore";
+import { appAtomRegistry } from "../rpc/atomRegistry";
+import { deviceEnvironment, useDeviceState } from "./device";
+
+vi.mock(import("./presentation"), async (original) => ({
+ ...(await original()),
+ environmentSummaries: {
+ ...(await original()).environmentSummaries,
+ connectedEnvironmentIdsAtom: Atom.make([EnvironmentId.make("device-loading")]),
+ },
+}));
+vi.mock(import("../hooks/useSettings"), () => ({
+ getClientSettings: (() => ({ browserAutoShowFloatingPreview: true })) as never,
+ useClientSettings: ((
+ select: (settings: { browserAutoShowFloatingPreview: boolean }) => unknown,
+ ) => select({ browserAutoShowFloatingPreview: true })) as never,
+}));
+vi.mock(import("./use-atom-command"), () => {
+ const run = () => Promise.resolve({ _tag: "Success", value: {} });
+ return { useAtomCommand: (() => run) as never };
+});
+vi.mock(import("../components/device/DeviceHostUpdates"), () => ({
+ DeviceHostUpdates: () => null,
+}));
+
+it("baselines the first device snapshot without reopening dismissed devices", async () => {
+ const viewport = { width: window.innerWidth, height: window.innerHeight };
+ await page.viewport(1280, 800);
+ await using _viewport = {
+ [Symbol.asyncDispose]: () => page.viewport(viewport.width, viewport.height),
+ };
+ const threadRef = {
+ environmentId: EnvironmentId.make("device-loading"),
+ threadId: ThreadId.make("thread"),
+ };
+ const threadKey = scopedThreadKey(threadRef);
+ const snapshot: DeviceServiceState = {
+ hosts: [],
+ hostStatus: "ready",
+ hostStatuses: {},
+ devices: [
+ {
+ hostId: "local",
+ id: "phone",
+ platform: "ios",
+ name: "Phone",
+ version: "26",
+ booted: true,
+ physical: false,
+ },
+ ],
+ sessions: [
+ {
+ threadId: threadRef.threadId,
+ hostId: "local",
+ deviceId: "phone",
+ platform: "ios",
+ openedAt: "2026-10-01T00:00:00Z",
+ },
+ ],
+ onboardingCompleted: true,
+ agentAccessEnabled: true,
+ hubBasePath: "/api/device-hub",
+ revision: 1,
+ };
+ const registry = appAtomRegistry;
+ resetDeviceSessionBaselinesForTests();
+ using _baselines = { [Symbol.dispose]: resetDeviceSessionBaselinesForTests };
+ const stateAtom = Atom.make<AsyncResult.AsyncResult<DeviceServiceState>>(AsyncResult.initial());
+ using _subscription = vi.spyOn(deviceEnvironment, "state").mockReturnValue(stateAtom);
+ using _store = {
+ [Symbol.dispose]: () => usePreviewMiniPlayerStore.getState().removeThread(threadRef),
+ };
+ using _following = { [Symbol.dispose]: startAgentOpenedPreviews() };
+ let loaded = false;
+ function Probe() {
+ const device = useDeviceState(threadRef.environmentId);
+ useEffect(() => {
+ loaded = device.loaded;
+ }, [device.loaded]);
+ return null;
+ }
+ await using _screen = await render(
+ <RegistryContext.Provider value={registry}>
+ <Probe />
+ </RegistryContext.Provider>,
+ );
+ const initiallyLoaded = loaded;
+ await act(() => registry.set(stateAtom, AsyncResult.success(snapshot)));
+ expect(usePreviewMiniPlayerStore.getState().byThreadKey[threadKey]).toBeUndefined();
+ expect(initiallyLoaded).toBe(false);
+ expect(loaded).toBe(true);
+
+ await act(() => registry.set(stateAtom, AsyncResult.waiting(AsyncResult.success(snapshot))));
+ expect(loaded).toBe(true);
+
+ const nextDevice = { ...snapshot.devices[0]!, id: "tablet", name: "Tablet" };
+ await act(() =>
+ registry.set(
+ stateAtom,
+ AsyncResult.success({
+ ...snapshot,
+ devices: [...snapshot.devices, nextDevice],
+ sessions: [...snapshot.sessions, { ...snapshot.sessions[0]!, deviceId: nextDevice.id }],
+ revision: 2,
+ }),
+ ),
+ );
+ expect(usePreviewMiniPlayerStore.getState().byThreadKey[threadKey]?.source).toEqual({
+ kind: "device",
+ hostId: "local",
+ deviceId: "tablet",
+ platform: "ios",
+ name: "Tablet",
+ });
+});
+
+it("reports a failed first device load and retries it", async () => {
+ let attempts = 0;
+ const stateAtom = Atom.make(
+ (): AsyncResult.AsyncResult<DeviceServiceState, EnvironmentAuthorizationError> => {
+ attempts += 1;
+ return attempts === 1
+ ? AsyncResult.fail(
+ new EnvironmentAuthorizationError({
+ requiredScope: "orchestration:read",
+ message: "Device access denied",
+ }),
+ )
+ : AsyncResult.success({
+ hosts: [],
+ hostStatus: "ready",
+ hostStatuses: {},
+ devices: [],
+ sessions: [],
+ onboardingCompleted: true,
+ agentAccessEnabled: false,
+ hubBasePath: "/api/device-hub",
+ revision: 1,
+ });
+ },
+ ).pipe(Atom.keepAlive);
+ const registry = AtomRegistry.make();
+ using _registry = { [Symbol.dispose]: () => registry.dispose() };
+ using _subscription = vi.spyOn(deviceEnvironment, "state").mockReturnValue(stateAtom);
+ await using screen = await render(
+ <RegistryContext.Provider value={registry}>
+ <DevicePanel
+ mode="embedded"
+ threadRef={{
+ environmentId: EnvironmentId.make("device-load-failure"),
+ threadId: ThreadId.make("thread"),
+ }}
+ surface={{ id: "device", kind: "device" }}
+ visible
+ onDismissSetup={() => undefined}
+ />
+ </RegistryContext.Provider>,
+ );
+
+ await expect.element(screen.getByRole("alert").getByText("Device access denied")).toBeVisible();
+
+ await screen.getByRole("button", { name: "Retry" }).click();
+ await expect.element(screen.getByRole("button", { name: "Refresh devices" })).toBeVisible();
+ expect(screen.container.querySelector('[role="alert"]')).toBeNull();
+});
diff --git a/apps/web/src/state/device.ts b/apps/web/src/state/device.ts
index da19cc7d5a2..ee43428f740 100644
--- a/apps/web/src/state/device.ts
+++ b/apps/web/src/state/device.ts
@@ -40,11 +40,18 @@ const EMPTY_DEVICE_STATE: DeviceServiceState = {
export function useDeviceState(environmentId: EnvironmentId | null): {
readonly state: DeviceServiceState;
readonly loaded: boolean;
+ readonly error: string | null;
+ readonly refresh: () => void;
} {
const query = useEnvironmentQuery(
environmentId === null ? null : deviceEnvironment.state({ environmentId, input: {} }),
);
- return { state: query.data ?? EMPTY_DEVICE_STATE, loaded: query.data !== undefined };
+ return {
+ state: query.data ?? EMPTY_DEVICE_STATE,
+ loaded: query.data !== null,
+ error: query.error,
+ refresh: query.refresh,
+ };
}
const deviceHubAccessAtom = Atom.family((environmentId: EnvironmentId) =>