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