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