@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
tsx 308 lines 10.9 KB
Raw
1 import { T, useGT } from "gt-react";
2 import { useState } from "react";
3 import * as Option from "effect/Option";
4 import type { SshDeviceHostConfig } from "@t3tools/contracts";
5 import { CheckIcon, ChevronDownIcon, MonitorIcon, XIcon } from "lucide-react";
6 import { Button, InlineButton } from "../ui/button";
7 import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from "../ui/collapsible";
8 import { Input } from "../ui/input";
9 import { Spinner } from "../ui/spinner";
10 import {
11 Dialog,
12 DialogPopup,
13 DialogHeader,
14 DialogTitle,
15 DialogDescription,
16 DialogPanel,
17 DialogFooter,
18 } from "../ui/dialog";
19 import { DeviceHostAvailability } from "../device/DeviceHostAvailability";
20 import { useHostConnectionChecks } from "./useHostConnectionChecks";
21 import {
22 deviceHostConnectionKey,
23 parseDeviceHostDraft,
24 type DeviceHostCheckTarget,
25 } from "./deviceHostConnectionChecks";
26
27 const EXAMPLE_HOST_NAME = "Mac mini";
28
29 export function DeviceHostEditor({
30 host,
31 isNew,
32 targets,
33 busy,
34 onSave,
35 onClose,
36 }: {
37 host: SshDeviceHostConfig;
38 isNew: boolean;
39 targets: ReadonlyArray<DeviceHostCheckTarget>;
40 busy: boolean;
41 onSave: (host: SshDeviceHostConfig) => void;
42 onClose: () => void;
43 }) {
44 return (
45 <Dialog
46 open
47 onOpenChange={(open) => {
48 if (!open && !busy) onClose();
49 }}
50 >
51 {/* The form owns the draft so a keystroke re-renders it, not the dialog chrome. */}
52 <DialogPopup showCloseButton={!busy}>
53 <DeviceHostEditorForm
54 host={host}
55 isNew={isNew}
56 targets={targets}
57 busy={busy}
58 onSave={onSave}
59 onClose={onClose}
60 />
61 </DialogPopup>
62 </Dialog>
63 );
64 }
65
66 function DeviceHostEditorForm({
67 host,
68 isNew,
69 targets,
70 busy,
71 onSave,
72 onClose,
73 }: {
74 host: SshDeviceHostConfig;
75 isNew: boolean;
76 targets: ReadonlyArray<DeviceHostCheckTarget>;
77 busy: boolean;
78 onSave: (host: SshDeviceHostConfig) => void;
79 onClose: () => void;
80 }) {
81 const gt = useGT();
82 const [draft, setDraft] = useState(host);
83 const [expandedChecks, setExpandedChecks] = useState<{ key: string; open: boolean } | null>(null);
84 const { checks, testConnection } = useHostConnectionChecks(targets);
85 const connectionKey = deviceHostConnectionKey(draft);
86 const results = checks[connectionKey];
87 const checking = Object.values(results ?? {}).some((check) => check.status === "pending");
88 const input = parseDeviceHostDraft({ ...draft, label: draft.label.trim() || draft.target });
89 const valid = Option.isSome(input);
90 const failed = Object.values(results ?? {}).filter((check) => check.status === "failed").length;
91 const checksOpen =
92 targets.length === 1 ||
93 (expandedChecks?.key === connectionKey ? expandedChecks.open : failed > 0);
94 const checkSummary = checking
95 ? gt("Checking environments…")
96 : results
97 ? failed
98 ? gt("{failed} of {total} failed", { failed, total: targets.length })
99 : gt("Connection checks passed")
100 : gt("Check access before saving");
101 return (
102 <form
103 className="flex min-h-0 flex-col"
104 onSubmit={(event) => {
105 event.preventDefault();
106 if (Option.isSome(input) && draft.label.trim() && !busy && !checking) onSave(input.value);
107 }}
108 >
109 <DialogHeader>
110 <DialogTitle>{isNew ? gt("Add device host") : gt("Edit device host")}</DialogTitle>
111 <DialogDescription>
112 {targets.length === 1
113 ? gt("Connect from {environment}.", { environment: targets[0]?.label })
114 : gt("{count, plural, other {Connect from {count} selected environments.}}", {
115 count: targets.length,
116 })}{" "}
117 <T>Hosts on the same machine are skipped.</T>
118 </DialogDescription>
119 </DialogHeader>
120 <DialogPanel>
121 <label className="block space-y-1.5 text-sm">
122 <span>
123 <T>Name</T>
124 </span>
125 <Input
126 autoFocus
127 required
128 value={draft.label}
129 disabled={busy}
130 onChange={(event) => {
131 const label = event.target.value;
132 setDraft((current) => ({ ...current, label }));
133 }}
134 placeholder={EXAMPLE_HOST_NAME}
135 />
136 </label>
137 <label className="block space-y-1.5 text-sm">
138 <span>
139 <T>SSH target</T>
140 </span>
141 <Input
142 required
143 value={draft.target}
144 disabled={busy}
145 onChange={(event) => {
146 const target = event.target.value;
147 setDraft((current) => ({ ...current, target }));
148 }}
149 placeholder={gt("user@host or SSH alias")}
150 />
151 </label>
152 <details
153 open={host.port !== undefined || host.identityFile !== undefined || undefined}
154 className="text-sm"
155 >
156 <summary className="cursor-pointer text-muted-foreground">
157 <T>SSH options</T>
158 </summary>
159 <div className="mt-3 grid grid-cols-[minmax(0,1fr)_7rem] gap-3">
160 <label className="block space-y-1.5">
161 <span>
162 <T>Identity file</T>
163 </span>
164 <Input
165 value={draft.identityFile ?? ""}
166 disabled={busy}
167 onChange={(event) => {
168 const identityFile = event.target.value;
169 setDraft((current) => {
170 const { identityFile: _, ...rest } = current;
171 return identityFile ? { ...rest, identityFile } : rest;
172 });
173 }}
174 placeholder={gt("SSH config default")}
175 />
176 </label>
177 <label className="block space-y-1.5">
178 <span>
179 <T>Port</T>
180 </span>
181 <Input
182 type="number"
183 min={1}
184 max={65535}
185 value={draft.port ?? ""}
186 disabled={busy}
187 onChange={(event) => {
188 const port = event.target.value;
189 setDraft((current) => {
190 const { port: _, ...rest } = current;
191 return port ? { ...rest, port: Number(port) } : rest;
192 });
193 }}
194 placeholder={gt("Default", { $context: "uses the default SSH port" })}
195 />
196 </label>
197 </div>
198 <p className="mt-2 text-xs text-muted-foreground">
199 <T>Optional. Resolved separately on each environment.</T>
200 </p>
201 </details>
202 <Collapsible
203 open={checksOpen}
204 onOpenChange={(open) => setExpandedChecks({ key: connectionKey, open })}
205 render={<div className="rounded-lg border border-border/60" />}
206 >
207 <div className="flex items-center justify-between gap-3 px-3 py-2.5">
208 {targets.length === 1 ? (
209 <p role="status" className="text-xs text-muted-foreground">
210 {checkSummary}
211 </p>
212 ) : (
213 <div className="min-w-0 text-xs [--inline-button-white-space:normal]">
214 <CollapsibleTrigger
215 disabled={!results}
216 aria-label={gt("{summary}. Per-machine results", { summary: checkSummary })}
217 render={<InlineButton tone="muted" />}
218 >
219 <span role="status" className="text-left">
220 {checkSummary}
221 </span>
222 {results ? (
223 <ChevronDownIcon
224 className={`size-3 shrink-0 ${checksOpen ? "rotate-180" : ""}`}
225 />
226 ) : null}
227 </CollapsibleTrigger>
228 </div>
229 )}
230 <Button
231 type="button"
232 size="sm"
233 variant="outline"
234 disabled={busy || checking || !valid}
235 onClick={() => {
236 if (Option.isSome(input)) void testConnection(input.value);
237 }}
238 >
239 {checking ? <Spinner size="xs" /> : null} <T>Test connection</T>
240 </Button>
241 </div>
242 {results ? (
243 <CollapsiblePanel
244 render={<ul className="border-t border-border/60 divide-y divide-border/40" />}
245 >
246 {targets.map((target) => {
247 const result = results[target.environmentId];
248 if (!result) return null;
249 return (
250 <li key={target.environmentId} className="px-3 py-2.5 text-xs">
251 <div className="flex items-center justify-between gap-3">
252 <span className="truncate font-medium">{target.label}</span>
253 <span
254 className={`flex shrink-0 items-center gap-1.5 ${result.status === "failed" ? "text-destructive" : "text-muted-foreground"}`}
255 >
256 {result.status === "pending" ? (
257 <>
258 <Spinner size="xs" /> <T>Checking…</T>
259 </>
260 ) : result.status === "local" ? (
261 <>
262 <MonitorIcon className="size-3" /> <T>Already available locally</T>
263 </>
264 ) : result.status === "failed" ? (
265 <>
266 <XIcon className="size-3" />{" "}
267 {gt("Failed", { $context: "connection check status" })}
268 </>
269 ) : (
270 <>
271 <CheckIcon className="size-3" /> <T>Connected</T>
272 </>
273 )}
274 </span>
275 </div>
276 {result.status === "connected" ? (
277 <div className="mt-1.5">
278 <DeviceHostAvailability platforms={result.platforms} />
279 </div>
280 ) : null}
281 {result.status === "failed" ? (
282 <details className="mt-1.5 text-muted-foreground">
283 <summary className="cursor-pointer">
284 <T>Show error</T>
285 </summary>
286 <p className="mt-1 max-h-32 overflow-auto whitespace-pre-wrap break-words">
287 {result.error}
288 </p>
289 </details>
290 ) : null}
291 </li>
292 );
293 })}
294 </CollapsiblePanel>
295 ) : null}
296 </Collapsible>
297 </DialogPanel>
298 <DialogFooter>
299 <Button type="button" variant="ghost" disabled={busy} onClick={onClose}>
300 <T>Cancel</T>
301 </Button>
302 <Button type="submit" disabled={busy || checking || !valid || !draft.label.trim()}>
303 {busy ? <Spinner size="xs" /> : null} <T>Save host</T>
304 </Button>
305 </DialogFooter>
306 </form>
307 );
308 }