Open History to see the last commit
1import {
2 AuthPreviewOperateScope,
3 type BrowserLinkTarget,
4 type ScopedThreadRef,
5} from "@t3tools/contracts";
6
7import { ensureClientSettingsHydrated, getClientSettings } from "~/hooks/useSettings";
8import { readEnvironmentScope } from "~/state/session";
9import { isPreviewAvailableFor } from "./previewRuntime";
10import { isWebUrl } from "./webUrl";
11
12export { isWebUrl };
13
14export interface ResolveLinkTargetInput {
15 readonly url: string;
16 readonly event: {
17 readonly metaKey: boolean;
18 readonly ctrlKey: boolean;
19 readonly shiftKey?: boolean | undefined;
20 readonly altKey?: boolean | undefined;
21 readonly button?: number | undefined;
22 };
23 readonly preference: BrowserLinkTarget;
24 readonly canOpenInApp: boolean;
25}
26
27export function resolveLinkTarget(input: ResolveLinkTargetInput): BrowserLinkTarget {
28 if (
29 input.event.metaKey ||
30 input.event.ctrlKey ||
31 input.event.shiftKey ||
32 input.event.altKey ||
33 input.event.button === 1
34 )
35 return "system";
36 if (input.preference !== "app") return "system";
37 if (!input.canOpenInApp) return "system";
38 if (!isWebUrl(input.url)) return "system";
39 return "app";
40}
41
42/** Before hydration, the default "system" would ignore a saved "app" link target. */
43export async function resolveBrowserLinkTargetPreference(): Promise<BrowserLinkTarget> {
44 await ensureClientSettingsHydrated();
45 return getClientSettings().browserLinkTarget;
46}
47
48export function canOpenLinksInApp(threadRef: ScopedThreadRef | null | undefined): boolean {
49 return (
50 threadRef != null &&
51 readEnvironmentScope(threadRef.environmentId, AuthPreviewOperateScope) &&
52 isPreviewAvailableFor(threadRef.environmentId)
53 );
54}