Open History to see the last commit
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";
6
7export function getThreadErrorBannerKey(threadKey: string, error: string | null): string | null {
8 return error === null ? null : `${threadKey}\u0000${error}`;
9}
10
11export function shouldShowThreadErrorBanner(
12 threadKey: string,
13 error: string | null,
14 isDismissed: boolean,
15): boolean {
16 return getThreadErrorBannerKey(threadKey, error) !== null && !isDismissed;
17}
18
19// Session-scoped (module-level so it survives ChatView remounts, e.g. route
20// changes between threads). Mirrors the branch-mismatch banner: a dismissal
21// is remembered per thread key plus message, so navigating away to a thread
22// with no error cannot resurrect the banner, while a different error message
23// on the same thread still appears.
24const sessionDismissedThreadErrorBannerKeys = new Set<string>();
25
26export function dismissThreadErrorBannerForSession(bannerKey: string | null): void {
27 if (bannerKey !== null) {
28 sessionDismissedThreadErrorBannerKeys.add(bannerKey);
29 }
30}
31
32export function isThreadErrorBannerDismissedForSession(bannerKey: string | null): boolean {
33 return bannerKey !== null && sessionDismissedThreadErrorBannerKeys.has(bannerKey);
34}
35
36export const ThreadErrorBanner = memo(function ThreadErrorBanner({
37 error,
38 onDismiss,
39}: {
40 error: string | null;
41 onDismiss?: () => void;
42}) {
43 if (!error) return null;
44 return (
45 <div className="pointer-events-auto mx-auto w-fit max-w-[min(48rem,calc(100%-2rem))] pt-3">
46 <Alert
47 variant="error"
48 controlAlignment="first-line"
49 className="alert-glass"
50 data-variant="error"
51 >
52 <CircleAlertIcon />
53 <AlertDescription>
54 <Tooltip>
55 <TooltipTrigger render={<div className="line-clamp-3" />}>{error}</TooltipTrigger>
56 <TooltipPopup side="top" className="max-w-96 whitespace-pre-wrap">
57 {error}
58 </TooltipPopup>
59 </Tooltip>
60 </AlertDescription>
61 {onDismiss && (
62 <AlertAction>
63 <Button variant="ghost" size="icon-xs" aria-label="Dismiss error" onClick={onDismiss}>
64 <XIcon className="text-destructive" />
65 </Button>
66 </AlertAction>
67 )}
68 </Alert>
69 </div>
70 );
71});