The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type { ContextMenuItem } from "@t3tools/contracts"; |
| 2 | import { msg } from "gt-react"; |
| 3 | |
| 4 | import { m } from "../../i18n/messages"; |
| 5 | |
| 6 | type ExternalLinkContextMenuAction = |
| 7 | | "open-in-preview" |
| 8 | | "open-external" |
| 9 | | "copy-link" |
| 10 | | "link-to-thread" |
| 11 | | "unlink-from-thread"; |
| 12 | |
| 13 | type ExternalLinkContextMenuFailureOperation = |
| 14 | | "show-link-context-menu" |
| 15 | | "open-link-in-preview" |
| 16 | | "open-link-external" |
| 17 | | "copy-link" |
| 18 | | "link-pull-request-to-thread" |
| 19 | | "unlink-pull-request-from-thread"; |
| 20 | |
| 21 | const FAILURE_OPERATION_BY_ACTION = { |
| 22 | "open-in-preview": "open-link-in-preview", |
| 23 | "open-external": "open-link-external", |
| 24 | "copy-link": "copy-link", |
| 25 | "link-to-thread": "link-pull-request-to-thread", |
| 26 | "unlink-from-thread": "unlink-pull-request-from-thread", |
| 27 | } as const satisfies Record<ExternalLinkContextMenuAction, ExternalLinkContextMenuFailureOperation>; |
| 28 | |
| 29 | const EXTERNAL_LINK_CONTEXT_MENU_ITEMS = [ |
| 30 | { id: "open-in-preview", label: msg("Open in integrated browser") }, |
| 31 | { id: "open-external", label: msg("Open in system browser") }, |
| 32 | { id: "copy-link", label: msg("Copy Link") }, |
| 33 | ] as const satisfies readonly ContextMenuItem<ExternalLinkContextMenuAction>[]; |
| 34 | |
| 35 | function externalLinkContextMenuItems(options: { |
| 36 | readonly canOpenInPreview: boolean; |
| 37 | readonly threadLinkAction?: "link-to-thread" | "unlink-from-thread" | undefined; |
| 38 | }): readonly ContextMenuItem<ExternalLinkContextMenuAction>[] { |
| 39 | const items = ( |
| 40 | options.canOpenInPreview |
| 41 | ? EXTERNAL_LINK_CONTEXT_MENU_ITEMS |
| 42 | : EXTERNAL_LINK_CONTEXT_MENU_ITEMS.filter((item) => item.id !== "open-in-preview") |
| 43 | ).map((item) => ({ id: item.id, label: m(item.label) })); |
| 44 | if (options.threadLinkAction === undefined) return items; |
| 45 | return [ |
| 46 | { |
| 47 | id: options.threadLinkAction, |
| 48 | label: |
| 49 | options.threadLinkAction === "link-to-thread" |
| 50 | ? m(msg("Link to thread")) |
| 51 | : m(msg("Unlink from thread")), |
| 52 | }, |
| 53 | ...items, |
| 54 | ]; |
| 55 | } |
| 56 | |
| 57 | interface ShowExternalLinkContextMenuOptions { |
| 58 | readonly href: string; |
| 59 | readonly position: { readonly x: number; readonly y: number }; |
| 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 | |
| 76 | function resolveExternalWebLink(href: string | null | undefined): URL | 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; |
| 82 | } catch { |
| 83 | return null; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | export function resolveExternalWebLinkHref(href: string | null | undefined): string | null { |
| 88 | return resolveExternalWebLink(href)?.href ?? null; |
| 89 | } |
| 90 | |
| 91 | export function resolveExternalWebLinkHost(href: string | undefined): string | null { |
| 92 | return resolveExternalWebLink(href)?.hostname || null; |
| 93 | } |
| 94 | |
| 95 | export async function showExternalLinkContextMenu({ |
| 96 | href, |
| 97 | position, |
| 98 | canOpenInPreview = true, |
| 99 | threadLinkAction, |
| 100 | showContextMenu, |
| 101 | openInPreview, |
| 102 | openExternal, |
| 103 | copyLink, |
| 104 | updateThreadLink, |
| 105 | reportFailure, |
| 106 | }: ShowExternalLinkContextMenuOptions): Promise<void> { |
| 107 | let action: ExternalLinkContextMenuAction | null; |
| 108 | try { |
| 109 | action = await showContextMenu( |
| 110 | externalLinkContextMenuItems({ canOpenInPreview, threadLinkAction }), |
| 111 | position, |
| 112 | ); |
| 113 | } catch (cause) { |
| 114 | reportFailure("show-link-context-menu", cause); |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | try { |
| 119 | if (action === "open-in-preview") { |
| 120 | await openInPreview(href); |
| 121 | } else if (action === "open-external") { |
| 122 | await openExternal(href); |
| 123 | } else if (action === "copy-link") { |
| 124 | await copyLink(href); |
| 125 | } else if (action === "link-to-thread" || action === "unlink-from-thread") { |
| 126 | await updateThreadLink?.(href, action === "link-to-thread"); |
| 127 | } |
| 128 | } catch (cause) { |
| 129 | if (action) reportFailure(FAILURE_OPERATION_BY_ACTION[action], cause); |
| 130 | } |
| 131 | } |