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