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 const id = ProviderInstanceId.make(`codex_${randomUUID()}`);
56 const result = await update({
57 environmentId,
58 input: {
59 patch: {
60 providerInstances: {
61 ...settings.providerInstances,
62 [id]: {
63 driver: ProviderDriverKind.make("codex"),
64 displayName,
65 enabled: true,
66 config: { enabled: true, setupMode: "managed" },
67 },
68 },
69 },
70 },
71 });
72 if (result._tag === "Success") {
73 if (onAccountCreated) {
74 onAccountCreated(id, displayName);
75 onClose();
76 } else {
77 setInstanceId(id);
78 }
79 }
80 setPending(false);
81 };
82
83 return (
84 <Dialog
85 open={!connected}
86 onOpenChange={(open) => {
87 if (!open) onClose();
88 }}
89 >
90 <WizardPopup size="wide">
91 <WizardHeader
92 title={instanceId ? displayName : "Add ChatGPT account"}
93 description="Each account has its own Codex instance and sign-in. Choose the other account on the sign-in page."
94 />
95 <WizardPanel>
96 {instanceId ? (
97 provider?.setup ? (
98 renderSetup(instanceId, provider)
99 ) : (
100 <SettingsRow title="Codex runtime" description="Preparing managed setup." />
101 )
102 ) : (
103 <form
104 id="add-codex-account"
105 onSubmit={(event) => {
106 event.preventDefault();
107 void createAccount();
108 }}
109 >
110 <SettingsRow
111 title="Account name"
112 description="Shown in the provider list and model picker."
113 control={
114 <Input
115 aria-label="Account name"
116 value={name}
117 disabled={pending}
118 onChange={(event) => setName(event.target.value)}
119 placeholder="e.g. Personal or Work"
120 />
121 }
122 />
123 </form>
124 )}
125 </WizardPanel>
126 <WizardFooter>
127 {instanceId ? (
128 <Button variant="outline" onClick={onClose}>
129 Finish later
130 </Button>
131 ) : (
132 <>
133 <Button variant="outline" disabled={pending} onClick={onClose}>
134 Cancel
135 </Button>
136 <Button
137 type="submit"
138 form="add-codex-account"
139 disabled={pending || !name.trim() || !canManageProviders}
140 >
141 {pending ? "Adding account…" : "Continue"}
142 </Button>
143 </>
144 )}
145 </WizardFooter>
146 </WizardPopup>
147 </Dialog>
148 );
149}