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

remoteOpen.ts

174 lines5.5 KB

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