Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

AddCodexAccountDialog.tsx

155 lines5.3 KB

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 readonly onAccountCreated?:
35 | ((instanceId: ProviderInstanceId, displayName: string) => void)
36 | undefined;
37 readonly renderSetup: (instanceId: ProviderInstanceId, provider: ServerProvider) => ReactNode;
38}) {
39 const canManageProviders = useEnvironmentScope(environmentId, AuthProvidersManageScope);
40 const settings = useEnvironmentSettings(environmentId);
41 const providers = useAtomValue(serverEnvironment.providersValueAtom(environmentId));
42 const update = useAtomCommand(serverEnvironment.updateSettings, "Add ChatGPT account");
43 const [name, setName] = useState("Personal");
44 const displayName = `ChatGPT - ${name.trim()}`;
45 const [instanceId, setInstanceId] = useState<ProviderInstanceId | null>(null);
46 const [pending, setPending] = useState(false);
47 const provider = providers?.find((candidate) => candidate.instanceId === instanceId);
48 // Closes itself once the new account connects; the owner unmounts it after
49 // the close animation.
50 const connected = usesChatGptSharing(provider);
51​
52 const createAccount = async () => {
53 if (pending || !name.trim() || !readEnvironmentScope(environmentId, AuthProvidersManageScope))
54 return;
55 setPending(true);
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: CODEX_DRIVER_KIND,
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 onOpenChangeComplete={(open) => {
91 if (!open && connected) onClose();
92 }}
93 >
94 <WizardPopup size="wide">
95 <WizardHeader
96 title={instanceId ? displayName : "Add ChatGPT account"}
97 description="Each account has its own Codex instance and sign-in. Choose the other account on the sign-in page."
98 />
99 <WizardPanel>
100 {instanceId ? (
101 provider?.setup ? (
102 renderSetup(instanceId, provider)
103 ) : (
104 <SettingsRow title="Codex runtime" description="Preparing managed setup." />
105 )
106 ) : (
107 <form
108 id="add-codex-account"
109 className="grid gap-3"
110 onSubmit={(event) => {
111 event.preventDefault();
112 void createAccount();
113 }}
114 >
115 <SettingsRow
116 title="Account name"
117 description="Shown in the provider list and model picker."
118 control={
119 <Input
120 aria-label="Account name"
121 value={name}
122 disabled={pending}
123 onChange={(event) => setName(event.target.value)}
124 placeholder="e.g. Personal or Work"
125 />
126 }
127 />
128 <MultipleAccountsNote environmentId={environmentId} driver={CODEX_DRIVER_KIND} />
129 </form>
130 )}
131 </WizardPanel>
132 <WizardFooter>
133 {instanceId ? (
134 <Button variant="outline" onClick={onClose}>
135 Finish later
136 </Button>
137 ) : (
138 <>
139 <Button variant="outline" disabled={pending} onClick={onClose}>
140 Cancel
141 </Button>
142 <Button
143 type="submit"
144 form="add-codex-account"
145 disabled={pending || !name.trim() || !canManageProviders}
146 >
147 {pending ? "Adding account…" : "Continue"}
148 </Button>
149 </>
150 )}
151 </WizardFooter>
152 </WizardPopup>
153 </Dialog>
154 );