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

ComposerBannerStack.tsx

178 lines5.8 KB

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;
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 ));
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 useLayoutEffect(() => {
66 const text = textRef.current;
67 if (!text) return;
68 const measure = () => {
69 // Ignore the space taken by the details button itself so it cannot
70 // sustain its own overflow after the text would otherwise fit.
71 const recoveredWidth = detailsRef.current ? detailsRef.current.offsetWidth + 4 : 0;
72 const hidden = getComputedStyle(text).position === "absolute";
73 setShowDetails(
74 hidden ||
75 [text, ...text.querySelectorAll("*")].some(
76 (element) => element.scrollWidth > element.clientWidth + recoveredWidth,
77 ),
78 );
79 };
80 measure();
81 const stopObserving = observeResize(text, measure);
82 const mutations = new MutationObserver(measure);
83 mutations.observe(text, { childList: true, subtree: true, characterData: true });
84 return () => {
85 stopObserving();
86 mutations.disconnect();
87 };
88 }, []);
89​
90 return (
91 <span className="contents">
92 <span ref={textRef} className={className}>
93 {children}
94 </span>
95 {showDetails ? (
96 <Popover>
97 <PopoverTrigger
98 openOnHover
99 render={
100 <Button
101 ref={detailsRef}
102 size="icon-xs"
103 variant="ghost-muted"
104 aria-label={detailsLabel}
105 className="flex-none"
106 />
107 }
108 >
109 <InfoIcon />
110 </PopoverTrigger>
111 <PopoverPopup
112 aria-label="Notice details"
113 tooltipStyle
114 side="top"
115 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"
116 >
117 <ComposerBanner.Scroll className="max-h-[min(var(--available-height),24rem,40dvh)]">
118 {children}
119 </ComposerBanner.Scroll>
120 </PopoverPopup>
121 </Popover>
122 ) : null}
123 </span>
124 );
126​
127function NoticeCard({ item }: { readonly item: ComposerBannerStackItem }) {
128 return (
129 <ComposerBanner.Root
130 role={item.variant === "error" || item.variant === "warning" ? "alert" : undefined}
131 variant={item.variant}
132 density="comfortable"
133 data-chat-composer-collapsed-controls="true"
134 >
135 <ComposerBanner.Row layout="wrap-actions">
136 <ComposerBanner.Icon className="h-(--composer-banner-icon-column) self-start">
137 {item.icon}
138 </ComposerBanner.Icon>
139 <ComposerBanner.Content className="whitespace-nowrap">
140 <NoticeText
141 detailsLabel={
142 typeof item.title === "string"
143 ? `Show full title: ${item.title}`
144 : "Show title details"
145 }
146 className="min-w-0 truncate font-medium leading-7 sm:leading-6"
147 >
148 {item.title}
149 </NoticeText>
150 {item.description ? (
151 <NoticeText
152 detailsLabel={
153 typeof item.title === "string"
154 ? `Show details: ${item.title}`
155 : "Show notice details"
156 }
157 className="w-0 min-w-0 grow truncate text-muted-foreground @max-[400px]:sr-only"
158 >
159 {item.description}
160 </NoticeText>
161 ) : null}
162 </ComposerBanner.Content>
163 {item.actions || item.onDismiss ? (
164 <ComposerBanner.Actions>
165 {item.actions}
166 {item.onDismiss ? (
167 <ComposerBanner.Dismiss
168 aria-label={item.dismissLabel ?? "Dismiss warning"}
169 onClick={item.onDismiss}
170 />
171 ) : null}
172 </ComposerBanner.Actions>
173 ) : null}
174 </ComposerBanner.Row>
175 {item.children ? <ComposerBanner.Children>{item.children}</ComposerBanner.Children> : null}
176 </ComposerBanner.Root>
177 );