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 { 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 { MultipleAccountsNote } from "./MultipleAccountsNote";
22import { SettingsRow } from "./settingsLayout";
23
24const CODEX_DRIVER_KIND = ProviderDriverKind.make("codex");
25
26export function AddCodexAccountDialog({
27 environmentId,
28 onClose,
29 renderSetup,
30 onAccountCreated,
31}: {
32 readonly environmentId: EnvironmentId;
33 readonly onClose: () => void;
34 // Told when the account is created; the dialog then runs its own sign-in.
35 readonly onAccountCreated?:
36 | ((instanceId: ProviderInstanceId, displayName: string) => void)
37 | undefined;
38 readonly renderSetup: (instanceId: ProviderInstanceId, provider: ServerProvider) => ReactNode;
39}) {
40 const canManageProviders = useEnvironmentScope(environmentId, AuthProvidersManageScope);
41 const settings = useEnvironmentSettings(environmentId);
42 const providers = useAtomValue(serverEnvironment.providersValueAtom(environmentId));
43 const update = useAtomCommand(serverEnvironment.updateSettings, "Add ChatGPT account");
44 const [name, setName] = useState("Personal");
45 const displayName = `ChatGPT - ${name.trim()}`;
46 const [instanceId, setInstanceId] = useState<ProviderInstanceId | null>(null);
47 const [pending, setPending] = useState(false);
48 const provider = providers?.find((candidate) => candidate.instanceId === instanceId);
49 // Closes itself once the new account connects; the owner unmounts it after
50 // the close animation.
51 const connected = usesChatGptSharing(provider);
52
53 const createAccount = async () => {
54 if (pending || !name.trim() || !readEnvironmentScope(environmentId, AuthProvidersManageScope))
55 return;
56 setPending(true);
57 const id = ProviderInstanceId.make(`codex_${randomUUID()}`);
58 const result = await update({
59 environmentId,
60 input: {
61 patch: {
62 providerInstances: {
63 ...settings.providerInstances,
64 [id]: {
65 driver: CODEX_DRIVER_KIND,
66 displayName,
67 enabled: true,
68 config: { enabled: true, setupMode: "managed" },
69 },
70 },
71 },
72 },
73 });
74 if (result._tag === "Success") {
75 onAccountCreated?.(id, displayName);
76 setInstanceId(id);
77 }
78 setPending(false);
79 };
80
81 return (
82 <Dialog
83 open={!connected}
84 onOpenChange={(open) => {
85 if (!open) onClose();
86 }}
87 onOpenChangeComplete={(open) => {
88 if (!open && connected) 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 className="grid gap-3"
107 onSubmit={(event) => {
108 event.preventDefault();
109 void createAccount();
110 }}
111 >
112 <SettingsRow
113 title="Account name"
114 description="Shown in the provider list and model picker."
115 control={
116 <Input
117 aria-label="Account name"
118 value={name}
119 disabled={pending}
120 onChange={(event) => setName(event.target.value)}
121 placeholder="e.g. Personal or Work"
122 />
123 }
124 />
125 <MultipleAccountsNote environmentId={environmentId} driver={CODEX_DRIVER_KIND} />
126 </form>
127 )}
128 </WizardPanel>
129 <WizardFooter>
130 {instanceId ? (
131 <Button variant="outline" onClick={onClose}>
132 Finish later
133 </Button>
134 ) : (
135 <>
136 <Button variant="outline" disabled={pending} onClick={onClose}>
137 Cancel
138 </Button>
139 <Button
140 type="submit"
141 form="add-codex-account"
142 disabled={pending || !name.trim() || !canManageProviders}
143 >
144 {pending ? "Adding account…" : "Continue"}
145 </Button>
146 </>
147 )}
148 </WizardFooter>
149 </WizardPopup>
150 </Dialog>
151 );
152}