Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

browserLinkTarget.ts

72 lines2.9 KB

1/**
2 * Where a link clicked inside a thread should open.
3 *
4 * Settings → Integrations → Browser lets the user choose between the OS
5 * default browser and a tab in the in-app browser. This module turns that
6 * preference plus the click itself into one answer, so chat markdown and the
7 * terminal drawer make the same decision and offer the same escape hatch.
8 *
9 * @module browserLinkTarget
10 */
11import type { BrowserLinkTarget, ScopedThreadRef } from "@t3tools/contracts";
12​
13import { ensureClientSettingsHydrated, getClientSettings } from "~/hooks/useSettings";
14import { isPreviewAvailableFor } from "./previewRuntime";
15​
16export interface ResolveLinkTargetInput {
17 readonly url: string;
18 /** Cmd/Ctrl-click and middle-click always go to the system browser, whatever the default. */
19 readonly event: {
20 readonly metaKey: boolean;
21 readonly ctrlKey: boolean;
22 readonly button?: number | undefined;
23 };
24 readonly preference: BrowserLinkTarget;
25 /** Whether this client has an in-app browser and a thread to open it beside. */
26 readonly canOpenInApp: boolean;
28​
29/**
30 * The target a click resolves to. "app" only comes back when the preference
31 * asks for it, the runtime can honour it, the URL is one the in-app browser
32 * can load, and the click carried no modifier — the modifier is the one-gesture
33 * way out when the default is in-app, mirroring how change-request links
34 * already treat it.
35 */
36export function resolveLinkTarget(input: ResolveLinkTargetInput): BrowserLinkTarget {
37 if (input.event.metaKey || input.event.ctrlKey || input.event.button === 1) return "system";
38 if (input.preference !== "app") return "system";
39 if (!input.canOpenInApp) return "system";
40 if (!isWebUrl(input.url)) return "system";
41 return "app";
43​
44/**
45 * Only http(s) can load in the in-app browser. Anything else — mailto:,
46 * vscode://, a bare fragment — belongs to the shell whatever the preference.
47 */
48export function isWebUrl(url: string): boolean {
49 try {
50 const { protocol } = new URL(url);
51 return protocol === "http:" || protocol === "https:";
52 } catch {
53 return false;
54 }
56​
57/**
58 * The configured default, once client settings have actually loaded. Before
59 * hydration the snapshot is the schema default ("system"), so a link clicked
60 * in the first moments after launch would ignore a persisted "app" — opening
61 * is asynchronous anyway, so waiting costs nothing the user can see.
62 * Read failures reject rather than choosing a browser without the saved preference.
63 */
64export async function resolveBrowserLinkTargetPreference(): Promise<BrowserLinkTarget> {
65 await ensureClientSettingsHydrated();
66 return getClientSettings().browserLinkTarget;
68​
69/** Whether this thread's environment has an in-app browser available to this client. */
70export function canOpenLinksInApp(threadRef: ScopedThreadRef | null | undefined): boolean {
71 return threadRef != null && isPreviewAvailableFor(threadRef.environmentId);