Open History to see the last commit
1import { DeviceHostUpdates } from "./DeviceHostUpdates";
2import type { DevicePlatform, DeviceServiceState, EnvironmentId } from "@t3tools/contracts";
3import { Check, CircleAlert } from "lucide-react";
4import { useState } from "react";
5
6import { Button } from "~/components/ui/button";
7import { DialogClose } from "~/components/ui/dialog";
8import { WizardHeader, WizardPanel, WizardSteps, WizardFooter } from "~/components/ui/wizard";
9import { Spinner } from "~/components/ui/spinner";
10import { Switch } from "~/components/ui/switch";
11import { deviceEnvironment } from "~/state/device";
12import { useAtomCommand } from "~/state/use-atom-command";
13import { cn } from "~/lib/utils";
14
15const platformName = (platform: DevicePlatform) => (platform === "ios" ? "iOS" : "Android");
16
17export const deviceHubDescription =
18 "Enable this environment to open simulators and emulators, whether they run here or on a remote device host.";
19export const agentDeviceDescription =
20 "Allow new agent sessions in this environment to start and control local and remote devices, with required tools set up automatically.";
21
22export function platformSetupStatus(state: DeviceServiceState, platform: DevicePlatform) {
23 const availability = state.hosts
24 .flatMap((host) => host.platforms)
25 .find((candidate) => candidate.platform === platform);
26 if (!availability?.available) {
27 return {
28 ready: false,
29 message: availability?.reason ?? `${platformName(platform)} support was not detected.`,
30 };
31 }
32 if (
33 state.hostStatus === "ready" &&
34 !state.devices.some((device) => device.platform === platform)
35 ) {
36 return {
37 ready: false,
38 message:
39 platform === "ios"
40 ? "Xcode is installed, but no iOS Simulator is available. Install a runtime in Xcode Settings → Components."
41 : "The Android SDK is installed, but no virtual device exists. Create one in Android Studio → Device Manager.",
42 };
43 }
44 return {
45 ready: true,
46 message:
47 platform === "ios"
48 ? "Xcode and iOS Simulator are available."
49 : "The Android SDK and Emulator are available.",
50 };
51}
52
53export function DeviceSetup(props: {
54 readonly environmentId: EnvironmentId;
55 readonly state: DeviceServiceState;
56 readonly onComplete?: () => void;
57}) {
58 const configure = useAtomCommand(deviceEnvironment.configure);
59 const list = useAtomCommand(deviceEnvironment.list, { reportFailure: false });
60 const [pending, setPending] = useState<"hub" | "check" | "agent" | "complete" | null>(null);
61 const [step, setStep] = useState(0);
62 const enabled = props.state.hostStatus !== "disabled";
63 const busy = props.state.hostStatus === "installing" || props.state.hostStatus === "starting";
64
65 const update = async (
66 kind: NonNullable<typeof pending>,
67 input: { enabled?: boolean; agentAccessEnabled?: boolean; onboardingCompleted?: boolean },
68 ) => {
69 setPending(kind);
70 try {
71 const result = await configure({ environmentId: props.environmentId, input });
72 if (kind === "complete" && result._tag === "Success") props.onComplete?.();
73 } finally {
74 setPending(null);
75 }
76 };
77
78 return (
79 <>
80 <WizardHeader
81 title="Set up devices"
82 description="Review what runs on this environment before using simulators and emulators."
83 >
84 <WizardSteps
85 steps={["Device hub", "Simulators", "Agent access"]}
86 currentStep={step}
87 onStepChange={setStep}
88 isStepDisabled={(requested) => busy || pending !== null || requested > step}
89 />
90 </WizardHeader>
91
92 <WizardPanel>
93 <DeviceHostUpdates state={props.state} environmentId={props.environmentId} />
94 {step === 0 ? (
95 <section className="space-y-3 text-sm">
96 <h3 className="font-medium">Enable the device hub</h3>
97 <div className="flex items-start justify-between gap-4">
98 <p className="text-muted-foreground">{deviceHubDescription}</p>
99 <Switch
100 checked={enabled}
101 disabled={busy || pending !== null}
102 aria-label="Enable device hub"
103 onCheckedChange={(checked) =>
104 void update("hub", {
105 enabled: Boolean(checked),
106 ...(checked ? {} : { agentAccessEnabled: false }),
107 })
108 }
109 />
110 </div>
111 <DeviceHubSetupStatus
112 state={props.state}
113 pending={pending === "hub" || (busy && pending !== "agent")}
114 />
115 </section>
116 ) : null}
117
118 {step === 1 ? (
119 <section className="space-y-3 text-sm">
120 <h3 className="font-medium">Check simulator support</h3>
121 <DevicePlatformSetup
122 state={props.state}
123 checking={pending === "check"}
124 disabled={!enabled || busy || pending !== null}
125 onCheck={() => {
126 setPending("check");
127 void list({ environmentId: props.environmentId, input: {} }).finally(() =>
128 setPending(null),
129 );
130 }}
131 />
132 </section>
133 ) : null}
134
135 {step === 2 ? (
136 <section className="space-y-3 text-sm">
137 <h3 className="font-medium">Allow agent control</h3>
138 <div className="flex items-start justify-between gap-4">
139 <p className="text-muted-foreground">{agentDeviceDescription}</p>
140 <Switch
141 checked={props.state.agentAccessEnabled}
142 disabled={!enabled || busy || pending !== null}
143 aria-label="Allow agents to control devices"
144 onCheckedChange={(checked) =>
145 void update("agent", { agentAccessEnabled: Boolean(checked) })
146 }
147 />
148 </div>
149 <AgentDeviceSetupStatus state={props.state} pending={pending === "agent"} />
150 <p className="text-xs text-muted-foreground">
151 Leave this off to keep manual device controls without giving agents access.
152 </p>
153 </section>
154 ) : null}
155 {props.state.hostStatus === "failed" && props.state.hostStatusDetail ? (
156 <p role="alert" className="mt-3 text-xs text-destructive">
157 {props.state.hostStatusDetail}
158 </p>
159 ) : null}
160 </WizardPanel>
161
162 <WizardFooter>
163 {step === 0 ? (
164 <DialogClose render={<Button variant="outline" />}>Cancel</DialogClose>
165 ) : (
166 <Button
167 variant="outline"
168 disabled={busy || pending !== null}
169 onClick={() => setStep(step - 1)}
170 >
171 Back
172 </Button>
173 )}
174 {step < 2 ? (
175 <Button
176 disabled={props.state.hostStatus !== "ready" || pending !== null}
177 onClick={() => setStep(step + 1)}
178 >
179 Continue
180 </Button>
181 ) : (
182 <Button
183 disabled={props.state.hostStatus !== "ready" || pending !== null}
184 onClick={() => void update("complete", { onboardingCompleted: true })}
185 >
186 {pending === "complete" ? "Saving…" : "Done"}
187 </Button>
188 )}
189 </WizardFooter>
190 </>
191 );
192}
193
194export function DeviceHubSetupStatus({
195 state,
196 pending,
197 compact = false,
198}: {
199 readonly state: DeviceServiceState;
200 readonly pending: boolean;
201 readonly compact?: boolean;
202}) {
203 if (!pending && state.hostStatus !== "ready") return null;
204 return (
205 <p role="status" className="flex items-center gap-2 text-xs text-muted-foreground">
206 {pending ? <Spinner size="xs" /> : <Check className="size-3 text-success" />}
207 {pending
208 ? state.hostStatus === "installing"
209 ? compact
210 ? "Installing…"
211 : "Installing device hub…"
212 : state.hostStatus === "starting"
213 ? compact
214 ? "Starting…"
215 : "Starting device hub…"
216 : compact
217 ? "Updating…"
218 : "Updating device hub…"
219 : "Device hub is ready."}
220 </p>
221 );
222}
223
224function DevicePlatformSetup(props: {
225 readonly state: DeviceServiceState;
226 readonly checking: boolean;
227 readonly disabled: boolean;
228 readonly onCheck: () => void;
229}) {
230 return (
231 <div className="space-y-3">
232 <PlatformStatus platform="iOS" status={platformSetupStatus(props.state, "ios")} />
233 <PlatformStatus platform="Android" status={platformSetupStatus(props.state, "android")} />
234 <p className="text-xs text-muted-foreground">
235 You can use either platform. Fixing a missing platform does not block the other one.
236 </p>
237 <Button size="compact" variant="outline" disabled={props.disabled} onClick={props.onCheck}>
238 {props.checking ? <Spinner size="xs" /> : null}
239 {props.checking ? "Checking…" : "Check again"}
240 </Button>
241 </div>
242 );
243}
244
245export function AgentDeviceSetupStatus(props: {
246 readonly state: DeviceServiceState;
247 readonly pending: boolean;
248 readonly compact?: boolean;
249}) {
250 if (props.pending) {
251 const label =
252 props.state.hostStatus === "installing"
253 ? props.compact
254 ? "Installing…"
255 : "Installing agent tools…"
256 : props.state.hostStatus === "starting"
257 ? props.compact
258 ? "Starting…"
259 : "Starting agent tools…"
260 : props.compact
261 ? "Updating…"
262 : "Updating agent access…";
263 return (
264 <p role="status" className="flex items-center gap-2 text-xs text-muted-foreground">
265 <Spinner size="xs" />
266 {label}
267 </p>
268 );
269 }
270 if (
271 props.state.agentAccessEnabled &&
272 props.state.hostStatus === "ready" &&
273 props.state.hosts.some((host) => host.agentDeviceInstalled)
274 ) {
275 return (
276 <p role="status" className="flex items-center gap-2 text-xs text-muted-foreground">
277 <Check className="size-3 text-success" />
278 Agent tools are ready.
279 </p>
280 );
281 }
282 return null;
283}
284
285export function PlatformStatus(props: {
286 readonly platform: string;
287 readonly status: { readonly ready: boolean; readonly message: string };
288 readonly compact?: boolean;
289}) {
290 const Icon = props.status.ready ? Check : CircleAlert;
291 return (
292 <div
293 className={cn("flex gap-2", !props.compact && "rounded-md border border-border/60 px-3 py-2")}
294 >
295 <Icon
296 className={cn(
297 "mt-0.5 size-4 shrink-0",
298 props.status.ready ? "text-success" : "text-muted-foreground",
299 )}
300 />
301 <div className={cn(props.compact && props.status.ready && "flex items-center gap-2")}>
302 <p className="font-medium">{props.platform}</p>
303 <p className="text-xs text-muted-foreground">
304 {props.compact && props.status.ready ? "Ready" : props.status.message}
305 </p>
306 </div>
307 </div>
308 );
309}