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

ChatGptAccountPicker.tsx

98 lines3.0 KB

1import { useState } from "react";
2import type { ProviderAuthMethod } from "@t3tools/contracts";
3import { RadioGroup, Radio } from "../ui/radio-group";
4import {
5 Dialog,
6 DialogPopup,
7 DialogHeader,
8 DialogTitle,
9 DialogDescription,
10 DialogFooter,
11} from "../ui/dialog";
12import { Button } from "../ui/button";
13import { ChatGptConnectionButton } from "./ChatGptConnectionButton";
14​
15export function ChatGptAccountPicker({
16 open,
17 methods,
18 onClose,
19 onSelect,
20 onRemove,
21 disabled,
22 canRemove,
23 error,
24}: {
25 open: boolean;
26 methods: readonly ProviderAuthMethod[];
27 onClose: () => void;
28 onSelect: (methodId: string) => void;
29 onRemove: (methodId: string) => void;
30 disabled: boolean;
31 canRemove: boolean;
32 error: string | null;
33}) {
34 const profiles = methods.filter((method) => method.id.startsWith("chatgpt-profile:"));
35 const [selection, setSelection] = useState<string | null>(null);
36 const selectedMethodId =
37 selection === "chatgpt-change-account"
38 ? selection
39 : (profiles.find((profile) => profile.id === selection)?.id ??
40 profiles[0]?.id ??
41 "chatgpt-change-account");
42 return (
43 <Dialog
44 open={open}
45 onOpenChange={(value) => {
46 if (!value) onClose();
47 }}
48 >
49 <DialogPopup>
50 <DialogHeader>
51 <DialogTitle>Reconnect ChatGPT</DialogTitle>
52 <DialogDescription>
53 On OpenAI, sign in with the account you choose here.
54 </DialogDescription>
55 </DialogHeader>
56 <div className="px-6 pb-6">
57 <RadioGroup
58 aria-label="ChatGPT account to connect"
59 disabled={disabled}
60 value={selectedMethodId}
61 onValueChange={(value) => setSelection(value)}
62 >
63 {profiles.map((profile) => (
64 <label
65 key={profile.id}
66 className="flex cursor-pointer items-center gap-3 rounded-lg border px-3 py-3 text-sm"
67 >
68 <Radio value={profile.id} />
69 <span className="min-w-0 break-all font-medium">{profile.name}</span>
70 </label>
71 ))}
72 <label className="flex cursor-pointer items-center gap-3 rounded-lg border px-3 py-3 text-sm">
73 <Radio value="chatgpt-change-account" />
74 <span className="font-medium">Use a different account</span>
75 </label>
76 </RadioGroup>
77 </div>
78 {error ? (
79 <p role="alert" className="px-6 pb-3 text-sm text-destructive">
80 {error}
81 </p>
82 ) : null}
83 <DialogFooter>
84 {profiles.find((profile) => profile.id === selectedMethodId)?.canRemove ? (
85 <Button
86 variant="ghost"
87 disabled={disabled || !canRemove}
88 onClick={() => onRemove(selectedMethodId)}
89 >
90 Remove connection
91 </Button>
92 ) : null}
93 <ChatGptConnectionButton disabled={disabled} onClick={() => onSelect(selectedMethodId)} />
94 </DialogFooter>
95 </DialogPopup>
96 </Dialog>
97 );