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

AddCodexAccountDialog.tsx

143 lines4.8 KB

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