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

ThreadErrorBanner.tsx

128 lines4.5 KB

1import type { OrchestrationV2ProviderFailureClass } from "@t3tools/contracts";
2import {
3 CHATGPT_USAGE_LIMIT_CODE,
4 CHATGPT_WORKSPACE_ADMIN_REQUEST,
5 CHATGPT_WORKSPACE_USAGE_LIMIT_CODE,
6} from "@t3tools/shared/usageLimits";
7import { memo } from "react";
8import { writeTextToClipboard } from "../../hooks/useCopyToClipboard";
9import { Alert, AlertAction, AlertDescription } from "../ui/alert";
10import { Button } from "../ui/button";
11import { toastManager } from "../ui/toast";
12import { CircleAlertIcon, XIcon } from "lucide-react";
13import { OpenAI } from "../Icons";
14import { ChatGptUsageButton } from "../settings/ChatGptUsageButton";
15import { ExpandableText } from "../settings/ExpandableText";
16​
17function copyAdminRequest() {
18 void writeTextToClipboard(CHATGPT_WORKSPACE_ADMIN_REQUEST, "admin request").then(
19 () =>
20 toastManager.add({
21 type: "success",
22 title: "Request copied",
23 description: "Send it to your workspace admin.",
24 }),
25 () => toastManager.add({ type: "error", title: "Could not copy the request" }),
26 );
28​
29export function getThreadErrorBannerMessage(error: string): string {
30 let payload: unknown;
31 try {
32 payload = JSON.parse(error);
33 } catch {
34 return error;
35 }
36 if (typeof payload !== "object" || payload === null || !("error" in payload)) {
37 return error;
38 }
39 const nestedError = payload.error;
40 if (typeof nestedError !== "object" || nestedError === null || !("message" in nestedError)) {
41 return error;
42 }
43 return typeof nestedError.message === "string" && nestedError.message.trim().length > 0
44 ? nestedError.message
45 : error;
47​
48export function getThreadErrorBannerKey(threadKey: string, error: string | null): string | null {
49 return error === null ? null : `${threadKey}\u0000${error}`;
51​
52export function shouldShowThreadErrorBanner(
53 threadKey: string,
54 error: string | null,
55 isDismissed: boolean,
56): boolean {
57 return getThreadErrorBannerKey(threadKey, error) !== null && !isDismissed;
59​
60const sessionDismissedThreadErrorBannerKeys = new Set<string>();
61​
62export function dismissThreadErrorBannerForSession(bannerKey: string | null): void {
63 if (bannerKey !== null) {
64 sessionDismissedThreadErrorBannerKeys.add(bannerKey);
65 }
67​
68export function isThreadErrorBannerDismissedForSession(bannerKey: string | null): boolean {
69 return bannerKey !== null && sessionDismissedThreadErrorBannerKeys.has(bannerKey);
71​
72export const ThreadErrorBanner = memo(function ThreadErrorBanner({
73 error,
74 onDismiss,
75 errorClass,
76 errorCode,
77}: {
78 error: string | null;
79 errorClass?: OrchestrationV2ProviderFailureClass | null;
80 errorCode?: string | null;
81 onDismiss?: () => void;
82}) {
83 if (!error) return null;
84 const workspaceLimit = errorCode === CHATGPT_WORKSPACE_USAGE_LIMIT_CODE;
85 const chatGptLimit = workspaceLimit || errorCode === CHATGPT_USAGE_LIMIT_CODE;
86 const variant = errorClass === "usage_limit" || chatGptLimit ? "warning" : "error";
87 return (
88 <div className="pointer-events-auto mx-auto w-fit max-w-[min(48rem,calc(100%-2rem))] pt-3">
89 <Alert variant={variant} surface="glass" controlAlignment="first-line" data-variant={variant}>
90 {chatGptLimit ? (
91 <OpenAI className="size-4 text-foreground!" aria-hidden="true" />
92 ) : (
93 <CircleAlertIcon />
94 )}
95 <AlertDescription>
96 {chatGptLimit ? (
97 <div className="space-y-1">
98 <p className="font-medium">ChatGPT usage limit reached</p>
99 <p>
100 {workspaceLimit
101 ? "You've reached your workspace's limit for using your ChatGPT plan in other apps. Copy a request to ask your workspace admin for increased usage limits."
102 : "Review your usage settings in ChatGPT."}
103 </p>
104 </div>
105 ) : (
106 <ExpandableText text={error} collapsedText={getThreadErrorBannerMessage(error)} />
107 )}
108 </AlertDescription>
109 {(chatGptLimit || onDismiss) && (
110 <AlertAction>
111 {workspaceLimit ? (
112 <Button variant="default" size="sm" onClick={copyAdminRequest}>
113 Copy request for admin
114 </Button>
115 ) : chatGptLimit ? (
116 <ChatGptUsageButton variant="default" size="sm" />
117 ) : null}
118 {onDismiss ? (
119 <Button variant="ghost" size="icon-xs" aria-label="Dismiss error" onClick={onDismiss}>
120 <XIcon />
121 </Button>
122 ) : null}
123 </AlertAction>
124 )}
125 </Alert>
126 </div>
127 );
128});