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 in the stack. 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
30const FADE_IN =
31 "transition-opacity duration-150 ease-out starting:opacity-0 motion-reduce:transition-none";
32
33/**
34 * Notices about sending from this composer, each its own attached card. Consecutive cards join
35 * edge to edge, and the last joins the composer or the drawer below it. Detached cards stand
36 * apart instead, for layouts where the composer must not move when a notice appears.
37 */
38export function ComposerBannerStack({
39 items,
40 detached = false,
41}: {
42 readonly items: ReadonlyArray<ComposerBannerStackItem>;
43 readonly detached?: boolean;
44}) {
45 const ordered = GROUPS.flatMap((group) =>
46 items.filter((item) => (item.group ?? "attention") === group),
47 );
48 if (detached) {
49 return ordered.map((item) => (
50 <div key={item.id} data-composer-banner-stack="true" className={FADE_IN}>
51 <NoticeCard item={item} detached />
52 </div>
53 ));
54 }
55 return ordered.map((item) => (
56 <ComposerBanner.Attachment key={item.id} data-composer-banner-stack="true" className={FADE_IN}>
57 <NoticeCard item={item} />
58 </ComposerBanner.Attachment>
59 ));
60}
61
62export function NoticeText({
63 children,
64 className,
65 detailsLabel,
66}: {
67 readonly children: ReactNode;
68 readonly className: string;
69 readonly detailsLabel: string;
70}) {
71 const textRef = useRef<HTMLSpanElement>(null);
72 const detailsRef = useRef<HTMLButtonElement>(null);
73 const [showDetails, setShowDetails] = useState(false);
74
75 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- measures text overflow in the DOM via ResizeObserver and MutationObserver to show the details button
76 useLayoutEffect(() => {
77 const text = textRef.current;
78 if (!text) return;
79 const measure = () => {
80 // Ignore the space taken by the details button itself so it cannot
81 // sustain its own overflow after the text would otherwise fit.
82 const recoveredWidth = detailsRef.current ? detailsRef.current.offsetWidth + 4 : 0;
83 const hidden = getComputedStyle(text).position === "absolute";
84 setShowDetails(
85 hidden ||
86 [text, ...text.querySelectorAll("*")].some(
87 (element) => element.scrollWidth > element.clientWidth + recoveredWidth,
88 ),
89 );
90 };
91 measure();
92 const stopObserving = observeResize(text, measure);
93 const mutations = new MutationObserver(measure);
94 mutations.observe(text, { childList: true, subtree: true, characterData: true });
95 return () => {
96 stopObserving();
97 mutations.disconnect();
98 };
99 }, []);
100
101 return (
102 <span className="contents">
103 <span ref={textRef} className={className}>
104 {children}
105 </span>
106 {showDetails ? (
107 <Popover>
108 <PopoverTrigger
109 openOnHover
110 render={
111 <Button
112 ref={detailsRef}
113 size="icon-xs"
114 variant="ghost-muted"
115 aria-label={detailsLabel}
116 className="flex-none"
117 />
118 }
119 >
120 <InfoIcon />
121 </PopoverTrigger>
122 <PopoverPopup
123 aria-label="Notice details"
124 tooltipStyle
125 side="top"
126 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"
127 >
128 <ComposerBanner.Scroll className="max-h-[min(var(--available-height),24rem,40dvh)]">
129 {children}
130 </ComposerBanner.Scroll>
131 </PopoverPopup>
132 </Popover>
133 ) : null}
134 </span>
135 );
136}
137
138function NoticeCard({
139 item,
140 detached = false,
141}: {
142 readonly item: ComposerBannerStackItem;
143 readonly detached?: boolean;
144}) {
145 return (
146 <ComposerBanner.Root
147 role={item.variant === "error" || item.variant === "warning" ? "alert" : undefined}
148 variant={item.variant}
149 detached={detached}
150 density="comfortable"
151 data-chat-composer-collapsed-controls="true"
152 >
153 <ComposerBanner.Row layout="wrap-actions">
154 <ComposerBanner.Icon className="h-(--composer-banner-icon-column) self-start">
155 {item.icon}
156 </ComposerBanner.Icon>
157 <ComposerBanner.Content className="whitespace-nowrap">
158 <NoticeText
159 detailsLabel={
160 typeof item.title === "string"
161 ? `Show full title: ${item.title}`
162 : "Show title details"
163 }
164 className="min-w-0 truncate font-medium leading-7 sm:leading-6"
165 >
166 {item.title}
167 </NoticeText>
168 {item.description ? (
169 <NoticeText
170 detailsLabel={
171 typeof item.title === "string"
172 ? `Show details: ${item.title}`
173 : "Show notice details"
174 }
175 className="w-0 min-w-0 grow truncate text-muted-foreground @max-[400px]:sr-only"
176 >
177 {item.description}
178 </NoticeText>
179 ) : null}
180 </ComposerBanner.Content>
181 {item.actions || item.onDismiss ? (
182 <ComposerBanner.Actions>
183 {item.actions}
184 {item.onDismiss ? (
185 <ComposerBanner.Dismiss
186 aria-label={item.dismissLabel ?? "Dismiss warning"}
187 onClick={item.onDismiss}
188 />
189 ) : null}
190 </ComposerBanner.Actions>
191 ) : null}
192 </ComposerBanner.Row>
193 {item.children ? <ComposerBanner.Children>{item.children}</ComposerBanner.Children> : null}
194 </ComposerBanner.Root>
195 );
196}