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

ThreadErrorBanner.tsx

144 lines4.9 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 { msg, T, useGT } from "gt-react";
8import { memo } from "react";
9import { m } from "../../i18n/messages";
10import { writeTextToClipboard } from "../../hooks/useCopyToClipboard";
11import { Alert, AlertAction, AlertDescription } from "../ui/alert";
12import { Button } from "../ui/button";
13import { toastManager } from "../ui/toast";
14import { CircleAlertIcon, XIcon } from "lucide-react";
15import { OpenAI } from "../Icons";
16import { ChatGptUsageButton } from "../settings/ChatGptUsageButton";
17import { ExpandableText } from "../settings/ExpandableText";
18​
19export function copyAdminRequest() {
20 void writeTextToClipboard(CHATGPT_WORKSPACE_ADMIN_REQUEST, {
21 target: "admin request",
22 unavailableMessage: msg("Clipboard API is unavailable while copying admin request."),
23 failureMessage: msg("Failed to copy admin request to the clipboard."),
24 }).then(
25 () =>
26 toastManager.add({
27 type: "success",
28 title: m(msg("Request copied")),
29 description: m(msg("Send it to your workspace admin.")),
30 }),
31 () => toastManager.add({ type: "error", title: m(msg("Could not copy the request")) }),
32 );
34​
35export function getThreadErrorBannerMessage(error: string): string {
36 let payload: unknown;
37 try {
38 payload = JSON.parse(error);
39 } catch {
40 return error;
41 }
42 if (typeof payload !== "object" || payload === null || !("error" in payload)) {
43 return error;
44 }
45 const nestedError = payload.error;
46 if (typeof nestedError !== "object" || nestedError === null || !("message" in nestedError)) {
47 return error;
48 }
49 return typeof nestedError.message === "string" && nestedError.message.trim().length > 0
50 ? nestedError.message
51 : error;
53​
54export function getThreadErrorBannerKey(threadKey: string, error: string | null): string | null {
55 return error === null ? null : `${threadKey}\u0000${error}`;
57​
58export function shouldShowThreadErrorBanner(
59 threadKey: string,
60 error: string | null,
61 isDismissed: boolean,
62): boolean {
63 return getThreadErrorBannerKey(threadKey, error) !== null && !isDismissed;
65​
66const sessionDismissedThreadErrorBannerKeys = new Set<string>();
67​
68export function dismissThreadErrorBannerForSession(bannerKey: string | null): void {
69 if (bannerKey !== null) {
70 sessionDismissedThreadErrorBannerKeys.add(bannerKey);
71 }
73​
74export function isThreadErrorBannerDismissedForSession(bannerKey: string | null): boolean {
75 return bannerKey !== null && sessionDismissedThreadErrorBannerKeys.has(bannerKey);
77​
78export const ThreadErrorBanner = memo(function ThreadErrorBanner({
79 error,
80 onDismiss,
81 errorClass,
82 errorCode,
83}: {
84 error: string | null;
85 errorClass?: OrchestrationV2ProviderFailureClass | null;
86 errorCode?: string | null;
87 onDismiss?: () => void;
88}) {
89 const gt = useGT();
90 if (!error) return null;
91 const workspaceLimit = errorCode === CHATGPT_WORKSPACE_USAGE_LIMIT_CODE;
92 const chatGptLimit = workspaceLimit || errorCode === CHATGPT_USAGE_LIMIT_CODE;
93 const variant = errorClass === "usage_limit" || chatGptLimit ? "warning" : "error";
94 return (
95 <div className="pointer-events-auto mx-auto w-fit max-w-[min(48rem,calc(100%-2rem))] pt-3">
96 <Alert variant={variant} surface="glass" controlAlignment="first-line" data-variant={variant}>
97 {chatGptLimit ? (
98 <OpenAI className="size-4 text-foreground!" aria-hidden="true" />
99 ) : (
100 <CircleAlertIcon />
101 )}
102 <AlertDescription>
103 {chatGptLimit ? (
104 <div className="space-y-1">
105 <p className="font-medium">
106 <T>ChatGPT usage limit reached</T>
107 </p>
108 {workspaceLimit ? (
109 <p>
110 {gt(
111 "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.",
112 )}
113 </p>
114 ) : null}
115 </div>
116 ) : (
117 <ExpandableText text={error} collapsedText={getThreadErrorBannerMessage(error)} />
118 )}
119 </AlertDescription>
120 {(chatGptLimit || onDismiss) && (
121 <AlertAction>
122 {workspaceLimit ? (
123 <Button variant="default" size="sm" onClick={copyAdminRequest}>
124 <T>Copy request for admin</T>
125 </Button>
126 ) : chatGptLimit ? (
127 <ChatGptUsageButton variant="default" size="sm" />
128 ) : null}
129 {onDismiss ? (
130 <Button
131 variant="ghost"
132 size="icon-xs"
133 aria-label={gt("Dismiss error")}
134 onClick={onDismiss}
135 >
136 <XIcon />
137 </Button>
138 ) : null}
139 </AlertAction>
140 )}
141 </Alert>
142 </div>
143 );
144});