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