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="mx-auto w-fit max-w-[min(48rem,calc(100%-2rem))] pt-3">
46 <Alert variant="error" controlAlignment="first-line">
47 <CircleAlertIcon />
48 <AlertDescription>
49 <Tooltip>
50 <TooltipTrigger render={<div className="line-clamp-3" />}>{error}</TooltipTrigger>
51 <TooltipPopup side="top" className="max-w-96 whitespace-pre-wrap">
52 {error}
53 </TooltipPopup>
54 </Tooltip>
55 </AlertDescription>
56 {onDismiss && (
57 <AlertAction>
58 <Button variant="ghost" size="icon-xs" aria-label="Dismiss error" onClick={onDismiss}>
59 <XIcon className="text-destructive" />
60 </Button>
61 </AlertAction>
62 )}
63 </Alert>
64 </div>
65 );
66});