Open History to see the last commit
1import { InfoIcon } from "lucide-react";
2import { useLayoutEffect, useRef, useState, type ReactNode } from "react";
3
4import { Button } from "../ui/button";
5import { Popover, PopoverPopup, PopoverTrigger } from "../ui/popover";
6import { ComposerBanner, type ComposerBannerVariant } from "./ComposerBanner";
7import { observeResize } from "~/lib/observeResize";
8
9/**
10 * Where a notice sits above the composer. The machine comes first, then what needs you; the
11 * order is fixed so rows never trade places as notices come and go.
12 */
13export type ComposerBannerGroup = "environment" | "attention";
14
15export interface ComposerBannerStackItem {
16 readonly id: string;
17 readonly variant: ComposerBannerVariant;
18 readonly group?: ComposerBannerGroup;
19 readonly icon: ReactNode;
20 readonly title: ReactNode;
21 readonly description?: ReactNode;
22 readonly children?: ReactNode;
23 readonly actions?: ReactNode;
24 readonly dismissLabel?: string;
25 readonly onDismiss?: () => void;
26}
27
28const GROUPS = ["environment", "attention"] as const;
29
30/**
31 * Notices about sending from this composer, each its own attached card. Consecutive cards join
32 * edge to edge, and the last joins the composer or the drawer below it.
33 */
34export function ComposerBannerStack({
35 items,
36}: {
37 readonly items: ReadonlyArray<ComposerBannerStackItem>;
38}) {
39 return GROUPS.flatMap((group) =>
40 items.filter((item) => (item.group ?? "attention") === group),
41 ).map((item) => (
42 <ComposerBanner.Attachment
43 key={item.id}
44 data-composer-banner-stack="true"
45 className="transition-opacity duration-150 ease-out starting:opacity-0 motion-reduce:transition-none"
46 >
47 <NoticeCard item={item} />
48 </ComposerBanner.Attachment>
49 ));
50}
51
52export function NoticeText({
53 children,
54 className,
55 detailsLabel,
56}: {
57 readonly children: ReactNode;
58 readonly className: string;
59 readonly detailsLabel: string;
60}) {
61 const textRef = useRef<HTMLSpanElement>(null);
62 const detailsRef = useRef<HTMLButtonElement>(null);
63 const [showDetails, setShowDetails] = useState(false);
64
65 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- measures text overflow in the DOM via ResizeObserver and MutationObserver to show the details button
66 useLayoutEffect(() => {
67 const text = textRef.current;
68 if (!text) return;
69 const measure = () => {
70 // Ignore the space taken by the details button itself so it cannot
71 // sustain its own overflow after the text would otherwise fit.
72 const recoveredWidth = detailsRef.current ? detailsRef.current.offsetWidth + 4 : 0;
73 const hidden = getComputedStyle(text).position === "absolute";
74 setShowDetails(
75 hidden ||
76 [text, ...text.querySelectorAll("*")].some(
77 (element) => element.scrollWidth > element.clientWidth + recoveredWidth,
78 ),
79 );
80 };
81 measure();
82 const stopObserving = observeResize(text, measure);
83 const mutations = new MutationObserver(measure);
84 mutations.observe(text, { childList: true, subtree: true, characterData: true });
85 return () => {
86 stopObserving();
87 mutations.disconnect();
88 };
89 }, []);
90
91 return (
92 <span className="contents">
93 <span ref={textRef} className={className}>
94 {children}
95 </span>
96 {showDetails ? (
97 <Popover>
98 <PopoverTrigger
99 openOnHover
100 render={
101 <Button
102 ref={detailsRef}
103 size="icon-xs"
104 variant="ghost-muted"
105 aria-label={detailsLabel}
106 className="flex-none"
107 />
108 }
109 >
110 <InfoIcon />
111 </PopoverTrigger>
112 <PopoverPopup
113 aria-label="Notice details"
114 tooltipStyle
115 side="top"
116 className="max-w-[min(30rem,calc(100vw-2rem))] whitespace-normal wrap-anywhere [--inline-button-text-align:start] [--inline-button-white-space:normal] [&_[data-slot=inline-button]]:max-w-full"
117 >
118 <ComposerBanner.Scroll className="max-h-[min(var(--available-height),24rem,40dvh)]">
119 {children}
120 </ComposerBanner.Scroll>
121 </PopoverPopup>
122 </Popover>
123 ) : null}
124 </span>
125 );
126}
127
128function NoticeCard({ item }: { readonly item: ComposerBannerStackItem }) {
129 return (
130 <ComposerBanner.Root
131 role={item.variant === "error" || item.variant === "warning" ? "alert" : undefined}
132 variant={item.variant}
133 density="comfortable"
134 data-chat-composer-collapsed-controls="true"
135 >
136 <ComposerBanner.Row layout="wrap-actions">
137 <ComposerBanner.Icon className="h-(--composer-banner-icon-column) self-start">
138 {item.icon}
139 </ComposerBanner.Icon>
140 <ComposerBanner.Content className="whitespace-nowrap">
141 <NoticeText
142 detailsLabel={
143 typeof item.title === "string"
144 ? `Show full title: ${item.title}`
145 : "Show title details"
146 }
147 className="min-w-0 truncate font-medium leading-7 sm:leading-6"
148 >
149 {item.title}
150 </NoticeText>
151 {item.description ? (
152 <NoticeText
153 detailsLabel={
154 typeof item.title === "string"
155 ? `Show details: ${item.title}`
156 : "Show notice details"
157 }
158 className="w-0 min-w-0 grow truncate text-muted-foreground @max-[400px]:sr-only"
159 >
160 {item.description}
161 </NoticeText>
162 ) : null}
163 </ComposerBanner.Content>
164 {item.actions || item.onDismiss ? (
165 <ComposerBanner.Actions>
166 {item.actions}
167 {item.onDismiss ? (
168 <ComposerBanner.Dismiss
169 aria-label={item.dismissLabel ?? "Dismiss warning"}
170 onClick={item.onDismiss}
171 />
172 ) : null}
173 </ComposerBanner.Actions>
174 ) : null}
175 </ComposerBanner.Row>
176 {item.children ? <ComposerBanner.Children>{item.children}</ComposerBanner.Children> : null}
177 </ComposerBanner.Root>
178 );
179}