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