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

ThreadErrorBanner.tsx

100 lines3.4 KB

1import { memo } from "react";
2import { Alert, AlertAction, AlertDescription } from "../ui/alert";
3import { Button } from "../ui/button";
4import { CircleAlertIcon, HourglassIcon, XIcon } from "lucide-react";
5import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
6import { UsageLimitCountdown } from "./UsageLimitCountdown";
7​
8export function getThreadErrorBannerKey(threadKey: string, error: string | null): string | null {
9 return error === null ? null : `${threadKey}\u0000${error}`;
11​
12export function shouldShowThreadErrorBanner(
13 threadKey: string,
14 error: string | null,
15 isDismissed: boolean,
16): boolean {
17 return getThreadErrorBannerKey(threadKey, error) !== null && !isDismissed;
19​
20// Session-scoped (module-level so it survives ChatView remounts, e.g. route
21// changes between threads). Mirrors the branch-mismatch banner: a dismissal
22// is remembered per thread key plus message, so navigating away to a thread
23// with no error cannot resurrect the banner, while a different error message
24// on the same thread still appears.
25const sessionDismissedThreadErrorBannerKeys = new Set<string>();
26​
27export function dismissThreadErrorBannerForSession(bannerKey: string | null): void {
28 if (bannerKey !== null) {
29 sessionDismissedThreadErrorBannerKeys.add(bannerKey);
30 }
32​
33export function isThreadErrorBannerDismissedForSession(bannerKey: string | null): boolean {
34 return bannerKey !== null && sessionDismissedThreadErrorBannerKeys.has(bannerKey);
36​
37export const ThreadErrorBanner = memo(function ThreadErrorBanner({
38 error,
39 onDismiss,
40 usageLimitResetsAt,
41}: {
42 error: string | null;
43 onDismiss?: () => void;
44 /** Renders the calm reached-your-limit notice instead of the raw error. */
45 usageLimitResetsAt?: string | null;
46}) {
47 if (!error) return null;
48 if (usageLimitResetsAt) {
49 return (
50 <div className="pointer-events-auto mx-auto w-fit max-w-[min(48rem,calc(100%-2rem))] pt-3">
51 <Alert variant="info" controlAlignment="first-line" className="alert-glass">
52 <HourglassIcon />
53 <AlertDescription>
54 Reached your plan's usage limit ·{" "}
55 <UsageLimitCountdown resetsAt={usageLimitResetsAt} prefix="tokens return in" />
56 </AlertDescription>
57 {onDismiss && (
58 <AlertAction>
59 <Button
60 variant="ghost"
61 size="icon-xs"
62 aria-label="Dismiss usage limit notice"
63 onClick={onDismiss}
64 >
65 <XIcon />
66 </Button>
67 </AlertAction>
68 )}
69 </Alert>
70 </div>
71 );
72 }
73 return (
74 <div className="pointer-events-auto mx-auto w-fit max-w-[min(48rem,calc(100%-2rem))] pt-3">
75 <Alert
76 variant="error"
77 controlAlignment="first-line"
78 className="alert-glass"
79 data-variant="error"
80 >
81 <CircleAlertIcon />
82 <AlertDescription>
83 <Tooltip>
84 <TooltipTrigger render={<div className="line-clamp-3" />}>{error}</TooltipTrigger>
85 <TooltipPopup side="top" className="max-w-96 whitespace-pre-wrap">
86 {error}
87 </TooltipPopup>
88 </Tooltip>
89 </AlertDescription>
90 {onDismiss && (
91 <AlertAction>
92 <Button variant="ghost" size="icon-xs" aria-label="Dismiss error" onClick={onDismiss}>
93 <XIcon className="text-destructive" />
94 </Button>
95 </AlertAction>
96 )}
97 </Alert>
98 </div>
99 );
100});