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

contextChipParts.tsx

299 lines8.6 KB

1import type { PullRequestContextMetadata } from "@t3tools/contracts";
2import { CircleDashedIcon, FilmIcon, GitPullRequestIcon, ImageIcon } from "lucide-react";
3import type { ComponentProps, MouseEvent, ReactNode } from "react";
4​
5import { cn } from "~/lib/utils";
6import { PierreEntryIcon } from "./chat/PierreEntryIcon";
7import {
8 COMPOSER_INLINE_CHIP_ICON_CLASS_NAME,
9 CONTEXT_INLINE_CHIP_FOCUS_CLASS_NAME,
10 CONTEXT_INLINE_CHIP_ICON_TONE_CLASS_NAMES,
11 CONTEXT_INLINE_CHIP_INTERACTIVE_CLASS_NAME,
12 CONTEXT_INLINE_CHIP_TONE_CLASS_NAMES,
13 middleTruncateAttachmentName,
14} from "./composerInlineChip";
15import { PullRequestContextDetails } from "./PullRequestContextDetails";
16import { Button } from "./ui/button";
17import { Popover, PopoverPopup, PopoverTitle, PopoverTrigger } from "./ui/popover";
18import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
19​
20/** Shared visual slots; each surface keeps ownership of payload lookup and actions. */
21export function ContextChipShell({
22 icon,
23 label,
24 labelClassName,
25 tooltip,
26 tooltipClassName = "max-w-96 whitespace-pre-wrap leading-tight",
27 interactive,
28 unresolved,
29 className,
30 ...props
31}: ComponentProps<"span"> & {
32 icon: ReactNode;
33 label: string;
34 labelClassName: string;
35 tooltip?: ReactNode;
36 tooltipClassName?: string;
37 interactive?: boolean;
38 unresolved?: boolean;
39}) {
40 const chip = (
41 <span
42 className={cn(
43 className,
44 interactive && CONTEXT_INLINE_CHIP_INTERACTIVE_CLASS_NAME,
45 tooltip && CONTEXT_INLINE_CHIP_FOCUS_CLASS_NAME,
46 unresolved && "border-dashed text-foreground",
47 )}
48 data-context-unresolved={unresolved ? "true" : undefined}
49 tabIndex={tooltip ? 0 : undefined}
50 {...props}
51 >
52 {icon}
53 <span className={labelClassName}>{label}</span>
54 </span>
55 );
56 if (!tooltip) return chip;
57 return (
58 <Tooltip>
59 <TooltipTrigger render={chip} />
60 <TooltipPopup side="top" className={tooltipClassName}>
61 {tooltip}
62 </TooltipPopup>
63 </Tooltip>
64 );
66​
67export function ContextChipPopover(props: {
68 copyMarkdown?: string;
69 accessibleLabel: string;
70 chip: ReactNode;
71 children: ReactNode;
72 triggerClassName?: string;
73 popupClassName?: string;
74 viewportClassName?: string;
75}) {
76 return (
77 <Popover>
78 <PopoverTrigger
79 render={
80 <Button
81 variant="chip"
82 className={cn(
83 "inline-flex max-w-full cursor-pointer items-center rounded-[0.5em] align-middle",
84 CONTEXT_INLINE_CHIP_FOCUS_CLASS_NAME,
85 props.triggerClassName,
86 )}
87 aria-label={`${props.accessibleLabel}. Show details`}
88 data-markdown-copy={props.copyMarkdown}
89 />
90 }
91 >
92 {props.chip}
93 </PopoverTrigger>
94 <PopoverPopup
95 side="top"
96 className={cn("w-[min(36rem,calc(100vw-2rem))]", props.popupClassName)}
97 viewportClassName={cn("overflow-x-auto p-2", props.viewportClassName)}
98 >
99 <PopoverTitle className="sr-only">{props.accessibleLabel}</PopoverTitle>
100 {props.children}
101 </PopoverPopup>
102 </Popover>
103 );
105​
106export function PullRequestChip(props: {
107 metadata: PullRequestContextMetadata;
108 label: string;
109 kindLabel: string;
110 className: string;
111 labelClassName: string;
112 copyMarkdown?: string;
113 onOpen: (event: MouseEvent<HTMLElement>, url: string) => void;
114}) {
115 return (
116 <ContextChipPopover
117 accessibleLabel={`${props.kindLabel} ${props.label}: ${props.metadata.title}`}
118 {...(props.copyMarkdown ? { copyMarkdown: props.copyMarkdown } : {})}
119 triggerClassName={props.className}
120 chip={
121 <>
122 <GitPullRequestIcon className={cn(COMPOSER_INLINE_CHIP_ICON_CLASS_NAME, "size-3.5")} />
123 <span className={props.labelClassName}>{props.label}</span>
124 </>
125 }
126 >
127 <div className="space-y-3 p-2">
128 <PullRequestContextDetails metadata={props.metadata} />
129 <p className="text-xs text-muted-foreground">Captured pull request context</p>
130 <Button
131 variant="outline"
132 size="sm"
133 onClick={(event) => props.onOpen(event, props.metadata.url)}
134 >
135 Open pull request
136 </Button>
137 </div>
138 </ContextChipPopover>
139 );
141​
142export function ImageChipButton({
143 name,
144 previewUrl,
145 className,
146 labelClassName,
147 size,
148 suffix,
149 ...props
150}: ComponentProps<"button"> & {
151 name: string;
152 previewUrl: string | undefined;
153 labelClassName: string;
154 /** Every attachment chip reports its size; images are no exception. */
155 size: string;
156 suffix?: string | null;
157}) {
158 return (
159 <Button
160 variant="chip"
161 className={cn(
162 className,
163 CONTEXT_INLINE_CHIP_TONE_CLASS_NAMES.image,
164 CONTEXT_INLINE_CHIP_INTERACTIVE_CLASS_NAME,
165 "cursor-zoom-in",
166 )}
167 aria-label={`Image attachment, ${name}, ${size}`}
168 {...props}
169 >
170 {previewUrl ? (
171 <img src={previewUrl} alt="" className="size-3.5 shrink-0 rounded-sm object-cover" />
172 ) : (
173 <ImageIcon
174 className={cn(
175 COMPOSER_INLINE_CHIP_ICON_CLASS_NAME,
176 CONTEXT_INLINE_CHIP_ICON_TONE_CLASS_NAMES.image,
177 "size-3.5",
178 )}
179 />
180 )}
181 <span className={cn(labelClassName, "max-w-72")}>{middleTruncateAttachmentName(name)}</span>
182 <span className="shrink-0 text-[10px] text-current">{size}</span>
183 {suffix ? <span className="text-[10px] text-current">{suffix}</span> : null}
184 </Button>
185 );
187​
188export function FileChip(props: {
189 name: string;
190 size: string;
191 isVideo: boolean;
192 theme: "light" | "dark";
193 className: string;
194 labelClassName: string;
195 accessibleLabel: string;
196 tooltip: string;
197 suffix?: string | null;
198 copyMarkdown?: string;
199 disabled?: boolean;
200 error?: boolean;
201 unresolved?: boolean;
202 onOpen?: (() => void) | undefined;
203}) {
204 const className = cn(
205 props.className,
206 props.isVideo
207 ? CONTEXT_INLINE_CHIP_TONE_CLASS_NAMES.video
208 : CONTEXT_INLINE_CHIP_TONE_CLASS_NAMES.file,
209 props.onOpen && !props.disabled && CONTEXT_INLINE_CHIP_INTERACTIVE_CLASS_NAME,
210 props.onOpen && !props.disabled && (props.isVideo ? "cursor-zoom-in" : "cursor-pointer"),
211 props.unresolved && "border-dashed text-foreground",
212 props.error && "border-destructive/35 bg-destructive/8 text-destructive",
213 );
214 const content = <FileChipContent {...props} />;
215 const attributes = {
216 className,
217 "aria-label": [props.accessibleLabel, props.suffix].filter(Boolean).join(", "),
218 "data-markdown-copy": props.copyMarkdown,
219 "data-context-unresolved": props.unresolved ? "true" : undefined,
220 };
221 return (
222 <Tooltip>
223 <TooltipTrigger
224 render={
225 props.onOpen ? (
226 <Button variant="chip" disabled={props.disabled} onClick={props.onOpen} {...attributes}>
227 {content}
228 </Button>
229 ) : (
230 <span tabIndex={0} {...attributes}>
231 {content}
232 </span>
233 )
234 }
235 />
236 <TooltipPopup side="top" className="max-w-80 whitespace-pre-wrap leading-tight">
237 {props.tooltip}
238 </TooltipPopup>
239 </Tooltip>
240 );
242​
243function FileChipContent(props: {
244 name: string;
245 size: string;
246 isVideo: boolean;
247 theme: "light" | "dark";
248 labelClassName: string;
249 suffix?: string | null;
250}) {
251 return (
252 <>
253 {props.isVideo ? (
254 <FilmIcon
255 className={cn(
256 COMPOSER_INLINE_CHIP_ICON_CLASS_NAME,
257 CONTEXT_INLINE_CHIP_ICON_TONE_CLASS_NAMES.video,
258 "size-3.5",
259 )}
260 />
261 ) : (
262 <PierreEntryIcon
263 pathValue={props.name}
264 kind="file"
265 theme={props.theme}
266 className="size-3.5"
267 />
268 )}
269 <span className={cn(props.labelClassName, "max-w-72")}>
270 {middleTruncateAttachmentName(props.name)}
271 </span>
272 <span className="shrink-0 text-[10px] text-current">{props.size}</span>
273 {props.suffix ? <span className="text-[10px] text-current">{props.suffix}</span> : null}
274 </>
275 );
277​
278export function UnresolvedChip(props: {
279 label: string;
280 className: string;
281 labelClassName: string;
282 tooltip: string;
283 tooltipClassName: string;
284 copyMarkdown?: string;
285}) {
286 return (
287 <ContextChipShell
288 icon={<CircleDashedIcon className={cn(COMPOSER_INLINE_CHIP_ICON_CLASS_NAME, "size-3.5")} />}
289 label={props.label}
290 className={props.className}
291 labelClassName={props.labelClassName}
292 aria-label={`Unavailable context, ${props.label}`}
293 data-markdown-copy={props.copyMarkdown}
294 tooltip={props.tooltip}
295 tooltipClassName={props.tooltipClassName}
296 unresolved
297 />
298 );