-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Restore Copy Link in chat link context menu #4161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
296ce02
fix(web): restore Copy Link for chat links
caezium 5eb5291
refactor(web): clarify preview link action
caezium 1f0312f
Merge branch 'main' into agent/copy-chat-link
caezium 825d0f0
Merge branch 'main' into agent/copy-chat-link
caezium d7370cb
Consolidate external link parsing
juliusmarminge 67e0a1f
Merge branch 'main' into agent/copy-chat-link
caezium 6f56bf5
Merge branch 'main' into agent/copy-chat-link
caezium File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
apps/web/src/components/chat/externalLinkContextMenu.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import { describe, expect, it, vi } from "vite-plus/test"; | ||
|
|
||
| import { resolveExternalWebLinkHost, 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", "example.com"], | ||
| ["http://localhost:3000/path", "localhost"], | ||
| ["#details", null], | ||
| ["mailto:hello@example.com", null], | ||
| ["file:///tmp/example.txt", null], | ||
| ["javascript:void(0)", null], | ||
| ["not a URL", null], | ||
| [undefined, null], | ||
| ])("resolves the external web-link host for %s as %s", (href, expected) => { | ||
| expect(resolveExternalWebLinkHost(href)).toBe(expected); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| 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 resolveExternalWebLinkHost(href: string | undefined): string | null { | ||
| if (!href) return null; | ||
| try { | ||
| const url = new URL(href); | ||
| if (url.protocol !== "http:" && url.protocol !== "https:") return null; | ||
| return url.hostname || null; | ||
| } catch { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| 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); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.