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

ThreadErrorBanner.tsx

130 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​
17export function 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 {workspaceLimit ? (
100 <p>
101 {
102 "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."
103 }
104 </p>
105 ) : null}
106 </div>
107 ) : (
108 <ExpandableText text={error} collapsedText={getThreadErrorBannerMessage(error)} />
109 )}
110 </AlertDescription>
111 {(chatGptLimit || onDismiss) && (
112 <AlertAction>
113 {workspaceLimit ? (
114 <Button variant="default" size="sm" onClick={copyAdminRequest}>
115 Copy request for admin
116 </Button>
117 ) : chatGptLimit ? (
118 <ChatGptUsageButton variant="default" size="sm" />
119 ) : null}
120 {onDismiss ? (
121 <Button variant="ghost" size="icon-xs" aria-label="Dismiss error" onClick={onDismiss}>
122 <XIcon />
123 </Button>
124 ) : null}
125 </AlertAction>
126 )}
127 </Alert>
128 </div>
129 );
130});