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

DeviceSetup.tsx

307 lines10.6 KB

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