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

contextChipParts.tsx

349 lines10.3 KB

1import type { PullRequestContextMetadata } from "@t3tools/contracts";
2import { CircleDashedIcon, FilmIcon, GitPullRequestIcon, ImageIcon } from "lucide-react";
3import {
4 useState,
5 type ComponentProps,
6 type CSSProperties,
7 type MouseEvent,
8 type ReactNode,
9} from "react";
10​
11import { cn } from "~/lib/utils";
12import { PierreEntryIcon } from "./chat/PierreEntryIcon";
13import {
14 COMPOSER_INLINE_CHIP_ICON_CLASS_NAME,
15 CONTEXT_INLINE_CHIP_FOCUS_CLASS_NAME,
16 CONTEXT_INLINE_CHIP_ICON_TONE_CLASS_NAMES,
17 CONTEXT_INLINE_CHIP_INTERACTIVE_CLASS_NAME,
18 CONTEXT_INLINE_CHIP_TONE_CLASS_NAMES,
19 middleTruncateAttachmentName,
20} from "./composerInlineChip";
21import { PullRequestContextDetails } from "./PullRequestContextDetails";
22import { Button } from "./ui/button";
23import { Popover, PopoverPopup, PopoverTitle, PopoverTrigger } from "./ui/popover";
24import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
25​
26/** Shared visual slots; each surface keeps ownership of payload lookup and actions. */
27export function ContextChipShell({
28 icon,
29 label,
30 labelClassName,
31 tooltip,
32 tooltipClassName = "max-w-96 whitespace-pre-wrap leading-tight",
33 interactive,
34 unresolved,
35 className,
36 ...props
37}: ComponentProps<"span"> & {
38 icon: ReactNode;
39 label: string;
40 labelClassName: string;
41 tooltip?: ReactNode;
42 tooltipClassName?: string;
43 interactive?: boolean;
44 unresolved?: boolean;
45}) {
46 const chip = (
47 <span
48 className={cn(
49 className,
50 interactive && CONTEXT_INLINE_CHIP_INTERACTIVE_CLASS_NAME,
51 tooltip && CONTEXT_INLINE_CHIP_FOCUS_CLASS_NAME,
52 unresolved && "border-dashed text-foreground",
53 )}
54 data-context-unresolved={unresolved ? "true" : undefined}
55 tabIndex={tooltip ? 0 : undefined}
56 {...props}
57 >
58 {icon}
59 <span className={labelClassName}>{label}</span>
60 </span>
61 );
62 if (!tooltip) return chip;
63 return (
64 <Tooltip>
65 <TooltipTrigger render={chip} />
66 <TooltipPopup side="top" className={tooltipClassName}>
67 {tooltip}
68 </TooltipPopup>
69 </Tooltip>
70 );
72​
73export function ContextChipPopover(props: {
74 copyMarkdown?: string;
75 accessibleLabel: string;
76 chip: ReactNode;
77 children: ReactNode;
78 triggerClassName?: string;
79 popupClassName?: string;
80 viewportClassName?: string;
81}) {
82 return (
83 <Popover>
84 <PopoverTrigger
85 render={
86 <Button
87 variant="chip"
88 className={cn(
89 "inline-flex max-w-full cursor-pointer items-center rounded-[0.5em] align-middle",
90 CONTEXT_INLINE_CHIP_FOCUS_CLASS_NAME,
91 props.triggerClassName,
92 )}
93 aria-label={`${props.accessibleLabel}. Show details`}
94 data-markdown-copy={props.copyMarkdown}
95 />
96 }
97 >
98 {props.chip}
99 </PopoverTrigger>
100 <PopoverPopup
101 side="top"
102 className={cn("w-[min(36rem,calc(100vw-2rem))]", props.popupClassName)}
103 viewportClassName={cn("overflow-x-auto p-2", props.viewportClassName)}
104 >
105 <PopoverTitle className="sr-only">{props.accessibleLabel}</PopoverTitle>
106 {props.children}
107 </PopoverPopup>
108 </Popover>
109 );
111​
112export function PullRequestChip(props: {
113 metadata: PullRequestContextMetadata;
114 label: string;
115 kindLabel: string;
116 className: string;
117 labelClassName: string;
118 copyMarkdown?: string;
119 onOpen: (event: MouseEvent<HTMLElement>, url: string) => void;
120}) {
121 return (
122 <Tooltip>
123 <TooltipTrigger
124 render={
125 <Button
126 variant="chip"
127 className={cn(
128 props.className,
129 CONTEXT_INLINE_CHIP_FOCUS_CLASS_NAME,
130 CONTEXT_INLINE_CHIP_INTERACTIVE_CLASS_NAME,
131 "cursor-pointer",
132 )}
133 aria-label={`Open ${props.kindLabel} ${props.label}: ${props.metadata.title}`}
134 data-markdown-copy={props.copyMarkdown}
135 onClick={(event) => props.onOpen(event, props.metadata.url)}
136 >
137 <GitPullRequestIcon className={cn(COMPOSER_INLINE_CHIP_ICON_CLASS_NAME, "size-3.5")} />
138 <span className={props.labelClassName}>{props.label}</span>
139 </Button>
140 }
141 />
142 <TooltipPopup side="top">
143 <PullRequestContextDetails metadata={props.metadata} />
144 </TooltipPopup>
145 </Tooltip>
146 );
148​
149/** Sample the loaded thumbnail once; transparent pixels should not darken its accent. */
150function averageImageColor(image: HTMLImageElement): string | undefined {
151 try {
152 const canvas = document.createElement("canvas");
153 canvas.width = canvas.height = 16;
154 const context = canvas.getContext("2d");
155 if (!context) return;
156 context.drawImage(image, 0, 0, 16, 16);
157 const { data } = context.getImageData(0, 0, 16, 16);
158 let red = 0;
159 let green = 0;
160 let blue = 0;
161 let alpha = 0;
162 for (let index = 0; index < data.length; index += 4) {
163 const weight = data[index + 3]!;
164 red += data[index]! * weight;
165 green += data[index + 1]! * weight;
166 blue += data[index + 2]! * weight;
167 alpha += weight;
168 }
169 if (alpha === 0) return;
170 return `rgb(${Math.round(red / alpha)} ${Math.round(green / alpha)} ${Math.round(blue / alpha)})`;
171 } catch {
172 // Cross-origin or unavailable pixels keep the default image tone and preview action.
173 return;
174 }
176​
177export function ImageChipButton({
178 name,
179 previewUrl,
180 className,
181 labelClassName,
182 size,
183 suffix,
184 style,
185 ...props
186}: ComponentProps<"button"> & {
187 name: string;
188 previewUrl: string | undefined;
189 labelClassName: string;
190 /** Every attachment chip reports its size; images are no exception. */
191 size: string;
192 suffix?: string | null;
193}) {
194 const [sample, setSample] = useState<{ url: string; color: string | undefined }>();
195 const [corsFailedUrl, setCorsFailedUrl] = useState<string>();
196 const accent = sample?.url === previewUrl ? sample?.color : undefined;
197 return (
198 <Button
199 variant="chip"
200 className={cn(
201 className,
202 CONTEXT_INLINE_CHIP_TONE_CLASS_NAMES.image,
203 CONTEXT_INLINE_CHIP_INTERACTIVE_CLASS_NAME,
204 "cursor-zoom-in",
205 )}
206 aria-label={`Image attachment, ${name}, ${size}`}
207 style={{ ...style, ...(accent ? { "--context-chip-accent": accent } : {}) } as CSSProperties}
208 {...props}
209 >
210 {previewUrl ? (
211 <img
212 key={previewUrl}
213 crossOrigin={corsFailedUrl === previewUrl ? undefined : "anonymous"}
214 src={previewUrl}
215 alt=""
216 className="size-3.5 shrink-0 rounded-sm object-cover"
217 onError={() => setCorsFailedUrl(previewUrl)}
218 onLoad={(event) =>
219 setSample({ url: previewUrl, color: averageImageColor(event.currentTarget) })
220 }
221 />
222 ) : (
223 <ImageIcon
224 className={cn(
225 COMPOSER_INLINE_CHIP_ICON_CLASS_NAME,
226 CONTEXT_INLINE_CHIP_ICON_TONE_CLASS_NAMES.image,
227 "size-3.5",
228 )}
229 />
230 )}
231 <span className={cn(labelClassName, "max-w-72")}>{middleTruncateAttachmentName(name)}</span>
232 <span className="shrink-0 text-[10px] text-current">{size}</span>
233 {suffix ? <span className="text-[10px] text-current">{suffix}</span> : null}
234 </Button>
235 );
237​
238export function FileChip(props: {
239 name: string;
240 size: string;
241 isVideo: boolean;
242 theme: "light" | "dark";
243 className: string;
244 labelClassName: string;
245 accessibleLabel: string;
246 tooltip: string;
247 suffix?: string | null;
248 copyMarkdown?: string;
249 disabled?: boolean;
250 error?: boolean;
251 unresolved?: boolean;
252 onOpen?: (() => void) | undefined;
253}) {
254 const className = cn(
255 props.className,
256 props.isVideo
257 ? CONTEXT_INLINE_CHIP_TONE_CLASS_NAMES.video
258 : CONTEXT_INLINE_CHIP_TONE_CLASS_NAMES.file,
259 props.onOpen && !props.disabled && CONTEXT_INLINE_CHIP_INTERACTIVE_CLASS_NAME,
260 props.onOpen && !props.disabled && (props.isVideo ? "cursor-zoom-in" : "cursor-pointer"),
261 props.unresolved && "border-dashed text-foreground",
262 props.error && "border-destructive/35 bg-destructive/8 text-destructive",
263 );
264 const content = <FileChipContent {...props} />;
265 const attributes = {
266 className,
267 "aria-label": [props.accessibleLabel, props.suffix].filter(Boolean).join(", "),
268 "data-markdown-copy": props.copyMarkdown,
269 "data-context-unresolved": props.unresolved ? "true" : undefined,
270 };
271 return (
272 <Tooltip>
273 <TooltipTrigger
274 render={
275 props.onOpen ? (
276 <Button variant="chip" disabled={props.disabled} onClick={props.onOpen} {...attributes}>
277 {content}
278 </Button>
279 ) : (
280 <span tabIndex={0} {...attributes}>
281 {content}
282 </span>
283 )
284 }
285 />
286 <TooltipPopup side="top" className="max-w-80 whitespace-pre-wrap leading-tight">
287 {props.tooltip}
288 </TooltipPopup>
289 </Tooltip>
290 );
292​
293function FileChipContent(props: {
294 name: string;
295 size: string;
296 isVideo: boolean;
297 theme: "light" | "dark";
298 labelClassName: string;
299 suffix?: string | null;
300}) {
301 return (
302 <>
303 {props.isVideo ? (
304 <FilmIcon
305 className={cn(
306 COMPOSER_INLINE_CHIP_ICON_CLASS_NAME,
307 CONTEXT_INLINE_CHIP_ICON_TONE_CLASS_NAMES.video,
308 "size-3.5",
309 )}
310 />
311 ) : (
312 <PierreEntryIcon
313 pathValue={props.name}
314 kind="file"
315 theme={props.theme}
316 className="size-3.5"
317 />
318 )}
319 <span className={cn(props.labelClassName, "max-w-72")}>
320 {middleTruncateAttachmentName(props.name)}
321 </span>
322 <span className="shrink-0 text-[10px] text-current">{props.size}</span>
323 {props.suffix ? <span className="text-[10px] text-current">{props.suffix}</span> : null}
324 </>
325 );
327​
328export function UnresolvedChip(props: {
329 label: string;
330 className: string;
331 labelClassName: string;
332 tooltip: string;
333 tooltipClassName: string;
334 copyMarkdown?: string;
335}) {
336 return (
337 <ContextChipShell
338 icon={<CircleDashedIcon className={cn(COMPOSER_INLINE_CHIP_ICON_CLASS_NAME, "size-3.5")} />}
339 label={props.label}
340 className={props.className}
341 labelClassName={props.labelClassName}
342 aria-label={`Unavailable context, ${props.label}`}
343 data-markdown-copy={props.copyMarkdown}
344 tooltip={props.tooltip}
345 tooltipClassName={props.tooltipClassName}
346 unresolved
347 />
348 );