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

ChatGptWelcomeCoordinator.tsx

83 lines2.8 KB

1import { useAtomValue } from "@effect/atom-react";
2import { usesChatGptSharing } from "@t3tools/shared/usageLimits";
3import { useState } from "react";
4import { environmentPresentations } from "../../state/presentation";
5import { OpenAI } from "../Icons";
6import { Button } from "../ui/button";
7import {
8 Dialog,
9 DialogPopup,
10 DialogHeader,
11 DialogTitle,
12 DialogDescription,
13 DialogFooter,
14} from "../ui/dialog";
15import { ChatGptUsageButton } from "./ChatGptUsageButton";
16​
17const STORAGE_KEY = "t3:chatgpt-sharing-welcome:v1";
18function readAcknowledgedProfiles(): string[] {
19 try {
20 const value: unknown = JSON.parse(localStorage.getItem(STORAGE_KEY) ?? "[]");
21 return Array.isArray(value)
22 ? value.filter((key): key is string => typeof key === "string")
23 : [];
24 } catch {
25 return [];
26 }
28​
29export function ChatGptWelcomeCoordinator() {
30 const presentations = useAtomValue(environmentPresentations.presentationsAtom);
31 const providersByEnvironment = useAtomValue(environmentPresentations.providersByEnvironmentAtom);
32 const [acknowledged, setAcknowledged] = useState(readAcknowledgedProfiles);
33 const profiles = [...presentations].flatMap(([environmentId, presentation]) =>
34 presentation.connection.phase !== "connected"
35 ? []
36 : (providersByEnvironment.get(environmentId) ?? [])
37 .filter(usesChatGptSharing)
38 .map((provider) => ({
39 key: JSON.stringify([
40 environmentId,
41 provider.instanceId,
42 provider.auth.profileId ?? provider.auth.email ?? "default",
43 ]),
44 environmentLabel: presentation.entry.target.label,
45 providerName: provider.displayName ?? "Codex",
46 })),
47 );
48 const next = profiles.find((profile) => !acknowledged.includes(profile.key));
49 const dismiss = () => {
50 if (!next) return;
51 const updated = [...acknowledged, next.key];
52 setAcknowledged(updated);
53 try {
54 localStorage.setItem(STORAGE_KEY, JSON.stringify(updated));
55 } catch {}
56 };
57 return (
58 <Dialog
59 open={next !== undefined}
60 onOpenChange={(open) => {
61 if (!open) dismiss();
62 }}
63 >
64 <DialogPopup>
65 <DialogHeader>
66 <OpenAI className="mb-2 size-8" aria-hidden="true" />
67 <DialogTitle>Your ChatGPT plan is connected</DialogTitle>
68 <DialogDescription>
69 Eligible usage in T3 Code uses your ChatGPT plan. Manage your shared usage and any
70 credit settings in ChatGPT.
71 </DialogDescription>
72 <p className="text-xs text-muted-foreground">
73 {next?.providerName} on {next?.environmentLabel}
74 </p>
75 </DialogHeader>
76 <DialogFooter>
77 <ChatGptUsageButton />
78 <Button onClick={dismiss}>Continue</Button>
79 </DialogFooter>
80 </DialogPopup>
81 </Dialog>
82 );