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

ThreadErrorBanner.tsx

148 lines5.0 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(
55 threadKey: string,
56 error: string | null,
57 source: string | null = null,
58): string | null {
59 return error === null ? null : `${threadKey}\u0000${source ?? ""}\u0000${error}`;
61​
62export function shouldShowThreadErrorBanner(
63 threadKey: string,
64 error: string | null,
65 isDismissed: boolean,
66): boolean {
67 return getThreadErrorBannerKey(threadKey, error) !== null && !isDismissed;
69​
70const sessionDismissedThreadErrorBannerKeys = new Set<string>();
71​
72export function dismissThreadErrorBannerForSession(bannerKey: string | null): void {
73 if (bannerKey !== null) {
74 sessionDismissedThreadErrorBannerKeys.add(bannerKey);
75 }
77​
78export function isThreadErrorBannerDismissedForSession(bannerKey: string | null): boolean {
79 return bannerKey !== null && sessionDismissedThreadErrorBannerKeys.has(bannerKey);
81​
82export const ThreadErrorBanner = memo(function ThreadErrorBanner({
83 error,
84 onDismiss,
85 errorClass,
86 errorCode,
87}: {
88 error: string | null;
89 errorClass?: OrchestrationV2ProviderFailureClass | null;
90 errorCode?: string | null;
91 onDismiss?: () => void;
92}) {
93 const gt = useGT();
94 if (!error) return null;
95 const workspaceLimit = errorCode === CHATGPT_WORKSPACE_USAGE_LIMIT_CODE;
96 const chatGptLimit = workspaceLimit || errorCode === CHATGPT_USAGE_LIMIT_CODE;
97 const variant = errorClass === "usage_limit" || chatGptLimit ? "warning" : "error";
98 return (
99 <div className="pointer-events-auto mx-auto w-fit max-w-[min(48rem,calc(100%-2rem))] pt-3">
100 <Alert variant={variant} surface="glass" controlAlignment="first-line" data-variant={variant}>
101 {chatGptLimit ? (
102 <OpenAI className="size-4 text-foreground!" aria-hidden="true" />
103 ) : (
104 <CircleAlertIcon />
105 )}
106 <AlertDescription>
107 {chatGptLimit ? (
108 <div className="space-y-1">
109 <p className="font-medium">
110 <T>ChatGPT usage limit reached</T>
111 </p>
112 {workspaceLimit ? (
113 <p>
114 {gt(
115 "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.",
116 )}
117 </p>
118 ) : null}
119 </div>
120 ) : (
121 <ExpandableText text={error} collapsedText={getThreadErrorBannerMessage(error)} />
122 )}
123 </AlertDescription>
124 {(chatGptLimit || onDismiss) && (
125 <AlertAction>
126 {workspaceLimit ? (
127 <Button variant="default" size="sm" onClick={copyAdminRequest}>
128 <T>Copy request for admin</T>
129 </Button>
130 ) : chatGptLimit ? (
131 <ChatGptUsageButton variant="default" size="sm" />
132 ) : null}
133 {onDismiss ? (
134 <Button
135 variant="ghost"
136 size="icon-xs"
137 aria-label={gt("Dismiss error")}
138 onClick={onDismiss}
139 >
140 <XIcon />
141 </Button>
142 ) : null}
143 </AlertAction>
144 )}
145 </Alert>
146 </div>
147 );
148});