The best way to control Claude Code, Codex and more from any computer or phone
perf(web): Codex setup compiles with the React Compiler (#20674)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 16, 2026 at 22:32 UTC
cffabdbb5935c33d1e3e2937b35957e35853a2e4
1 file changed
+167
-191
apps/web/src/components/settings/CodexSetupSection.tsx
+167
-191
@@ -16,7 +16,15 @@ import { codexAuthHandoffUrl } from "@t3tools/shared/codexAuthHandoff";
16
import { providerAuthReturnUrl } from "@t3tools/shared/providerAuthReturnUrl";
17
import { isLoopbackHost } from "@t3tools/shared/preview";
18
import { CheckIcon, ChevronRightIcon, ExternalLinkIcon } from "lucide-react";
19
-import { Children, useCallback, useEffect, useId, useRef, useState, type ReactNode } from "react";
19
+import {
20
+ Children,
21
+ useEffect,
22
+ useEffectEvent,
23
+ useId,
24
+ useRef,
25
+ useState,
26
+ type ReactNode,
27
+} from "react";
28
29
import { ensureLocalApi } from "../../localApi";
30
import {
@@ -286,11 +294,12 @@ function ManagedCodexSetup({
294
awaitingProvider !== null &&
295
!authenticated &&
296
(auth?.phase === "succeeded" || awaitingProvider === "handoff");
289
- useEffect(() => {
290
- if (authenticated || auth?.phase === "failed" || auth?.phase === "cancelled") {
291
- setAwaitingProvider(null);
292
- }
293
- }, [authenticated, auth?.phase]);
297
+ if (
298
+ awaitingProvider !== null &&
299
+ (authenticated || auth?.phase === "failed" || auth?.phase === "cancelled")
300
+ ) {
301
+ setAwaitingProvider(null);
302
+ }
303
useEffect(() => {
304
if (awaitingProvider && auth?.phase === "succeeded") {
305
void refreshProviders({ environmentId, input: { instanceId } });
@@ -315,193 +324,159 @@ function ManagedCodexSetup({
324
readOnly || !enabled || pending || authQuery.error !== null || installQuery.error !== null;
325
const busy = pending || authInProgress || installActive;
326
318
- const run = useCallback(
319
- async <A, E>(
320
- request: () => Promise<AtomCommandResult<A, E>>,
321
- onSuccess?: (value: A) => void,
322
- ) => {
323
- if (pendingRef.current) return false;
324
- pendingRef.current = true;
325
- setPending(true);
326
- setError(null);
327
- let succeeded = false;
328
- try {
329
- const result = await request();
330
- if (result._tag === "Failure") {
331
- if (!isAtomCommandInterrupted(result)) {
332
- const failure = squashAtomCommandFailure(result);
333
- setError(failure instanceof Error ? failure.message : "Codex setup failed. Try again.");
334
- }
335
- } else {
336
- succeeded = true;
337
- onSuccess?.(result.value);
327
+ const run = async <A, E>(
328
+ request: () => Promise<AtomCommandResult<A, E>>,
329
+ onSuccess?: (value: A) => void,
330
+ ) => {
331
+ if (pendingRef.current) return false;
332
+ pendingRef.current = true;
333
+ setPending(true);
334
+ setError(null);
335
+ let succeeded = false;
336
+ try {
337
+ const result = await request();
338
+ if (result._tag === "Failure") {
339
+ if (!isAtomCommandInterrupted(result)) {
340
+ const failure = squashAtomCommandFailure(result);
341
+ setError(failure instanceof Error ? failure.message : "Codex setup failed. Try again.");
342
}
339
- } catch {
340
- setError("Codex setup failed. Try again.");
343
+ } else {
344
+ succeeded = true;
345
+ onSuccess?.(result.value);
346
}
342
- pendingRef.current = false;
343
- setPending(false);
344
- return succeeded;
345
- },
346
- [],
347
- );
347
+ } catch {
348
+ setError("Codex setup failed. Try again.");
349
+ }
350
+ pendingRef.current = false;
351
+ setPending(false);
352
+ return succeeded;
353
+ };
354
355
const handoffId = useId();
356
const handoffSequence = useRef(0);
357
const importedAttempt = useRef<string | null>(null);
358
const [transferFailed, setTransferFailed] = useState(false);
353
- const transferProfile = useCallback(
354
- async (profile: ChatGptTransferredProfile) => {
355
- const succeeded = await run(() =>
356
- importProfile({ environmentId, input: { instanceId, profile } }),
357
- );
358
- if (succeeded) {
359
- setAwaitingProvider("handoff");
360
- setHandoff(null);
361
- }
362
- setTransferFailed(!succeeded);
363
- },
364
- [run, importProfile, environmentId, instanceId],
359
+ const transferProfile = async (profile: ChatGptTransferredProfile) => {
360
+ const succeeded = await run(() =>
361
+ importProfile({ environmentId, input: { instanceId, profile } }),
362
+ );
363
+ if (succeeded) {
364
+ setAwaitingProvider("handoff");
365
+ setHandoff(null);
366
+ }
367
+ setTransferFailed(!succeeded);
368
+ };
369
+ const transferFinishedProfile = useEffectEvent((profile: ChatGptTransferredProfile) =>
370
+ transferProfile(profile),
371
);
372
useEffect(() => {
373
if (!handoff || handoffQuery.data?.phase !== "finished") return;
374
const attemptId = handoff.input.attemptId;
375
if (importedAttempt.current === attemptId) return;
376
importedAttempt.current = attemptId;
371
- void transferProfile(handoffQuery.data.profile);
372
- }, [handoff, handoffQuery.data, transferProfile]);
373
- useEffect(() => {
374
- if (!handoff) return;
375
- if (
376
- handoffQuery.error ||
377
+ void transferFinishedProfile(handoffQuery.data.profile);
378
+ }, [handoff, handoffQuery.data]);
379
+ if (
380
+ handoff &&
381
+ (handoffQuery.error ||
382
(handoffQuery.data?.phase === "auth" &&
378
- ["failed", "cancelled"].includes(handoffQuery.data.state.phase))
379
- ) {
380
- setError(
381
- handoffQuery.data?.phase === "auth"
382
- ? (handoffQuery.data.state.message ?? "ChatGPT sign-in could not finish. Try again.")
383
- : "ChatGPT sign-in on the primary environment was interrupted. Try again.",
384
- );
385
- setHandoff(null);
386
- }
387
- }, [handoff, handoffQuery.data, handoffQuery.error]);
388
- const cancelSignIn = useCallback(() => {
383
+ ["failed", "cancelled"].includes(handoffQuery.data.state.phase)))
384
+ ) {
385
+ setError(
386
+ handoffQuery.data?.phase === "auth"
387
+ ? (handoffQuery.data.state.message ?? "ChatGPT sign-in could not finish. Try again.")
388
+ : "ChatGPT sign-in on the primary environment was interrupted. Try again.",
389
+ );
390
+ setHandoff(null);
391
+ }
392
+ const cancelSignIn = () => {
393
setAwaitingProvider(null);
394
if (handoff) {
395
setHandoff(null);
396
return Promise.resolve();
397
}
398
return run(() => cancelAuth({ environmentId, input: { instanceId, flowId: auth!.flowId! } }));
395
- }, [handoff, run, cancelAuth, environmentId, instanceId, auth]);
399
+ };
400
397
- const signIn = useCallback(
398
- async (methodId = "chatgpt") => {
399
- if (pendingRef.current) return;
400
- openRequested.current = true;
401
- setTransferFailed(false);
402
- setRequestedMethodId(methodId);
403
- const returnUrl = new URL(window.location.href);
404
- if (returnUrl.pathname === "/welcome") returnUrl.hash = `agents:${environmentId}`;
405
- if (returnUrl.pathname === "/settings/providers")
406
- returnUrl.searchParams.set("instanceId", instanceId);
407
- if (primaryAuthEnvironmentId) {
408
- const attemptId = `${handoffId}:${++handoffSequence.current}`;
409
- const succeeded = await run(
410
- () => reconnectProfile({ environmentId, input: { instanceId, methodId } }),
411
- (profile) =>
412
- setHandoff({
413
- environmentId: primaryAuthEnvironmentId,
414
- input: {
415
- instanceId,
416
- environmentId,
417
- attemptId,
418
- returnUrl: returnUrl.toString(),
419
- profile,
420
- },
421
- }),
422
- );
423
- if (!succeeded) openRequested.current = false;
424
- return;
425
- }
426
- if (
427
- !(await run(
428
- () =>
429
- startAuth({
401
+ const signIn = async (methodId = "chatgpt") => {
402
+ if (pendingRef.current) return;
403
+ openRequested.current = true;
404
+ setTransferFailed(false);
405
+ setRequestedMethodId(methodId);
406
+ const returnUrl = new URL(window.location.href);
407
+ if (returnUrl.pathname === "/welcome") returnUrl.hash = `agents:${environmentId}`;
408
+ if (returnUrl.pathname === "/settings/providers")
409
+ returnUrl.searchParams.set("instanceId", instanceId);
410
+ if (primaryAuthEnvironmentId) {
411
+ const attemptId = `${handoffId}:${++handoffSequence.current}`;
412
+ const succeeded = await run(
413
+ () => reconnectProfile({ environmentId, input: { instanceId, methodId } }),
414
+ (profile) =>
415
+ setHandoff({
416
+ environmentId: primaryAuthEnvironmentId,
417
+ input: {
418
+ instanceId,
419
environmentId,
431
- input: {
432
- instanceId,
433
- methodId,
434
- returnUrl: returnUrl.toString(),
435
- callbackMode: clientCallback ? "client" : "server",
436
- },
437
- }),
438
- () => setAwaitingProvider("sign-in"),
439
- ))
440
- ) {
441
- openRequested.current = false;
442
- }
443
- },
444
- [
445
- environmentId,
446
- instanceId,
447
- run,
448
- startAuth,
449
- clientCallback,
450
- primaryAuthEnvironmentId,
451
- reconnectProfile,
452
- handoffId,
453
- ],
454
- );
420
+ attemptId,
421
+ returnUrl: returnUrl.toString(),
422
+ profile,
423
+ },
424
+ }),
425
+ );
426
+ if (!succeeded) openRequested.current = false;
427
+ return;
428
+ }
429
+ if (
430
+ !(await run(
431
+ () =>
432
+ startAuth({
433
+ environmentId,
434
+ input: {
435
+ instanceId,
436
+ methodId,
437
+ returnUrl: returnUrl.toString(),
438
+ callbackMode: clientCallback ? "client" : "server",
439
+ },
440
+ }),
441
+ () => setAwaitingProvider("sign-in"),
442
+ ))
443
+ ) {
444
+ openRequested.current = false;
445
+ }
446
+ };
447
456
- const setup = useCallback(
457
- async (methodId = "chatgpt") => {
458
- if (unavailable || busy) return;
459
- if (installed && !updateAvailable) {
460
- await signIn(methodId);
461
- } else {
462
- continueWithSignIn.current = methodId;
463
- if (!(await run(() => startInstall({ environmentId, input: { instanceId } })))) {
464
- continueWithSignIn.current = null;
465
- }
448
+ const setup = async (methodId = "chatgpt") => {
449
+ if (unavailable || busy) return;
450
+ if (installed && !updateAvailable) {
451
+ await signIn(methodId);
452
+ } else {
453
+ continueWithSignIn.current = methodId;
454
+ if (!(await run(() => startInstall({ environmentId, input: { instanceId } })))) {
455
+ continueWithSignIn.current = null;
456
}
467
- },
468
- [
469
- unavailable,
470
- busy,
471
- installed,
472
- updateAvailable,
473
- signIn,
474
- run,
475
- startInstall,
476
- environmentId,
477
- instanceId,
478
- ],
479
- );
457
+ }
458
+ };
459
481
- useEffect(() => {
482
- if (
483
- !autoStart ||
484
- autoStartHandled ||
485
- unavailable ||
486
- busy ||
487
- installation === null ||
488
- !provider?.setup?.canInstall
489
- )
490
- return;
460
+ if (
461
+ autoStart &&
462
+ !autoStartHandled &&
463
+ !unavailable &&
464
+ !busy &&
465
+ installation !== null &&
466
+ provider?.setup?.canInstall
467
+ ) {
468
setAutoStartHandled(true);
469
+ }
470
+ const startAutomatically = useEffectEvent(() => {
471
onAutoStartConsumed();
472
void setup();
494
- }, [
495
- autoStart,
496
- autoStartHandled,
497
- unavailable,
498
- busy,
499
- installation,
500
- provider?.setup?.canInstall,
501
- onAutoStartConsumed,
502
- setup,
503
- ]);
473
+ });
474
+ useEffect(() => {
475
+ // oxlint-disable-next-line react/set-state-in-effect -- Starting setup dispatches a server command, which marks itself pending before it awaits.
476
+ if (autoStartHandled) startAutomatically();
477
+ }, [autoStartHandled]);
478
479
+ const signInAfterInstall = useEffectEvent((methodId: string) => signIn(methodId));
480
useEffect(() => {
481
if (!continueWithSignIn.current || pending || installActive) return;
482
if (installation?.phase === "failed" || installation?.phase === "cancelled") {
@@ -509,39 +484,39 @@ function ManagedCodexSetup({
484
} else if (installed) {
485
const methodId = continueWithSignIn.current;
486
continueWithSignIn.current = null;
512
- void signIn(methodId);
487
+ void signInAfterInstall(methodId);
488
}
514
- }, [pending, installActive, installation?.phase, installed, signIn]);
489
+ }, [pending, installActive, installation?.phase, installed]);
490
491
const receivingCallback = useRef<string | null>(null);
492
const flowId = auth?.flowId;
518
- const openPage = useCallback(
519
- async (authorizationUrl: string) => {
520
- try {
521
- const receive = window.desktopBridge?.receiveProviderAuthCallback;
522
- if (receive && clientCallback && flowId) {
523
- if (receivingCallback.current === authorizationUrl) {
524
- await ensureLocalApi().shell.openExternal(authorizationUrl);
525
- return;
526
- }
527
- receivingCallback.current = authorizationUrl;
528
- const callbackUrl = await receive(authorizationUrl);
529
- if (receivingCallback.current !== authorizationUrl) return;
530
- receivingCallback.current = null;
531
- await run(() =>
532
- completeAuth({ environmentId, input: { instanceId, flowId, callbackUrl } }),
533
- );
534
- } else {
493
+ const openPage = async (authorizationUrl: string) => {
494
+ try {
495
+ const receive = window.desktopBridge?.receiveProviderAuthCallback;
496
+ if (receive && clientCallback && flowId) {
497
+ if (receivingCallback.current === authorizationUrl) {
498
await ensureLocalApi().shell.openExternal(authorizationUrl);
499
+ return;
500
}
537
- } catch {
538
- setError(
539
- "Could not finish sign-in on this computer. Try again or paste the redirect URL below.",
501
+ receivingCallback.current = authorizationUrl;
502
+ const callbackUrl = await receive(authorizationUrl);
503
+ if (receivingCallback.current !== authorizationUrl) return;
504
+ receivingCallback.current = null;
505
+ await run(() =>
506
+ completeAuth({ environmentId, input: { instanceId, flowId, callbackUrl } }),
507
);
508
+ } else {
509
+ await ensureLocalApi().shell.openExternal(authorizationUrl);
510
}
542
- },
543
- [clientCallback, flowId, run, completeAuth, environmentId, instanceId],
544
- );
511
+ } catch {
512
+ setError(
513
+ "Could not finish sign-in on this computer. Try again or paste the redirect URL below.",
514
+ );
515
+ }
516
+ };
517
+ const openPageForFlow = useEffectEvent((authorizationUrl: string) => {
518
+ void openPage(authorizationUrl);
519
+ });
520
521
useEffect(() => {
522
if (!clientCallback || !url || !window.desktopBridge?.cancelProviderAuthCallback) return;
@@ -562,8 +537,9 @@ function ManagedCodexSetup({
537
return;
538
openedFlow.current = auth.flowId;
539
openRequested.current = false;
565
- if (!remoteWeb) void openPage(url);
566
- }, [auth?.phase, auth?.flowId, url, openPage, remoteWeb]);
540
+ // oxlint-disable-next-line react/set-state-in-effect -- Opening the page may complete the flow through a server command, which marks itself pending.
541
+ if (!remoteWeb) openPageForFlow(url);
542
+ }, [auth?.phase, auth?.flowId, url, remoteWeb]);
543
544
const runtimeDescription =
545
installation?.phase === "downloading"