Open History to see the last commit
1import { T, useGT, plural, part } from "gt-react";
2import { useState } from "react";
3import * as Option from "effect/Option";
4import type { SshDeviceHostConfig } from "@t3tools/contracts";
5import { CheckIcon, ChevronDownIcon, MonitorIcon, XIcon } from "lucide-react";
6import { Button, InlineButton } from "../ui/button";
7import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from "../ui/collapsible";
8import { Input } from "../ui/input";
9import { Spinner } from "../ui/spinner";
10import {
11 Dialog,
12 DialogPopup,
13 DialogHeader,
14 DialogTitle,
15 DialogDescription,
16 DialogPanel,
17 DialogFooter,
18} from "../ui/dialog";
19import { DeviceHostAvailability } from "../device/DeviceHostAvailability";
20import { useHostConnectionChecks } from "./useHostConnectionChecks";
21import {
22 deviceHostConnectionKey,
23 parseDeviceHostDraft,
24 type DeviceHostCheckTarget,
25} from "./deviceHostConnectionChecks";
26
27const EXAMPLE_HOST_NAME = "Mac mini";
28
29export 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
66function 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: targets.length }} failed`
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: targets[0]?.label }}.`
114 : gt`${plural(
115 { count: targets.length },
116 {
117 other: part`Connect from ${{ count: targets.length }} selected environments.`,
118 },
119 )}`}{" "}
120 <T>Hosts on the same machine are skipped.</T>
121 </DialogDescription>
122 </DialogHeader>
123 <DialogPanel>
124 <label className="block space-y-1.5 text-sm">
125 <span>
126 <T>Name</T>
127 </span>
128 <Input
129 autoFocus
130 required
131 value={draft.label}
132 disabled={busy}
133 onChange={(event) => {
134 const label = event.target.value;
135 setDraft((current) => ({ ...current, label }));
136 }}
137 placeholder={EXAMPLE_HOST_NAME}
138 />
139 </label>
140 <label className="block space-y-1.5 text-sm">
141 <span>
142 <T>SSH target</T>
143 </span>
144 <Input
145 required
146 value={draft.target}
147 disabled={busy}
148 onChange={(event) => {
149 const target = event.target.value;
150 setDraft((current) => ({ ...current, target }));
151 }}
152 placeholder={gt`user@host or SSH alias`}
153 />
154 </label>
155 <details
156 open={host.port !== undefined || host.identityFile !== undefined || undefined}
157 className="text-sm"
158 >
159 <summary className="cursor-pointer text-muted-foreground">
160 <T>SSH options</T>
161 </summary>
162 <div className="mt-3 grid grid-cols-[minmax(0,1fr)_7rem] gap-3">
163 <label className="block space-y-1.5">
164 <span>
165 <T>Identity file</T>
166 </span>
167 <Input
168 value={draft.identityFile ?? ""}
169 disabled={busy}
170 onChange={(event) => {
171 const identityFile = event.target.value;
172 setDraft((current) => {
173 const { identityFile: _, ...rest } = current;
174 return identityFile ? { ...rest, identityFile } : rest;
175 });
176 }}
177 placeholder={gt`SSH config default`}
178 />
179 </label>
180 <label className="block space-y-1.5">
181 <span>
182 <T>Port</T>
183 </span>
184 <Input
185 type="number"
186 min={1}
187 max={65535}
188 value={draft.port ?? ""}
189 disabled={busy}
190 onChange={(event) => {
191 const port = event.target.value;
192 setDraft((current) => {
193 const { port: _, ...rest } = current;
194 return port ? { ...rest, port: Number(port) } : rest;
195 });
196 }}
197 placeholder={gt({ context: "uses the default SSH port" })`Default`}
198 />
199 </label>
200 </div>
201 <p className="mt-2 text-xs text-muted-foreground">
202 <T>Optional. Resolved separately on each environment.</T>
203 </p>
204 </details>
205 <Collapsible
206 open={checksOpen}
207 onOpenChange={(open) => setExpandedChecks({ key: connectionKey, open })}
208 render={<div className="rounded-lg border border-border/60" />}
209 >
210 <div className="flex items-center justify-between gap-3 px-3 py-2.5">
211 {targets.length === 1 ? (
212 <p role="status" className="text-xs text-muted-foreground">
213 {checkSummary}
214 </p>
215 ) : (
216 <div className="min-w-0 text-xs [--inline-button-white-space:normal]">
217 <CollapsibleTrigger
218 disabled={!results}
219 aria-label={gt`${{ summary: checkSummary }}. Per-machine results`}
220 render={<InlineButton tone="muted" />}
221 >
222 <span role="status" className="text-left">
223 {checkSummary}
224 </span>
225 {results ? (
226 <ChevronDownIcon
227 className={`size-3 shrink-0 ${checksOpen ? "rotate-180" : ""}`}
228 />
229 ) : null}
230 </CollapsibleTrigger>
231 </div>
232 )}
233 <Button
234 type="button"
235 size="sm"
236 variant="outline"
237 disabled={busy || checking || !valid}
238 onClick={() => {
239 if (Option.isSome(input)) void testConnection(input.value);
240 }}
241 >
242 {checking ? <Spinner size="xs" /> : null} <T>Test connection</T>
243 </Button>
244 </div>
245 {results ? (
246 <CollapsiblePanel
247 render={<ul className="border-t border-border/60 divide-y divide-border/40" />}
248 >
249 {targets.map((target) => {
250 const result = results[target.environmentId];
251 if (!result) return null;
252 return (
253 <li key={target.environmentId} className="px-3 py-2.5 text-xs">
254 <div className="flex items-center justify-between gap-3">
255 <span className="truncate font-medium">{target.label}</span>
256 <span
257 className={`flex shrink-0 items-center gap-1.5 ${result.status === "failed" ? "text-destructive" : "text-muted-foreground"}`}
258 >
259 {result.status === "pending" ? (
260 <>
261 <Spinner size="xs" /> <T>Checking…</T>
262 </>
263 ) : result.status === "local" ? (
264 <>
265 <MonitorIcon className="size-3" /> <T>Already available locally</T>
266 </>
267 ) : result.status === "failed" ? (
268 <>
269 <XIcon className="size-3" />{" "}
270 {gt({ context: "connection check status" })`Failed`}
271 </>
272 ) : (
273 <>
274 <CheckIcon className="size-3" /> <T>Connected</T>
275 </>
276 )}
277 </span>
278 </div>
279 {result.status === "connected" ? (
280 <div className="mt-1.5">
281 <DeviceHostAvailability platforms={result.platforms} />
282 </div>
283 ) : null}
284 {result.status === "failed" ? (
285 <details className="mt-1.5 text-muted-foreground">
286 <summary className="cursor-pointer">
287 <T>Show error</T>
288 </summary>
289 <p className="mt-1 max-h-32 overflow-auto whitespace-pre-wrap break-words">
290 {result.error}
291 </p>
292 </details>
293 ) : null}
294 </li>
295 );
296 })}
297 </CollapsiblePanel>
298 ) : null}
299 </Collapsible>
300 </DialogPanel>
301 <DialogFooter>
302 <Button type="button" variant="ghost" disabled={busy} onClick={onClose}>
303 <T>Cancel</T>
304 </Button>
305 <Button type="submit" disabled={busy || checking || !valid || !draft.label.trim()}>
306 {busy ? <Spinner size="xs" /> : null} <T>Save host</T>
307 </Button>
308 </DialogFooter>
309 </form>
310 );
311}