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

SimlockDeviceHostEditor.tsx

224 lines7.6 KB

1import { useId, useState, type ReactNode } from "react";
2import type { EnvironmentId, SimlockDeviceHostConfig } from "@t3tools/contracts";
3import { XIcon } from "lucide-react";
4​
5import { Button } from "../ui/button";
6import {
7 Dialog,
8 DialogDescription,
9 DialogFooter,
10 DialogHeader,
11 DialogPanel,
12 DialogPopup,
13 DialogTitle,
14} from "../ui/dialog";
15import { Input } from "../ui/input";
16import { Spinner } from "../ui/spinner";
17import { SecretInput } from "./SecretInput";
18import {
19 planSimlockDeviceHostSave,
20 type SimlockHostEnvironment,
21 type SimlockTokenEntry,
22} from "./simlockDeviceHostsSettings.logic";
23​
24/** Shared fields once, then one agent token per environment: Simlock counts leases per token. */
25export function SimlockDeviceHostEditor(props: {
26 host: SimlockDeviceHostConfig;
27 isNew: boolean;
28 environments: ReadonlyArray<SimlockHostEnvironment>;
29 busy: boolean;
30 onSave: (
31 host: SimlockDeviceHostConfig,
32 tokens: ReadonlyMap<EnvironmentId, SimlockTokenEntry>,
33 ) => void;
34 onClose: () => void;
35}) {
36 const [draft, setDraft] = useState(props.host);
37 const savedToken = (environment: SimlockHostEnvironment) =>
38 environment.hosts.some((host) => host.id === props.host.id && host.agentToken !== "");
39 const [tokens, setTokens] = useState<ReadonlyMap<EnvironmentId, SimlockTokenEntry>>(
40 () =>
41 new Map(
42 props.environments.map((environment) => [
43 environment.environmentId,
44 savedToken(environment) ? { kind: "saved" } : { kind: "cleared" },
45 ]),
46 ),
47 );
48 const next: SimlockDeviceHostConfig = {
49 ...draft,
50 label: draft.label.trim(),
51 gatewayUrl: draft.gatewayUrl.trim(),
52 };
53 const plan = planSimlockDeviceHostSave(props.environments, next, tokens);
54 const problem = "problem" in plan ? plan.problem : null;
55 const several = props.environments.length > 1;
56​
57 const limit = (
58 field: "maxLeases" | "maxLeaseMinutes",
59 label: string,
60 min: number,
61 max: number,
62 ) => (
63 <label className="block space-y-1.5">
64 <span>{label}</span>
65 <Input
66 type="number"
67 min={min}
68 max={max}
69 value={draft[field]}
70 disabled={props.busy}
71 onChange={(event) => {
72 const value = Number(event.target.value);
73 setDraft((current) => ({ ...current, [field]: value }));
74 }}
75 />
76 </label>
77 );
78​
79 return (
80 <Dialog
81 open
82 onOpenChange={(open) => {
83 if (!open && !props.busy) props.onClose();
84 }}
85 >
86 <DialogPopup showCloseButton={!props.busy}>
87 <form
88 className="flex min-h-0 flex-col"
89 onSubmit={(event) => {
90 event.preventDefault();
91 if (problem === null && !props.busy) props.onSave(next, tokens);
92 }}
93 >
94 <DialogHeader>
95 <DialogTitle>{props.isNew ? "Add Simlock host" : "Edit Simlock host"}</DialogTitle>
96 <DialogDescription>
97 {several
98 ? "Each selected environment's server leases devices from your Simlock gateway with its own agent token, and releases them when they close."
99 : "The environment's server leases devices from your Simlock gateway and releases them when they close."}
100 </DialogDescription>
101 </DialogHeader>
102 <DialogPanel>
103 <label className="block space-y-1.5 text-sm">
104 <span>Name</span>
105 <Input
106 autoFocus
107 required
108 value={draft.label}
109 disabled={props.busy}
110 onChange={(event) => {
111 const label = event.target.value;
112 setDraft((current) => ({ ...current, label }));
113 }}
114 />
115 </label>
116 <label className="block space-y-1.5 text-sm">
117 <span>Gateway URL</span>
118 <Input
119 required
120 type="url"
121 placeholder="https://gateway.example.ts.net:4700"
122 value={draft.gatewayUrl}
123 disabled={props.busy}
124 onChange={(event) => {
125 const gatewayUrl = event.target.value;
126 setDraft((current) => ({ ...current, gatewayUrl }));
127 }}
128 />
129 </label>
130 <div className="space-y-3">
131 {props.environments.map((environment) => (
132 <SimlockTokenField
133 key={environment.environmentId}
134 label={several ? `Agent token · ${environment.label}` : "Agent token"}
135 token={tokens.get(environment.environmentId) ?? { kind: "cleared" }}
136 hasSaved={savedToken(environment)}
137 busy={props.busy}
138 onChange={(token) =>
139 setTokens((current) => new Map(current).set(environment.environmentId, token))
140 }
141 />
142 ))}
143 <p className="text-xs text-muted-foreground">
144 Leases, drives, and streams devices. Create one{several ? " per environment" : ""}{" "}
145 with <code>simlock --gateway token create --role agent</code>.
146 </p>
147 </div>
148 <div className="grid grid-cols-2 gap-3 text-sm">
149 {limit("maxLeases", "Devices at once", 1, 20)}
150 {limit("maxLeaseMinutes", "Lease minutes", 5, 240)}
151 </div>
152 <p className="text-xs text-muted-foreground">
153 A lease ends after the lease minutes even while in use.
154 {props.isNew ? null : " Saving restarts this host, which releases its devices."}
155 </p>
156 {problem === null || !draft.gatewayUrl ? null : (
157 <p className="text-xs text-destructive" role="alert">
158 {problem}
159 </p>
160 )}
161 </DialogPanel>
162 <DialogFooter>
163 <Button type="button" variant="ghost" disabled={props.busy} onClick={props.onClose}>
164 Cancel
165 </Button>
166 <Button type="submit" disabled={props.busy || problem !== null}>
167 {props.busy ? <Spinner size="xs" /> : null} Save host
168 </Button>
169 </DialogFooter>
170 </form>
171 </DialogPopup>
172 </Dialog>
173 );
175​
176function SimlockTokenField(props: {
177 label: ReactNode;
178 token: SimlockTokenEntry;
179 hasSaved: boolean;
180 busy: boolean;
181 onChange: (token: SimlockTokenEntry) => void;
182}) {
183 const id = useId();
184 return (
185 <div className="space-y-1.5 text-sm">
186 <label htmlFor={id} className="block">
187 {props.label}
188 </label>
189 <div className="flex items-center gap-2">
190 <SecretInput
191 id={id}
192 className="min-w-0 flex-1"
193 configured={props.token.kind === "saved"}
194 value={props.token.kind === "new" ? props.token.value : ""}
195 disabled={props.busy}
196 onChange={(event) => {
197 const value = event.target.value;
198 // Emptying the field is not clearing the saved token; the Clear
199 // button is, so a saved token comes back here.
200 props.onChange(
201 value
202 ? { kind: "new", value }
203 : props.hasSaved
204 ? { kind: "saved" }
205 : { kind: "cleared" },
206 );
207 }}
208 />
209 {props.token.kind !== "cleared" ? (
210 <Button
211 type="button"
212 size="icon-sm"
213 variant="ghost-destructive"
214 aria-label="Clear agent token"
215 disabled={props.busy}
216 onClick={() => props.onChange({ kind: "cleared" })}
217 >
218 <XIcon className="size-3.5" />
219 </Button>
220 ) : null}
221 </div>
222 </div>
223 );