Open History to see the last commit
1import type { ConnectionTarget } from "@t3tools/client-runtime/connection";
2import {
3 REMOTE_CAPABLE_EDITOR_IDS,
4 type EditorId,
5 type EnvironmentId,
6 type RemoteOpenTarget,
7} from "@t3tools/contracts";
8import * as Option from "effect/Option";
9import * as Schema from "effect/Schema";
10import { useEffect, useMemo, useState } from "react";
11
12import { isDesktopLocalConnectionTarget } from "~/connection/desktopLocal";
13import { isLoopbackHostname } from "~/environments/primary/target";
14import { useLocalStorage } from "~/hooks/useLocalStorage";
15import { useEnvironmentPresentation } from "~/state/presentation";
16
17export interface RemoteOpenHost {
18 readonly kind: "ssh-alias" | RemoteOpenTarget["kind"];
19 readonly host: string;
20}
21
22export type RemoteOpenState =
23 | { readonly mode: "local-exec" }
24 | { readonly mode: "remote-links"; readonly host: RemoteOpenHost }
25 | { readonly mode: "remote-unavailable" };
26
27export type RemoteOpenMode = RemoteOpenState["mode"];
28
29export interface RemoteOpenResolution {
30 readonly state: RemoteOpenState;
31 readonly isResolved: boolean;
32}
33
34const LOCAL_EXEC: RemoteOpenState = { mode: "local-exec" };
35const REMOTE_UNAVAILABLE: RemoteOpenState = { mode: "remote-unavailable" };
36const UNRESOLVED_REMOTE_OPEN: RemoteOpenResolution = {
37 state: LOCAL_EXEC,
38 isResolved: false,
39};
40
41function parseHostname(url: string): string | null {
42 try {
43 return new URL(url).hostname;
44 } catch {
45 return null;
46 }
47}
48
49export function resolveRemoteOpenState(input: {
50 readonly target: ConnectionTarget | null;
51 readonly sshAlias: string | null;
52 readonly remoteOpenTargets: ReadonlyArray<RemoteOpenTarget> | undefined;
53 readonly isDesktopRenderer: boolean;
54}): RemoteOpenState {
55 const { target } = input;
56 if (target === null) {
57 return LOCAL_EXEC;
58 }
59 if (target._tag === "PrimaryConnectionTarget") {
60 // The desktop app manages its own primary backend, so it is always on this machine even
61 // when its URL is not loopback (wsl-only mode binds the WSL2 NAT address).
62 if (input.isDesktopRenderer) {
63 return LOCAL_EXEC;
64 }
65 const hostname = parseHostname(target.httpBaseUrl);
66 if (hostname !== null && isLoopbackHostname(hostname)) {
67 return LOCAL_EXEC;
68 }
69 } else if (isDesktopLocalConnectionTarget(target)) {
70 return LOCAL_EXEC;
71 }
72
73 if (input.sshAlias !== null && input.sshAlias.length > 0) {
74 return { mode: "remote-links", host: { kind: "ssh-alias", host: input.sshAlias } };
75 }
76 const advertised = input.remoteOpenTargets?.[0];
77 if (advertised !== undefined) {
78 return { mode: "remote-links", host: advertised };
79 }
80 return REMOTE_UNAVAILABLE;
81}
82
83export function useRemoteOpenResolution(environmentId: EnvironmentId | null): RemoteOpenResolution {
84 const { presentation } = useEnvironmentPresentation(environmentId);
85
86 return useMemo(() => {
87 if (presentation === null) {
88 return UNRESOLVED_REMOTE_OPEN;
89 }
90 const profile = Option.getOrNull(presentation.entry.profile);
91 const sshAlias =
92 profile !== null && profile._tag === "SshConnectionProfile" ? profile.target.alias : null;
93 return {
94 state: resolveRemoteOpenState({
95 target: presentation.entry.target,
96 sshAlias,
97 remoteOpenTargets: presentation.serverConfig?.remoteOpenTargets,
98 isDesktopRenderer: window.desktopBridge !== undefined,
99 }),
100 isResolved: true,
101 };
102 }, [presentation]);
103}
104
105export function useRemoteOpenState(environmentId: EnvironmentId | null): RemoteOpenState {
106 return useRemoteOpenResolution(environmentId).state;
107}
108
109const REMOTE_FALLBACK_EDITORS: ReadonlyArray<EditorId> = ["vscode"];
110
111let cachedProbedEditors: ReadonlyArray<EditorId> | null = null;
112
113export function useRemoteCapableEditors(): ReadonlyArray<EditorId> {
114 const [editors, setEditors] = useState<ReadonlyArray<EditorId>>(
115 () => cachedProbedEditors ?? REMOTE_FALLBACK_EDITORS,
116 );
117
118 useEffect(() => {
119 if (cachedProbedEditors !== null) {
120 return;
121 }
122 const probe = window.desktopBridge?.probeRemoteEditors;
123 if (probe === undefined) {
124 cachedProbedEditors = REMOTE_FALLBACK_EDITORS;
125 return;
126 }
127 let cancelled = false;
128 probe().then(
129 (ids) => {
130 const remoteCapable = ids.filter((id) => REMOTE_CAPABLE_EDITOR_IDS.includes(id));
131 cachedProbedEditors = remoteCapable.length > 0 ? remoteCapable : REMOTE_FALLBACK_EDITORS;
132 if (!cancelled) {
133 setEditors(cachedProbedEditors);
134 }
135 },
136 () => {
137 cachedProbedEditors = REMOTE_FALLBACK_EDITORS;
138 },
139 );
140 return () => {
141 cancelled = true;
142 };
143 }, []);
144
145 return editors;
146}
147
148/**
149 * Resolves false when the desktop shell refused the URL (e.g. an older build whose protocol
150 * allowlist predates editor schemes) so callers do not record a successful open that never
151 * happened.
152 */
153export async function openRemoteEditorUrl(url: string): Promise<boolean> {
154 const bridge = window.desktopBridge;
155 if (bridge !== undefined) {
156 try {
157 return await bridge.openExternal(url);
158 } catch {
159 return false;
160 }
161 }
162 window.location.assign(url);
163 return true;
164}
165
166const REMOTE_OPEN_HINT_KEY = "t3code:remote-open-hint-seen";
167
168export function useRemoteOpenHint(): readonly [seen: boolean, markSeen: () => void] {
169 const [seen, setSeen] = useLocalStorage(REMOTE_OPEN_HINT_KEY, false, Schema.Boolean);
170 return [seen, () => setSeen(true)] as const;
171}