Open History to see the last commit
1import type { ContextMenuItem } from "@t3tools/contracts";
2
3export type ExternalLinkContextMenuAction = "open-in-preview" | "open-external" | "copy-link";
4
5export type ExternalLinkContextMenuFailureOperation =
6 | "show-link-context-menu"
7 | "open-link-in-preview"
8 | "open-link-external"
9 | "copy-link";
10
11const FAILURE_OPERATION_BY_ACTION = {
12 "open-in-preview": "open-link-in-preview",
13 "open-external": "open-link-external",
14 "copy-link": "copy-link",
15} as const satisfies Record<ExternalLinkContextMenuAction, ExternalLinkContextMenuFailureOperation>;
16
17const EXTERNAL_LINK_CONTEXT_MENU_ITEMS = [
18 { id: "open-in-preview", label: "Open in integrated browser" },
19 { id: "open-external", label: "Open in system browser" },
20 { id: "copy-link", label: "Copy Link" },
21] as const satisfies readonly ContextMenuItem<ExternalLinkContextMenuAction>[];
22
23/**
24 * The integrated browser is not always there to offer — it needs a thread to open beside and a
25 * runtime that can show it — but the other two answers hold wherever a link does. Dropping the
26 * whole menu with the one item that cannot be honoured is what left a right-click on a link
27 * showing the platform's cut-and-paste menu instead of a way to copy the link.
28 */
29export function externalLinkContextMenuItems(options: {
30 readonly canOpenInPreview: boolean;
31}): readonly ContextMenuItem<ExternalLinkContextMenuAction>[] {
32 return options.canOpenInPreview
33 ? EXTERNAL_LINK_CONTEXT_MENU_ITEMS
34 : EXTERNAL_LINK_CONTEXT_MENU_ITEMS.filter((item) => item.id !== "open-in-preview");
35}
36
37interface ShowExternalLinkContextMenuOptions {
38 readonly href: string;
39 readonly position: { readonly x: number; readonly y: number };
40 /** Absent means yes, which is what every caller before the browser could be missing meant. */
41 readonly canOpenInPreview?: boolean;
42 readonly showContextMenu: (
43 items: readonly ContextMenuItem<ExternalLinkContextMenuAction>[],
44 position: { readonly x: number; readonly y: number },
45 ) => Promise<ExternalLinkContextMenuAction | null>;
46 readonly openInPreview: (href: string) => Promise<void>;
47 readonly openExternal: (href: string) => Promise<void>;
48 readonly copyLink: (href: string) => Promise<unknown>;
49 readonly reportFailure: (
50 operation: ExternalLinkContextMenuFailureOperation,
51 cause: unknown,
52 ) => void;
53}
54
55export function resolveExternalWebLinkHost(href: string | undefined): string | null {
56 if (!href) return null;
57 try {
58 const url = new URL(href);
59 if (url.protocol !== "http:" && url.protocol !== "https:") return null;
60 return url.hostname || null;
61 } catch {
62 return null;
63 }
64}
65
66export async function showExternalLinkContextMenu({
67 href,
68 position,
69 canOpenInPreview = true,
70 showContextMenu,
71 openInPreview,
72 openExternal,
73 copyLink,
74 reportFailure,
75}: ShowExternalLinkContextMenuOptions): Promise<void> {
76 let action: ExternalLinkContextMenuAction | null;
77 try {
78 action = await showContextMenu(externalLinkContextMenuItems({ canOpenInPreview }), position);
79 } catch (cause) {
80 reportFailure("show-link-context-menu", cause);
81 return;
82 }
83
84 try {
85 if (action === "open-in-preview") {
86 await openInPreview(href);
87 } else if (action === "open-external") {
88 await openExternal(href);
89 } else if (action === "copy-link") {
90 await copyLink(href);
91 }
92 } catch (cause) {
93 if (action) reportFailure(FAILURE_OPERATION_BY_ACTION[action], cause);
94 }
95}