@devries-cameron20 / t3code-1 / commits / 2fae0d0ac3
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(preview): preserve direct localhost navigation (#3939)

Co-authored-by: Julius Marminge <julius0216@outlook.com> Co-authored-by: codex <codex@users.noreply.github.com>

Chris Michael Guzman committed Jul 20, 2026 at 05:49 UTC 2fae0d0ac300353be774c9f9fe79d3af025d4f91
2 files changed +227 -15
apps/web/src/components/preview/PreviewView.test.tsx new
+194
@@ -0,0 +1,194 @@
1 +import { EnvironmentId, ThreadId } from "@t3tools/contracts";
2 +import { renderToStaticMarkup } from "react-dom/server";
3 +import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
4 +
5 +const mocks = vi.hoisted(() => ({
6 + navigate: vi.fn(async (_tabId: string, _url: string): Promise<void> => undefined),
7 + rememberPreviewUrl: vi.fn(),
8 + readPreparedConnection: vi.fn(() => ({ httpBaseUrl: "http://172.25.85.75:3773" })),
9 + submittedUrl: null as ((url: string) => void) | null,
10 + emptyStateUrl: null as ((url: string) => void) | null,
11 + showEmptyState: false,
12 +}));
13 +
14 +vi.mock("~/state/session", () => ({
15 + readPreparedConnection: mocks.readPreparedConnection,
16 +}));
17 +
18 +vi.mock("~/composerDraftStore", () => ({
19 + useComposerDraftStore: (
20 + select: (store: { addPreviewAnnotation: () => void; addImage: () => void }) => unknown,
21 + ) => select({ addPreviewAnnotation: vi.fn(), addImage: vi.fn() }),
22 +}));
23 +
24 +vi.mock("~/lib/previewAnnotation", () => ({
25 + previewAnnotationScreenshotFile: vi.fn(),
26 +}));
27 +
28 +vi.mock("~/localApi", () => ({
29 + ensureLocalApi: vi.fn(),
30 +}));
31 +
32 +vi.mock("~/previewStateStore", () => ({
33 + rememberPreviewUrl: mocks.rememberPreviewUrl,
34 + updatePreviewServerSnapshot: vi.fn(),
35 + useThreadPreviewState: () => ({
36 + activeTabId: "tab-1",
37 + desktopByTabId: {
38 + "tab-1": {
39 + canGoBack: false,
40 + canGoForward: false,
41 + loading: false,
42 + zoomFactor: 1,
43 + controller: "none",
44 + },
45 + },
46 + recentlySeenUrls: [],
47 + sessions: mocks.showEmptyState
48 + ? {}
49 + : {
50 + "tab-1": {
51 + threadId: "thread-1",
52 + tabId: "tab-1",
53 + navStatus: {
54 + _tag: "Success",
55 + url: "http://example.com/",
56 + title: "Example",
57 + },
58 + canGoBack: false,
59 + canGoForward: false,
60 + updatedAt: "2026-07-13T00:00:00.000Z",
61 + },
62 + },
63 + }),
64 +}));
65 +
66 +vi.mock("~/state/environments", () => ({
67 + useEnvironment: () => ({ label: "WSL" }),
68 + useEnvironmentHttpBaseUrl: () => "http://172.25.85.75:3773",
69 +}));
70 +
71 +vi.mock("~/state/preview", () => ({
72 + previewEnvironment: { open: {}, resize: {} },
73 +}));
74 +
75 +vi.mock("~/state/use-atom-command", () => ({
76 + useAtomCommand: () => vi.fn(),
77 +}));
78 +
79 +vi.mock("~/browser/browserRecording", () => ({
80 + startBrowserRecording: vi.fn(),
81 + stopBrowserRecording: vi.fn(),
82 + useActiveBrowserRecordingTabId: () => null,
83 +}));
84 +
85 +vi.mock("~/browser/browserSurfaceStore", () => ({
86 + useBrowserSurfaceStore: (
87 + select: (state: { byTabId: Record<string, { rect?: unknown }> }) => unknown,
88 + ) => select({ byTabId: {} }),
89 +}));
90 +
91 +vi.mock("~/components/ui/toast", () => ({
92 + stackedThreadToast: vi.fn(),
93 + toastManager: { add: vi.fn() },
94 +}));
95 +
96 +vi.mock("./previewBridge", () => ({
97 + previewBridge: { navigate: mocks.navigate },
98 +}));
99 +
100 +vi.mock("./PreviewChromeRow", () => ({
101 + PreviewChromeRow: (props: { onSubmit: (url: string) => void }) => {
102 + mocks.submittedUrl = props.onSubmit;
103 + return null;
104 + },
105 +}));
106 +
107 +vi.mock("./PreviewEmptyState", () => ({
108 + PreviewEmptyState: (props: { onOpenUrl: (url: string) => void }) => {
109 + mocks.emptyStateUrl = props.onOpenUrl;
110 + return null;
111 + },
112 +}));
113 +vi.mock("./PreviewMoreMenu", () => ({ PreviewMoreMenu: () => null }));
114 +vi.mock("./PreviewUnreachable", () => ({ PreviewUnreachable: () => null }));
115 +vi.mock("./ZoomIndicator", () => ({ ZoomIndicator: () => null }));
116 +vi.mock("./AgentBrowserCursor", () => ({ AgentBrowserCursor: () => null }));
117 +vi.mock("~/browser/BrowserSurfaceSlot", () => ({ BrowserSurfaceSlot: () => null }));
118 +vi.mock("./useLoadingProgress", () => ({ useLoadingProgress: () => 0 }));
119 +vi.mock("./usePreviewSession", () => ({ usePreviewSession: vi.fn() }));
120 +
121 +import { PreviewView } from "./PreviewView";
122 +
123 +describe("PreviewView navigation", () => {
124 + beforeEach(() => {
125 + mocks.navigate.mockClear();
126 + mocks.rememberPreviewUrl.mockClear();
127 + mocks.readPreparedConnection.mockClear();
128 + mocks.submittedUrl = null;
129 + mocks.emptyStateUrl = null;
130 + mocks.showEmptyState = false;
131 + });
132 +
133 + it.each([
134 + [
135 + "https://localhost:8000/dashboard?mode=test#top",
136 + "https://localhost:8000/dashboard?mode=test#top",
137 + ],
138 + ["localhost:5173/app", "http://localhost:5173/app"],
139 + ])("preserves a direct localhost URL in a WSL environment", async (submitted, expected) => {
140 + renderToStaticMarkup(
141 + <PreviewView
142 + threadRef={{
143 + environmentId: EnvironmentId.make("environment-1"),
144 + threadId: ThreadId.make("thread-1"),
145 + }}
146 + tabId="tab-1"
147 + visible
148 + />,
149 + );
150 +
151 + expect(mocks.submittedUrl).not.toBeNull();
152 + mocks.submittedUrl?.(submitted);
153 +
154 + await vi.waitFor(() => expect(mocks.navigate).toHaveBeenCalledWith("tab-1", expected));
155 + expect(mocks.rememberPreviewUrl).toHaveBeenCalledWith(
156 + {
157 + environmentId: "environment-1",
158 + threadId: "thread-1",
159 + },
160 + expected,
161 + );
162 + });
163 +
164 + it("maps an empty-state localhost server onto the WSL host", async () => {
165 + mocks.showEmptyState = true;
166 + renderToStaticMarkup(
167 + <PreviewView
168 + threadRef={{
169 + environmentId: EnvironmentId.make("environment-1"),
170 + threadId: ThreadId.make("thread-1"),
171 + }}
172 + tabId="tab-1"
173 + visible
174 + />,
175 + );
176 +
177 + expect(mocks.emptyStateUrl).not.toBeNull();
178 + mocks.emptyStateUrl?.("http://localhost:5173/app?mode=test#top");
179 +
180 + await vi.waitFor(() =>
181 + expect(mocks.navigate).toHaveBeenCalledWith(
182 + "tab-1",
183 + "http://172.25.85.75:5173/app?mode=test#top",
184 + ),
185 + );
186 + expect(mocks.rememberPreviewUrl).toHaveBeenCalledWith(
187 + {
188 + environmentId: "environment-1",
189 + threadId: "thread-1",
190 + },
191 + "http://172.25.85.75:5173/app?mode=test#top",
192 + );
193 + });
194 +});
apps/web/src/components/preview/PreviewView.tsx
+33 -15
@@ -7,6 +7,7 @@ import {
7 type PreviewViewportSetting,
8 type ScopedThreadRef,
9 } from "@t3tools/contracts";
10 +import { normalizePreviewUrl } from "@t3tools/shared/preview";
11 import { useCallback, useEffect, useRef, useState } from "react";
12
13 import { useComposerDraftStore } from "~/composerDraftStore";
@@ -112,27 +113,44 @@ export function PreviewView({ threadRef, tabId: requestedTabId, configuredUrls,
113 tabId ? (state.byTabId[tabId]?.rect ?? null) : null,
114 );
115
116 + const navigateToResolvedUrl = useCallback(
117 + async (resolvedUrl: string) => {
118 + if (tabId && previewBridge) {
119 + // Drive the webview imperatively; `usePreviewBridge` mirrors the
120 + // resolved URL back to the server so other clients stay in sync.
121 + await previewBridge.navigate(tabId, resolvedUrl);
122 + rememberPreviewUrl(threadRef, resolvedUrl);
123 + } else {
124 + await openPreviewSession({
125 + openPreview: open,
126 + threadRef,
127 + url: resolvedUrl,
128 + });
129 + }
130 + },
131 + [open, tabId, threadRef],
132 + );
133 +
134 const handleSubmitUrl = useCallback(
135 async (next: string) => {
136 try {
118 - const resolvedUrl = resolveDiscoveredServerUrl(threadRef.environmentId, next);
119 - if (tabId && previewBridge) {
120 - // Drive the webview imperatively; `usePreviewBridge` mirrors the
121 - // resolved URL back to the server so other clients stay in sync.
122 - await previewBridge.navigate(tabId, resolvedUrl);
123 - rememberPreviewUrl(threadRef, resolvedUrl);
124 - } else {
125 - await openPreviewSession({
126 - openPreview: open,
127 - threadRef,
128 - url: resolvedUrl,
129 - });
130 - }
137 + await navigateToResolvedUrl(normalizePreviewUrl(next));
138 } catch {
139 // Server-side `failed` event renders the unreachable view.
140 }
141 },
135 - [open, tabId, threadRef],
142 + [navigateToResolvedUrl],
143 + );
144 +
145 + const handleOpenServerUrl = useCallback(
146 + async (next: string) => {
147 + try {
148 + await navigateToResolvedUrl(resolveDiscoveredServerUrl(threadRef.environmentId, next));
149 + } catch {
150 + // Server-side `failed` event renders the unreachable view.
151 + }
152 + },
153 + [navigateToResolvedUrl, threadRef.environmentId],
154 );
155
156 const handleRefresh = useCallback(() => {
@@ -613,7 +631,7 @@ export function PreviewView({ threadRef, tabId: requestedTabId, configuredUrls,
631 environmentId={threadRef.environmentId}
632 configuredUrls={configuredUrls}
633 recentlySeenUrls={previewState.recentlySeenUrls}
616 - onOpenUrl={(next) => void handleSubmitUrl(next)}
634 + onOpenUrl={(next) => void handleOpenServerUrl(next)}
635 />
636 ) : null}
637 {snapshot && desktopOverlay ? (