Open History to see the last commit
1import { Tooltip, TooltipTrigger, TooltipPopup } from "../ui/tooltip";
2import { AppleIcon, AndroidIcon } from "../Icons";
3import { DeviceHostAvailability } from "../device/DeviceHostAvailability";
4import { Spinner } from "../ui/spinner";
5import type {
6 DevicePlatformAvailability,
7 EnvironmentId,
8 SshDeviceHostConfig,
9} from "@t3tools/contracts";
10import * as Cause from "effect/Cause";
11import { randomUUID } from "../../lib/utils";
12import { useState } from "react";
13import { deviceEnvironment, useDeviceState } from "../../state/device";
14import { serverEnvironment } from "../../state/server";
15import { useAtomCommand } from "../../state/use-atom-command";
16import { Button } from "../ui/button";
17import { Input } from "../ui/input";
18import { MoreVertical, PlusIcon } from "lucide-react";
19import { Menu, MenuTrigger, MenuPopup, MenuItem } from "../ui/menu";
20import { SettingsRow } from "./settingsLayout";
21
22/** Host names and identity paths belong to the selected environment, never all environments. */
23export function DeviceHostsSettings(props: {
24 environmentId: EnvironmentId | null;
25 hosts: ReadonlyArray<SshDeviceHostConfig>;
26}) {
27 const update = useAtomCommand(serverEnvironment.updateSettings);
28 const test = useAtomCommand(deviceEnvironment.testHost, { reportFailure: false });
29 const { state } = useDeviceState(props.environmentId);
30 const [editing, setEditing] = useState<SshDeviceHostConfig | null>(null);
31 const [busy, setBusy] = useState(false);
32 const validPort = (port: number | undefined) =>
33 port === undefined || (Number.isInteger(port) && port >= 1 && port <= 65535);
34 const [checks, setChecks] = useState<
35 Record<
36 string,
37 { pending?: boolean; platforms?: ReadonlyArray<DevicePlatformAvailability>; error?: string }
38 >
39 >({});
40 const setCheck = (id: string, value: (typeof checks)[string]) =>
41 setChecks((current) => ({ ...current, [id]: value }));
42 const save = async (hosts: ReadonlyArray<SshDeviceHostConfig>) => {
43 if (!props.environmentId) return;
44 setBusy(true);
45 try {
46 const saved = await update({
47 environmentId: props.environmentId,
48 input: { patch: { deviceHosts: hosts } },
49 });
50 if (saved._tag === "Success") {
51 setEditing(null);
52 }
53 } finally {
54 setBusy(false);
55 }
56 };
57 const testConnection = async (host: SshDeviceHostConfig) => {
58 if (!props.environmentId || checks[host.id]?.pending) return;
59 setCheck(host.id, { pending: true });
60 try {
61 const summary = await test({ environmentId: props.environmentId, input: host });
62 setCheck(
63 host.id,
64 summary._tag === "Failure"
65 ? { error: Cause.pretty(summary.cause) }
66 : { platforms: summary.value.platforms },
67 );
68 } catch (error) {
69 setCheck(host.id, { error: error instanceof Error ? error.message : String(error) });
70 }
71 };
72 return (
73 <SettingsRow
74 id="device-hosts"
75 title="Device hosts"
76 description="Add remote machines with simulator or emulator runtimes installed, and this environment will connect over SSH and set up device tools automatically."
77 control={
78 <Button
79 size="sm"
80 variant="outline"
81 disabled={busy || !props.environmentId || editing !== null}
82 onClick={() => {
83 setEditing({ id: randomUUID(), label: "", target: "" });
84 }}
85 >
86 <PlusIcon className="size-3.5" /> Add host
87 </Button>
88 }
89 >
90 <div className="pt-3 pb-2">
91 {!props.environmentId ? (
92 <p className="text-sm text-muted-foreground">
93 Select one connected environment to manage its device hosts.
94 </p>
95 ) : (
96 <>
97 {props.hosts.map((host) => {
98 const status = state.hostStatuses[host.id];
99 const check = checks[host.id];
100 const platforms =
101 check?.platforms ??
102 state.hosts.find((value) => value.id === host.id)?.platforms ??
103 [];
104 const progress = check?.pending
105 ? "Checking connection…"
106 : status?.status === "installing"
107 ? "Installing device support…"
108 : status?.status === "starting"
109 ? "Connecting…"
110 : null;
111 const error =
112 check?.error ?? (status?.status === "failed" ? status.detail : undefined);
113 return (
114 <div
115 key={host.id}
116 className="flex items-center gap-2 border-t border-border/50 py-2.5"
117 >
118 <div className="min-w-0 flex-1">
119 <div className="flex min-w-0 items-center gap-2">
120 <p className="truncate text-sm font-medium">{host.label}</p>
121 {platforms
122 .filter((platform) => platform.available)
123 .map((platform) => (
124 <Tooltip key={platform.platform}>
125 <TooltipTrigger
126 render={
127 <span
128 tabIndex={0}
129 role="img"
130 aria-label={
131 platform.platform === "ios"
132 ? "iOS available"
133 : "Android available"
134 }
135 className="shrink-0 text-muted-foreground"
136 />
137 }
138 >
139 {platform.platform === "ios" ? (
140 <AppleIcon className="size-3.5" />
141 ) : (
142 <AndroidIcon className="size-3.5" />
143 )}
144 </TooltipTrigger>
145 <TooltipPopup>
146 {platform.platform === "ios" ? "iOS available" : "Android available"}
147 </TooltipPopup>
148 </Tooltip>
149 ))}
150 </div>
151 <p className="truncate text-xs text-muted-foreground">{host.target}</p>
152 {error ? (
153 <div className="mt-1" role="status">
154 <details className="text-xs text-destructive">
155 <summary>Connection failed</summary>
156 <p className="mt-1 whitespace-pre-wrap break-words">{error}</p>
157 </details>
158 </div>
159 ) : null}
160 </div>
161 {progress ? (
162 <span
163 role="status"
164 className="inline-flex items-center gap-1.5 text-xs text-muted-foreground"
165 >
166 <Spinner className="size-3" />
167 {progress}
168 </span>
169 ) : null}
170 <Menu>
171 <MenuTrigger
172 render={
173 <Button
174 size="icon-sm"
175 variant="ghost-muted"
176 disabled={busy}
177 aria-label={host.label + " options"}
178 />
179 }
180 >
181 <MoreVertical />
182 </MenuTrigger>
183 <MenuPopup align="end">
184 <MenuItem
185 onClick={() => {
186 setEditing(host);
187 }}
188 >
189 Edit
190 </MenuItem>
191 <MenuItem
192 variant="destructive"
193 onClick={() =>
194 void save(props.hosts.filter((value) => value.id !== host.id))
195 }
196 >
197 Remove
198 </MenuItem>
199 </MenuPopup>
200 </Menu>
201 <Button
202 size="sm"
203 variant="outline"
204 disabled={busy || progress !== null}
205 onClick={() => void testConnection(host)}
206 >
207 Test connection
208 </Button>
209 </div>
210 );
211 })}
212 {editing ? (
213 <form
214 className="space-y-3 border-t border-border/50 py-3"
215 onSubmit={(event) => {
216 event.preventDefault();
217 void save([...props.hosts.filter((host) => host.id !== editing.id), editing]);
218 }}
219 >
220 <label className="block space-y-1 text-sm">
221 <span>Name</span>
222 <Input
223 required
224 value={editing.label}
225 disabled={busy}
226 onChange={(event) => setEditing({ ...editing, label: event.target.value })}
227 placeholder="Mac mini"
228 />
229 </label>
230 <label className="block space-y-1 text-sm">
231 <span>SSH target</span>
232 <Input
233 required
234 value={editing.target}
235 disabled={busy}
236 onChange={(event) => setEditing({ ...editing, target: event.target.value })}
237 placeholder="user@host or SSH alias"
238 />
239 </label>
240 <label className="block space-y-1 text-sm">
241 <span>Identity file, optional</span>
242 <Input
243 value={editing.identityFile ?? ""}
244 disabled={busy}
245 onChange={(event) => {
246 const { identityFile: _, ...rest } = editing;
247 setEditing(
248 event.target.value ? { ...rest, identityFile: event.target.value } : rest,
249 );
250 }}
251 placeholder="~/.ssh/id_ed25519"
252 />
253 </label>
254 <label className="block space-y-1 text-sm">
255 <span>Port, optional</span>
256 <Input
257 type="number"
258 min={1}
259 max={65535}
260 value={editing.port ?? ""}
261 disabled={busy}
262 onChange={(event) => {
263 const { port: _, ...rest } = editing;
264 setEditing(
265 event.target.value ? { ...rest, port: Number(event.target.value) } : rest,
266 );
267 }}
268 placeholder="SSH config default"
269 />
270 </label>
271 <div className="flex flex-wrap gap-2">
272 <Button
273 size="sm"
274 type="submit"
275 disabled={
276 busy ||
277 !editing.label.trim() ||
278 !editing.target.trim() ||
279 !validPort(editing.port)
280 }
281 >
282 Save host
283 </Button>
284 <Button
285 size="sm"
286 type="button"
287 variant="outline"
288 disabled={
289 busy ||
290 !editing.label.trim() ||
291 !editing.target.trim() ||
292 !validPort(editing.port)
293 }
294 onClick={() => void testConnection(editing)}
295 >
296 Test connection
297 </Button>
298 <Button
299 size="sm"
300 type="button"
301 variant="ghost"
302 disabled={busy}
303 onClick={() => {
304 setEditing(null);
305 }}
306 >
307 Cancel
308 </Button>
309 </div>
310 {checks[editing.id]?.pending ? (
311 <span
312 role="status"
313 className="inline-flex items-center gap-1.5 text-xs text-muted-foreground"
314 >
315 <Spinner className="size-3" />
316 Checking connection…
317 </span>
318 ) : null}
319 {checks[editing.id]?.platforms ? (
320 <DeviceHostAvailability platforms={checks[editing.id]?.platforms ?? []} />
321 ) : null}
322 {checks[editing.id]?.error ? (
323 <p role="alert" className="text-xs text-destructive">
324 {checks[editing.id]?.error}
325 </p>
326 ) : null}
327 </form>
328 ) : null}
329 </>
330 )}
331 </div>
332 </SettingsRow>
333 );
334}