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

ThreadErrorBanner.tsx

84 lines3.1 KB

1import { memo } from "react";
2import { Alert, AlertAction, AlertDescription } from "../ui/alert";
3import { Button } from "../ui/button";
4import { CircleAlertIcon, XIcon } from "lucide-react";
5import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
6import { OpenAI } from "../Icons";
7import { ChatGptUsageButton } from "../settings/ChatGptUsageButton";
8​
9export function getThreadErrorBannerKey(threadKey: string, error: string | null): string | null {
10 return error === null ? null : `${threadKey}\u0000${error}`;
12​
13export function shouldShowThreadErrorBanner(
14 threadKey: string,
15 error: string | null,
16 isDismissed: boolean,
17): boolean {
18 return getThreadErrorBannerKey(threadKey, error) !== null && !isDismissed;
20​
21// Session-scoped (module-level so it survives ChatView remounts, e.g. route
22// changes between threads). Mirrors the branch-mismatch banner: a dismissal
23// is remembered per thread key plus message, so navigating away to a thread
24// with no error cannot resurrect the banner, while a different error message
25// on the same thread still appears.
26const sessionDismissedThreadErrorBannerKeys = new Set<string>();
27​
28export function dismissThreadErrorBannerForSession(bannerKey: string | null): void {
29 if (bannerKey !== null) {
30 sessionDismissedThreadErrorBannerKeys.add(bannerKey);
31 }
33​
34export function isThreadErrorBannerDismissedForSession(bannerKey: string | null): boolean {
35 return bannerKey !== null && sessionDismissedThreadErrorBannerKeys.has(bannerKey);
37​
38export const ThreadErrorBanner = memo(function ThreadErrorBanner({
39 error,
40 onDismiss,
41 chatGptUsageLimit = false,
42}: {
43 error: string | null;
44 onDismiss?: () => void;
45 chatGptUsageLimit?: boolean;
46}) {
47 if (!error) return null;
48 return (
49 <div className="pointer-events-auto mx-auto w-fit max-w-[min(48rem,calc(100%-2rem))] pt-3">
50 <Alert variant="error" surface="glass" controlAlignment="first-line">
51 {chatGptUsageLimit ? (
52 <OpenAI className="size-4 text-foreground!" aria-hidden="true" />
53 ) : (
54 <CircleAlertIcon />
55 )}
56 <AlertDescription>
57 {chatGptUsageLimit ? (
58 <div className="space-y-1">
59 <p className="font-medium">ChatGPT usage limit reached</p>
60 <p>Review your usage settings in ChatGPT to continue.</p>
61 </div>
62 ) : (
63 <Tooltip>
64 <TooltipTrigger render={<div className="line-clamp-3" />}>{error}</TooltipTrigger>
65 <TooltipPopup side="top" className="whitespace-pre-wrap">
66 {error}
67 </TooltipPopup>
68 </Tooltip>
69 )}
70 </AlertDescription>
71 {(chatGptUsageLimit || onDismiss) && (
72 <AlertAction>
73 {chatGptUsageLimit ? <ChatGptUsageButton variant="default" size="sm" /> : null}
74 {onDismiss ? (
75 <Button variant="ghost" size="icon-xs" aria-label="Dismiss error" onClick={onDismiss}>
76 <XIcon className="text-destructive" />
77 </Button>
78 ) : null}
79 </AlertAction>
80 )}
81 </Alert>
82 </div>
83 );
84});