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

DeviceSetup.tsx

311 lines10.8 KB

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 };
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​
67 const update = async (
68 kind: NonNullable<typeof pending>,
69 input: { enabled?: boolean; agentAccessEnabled?: boolean; onboardingCompleted?: boolean },
70 ) => {
71 setPending(kind);
72 try {
73 const result = await configure({ environmentId: props.environmentId, input });
74 if (kind === "complete" && result._tag === "Success") props.onComplete?.();
75 } finally {
76 setPending(null);
77 }
78 };
79​
80 return (
81 <>
82 <WizardHeader
83 title="Set up devices"
84 description="Review what runs on this environment before using simulators and emulators."
85 >
86 <WizardSteps
87 steps={["Device hub", "Simulators", "Agent access"]}
88 currentStep={step}
89 onStepChange={setStep}
90 isStepDisabled={(requested) => busy || pending !== null || requested > step}
91 />
92 </WizardHeader>
93​
94 <WizardPanel>
95 <DeviceHostUpdates state={props.state} environmentId={props.environmentId} />
96 {step === 0 ? (
97 <section className="space-y-3 text-sm">
98 <h3 className="font-medium">Enable the device hub</h3>
99 <div className="flex items-start justify-between gap-4">
100 <p className="text-muted-foreground">{deviceHubDescription}</p>
101 <Switch
102 checked={enabled}
103 disabled={busy || pending !== null}
104 aria-label="Enable device hub"
105 onCheckedChange={(checked) =>
106 void update("hub", {
107 enabled: Boolean(checked),
108 ...(checked ? {} : { agentAccessEnabled: false }),
109 })
110 }
111 />
112 </div>
113 <DeviceHubSetupStatus
114 state={props.state}
115 pending={pending === "hub" || (busy && pending !== "agent")}
116 />
117 </section>
118 ) : null}
119​
120 {step === 1 ? (
121 <section className="space-y-3 text-sm">
122 <h3 className="font-medium">Check simulator support</h3>
123 <DevicePlatformSetup
124 state={props.state}
125 checking={pending === "check"}
126 disabled={!enabled || busy || pending !== null}
127 onCheck={() => {
128 setPending("check");
129 void list({ environmentId: props.environmentId, input: {} }).finally(() =>
130 setPending(null),
131 );
132 }}
133 />
134 </section>
135 ) : null}
136​
137 {step === 2 ? (
138 <section className="space-y-3 text-sm">
139 <h3 className="font-medium">Allow agent control</h3>
140 <div className="flex items-start justify-between gap-4">
141 <p className="text-muted-foreground">{agentDeviceDescription}</p>
142 <Switch
143 checked={props.state.agentAccessEnabled}
144 disabled={!enabled || busy || pending !== null}
145 aria-label="Allow agents to control devices"
146 onCheckedChange={(checked) =>
147 void update("agent", { agentAccessEnabled: Boolean(checked) })
148 }
149 />
150 </div>
151 <AgentDeviceSetupStatus state={props.state} pending={pending === "agent"} />
152 <p className="text-xs text-muted-foreground">
153 Leave this off to keep manual device controls without giving agents access.
154 </p>
155 </section>
156 ) : null}
157 {props.state.hostStatus === "failed" && props.state.hostStatusDetail ? (
158 <p role="alert" className="mt-3 text-xs text-destructive">
159 {props.state.hostStatusDetail}
160 </p>
161 ) : null}
162 </WizardPanel>
163​
164 <WizardFooter>
165 {step === 0 ? (
166 <DialogClose render={<Button variant="outline" />}>Cancel</DialogClose>
167 ) : (
168 <Button
169 variant="outline"
170 disabled={busy || pending !== null}
171 onClick={() => setStep(step - 1)}
172 >
173 Back
174 </Button>
175 )}
176 {step < 2 ? (
177 <Button
178 disabled={props.state.hostStatus !== "ready" || pending !== null}
179 onClick={() => setStep(step + 1)}
180 >
181 Continue
182 </Button>
183 ) : (
184 <Button
185 disabled={props.state.hostStatus !== "ready" || pending !== null}
186 onClick={() => void update("complete", { onboardingCompleted: true })}
187 >
188 {pending === "complete" ? "Saving…" : "Done"}
189 </Button>
190 )}
191 </WizardFooter>
192 </>
193 );
195​
196export function DeviceHubSetupStatus({
197 state,
198 pending,
199 compact = false,
200}: {
201 readonly state: DeviceServiceState;
202 readonly pending: boolean;
203 readonly compact?: boolean;
204}) {
205 if (!pending && state.hostStatus !== "ready") return null;
206 return (
207 <p role="status" className="flex items-center gap-2 text-xs text-muted-foreground">
208 {pending ? <Spinner size="xs" /> : <Check className="size-3 text-success" />}
209 {pending
210 ? state.hostStatus === "installing"
211 ? compact
212 ? "Installing…"
213 : "Installing device hub…"
214 : state.hostStatus === "starting"
215 ? compact
216 ? "Starting…"
217 : "Starting device hub…"
218 : compact
219 ? "Updating…"
220 : "Updating device hub…"
221 : "Device hub is ready."}
222 </p>
223 );
225​
226function DevicePlatformSetup(props: {
227 readonly state: DeviceServiceState;
228 readonly checking: boolean;
229 readonly disabled: boolean;
230 readonly onCheck: () => void;
231}) {
232 return (
233 <div className="space-y-3">
234 <PlatformStatus platform="iOS" status={platformSetupStatus(props.state, "ios")} />
235 <PlatformStatus platform="Android" status={platformSetupStatus(props.state, "android")} />
236 <p className="text-xs text-muted-foreground">
237 You can use either platform. Fixing a missing platform does not block the other one.
238 </p>
239 <Button size="compact" variant="outline" disabled={props.disabled} onClick={props.onCheck}>
240 {props.checking ? <Spinner size="xs" /> : null}
241 {props.checking ? "Checking…" : "Check again"}
242 </Button>
243 </div>
244 );
246​
247export function AgentDeviceSetupStatus(props: {
248 readonly state: DeviceServiceState;
249 readonly pending: boolean;
250 readonly compact?: boolean;
251}) {
252 if (props.pending) {
253 const label =
254 props.state.hostStatus === "installing"
255 ? props.compact
256 ? "Installing…"
257 : "Installing agent tools…"
258 : props.state.hostStatus === "starting"
259 ? props.compact
260 ? "Starting…"
261 : "Starting agent tools…"
262 : props.compact
263 ? "Updating…"
264 : "Updating agent access…";
265 return (
266 <p role="status" className="flex items-center gap-2 text-xs text-muted-foreground">
267 <Spinner size="xs" />
268 {label}
269 </p>
270 );
271 }
272 if (
273 props.state.agentAccessEnabled &&
274 props.state.hostStatus === "ready" &&
275 props.state.hosts.some((host) => host.agentDeviceInstalled)
276 ) {
277 return (
278 <p role="status" className="flex items-center gap-2 text-xs text-muted-foreground">
279 <Check className="size-3 text-success" />
280 Agent tools are ready.
281 </p>
282 );
283 }
284 return null;
286​
287export function PlatformStatus(props: {
288 readonly platform: string;
289 readonly status: { readonly ready: boolean; readonly message: string };
290 readonly compact?: boolean;
291}) {
292 return (
293 <div
294 className={cn("flex gap-2", !props.compact && "rounded-md border border-border/60 px-3 py-2")}
295 >
296 <MorphIcon
297 className={cn(
298 "mt-0.5 size-4 shrink-0",
299 props.status.ready ? "text-success" : "text-muted-foreground",
300 )}
301 icon={props.status.ready ? CheckGlyph : CircleAlert}
302 />
303 <div className={cn(props.compact && props.status.ready && "flex items-center gap-2")}>
304 <p className="font-medium">{props.platform}</p>
305 <p className="text-xs text-muted-foreground">
306 {props.compact && props.status.ready ? "Ready" : props.status.message}
307 </p>
308 </div>
309 </div>
310 );