Skip to content

Commit 1c99a55

Browse files
only21milcodex
andauthored
fix(web): report invalid preview addresses (#23577)
Co-authored-by: GPT-6.1 Sol <[email protected]>
1 parent f46f838 commit 1c99a55

2 files changed

Lines changed: 41 additions & 7 deletions

File tree

‎apps/web/src/components/preview/PreviewView.browser.test.tsx‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ const mocks = vi.hoisted(() => ({
2828
pickElement: vi.fn(),
2929
addPreviewAnnotation: vi.fn(),
3030
addImage: vi.fn(),
31-
toast: vi.fn(),
3231
pictureInPicture: false,
3332
showEmptyState: false,
3433
loading: false,
@@ -136,10 +135,6 @@ vi.mock(import("~/browser/browserRecording"), async (original) => ({
136135
findActiveBrowserRecordingRuntimeTabId: () => mocks.recordingRuntimeTabId,
137136
useActiveBrowserRecordingTabIds: () => mocks.recordingTabIds,
138137
}));
139-
vi.mock(import("~/components/ui/toast"), async (original) => {
140-
const actual = await original();
141-
return { ...actual, toastManager: { ...actual.toastManager, add: mocks.toast } };
142-
});
143138
vi.mock(import("./useDiscoveredLocalServers"), async (original) => ({
144139
...(await original()),
145140
useDiscoveredLocalServers: () => mocks.servers,
@@ -162,6 +157,7 @@ import { previewRuntimeTabId } from "~/browser/previewRuntimeTabId";
162157
import { usePreviewMiniPlayerStore } from "~/previewMiniPlayerStore";
163158
import { dispatchPreviewAction } from "./previewActionBus";
164159
import { PreviewView } from "./PreviewView";
160+
import { ToastProvider } from "~/components/ui/toast";
165161

166162
const TEST_THREAD_REF = {
167163
environmentId: EnvironmentId.make("environment-1"),
@@ -219,6 +215,23 @@ function setup() {
219215
}
220216

221217
describe("PreviewView", () => {
218+
it.each(["mailto:[email protected]", "http://"])(
219+
"reports rejected address %s without navigating or recording it",
220+
async (address) => {
221+
setup();
222+
await using _screen = await render(<ToastProvider timeout={0}>{view()}</ToastProvider>);
223+
const input = page.getByPlaceholder("Search or enter URL");
224+
await input.fill(address);
225+
await userEvent.keyboard("{Enter}");
226+
227+
await expect.element(page.getByText("Could not open this address")).toBeVisible();
228+
await expect.element(page.getByText("Enter an http or https URL.")).toBeVisible();
229+
expect(mocks.navigate).not.toHaveBeenCalled();
230+
expect(mocks.recordVisitForThread).not.toHaveBeenCalled();
231+
await userEvent.click(page.getByRole("button", { name: "Dismiss notification" }));
232+
},
233+
);
234+
222235
it("retains the annotation locally when its own environment loses access during capture", async () => {
223236
setup();
224237
const picked = annotation("annotation-revoked");

‎apps/web/src/components/preview/PreviewView.tsx‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
} from "@t3tools/contracts";
1818
import {
1919
normalizePreviewUrl,
20+
isPreviewUrlNormalizationError,
2021
resolveAddressBarInput,
2122
previewPdfAssetUrl,
2223
resolveSearchUrlTemplate,
@@ -259,7 +260,17 @@ export function PreviewView({
259260
if (await navigateToResolvedUrl(resolved)) {
260261
recordVisitForThread(threadRef, resolved);
261262
}
262-
} catch {}
263+
} catch (error) {
264+
toastManager.add({
265+
type: "error",
266+
title: "Could not open this address",
267+
description: isPreviewUrlNormalizationError(error)
268+
? "Enter an http or https URL."
269+
: error instanceof Error
270+
? error.message
271+
: "An error occurred.",
272+
});
273+
}
263274
};
264275

265276
const handleOpenServerUrl = async (next: string) => {
@@ -271,7 +282,17 @@ export function PreviewView({
271282
if (await navigateToResolvedUrl(resolved)) {
272283
recordVisitForThread(threadRef, next);
273284
}
274-
} catch {}
285+
} catch (error) {
286+
toastManager.add({
287+
type: "error",
288+
title: "Could not open this address",
289+
description: isPreviewUrlNormalizationError(error)
290+
? "Enter an http or https URL."
291+
: error instanceof Error
292+
? error.message
293+
: "An error occurred.",
294+
});
295+
}
275296
};
276297

277298
const handleRefresh = () => {

0 commit comments

Comments
 (0)