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

AddCodexAccountDialog.tsx

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