Open History to see the last commit
1import {
2 isAtomCommandInterrupted,
3 squashAtomCommandFailure,
4 type AtomCommandResult,
5} from "@t3tools/client-runtime/state/runtime";
6import {
7 ANTIGRAVITY_AUTH_METHODS,
8 type AntigravityAuthMethod,
9 type EnvironmentId,
10 type ProviderInstanceId,
11 type ServerProvider,
12} from "@t3tools/contracts";
13import { useRef, useState } from "react";
14import { Trash2Icon } from "lucide-react";
15
16import { ensureLocalApi } from "../../localApi";
17import { useEnvironmentQuery } from "../../state/query";
18import { serverEnvironment } from "../../state/server";
19import { useAtomCommand } from "../../state/use-atom-command";
20import { Button } from "../ui/button";
21import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
22import { SettingsRow } from "./settingsLayout";
23import { ProviderAuthenticationSection } from "./ProviderAuthenticationSection";
24
25interface ProviderSetupSectionProps {
26 readonly environmentId: EnvironmentId;
27 readonly environmentLabel: string;
28 readonly instanceId: ProviderInstanceId;
29 readonly provider: ServerProvider | undefined;
30 readonly binaryPath?: string | undefined;
31 readonly authMethod?: AntigravityAuthMethod | undefined;
32 readonly enabled: boolean;
33 readonly readOnly: boolean;
34 readonly onEnable: () => void;
35}
36
37export function readAntigravityAuthMethod(config: unknown): AntigravityAuthMethod {
38 const value =
39 config !== null && typeof config === "object" && "authMethod" in config
40 ? config.authMethod
41 : undefined;
42 return (
43 ANTIGRAVITY_AUTH_METHODS.find((method) => method.value === value)?.value ?? "oauth-personal"
44 );
45}
46
47export function ProviderSetupSection(props: ProviderSetupSectionProps) {
48 return (
49 <section
50 aria-label="Antigravity setup"
51 className="@container/setup divide-y divide-border/50 text-xs"
52 >
53 <SettingsRow
54 className="@max-lg/setup:[&>div:first-child]:flex @max-lg/setup:[&>div:first-child]:items-stretch @max-lg/setup:[&>div:first-child]:gap-3"
55 title="Environment"
56 description="Device that runs this provider."
57 control={
58 <div className="flex min-w-0 flex-col gap-2 sm:items-end">
59 <span className="text-muted-foreground [overflow-wrap:anywhere]">
60 {props.environmentLabel}
61 </span>
62 {!props.enabled && !props.readOnly ? (
63 <Button size="sm" variant="outline" onClick={props.onEnable}>
64 Enable Antigravity
65 </Button>
66 ) : null}
67 </div>
68 }
69 />
70 {props.readOnly ? (
71 <SettingsRow title="Setup unavailable" description="Provider setup is read-only." />
72 ) : props.provider?.setup === undefined ? (
73 <SettingsRow
74 title="Update required"
75 description="Update this environment to manage Antigravity."
76 />
77 ) : (
78 <ProviderSetupActions
79 key={`${props.environmentId}:${props.instanceId}`}
80 environmentId={props.environmentId}
81 environmentLabel={props.environmentLabel}
82 instanceId={props.instanceId}
83 provider={props.provider}
84 binaryPath={props.binaryPath}
85 authMethod={props.authMethod ?? "oauth-personal"}
86 enabled={props.enabled}
87 />
88 )}
89 </section>
90 );
91}
92
93function ProviderSetupActions({
94 environmentId,
95 environmentLabel,
96 instanceId,
97 provider,
98 enabled,
99 binaryPath,
100}: Pick<
101 ProviderSetupSectionProps,
102 "environmentId" | "environmentLabel" | "instanceId" | "enabled" | "binaryPath"
103> & {
104 readonly provider: ServerProvider;
105 readonly authMethod: AntigravityAuthMethod;
106}) {
107 const target = { environmentId, input: { instanceId } };
108 const authQuery = useEnvironmentQuery(serverEnvironment.providerAuthState(target));
109 const installQuery = useEnvironmentQuery(serverEnvironment.providerInstallState(target));
110 const auth = authQuery.data;
111 const installation = installQuery.data;
112 const commandOptions = { reportFailure: false, reportDefect: false };
113 const startInstall = useAtomCommand(serverEnvironment.startProviderInstall, commandOptions);
114 const cancelInstall = useAtomCommand(serverEnvironment.cancelProviderInstall, commandOptions);
115 const removeInstall = useAtomCommand(
116 serverEnvironment.removeProviderInstallation,
117 commandOptions,
118 );
119 const [pendingLabel, setPendingLabel] = useState<string | null>(null);
120 const pendingRef = useRef(false);
121 const [error, setError] = useState<string | null>(null);
122 const authActive =
123 auth?.phase === "starting" || auth?.phase === "waiting" || auth?.phase === "verifying";
124 const installActive =
125 installation?.phase === "downloading" ||
126 installation?.phase === "extracting" ||
127 installation?.phase === "verifying";
128 const usesCustomBinary = Boolean(binaryPath?.trim());
129 const installed =
130 provider.installed || (!usesCustomBinary && installation?.installedVersion != null);
131 const queryError = authQuery.error ?? installQuery.error;
132 const actionsDisabled = pendingLabel !== null || queryError !== null;
133 const installationStatusMessage =
134 installation?.phase === "downloading"
135 ? `Downloading ${(installation.downloadedBytes / 1_000_000).toFixed(1)} MB${installation.totalBytes === null ? "" : ` of ${(installation.totalBytes / 1_000_000).toFixed(1)} MB`}.`
136 : installation?.phase === "extracting"
137 ? "Extracting Antigravity."
138 : installation?.phase === "verifying"
139 ? "Checking the downloaded runtime."
140 : installed
141 ? "Installed."
142 : usesCustomBinary
143 ? enabled
144 ? "The configured Antigravity runtime is unavailable."
145 : "The configured Antigravity runtime has not been checked."
146 : installation?.totalBytes
147 ? `${Math.ceil(installation.totalBytes / 1_000_000)} MB download.`
148 : "Not installed.";
149
150 async function runCommand<A, E>(
151 label: string,
152 request: () => Promise<AtomCommandResult<A, E>>,
153 ): Promise<boolean> {
154 if (pendingRef.current) return false;
155 pendingRef.current = true;
156 setPendingLabel(label);
157 setError(null);
158 try {
159 const result = await request();
160 if (result._tag === "Failure") {
161 if (!isAtomCommandInterrupted(result)) {
162 const failure = squashAtomCommandFailure(result);
163 setError(failure instanceof Error ? failure.message : "Provider setup failed.");
164 }
165 return false;
166 }
167 return true;
168 } catch {
169 setError("Provider setup failed. Try again.");
170 return false;
171 } finally {
172 pendingRef.current = false;
173 setPendingLabel(null);
174 }
175 }
176
177 async function removeRuntime() {
178 const confirmed = await ensureLocalApi().dialogs.confirm(
179 `Remove the downloaded Antigravity runtime from ${environmentLabel}? Google sign-in and thread history are kept.`,
180 );
181 if (confirmed) {
182 await runCommand("Removing runtime", () => removeInstall(target));
183 }
184 }
185
186 return (
187 <div className="divide-y divide-border/50">
188 <SettingsRow
189 title="Runtime"
190 className="@max-lg/setup:[&>div:first-child]:flex @max-lg/setup:[&>div:first-child]:items-stretch @max-lg/setup:[&>div:first-child]:gap-3"
191 description="Install and manage Antigravity."
192 status={
193 <div className="space-y-2">
194 {usesCustomBinary ? (
195 <p className="text-muted-foreground">
196 Uses the custom binary path below. Installation keeps that path.
197 </p>
198 ) : null}
199 {!installed && !provider.setup?.canInstall ? (
200 <p className="text-muted-foreground">
201 Automatic installation unavailable. Set a binary path or use another environment.
202 </p>
203 ) : null}
204 </div>
205 }
206 control={
207 <div className="flex w-full min-w-0 flex-col gap-2 @min-[32rem]/settings-row:w-56 @min-[32rem]/settings-row:text-right">
208 <p role="status" className="min-h-4 text-muted-foreground tabular-nums">
209 {installationStatusMessage}
210 </p>
211 <div className="h-1">
212 {installation?.phase === "downloading" &&
213 installation.totalBytes !== null &&
214 installation.totalBytes > 0 ? (
215 <progress
216 aria-label="Antigravity download"
217 className="block h-1 w-full accent-foreground"
218 value={installation.downloadedBytes}
219 max={installation.totalBytes}
220 />
221 ) : null}
222 </div>
223 {!installActive &&
224 installation?.message &&
225 installation.message !== installationStatusMessage ? (
226 <p className="text-muted-foreground [overflow-wrap:anywhere]">
227 {installation.message}
228 </p>
229 ) : null}
230 <div className="grid min-h-7 grid-cols-[1.75rem_minmax(0,1fr)] gap-2">
231 <div className="col-start-2 row-start-1 grid">
232 {installActive && installation.operationId ? (
233 <Button
234 size="sm"
235 variant="outline"
236 disabled={actionsDisabled}
237 onClick={() => {
238 const operationId = installation.operationId;
239 if (!operationId) return;
240 void runCommand("Cancelling installation", () =>
241 cancelInstall({ environmentId, input: { instanceId, operationId } }),
242 );
243 }}
244 >
245 Cancel installation
246 </Button>
247 ) : !installActive && provider.setup?.canInstall ? (
248 <Button
249 size="sm"
250 variant="outline"
251 disabled={actionsDisabled || installation === null || authActive}
252 onClick={() =>
253 void runCommand("Starting installation", () => startInstall(target))
254 }
255 >
256 {installation?.installedVersion
257 ? installation.version &&
258 installation.version !== installation.installedVersion
259 ? "Update Antigravity"
260 : "Reinstall Antigravity"
261 : installation?.phase === "failed" || installation?.phase === "cancelled"
262 ? "Retry installation"
263 : installed
264 ? "Install managed runtime"
265 : "Install Antigravity"}
266 </Button>
267 ) : null}
268 </div>
269 {installation?.canRemove && !installActive ? (
270 <Tooltip>
271 <TooltipTrigger
272 render={
273 <Button
274 size="icon-sm"
275 variant="ghost"
276 className="col-start-1 row-start-1"
277 aria-label="Remove downloaded runtime"
278 disabled={actionsDisabled || authActive}
279 onClick={() => void removeRuntime()}
280 />
281 }
282 >
283 <Trash2Icon className="size-3.5" />
284 </TooltipTrigger>
285 <TooltipPopup>Remove downloaded runtime</TooltipPopup>
286 </Tooltip>
287 ) : null}
288 </div>
289 </div>
290 }
291 />
292
293 <ProviderAuthenticationSection
294 environmentId={environmentId}
295 environmentLabel={environmentLabel}
296 instanceId={instanceId}
297 provider={provider}
298 readOnly={false}
299 />
300
301 <p className="sr-only" role="status">
302 {pendingLabel ? `${pendingLabel}.` : null}
303 </p>
304 {error || queryError ? (
305 <div className="grid gap-2 px-3 py-3 sm:px-4">
306 <p role="alert" className="text-destructive [overflow-wrap:anywhere]">
307 {error ?? queryError}
308 </p>
309 {queryError ? (
310 <Button
311 size="sm"
312 variant="outline"
313 className="w-fit"
314 onClick={() => {
315 authQuery.refresh();
316 installQuery.refresh();
317 }}
318 >
319 Retry setup status
320 </Button>
321 ) : null}
322 </div>
323 ) : null}
324 </div>
325 );
326}