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