TypeScript
fix(web): report invalid preview addresses (#23577)
Victor Vogel1c99a55444b6
diff --git a/apps/web/src/components/preview/PreviewView.browser.test.tsx b/apps/web/src/components/preview/PreviewView.browser.test.tsxindex c17655db964..43f7845d10d 100644--- a/apps/web/src/components/preview/PreviewView.browser.test.tsx+++ b/apps/web/src/components/preview/PreviewView.browser.test.tsx@@ -28,7 +28,6 @@ const mocks = vi.hoisted(() => ({pickElement: vi.fn(),addPreviewAnnotation: vi.fn(),addImage: vi.fn(),- toast: vi.fn(),pictureInPicture: false,showEmptyState: false,loading: false,@@ -136,10 +135,6 @@ vi.mock(import("~/browser/browserRecording"), async (original) => ({findActiveBrowserRecordingRuntimeTabId: () => mocks.recordingRuntimeTabId,useActiveBrowserRecordingTabIds: () => mocks.recordingTabIds,}));-vi.mock(import("~/components/ui/toast"), async (original) => {- const actual = await original();- return { ...actual, toastManager: { ...actual.toastManager, add: mocks.toast } };-});vi.mock(import("./useDiscoveredLocalServers"), async (original) => ({...(await original()),useDiscoveredLocalServers: () => mocks.servers,@@ -162,6 +157,7 @@ import { previewRuntimeTabId } from "~/browser/previewRuntimeTabId";import { usePreviewMiniPlayerStore } from "~/previewMiniPlayerStore";import { dispatchPreviewAction } from "./previewActionBus";import { PreviewView } from "./PreviewView";+import { ToastProvider } from "~/components/ui/toast";const TEST_THREAD_REF = {environmentId: EnvironmentId.make("environment-1"),@@ -219,6 +215,23 @@ function setup() {}describe("PreviewView", () => {+ it.each(["mailto:[email protected]", "http://"])(+ "reports rejected address %s without navigating or recording it",+ async (address) => {+ setup();+ await using _screen = await render(<ToastProvider timeout={0}>{view()}</ToastProvider>);+ const input = page.getByPlaceholder("Search or enter URL");+ await input.fill(address);+ await userEvent.keyboard("{Enter}");++ await expect.element(page.getByText("Could not open this address")).toBeVisible();+ await expect.element(page.getByText("Enter an http or https URL.")).toBeVisible();+ expect(mocks.navigate).not.toHaveBeenCalled();+ expect(mocks.recordVisitForThread).not.toHaveBeenCalled();+ await userEvent.click(page.getByRole("button", { name: "Dismiss notification" }));+ },+ );+it("retains the annotation locally when its own environment loses access during capture", async () => {setup();const picked = annotation("annotation-revoked");diff --git a/apps/web/src/components/preview/PreviewView.tsx b/apps/web/src/components/preview/PreviewView.tsxindex 4eed7d21bc0..5126b18ec7a 100644--- a/apps/web/src/components/preview/PreviewView.tsx+++ b/apps/web/src/components/preview/PreviewView.tsx@@ -17,6 +17,7 @@ import {} from "@t3tools/contracts";import {normalizePreviewUrl,+ isPreviewUrlNormalizationError,resolveAddressBarInput,previewPdfAssetUrl,resolveSearchUrlTemplate,@@ -259,7 +260,17 @@ export function PreviewView({if (await navigateToResolvedUrl(resolved)) {recordVisitForThread(threadRef, resolved);}- } catch {}+ } catch (error) {+ toastManager.add({+ type: "error",+ title: "Could not open this address",+ description: isPreviewUrlNormalizationError(error)+ ? "Enter an http or https URL."+ : error instanceof Error+ ? error.message+ : "An error occurred.",+ });+ }};const handleOpenServerUrl = async (next: string) => {@@ -271,7 +282,17 @@ export function PreviewView({if (await navigateToResolvedUrl(resolved)) {recordVisitForThread(threadRef, next);}- } catch {}+ } catch (error) {+ toastManager.add({+ type: "error",+ title: "Could not open this address",+ description: isPreviewUrlNormalizationError(error)+ ? "Enter an http or https URL."+ : error instanceof Error+ ? error.message+ : "An error occurred.",+ });+ }};const handleRefresh = () => {