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

ThreadErrorBanner.tsx

75 lines2.6 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 { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
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 errorClass,
41}: {
42 error: string | null;
43 errorClass?: OrchestrationV2ProviderFailureClass | null;
44 onDismiss?: () => void;
45}) {
46 if (!error) return null;
47 const variant = errorClass === "usage_limit" ? "warning" : "error";
48 return (
49 <div className="pointer-events-auto mx-auto w-fit max-w-[min(48rem,calc(100%-2rem))] pt-3">
50 <Alert
51 variant={variant}
52 controlAlignment="first-line"
53 className="alert-glass"
54 data-variant={variant}
55 >
56 <CircleAlertIcon />
57 <AlertDescription>
58 <Tooltip>
59 <TooltipTrigger render={<div className="line-clamp-3" />}>{error}</TooltipTrigger>
60 <TooltipPopup side="top" className="max-w-96 whitespace-pre-wrap">
61 {error}
62 </TooltipPopup>
63 </Tooltip>
64 </AlertDescription>
65 {onDismiss && (
66 <AlertAction>
67 <Button variant="ghost" size="icon-xs" aria-label="Dismiss error" onClick={onDismiss}>
68 <XIcon />
69 </Button>
70 </AlertAction>
71 )}
72 </Alert>
73 </div>
74 );
75});