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

ThreadErrorBanner.tsx

97 lines3.3 KB

1import type { OrchestrationV2ProviderFailureClass } from "@t3tools/contracts";
2import { memo } from "react";
3import { Alert, AlertAction, AlertDescription } from "../ui/alert";
4import { Button } from "../ui/button";
5import { CircleAlertIcon, XIcon } from "lucide-react";
6import { OpenAI } from "../Icons";
7import { ChatGptUsageButton } from "../settings/ChatGptUsageButton";
8import { ExpandableText } from "../settings/ExpandableText";
9​
10export function getThreadErrorBannerMessage(error: string): string {
11 let payload: unknown;
12 try {
13 payload = JSON.parse(error);
14 } catch {
15 return error;
16 }
17 if (typeof payload !== "object" || payload === null || !("error" in payload)) {
18 return error;
19 }
20 const nestedError = payload.error;
21 if (typeof nestedError !== "object" || nestedError === null || !("message" in nestedError)) {
22 return error;
23 }
24 return typeof nestedError.message === "string" && nestedError.message.trim().length > 0
25 ? nestedError.message
26 : error;
28​
29export function getThreadErrorBannerKey(threadKey: string, error: string | null): string | null {
30 return error === null ? null : `${threadKey}\u0000${error}`;
32​
33export function shouldShowThreadErrorBanner(
34 threadKey: string,
35 error: string | null,
36 isDismissed: boolean,
37): boolean {
38 return getThreadErrorBannerKey(threadKey, error) !== null && !isDismissed;
40​
41const sessionDismissedThreadErrorBannerKeys = new Set<string>();
42​
43export function dismissThreadErrorBannerForSession(bannerKey: string | null): void {
44 if (bannerKey !== null) {
45 sessionDismissedThreadErrorBannerKeys.add(bannerKey);
46 }
48​
49export function isThreadErrorBannerDismissedForSession(bannerKey: string | null): boolean {
50 return bannerKey !== null && sessionDismissedThreadErrorBannerKeys.has(bannerKey);
52​
53export const ThreadErrorBanner = memo(function ThreadErrorBanner({
54 error,
55 onDismiss,
56 errorClass,
57 chatGptUsageLimit = false,
58}: {
59 error: string | null;
60 errorClass?: OrchestrationV2ProviderFailureClass | null;
61 onDismiss?: () => void;
62 chatGptUsageLimit?: boolean;
63}) {
64 if (!error) return null;
65 const variant = errorClass === "usage_limit" ? "warning" : "error";
66 return (
67 <div className="pointer-events-auto mx-auto w-fit max-w-[min(48rem,calc(100%-2rem))] pt-3">
68 <Alert variant={variant} surface="glass" controlAlignment="first-line" data-variant={variant}>
69 {chatGptUsageLimit ? (
70 <OpenAI className="size-4 text-foreground!" aria-hidden="true" />
71 ) : (
72 <CircleAlertIcon />
73 )}
74 <AlertDescription>
75 {chatGptUsageLimit ? (
76 <div className="space-y-1">
77 <p className="font-medium">ChatGPT usage limit reached</p>
78 <p>Review your usage settings in ChatGPT to continue.</p>
79 </div>
80 ) : (
81 <ExpandableText text={error} collapsedText={getThreadErrorBannerMessage(error)} />
82 )}
83 </AlertDescription>
84 {(chatGptUsageLimit || onDismiss) && (
85 <AlertAction>
86 {chatGptUsageLimit ? <ChatGptUsageButton variant="default" size="sm" /> : null}
87 {onDismiss ? (
88 <Button variant="ghost" size="icon-xs" aria-label="Dismiss error" onClick={onDismiss}>
89 <XIcon />
90 </Button>
91 ) : null}
92 </AlertAction>
93 )}
94 </Alert>
95 </div>
96 );
97});