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 // The presentation changes with every server config push. Memoize on the
86 // pieces read here so callers keep a stable result across provider updates.
87 const entry = presentation?.entry ?? null;
88 const remoteOpenTargets = presentation?.serverConfig?.remoteOpenTargets;
89
90 return useMemo(() => {
91 if (entry === null) {
92 return UNRESOLVED_REMOTE_OPEN;
93 }
94 const profile = Option.getOrNull(entry.profile);
95 const sshAlias =
96 profile !== null && profile._tag === "SshConnectionProfile" ? profile.target.alias : null;
97 return {
98 state: resolveRemoteOpenState({
99 target: entry.target,
100 sshAlias,
101 remoteOpenTargets,
102 isDesktopRenderer: window.desktopBridge !== undefined,
103 }),
104 isResolved: true,
105 };
106 }, [entry, remoteOpenTargets]);
107}
108
109export function useRemoteOpenState(environmentId: EnvironmentId | null): RemoteOpenState {
110 return useRemoteOpenResolution(environmentId).state;
111}
112
113const REMOTE_FALLBACK_EDITORS: ReadonlyArray<EditorId> = ["vscode"];
114
115let cachedProbedEditors: ReadonlyArray<EditorId> | null = null;
116
117export function useRemoteCapableEditors(): ReadonlyArray<EditorId> {
118 const [editors, setEditors] = useState<ReadonlyArray<EditorId>>(
119 () => cachedProbedEditors ?? REMOTE_FALLBACK_EDITORS,
120 );
121
122 useEffect(() => {
123 if (cachedProbedEditors !== null) {
124 return;
125 }
126 const probe = window.desktopBridge?.probeRemoteEditors;
127 if (probe === undefined) {
128 cachedProbedEditors = REMOTE_FALLBACK_EDITORS;
129 return;
130 }
131 let cancelled = false;
132 probe().then(
133 (ids) => {
134 const remoteCapable = ids.filter((id) => REMOTE_CAPABLE_EDITOR_IDS.includes(id));
135 cachedProbedEditors = remoteCapable.length > 0 ? remoteCapable : REMOTE_FALLBACK_EDITORS;
136 if (!cancelled) {
137 setEditors(cachedProbedEditors);
138 }
139 },
140 () => {
141 cachedProbedEditors = REMOTE_FALLBACK_EDITORS;
142 },
143 );
144 return () => {
145 cancelled = true;
146 };
147 }, []);
148
149 return editors;
150}
151
152/**
153 * Resolves false when the desktop shell refused the URL (e.g. an older build whose protocol
154 * allowlist predates editor schemes) so callers do not record a successful open that never
155 * happened.
156 */
157export async function openRemoteEditorUrl(url: string): Promise<boolean> {
158 const bridge = window.desktopBridge;
159 if (bridge !== undefined) {
160 try {
161 return await bridge.openExternal(url);
162 } catch {
163 return false;
164 }
165 }
166 window.location.assign(url);
167 return true;
168}
169
170const REMOTE_OPEN_HINT_KEY = "t3code:remote-open-hint-seen";
171
172export function useRemoteOpenHint(): readonly [seen: boolean, markSeen: () => void] {
173 const [seen, setSeen] = useLocalStorage(REMOTE_OPEN_HINT_KEY, false, Schema.Boolean);
174 return [seen, () => setSeen(true)] as const;
175}