Open History to see the last commit
1import { AuthProvidersManageScope } from "@t3tools/contracts";
2import { readEnvironmentScope, useEnvironmentScope } from "../../state/session";
3import { useAtomValue } from "@effect/atom-react";
4import {
5 ProviderDriverKind,
6 ProviderInstanceId,
7 type EnvironmentId,
8 type ServerProvider,
9} from "@t3tools/contracts";
10import { useEffect, useEffectEvent, useState, type ReactNode } from "react";
11import { usesChatGptSharing } from "@t3tools/shared/usageLimits";
12
13import { useEnvironmentSettings } from "../../hooks/useSettings";
14import { randomUUID } from "../../lib/utils";
15import { serverEnvironment } from "../../state/server";
16import { useAtomCommand } from "../../state/use-atom-command";
17import { Button } from "../ui/button";
18import { Dialog } from "../ui/dialog";
19import { Input } from "../ui/input";
20import { WizardFooter, WizardHeader, WizardPanel, WizardPopup } from "../ui/wizard";
21import { SettingsRow } from "./settingsLayout";
22
23export function AddCodexAccountDialog({
24 environmentId,
25 onClose,
26 renderSetup,
27 onAccountCreated,
28}: {
29 readonly environmentId: EnvironmentId;
30 readonly onClose: () => void;
31 readonly onAccountCreated?:
32 | ((instanceId: ProviderInstanceId, displayName: string) => void)
33 | undefined;
34 readonly renderSetup: (instanceId: ProviderInstanceId, provider: ServerProvider) => ReactNode;
35}) {
36 const canManageProviders = useEnvironmentScope(environmentId, AuthProvidersManageScope);
37 const settings = useEnvironmentSettings(environmentId);
38 const providers = useAtomValue(serverEnvironment.providersValueAtom(environmentId));
39 const update = useAtomCommand(serverEnvironment.updateSettings, "Add ChatGPT account");
40 const [name, setName] = useState("Personal");
41 const displayName = `ChatGPT - ${name.trim()}`;
42 const [instanceId, setInstanceId] = useState<ProviderInstanceId | null>(null);
43 const [pending, setPending] = useState(false);
44 const provider = providers?.find((candidate) => candidate.instanceId === instanceId);
45 const connected = usesChatGptSharing(provider);
46 const closeAfterConnection = useEffectEvent(onClose);
47 useEffect(() => {
48 if (connected) closeAfterConnection();
49 }, [connected]);
50
51 const createAccount = async () => {
52 if (pending || !name.trim() || !readEnvironmentScope(environmentId, AuthProvidersManageScope))
53 return;
54 setPending(true);
55 // The ID is routing identity; the name is editable and need not be unique.
56 const id = ProviderInstanceId.make(`codex_${randomUUID()}`);
57 const result = await update({
58 environmentId,
59 input: {
60 patch: {
61 providerInstances: {
62 ...settings.providerInstances,
63 [id]: {
64 driver: ProviderDriverKind.make("codex"),
65 displayName,
66 enabled: true,
67 config: { enabled: true, setupMode: "managed" },
68 },
69 },
70 },
71 },
72 });
73 if (result._tag === "Success") {
74 if (onAccountCreated) {
75 onAccountCreated(id, displayName);
76 onClose();
77 } else {
78 setInstanceId(id);
79 }
80 }
81 setPending(false);
82 };
83
84 return (
85 <Dialog
86 open={!connected}
87 onOpenChange={(open) => {
88 if (!open) onClose();
89 }}
90 >
91 <WizardPopup size="wide">
92 <WizardHeader
93 title={instanceId ? displayName : "Add ChatGPT account"}
94 description="Each account has its own Codex instance and sign-in. Choose the other account on the sign-in page."
95 />
96 <WizardPanel>
97 {instanceId ? (
98 provider?.setup ? (
99 renderSetup(instanceId, provider)
100 ) : (
101 <SettingsRow title="Codex runtime" description="Preparing managed setup." />
102 )
103 ) : (
104 <form
105 id="add-codex-account"
106 onSubmit={(event) => {
107 event.preventDefault();
108 void createAccount();
109 }}
110 >
111 <SettingsRow
112 title="Account name"
113 description="Shown in the provider list and model picker."
114 control={
115 <Input
116 aria-label="Account name"
117 value={name}
118 disabled={pending}
119 onChange={(event) => setName(event.target.value)}
120 placeholder="e.g. Personal or Work"
121 />
122 }
123 />
124 </form>
125 )}
126 </WizardPanel>
127 <WizardFooter>
128 {instanceId ? (
129 <Button variant="outline" onClick={onClose}>
130 Finish later
131 </Button>
132 ) : (
133 <>
134 <Button variant="outline" disabled={pending} onClick={onClose}>
135 Cancel
136 </Button>
137 <Button
138 type="submit"
139 form="add-codex-account"
140 disabled={pending || !name.trim() || !canManageProviders}
141 >
142 {pending ? "Adding account…" : "Continue"}
143 </Button>
144 </>
145 )}
146 </WizardFooter>
147 </WizardPopup>
148 </Dialog>
149 );
150}