Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 20 additions & 25 deletions apps/web/src/components/ChatMarkdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import remarkGfm from "remark-gfm";
import { renderSkillInlineMarkdownChildren } from "./chat/SkillInlineText";
import { CHAT_FILE_TAG_CHIP_CLASS_NAME, FileTagChipContent } from "./chat/FileTagChip";
import { PierreEntryIcon } from "./chat/PierreEntryIcon";
import { isExternalWebLink, showExternalLinkContextMenu } from "./chat/externalLinkContextMenu";
import { hasSpecificPierreIconForFileName, syntheticFileNameForLanguageId } from "../pierre-icons";
import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
import { Button } from "./ui/button";
Expand Down Expand Up @@ -76,6 +77,7 @@ import { usePreparedConnection } from "../state/session";
import { previewEnvironment } from "../state/preview";
import { useAtomCommand } from "../state/use-atom-command";
import { useAtomQueryRunner } from "../state/use-atom-query-runner";
import { writeTextToClipboard } from "../hooks/useCopyToClipboard";
import { isPreviewSupportedInRuntime } from "../previewStateStore";
import {
isBrowserPreviewFile,
Expand Down Expand Up @@ -1410,37 +1412,30 @@ function ChatMarkdown({
}
}}
onContextMenu={(event) => {
if (!canOpenInPreview || !href) return;
if (!canOpenInPreview || !href || !isExternalWebLink(href)) return;
event.preventDefault();
event.stopPropagation();
const api = readLocalApi();
if (!api) return;
void (async () => {
let operation = "show-link-context-menu";
try {
const clicked = await api.contextMenu.show(
[
{ id: "open-in-browser", label: "Open in integrated browser" },
{ id: "open-external", label: "Open in system browser" },
] as const,
{ x: event.clientX, y: event.clientY },
);
if (clicked === "open-in-browser") {
operation = "open-link-in-preview";
const result = await openExternalLinkInPreview(href);
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
reportMarkdownActionFailure({ operation, target: href }, result.cause);
}
return;
void showExternalLinkContextMenu({
href,
position: { x: event.clientX, y: event.clientY },
showContextMenu: (items, position) => api.contextMenu.show(items, position),
openInPreview: async (target) => {
const result = await openExternalLinkInPreview(target);
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
reportMarkdownActionFailure(
{ operation: "open-link-in-preview", target },
result.cause,
);
}
if (clicked === "open-external") {
operation = "open-link-external";
await api.shell.openExternal(href);
}
} catch (cause) {
},
openExternal: (target) => api.shell.openExternal(target),
copyLink: (target) => writeTextToClipboard(target, "link"),
reportFailure: (operation, cause) => {
reportMarkdownActionFailure({ operation, target: href }, cause);
}
})();
},
});
}}
>
{faviconHost ? (
Expand Down
127 changes: 127 additions & 0 deletions apps/web/src/components/chat/externalLinkContextMenu.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { describe, expect, it, vi } from "vite-plus/test";

import { isExternalWebLink, showExternalLinkContextMenu } from "./externalLinkContextMenu";

function createHarness(selection: "open-in-preview" | "open-external" | "copy-link" | null) {
const showContextMenu = vi.fn().mockResolvedValue(selection);
const openInPreview = vi.fn().mockResolvedValue(undefined);
const openExternal = vi.fn().mockResolvedValue(undefined);
const copyLink = vi.fn().mockResolvedValue(undefined);
const reportFailure = vi.fn();

return {
showContextMenu,
openInPreview,
openExternal,
copyLink,
reportFailure,
};
}

describe("external chat link context menu", () => {
it("offers both open actions and Copy Link", async () => {
const harness = createHarness(null);

await showExternalLinkContextMenu({
href: "https://example.com/docs?topic=menus#copy",
position: { x: 12, y: 24 },
...harness,
});

expect(harness.showContextMenu).toHaveBeenCalledWith(
[
{ id: "open-in-preview", label: "Open in integrated browser" },
{ id: "open-external", label: "Open in system browser" },
{ id: "copy-link", label: "Copy Link" },
],
{ x: 12, y: 24 },
);
expect(harness.openInPreview).not.toHaveBeenCalled();
expect(harness.openExternal).not.toHaveBeenCalled();
expect(harness.copyLink).not.toHaveBeenCalled();
});

it("copies the exact destination without opening it", async () => {
const harness = createHarness("copy-link");
const href = "https://example.com/docs?topic=menus#copy";

await showExternalLinkContextMenu({ href, position: { x: 1, y: 2 }, ...harness });

expect(harness.copyLink).toHaveBeenCalledWith(href);
expect(harness.openInPreview).not.toHaveBeenCalled();
expect(harness.openExternal).not.toHaveBeenCalled();
});

it.each([
["open-in-preview" as const, "openInPreview" as const],
["open-external" as const, "openExternal" as const],
])("preserves the %s action", async (selection, expectedCallback) => {
const harness = createHarness(selection);
const href = "https://example.com/docs";

await showExternalLinkContextMenu({ href, position: { x: 1, y: 2 }, ...harness });

expect(harness[expectedCallback]).toHaveBeenCalledWith(href);
expect(harness.copyLink).not.toHaveBeenCalled();
});

it("reports the selected action when it fails", async () => {
const harness = createHarness("copy-link");
const cause = new Error("clipboard denied");
harness.copyLink.mockRejectedValue(cause);

await showExternalLinkContextMenu({
href: "https://example.com/docs",
position: { x: 1, y: 2 },
...harness,
});

expect(harness.reportFailure).toHaveBeenCalledWith("copy-link", cause);
});

it("reports the menu operation when the native menu cannot be shown", async () => {
const harness = createHarness(null);
const cause = new Error("menu unavailable");
harness.showContextMenu.mockRejectedValue(cause);

await showExternalLinkContextMenu({
href: "https://example.com/docs",
position: { x: 1, y: 2 },
...harness,
});

expect(harness.reportFailure).toHaveBeenCalledWith("show-link-context-menu", cause);
expect(harness.openInPreview).not.toHaveBeenCalled();
expect(harness.openExternal).not.toHaveBeenCalled();
expect(harness.copyLink).not.toHaveBeenCalled();
});

it.each([
["open-in-preview" as const, "openInPreview" as const, "open-link-in-preview"],
["open-external" as const, "openExternal" as const, "open-link-external"],
])("reports a failed %s action", async (selection, callback, operation) => {
const harness = createHarness(selection);
const cause = new Error("open failed");
harness[callback].mockRejectedValue(cause);

await showExternalLinkContextMenu({
href: "https://example.com/docs",
position: { x: 1, y: 2 },
...harness,
});

expect(harness.reportFailure).toHaveBeenCalledWith(operation, cause);
});

it.each([
["https://example.com", true],
["http://localhost:3000/path", true],
["#details", false],
["mailto:hello@example.com", false],
["file:///tmp/example.txt", false],
["javascript:void(0)", false],
["not a URL", false],
])("classifies %s as an external web link: %s", (href, expected) => {
expect(isExternalWebLink(href)).toBe(expected);
});
});
76 changes: 76 additions & 0 deletions apps/web/src/components/chat/externalLinkContextMenu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import type { ContextMenuItem } from "@t3tools/contracts";

export type ExternalLinkContextMenuAction = "open-in-preview" | "open-external" | "copy-link";

export type ExternalLinkContextMenuFailureOperation =
| "show-link-context-menu"
| "open-link-in-preview"
| "open-link-external"
| "copy-link";

const FAILURE_OPERATION_BY_ACTION = {
"open-in-preview": "open-link-in-preview",
"open-external": "open-link-external",
"copy-link": "copy-link",
} as const satisfies Record<ExternalLinkContextMenuAction, ExternalLinkContextMenuFailureOperation>;

const EXTERNAL_LINK_CONTEXT_MENU_ITEMS = [
{ id: "open-in-preview", label: "Open in integrated browser" },
{ id: "open-external", label: "Open in system browser" },
{ id: "copy-link", label: "Copy Link" },
] as const satisfies readonly ContextMenuItem<ExternalLinkContextMenuAction>[];

interface ShowExternalLinkContextMenuOptions {
readonly href: string;
readonly position: { readonly x: number; readonly y: number };
readonly showContextMenu: (
items: readonly ContextMenuItem<ExternalLinkContextMenuAction>[],
position: { readonly x: number; readonly y: number },
) => Promise<ExternalLinkContextMenuAction | null>;
readonly openInPreview: (href: string) => Promise<void>;
readonly openExternal: (href: string) => Promise<void>;
readonly copyLink: (href: string) => Promise<unknown>;
readonly reportFailure: (
operation: ExternalLinkContextMenuFailureOperation,
cause: unknown,
) => void;
}

export function isExternalWebLink(href: string): boolean {
try {
const url = new URL(href);
return (url.protocol === "http:" || url.protocol === "https:") && url.hostname.length > 0;
} catch {
return false;
}
}
Comment thread
cursor[bot] marked this conversation as resolved.

export async function showExternalLinkContextMenu({
href,
position,
showContextMenu,
openInPreview,
openExternal,
copyLink,
reportFailure,
}: ShowExternalLinkContextMenuOptions): Promise<void> {
let action: ExternalLinkContextMenuAction | null;
try {
action = await showContextMenu(EXTERNAL_LINK_CONTEXT_MENU_ITEMS, position);
} catch (cause) {
reportFailure("show-link-context-menu", cause);
return;
}

try {
if (action === "open-in-preview") {
await openInPreview(href);
} else if (action === "open-external") {
await openExternal(href);
} else if (action === "copy-link") {
await copyLink(href);
}
} catch (cause) {
if (action) reportFailure(FAILURE_OPERATION_BY_ACTION[action], cause);
}
}
Loading