Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import { useId, useRef, useState } from "react";
3import * as Cause from "effect/Cause";
4import * as Option from "effect/Option";
5import {
6 isSecretReference,
7 type DeviceEasAccount,
8 type EasDeviceHostConfig,
9 type EnvironmentId,
10 type SettingsSecretValue,
11} from "@t3tools/contracts";
12import { XIcon } from "lucide-react";
13
14import { deviceEasAccountCheck, deviceEnvironment } from "../../state/device";
15import { useAtomCommand } from "../../state/use-atom-command";
16import { Button } from "../ui/button";
17import {
18 Dialog,
19 DialogDescription,
20 DialogFooter,
21 DialogHeader,
22 DialogPanel,
23 DialogPopup,
24 DialogTitle,
25} from "../ui/dialog";
26import { Input } from "../ui/input";
27import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select";
28import { Spinner } from "../ui/spinner";
29import { parseEasDeviceHostDraft } from "./easDeviceHostsSettings.logic";
30import { SecretSourceInput, useSecretSources } from "./SecretSourceInput";
31
32const FROM_THREAD_PROJECT = "thread-project";
33
34const CREDENTIAL_SOURCES: Record<DeviceEasAccount["credentialSource"], string> = {
35 settings: "the access token saved here",
36 env: "EXPO_TOKEN on the server",
37 "eas-login": "the server's eas login",
38};
39
40const failureMessage = (cause: Cause.Cause<unknown>) => {
41 const error = Cause.squash(cause);
42 return error instanceof Error ? error.message : String(error);
43};
44
45type AccountCheck =
46 | { status: "idle" }
47 | { status: "checking" }
48 | { status: "signed-in"; account: DeviceEasAccount }
49 | { status: "failed"; error: string };
50
51export function EasDeviceHostEditor(props: {
52 environmentId: EnvironmentId;
53 host: EasDeviceHostConfig;
54 isNew: boolean;
55 busy: boolean;
56 onSave: (host: EasDeviceHostConfig) => void;
57 onClose: () => void;
58}) {
59 const check = useAtomCommand(deviceEnvironment.easAccount, { reportFailure: false });
60 const [draft, setDraft] = useState(props.host);
61 const tokenId = useId();
62 const secretSources = useSecretSources(props.environmentId);
63 const [tokenValid, setTokenValid] = useState(true);
64 const [tokenInputKey, setTokenInputKey] = useState(0);
65 // A saved token arrives redacted; only a replacement or a clear changes it.
66 const [token, setToken] = useState<
67 { kind: "saved" } | { kind: "new"; value: SettingsSecretValue } | { kind: "cleared" }
68 >(props.host.accessToken ? { kind: "saved" } : { kind: "cleared" });
69 // Checking on open names the saved project and shows a rejected credential
70 // before anything is changed. A manual check or a token edit replaces it.
71 const openCheck = useAtomValue(
72 deviceEasAccountCheck({
73 environmentId: props.environmentId,
74 input: props.host.accessToken
75 ? props.isNew
76 ? {}
77 : { hostId: props.host.id }
78 : { accessToken: "" },
79 }),
80 );
81 const [checked, setAccount] = useState<AccountCheck | null>(null);
82 const account: AccountCheck =
83 checked ??
84 (openCheck._tag === "Initial" || openCheck.waiting
85 ? { status: "checking" }
86 : openCheck._tag === "Success"
87 ? { status: "signed-in", account: openCheck.value }
88 : { status: "failed", error: failureMessage(openCheck.cause) });
89 const next: EasDeviceHostConfig = {
90 ...draft,
91 label: draft.label.trim(),
92 accessToken:
93 token.kind === "saved" ? props.host.accessToken : token.kind === "new" ? token.value : "",
94 };
95 const valid = tokenValid && Option.isSome(parseEasDeviceHostDraft(next));
96 // Why Save is off, for the limits the fields' ranges do not show.
97 const problem = valid
98 ? null
99 : !tokenValid
100 ? "Enter a valid secret reference."
101 : draft.maxSessionMinutes > 0 && draft.idleMinutes >= draft.maxSessionMinutes
102 ? "Idle minutes must be smaller than session minutes."
103 : "Give the host a label and limits within the ranges shown.";
104 const apps = account.status === "signed-in" ? account.account.apps : [];
105
106 // A check answers for the credential it was asked about: a later check or a
107 // token edit makes an earlier answer stale.
108 const latestCheck = useRef(0);
109 const resetAccount = () => {
110 latestCheck.current++;
111 setAccount({ status: "idle" });
112 };
113 const requestCheck = async () => {
114 const run = ++latestCheck.current;
115 const result = await check({
116 environmentId: props.environmentId,
117 input: {
118 ...(token.kind === "saved" && !props.isNew ? { hostId: props.host.id } : {}),
119 // A cleared token is sent as empty, so the check does not fall back to
120 // the saved one that Save is about to drop.
121 ...(token.kind === "new"
122 ? { accessToken: token.value }
123 : token.kind === "cleared"
124 ? { accessToken: "" }
125 : {}),
126 },
127 });
128 if (run !== latestCheck.current) return;
129 setAccount(
130 result._tag === "Success"
131 ? { status: "signed-in", account: result.value }
132 : { status: "failed", error: failureMessage(result.cause) },
133 );
134 };
135 const checkAccount = () => {
136 setAccount({ status: "checking" });
137 void requestCheck();
138 };
139
140 const limit = (
141 field: "maxSessionMinutes" | "idleMinutes" | "maxConcurrentSessions",
142 label: string,
143 max: number,
144 ) => (
145 <label className="block space-y-1.5">
146 <span>{label}</span>
147 <Input
148 type="number"
149 min={1}
150 max={max}
151 value={draft[field]}
152 disabled={props.busy}
153 onChange={(event) => {
154 const value = Number(event.target.value);
155 setDraft((current) => ({ ...current, [field]: value }));
156 }}
157 />
158 </label>
159 );
160
161 return (
162 <Dialog
163 open
164 onOpenChange={(open) => {
165 if (!open && !props.busy) props.onClose();
166 }}
167 >
168 <DialogPopup showCloseButton={!props.busy}>
169 <form
170 className="flex min-h-0 flex-col"
171 onSubmit={(event) => {
172 event.preventDefault();
173 if (valid && !props.busy) props.onSave(next);
174 }}
175 >
176 <DialogHeader>
177 <DialogTitle>{props.isNew ? "Add EAS host" : "Edit EAS host"}</DialogTitle>
178 <DialogDescription>
179 The environment's server signs in to Expo and starts the sessions. Without a token
180 here it uses EXPO_TOKEN, then its eas login.
181 </DialogDescription>
182 </DialogHeader>
183 <DialogPanel>
184 <label className="block space-y-1.5 text-sm">
185 <span>Name</span>
186 <Input
187 autoFocus
188 required
189 value={draft.label}
190 disabled={props.busy}
191 onChange={(event) => {
192 const label = event.target.value;
193 setDraft((current) => ({ ...current, label }));
194 }}
195 />
196 </label>
197 <div className="space-y-1.5 text-sm">
198 <label htmlFor={tokenId} className="block">
199 Expo access token
200 </label>
201 <div className="flex items-center gap-2">
202 <SecretSourceInput
203 key={tokenInputKey}
204 id={tokenId}
205 aria-label="Expo access token"
206 className="min-w-0 flex-1"
207 configured={token.kind === "saved" && typeof props.host.accessToken === "string"}
208 value={
209 token.kind === "new"
210 ? token.value
211 : token.kind === "saved" && isSecretReference(props.host.accessToken)
212 ? props.host.accessToken
213 : ""
214 }
215 sources={secretSources}
216 onValidityChange={(valid) => {
217 setTokenValid(valid);
218 resetAccount();
219 }}
220 onSecretChange={(secret) => {
221 setToken(secret ? { kind: "new", value: secret } : { kind: "cleared" });
222 resetAccount();
223 }}
224 disabled={props.busy}
225 placeholder="Optional"
226 onChange={(event) => {
227 const value = event.target.value;
228 // Emptying the field is not clearing the saved token; the
229 // Clear button is, so a saved token comes back here.
230 setToken(
231 value
232 ? { kind: "new", value }
233 : props.host.accessToken
234 ? { kind: "saved" }
235 : { kind: "cleared" },
236 );
237 resetAccount();
238 }}
239 />
240 {token.kind !== "cleared" ? (
241 <Button
242 type="button"
243 size="icon-sm"
244 variant="ghost-destructive"
245 aria-label="Clear access token"
246 onClick={() => {
247 setToken({ kind: "cleared" });
248 setTokenValid(true);
249 setTokenInputKey((key) => key + 1);
250 resetAccount();
251 }}
252 >
253 <XIcon className="size-3.5" />
254 </Button>
255 ) : null}
256 </div>
257 </div>
258 <div className="flex items-center justify-between gap-3 rounded-lg border border-border/60 px-3 py-2.5">
259 <p role="status" className="min-w-0 text-xs text-muted-foreground">
260 {account.status === "signed-in"
261 ? `Signed in as ${account.account.signedInAs} with ${CREDENTIAL_SOURCES[account.account.credentialSource]}.`
262 : account.status === "failed"
263 ? account.error
264 : account.status === "checking"
265 ? "Checking the Expo account…"
266 : "Check the Expo account before choosing a project."}
267 </p>
268 <Button
269 type="button"
270 size="sm"
271 variant="outline"
272 disabled={props.busy || !tokenValid || account.status === "checking"}
273 onClick={() => void checkAccount()}
274 >
275 {account.status === "checking" ? <Spinner size="xs" /> : null} Check account
276 </Button>
277 </div>
278 <div className="space-y-1.5 text-sm">
279 <span>EAS project</span>
280 <Select
281 value={draft.appId ?? FROM_THREAD_PROJECT}
282 disabled={props.busy}
283 onValueChange={(value) => {
284 if (typeof value !== "string") return;
285 setDraft((current) => {
286 const { appId: _, ...rest } = current;
287 return value === FROM_THREAD_PROJECT ? rest : { ...rest, appId: value };
288 });
289 }}
290 >
291 <SelectTrigger size="sm" aria-label="EAS project">
292 <SelectValue>
293 {(value: string) =>
294 value === FROM_THREAD_PROJECT
295 ? "From the thread's project"
296 : (apps.find((app) => app.id === value)?.fullName ?? value)
297 }
298 </SelectValue>
299 </SelectTrigger>
300 <SelectPopup>
301 <SelectItem value={FROM_THREAD_PROJECT}>From the thread's project</SelectItem>
302 {draft.appId && !apps.some((app) => app.id === draft.appId) ? (
303 <SelectItem value={draft.appId}>{draft.appId}</SelectItem>
304 ) : null}
305 {apps.map((app) => (
306 <SelectItem key={app.id} value={app.id}>
307 {app.fullName}
308 </SelectItem>
309 ))}
310 </SelectPopup>
311 </Select>
312 <p className="text-xs text-muted-foreground">
313 Sessions bill the project's Expo account. "From the thread's project" reads
314 extra.eas.projectId from the project's app config.
315 </p>
316 </div>
317 <div className="grid grid-cols-3 gap-3 text-sm">
318 {limit("maxConcurrentSessions", "Sessions at once", 20)}
319 {limit("maxSessionMinutes", "Session minutes", 115)}
320 {limit("idleMinutes", "Idle minutes", 60)}
321 </div>
322 <p className="text-xs text-muted-foreground">
323 Expo allows sessions of up to 40 minutes on free accounts and 115 on paid ones, and
324 ends one after the idle minutes without activity, so idle minutes must be smaller than
325 session minutes.
326 {props.isNew ? null : " Saving restarts this host, which stops its running sessions."}
327 </p>
328 {problem === null ? null : (
329 <p className="text-xs text-destructive" role="alert">
330 {problem}
331 </p>
332 )}
333 </DialogPanel>
334 <DialogFooter>
335 <Button type="button" variant="ghost" disabled={props.busy} onClick={props.onClose}>
336 Cancel
337 </Button>
338 <Button type="submit" disabled={props.busy || !valid}>
339 {props.busy ? <Spinner size="xs" /> : null} Save host
340 </Button>
341 </DialogFooter>
342 </form>
343 </DialogPopup>
344 </Dialog>
345 );
346}