Open History to see the last commit
1import type { EnvironmentId, PullRequestContextMetadata } from "@t3tools/contracts";
2import { CircleDashedIcon, FilmIcon, ImageIcon } from "lucide-react";
3import { useEffect, useState, type ComponentProps, type MouseEvent, type ReactNode } from "react";
4import { useAssetUrlState } from "~/assets/assetUrls";
5import { documentChipKindForFileName } from "~/pierre-icons";
6
7import { PULL_REQUEST_STATE_PRESENTATION } from "~/components/pullRequest/pullRequestIcons";
8import type { PullRequestContextDisplayState } from "~/lib/composerContextRecords";
9import { PierreEntryIcon } from "./chat/PierreEntryIcon";
10import { middleTruncateAttachmentName } from "./composerInlineChip";
11import { PullRequestContextDetails } from "./PullRequestContextDetails";
12import { ContextChip, ContextChipLabel, type ContextChipKind } from "./ContextChip";
13import { Popover, PopoverPopup, PopoverTitle, PopoverTrigger } from "./ui/popover";
14import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
15import { PullRequestLinkPreview } from "./pullRequest/PullRequestLinkPreview";
16import { usePullRequestPreviewTarget } from "~/lib/openPullRequestLink";
17
18export const PULL_REQUEST_CHIP_KINDS = {
19 open: "pr-open",
20 draft: "pr-draft",
21 merged: "pr-merged",
22 closed: "pr-closed",
23 unknown: "pull-request",
24} as const satisfies Record<PullRequestContextDisplayState | "unknown", ContextChipKind>;
25
26export function ContextChipShell({
27 icon,
28 label,
29 tooltip,
30 ...props
31}: Omit<ComponentProps<typeof ContextChip>, "className" | "render"> & {
32 icon: ReactNode;
33 label: string;
34 tooltip?: ReactNode;
35}) {
36 const chip = (
37 <ContextChip
38 data-context-unresolved={props.state === "unresolved" ? "true" : undefined}
39 tabIndex={tooltip ? 0 : undefined}
40 {...props}
41 >
42 {icon}
43 <ContextChipLabel>{label}</ContextChipLabel>
44 </ContextChip>
45 );
46 if (!tooltip) return chip;
47 return (
48 <Tooltip>
49 <TooltipTrigger render={chip} />
50 <TooltipPopup side="top" className="whitespace-pre-wrap">
51 {tooltip}
52 </TooltipPopup>
53 </Tooltip>
54 );
55}
56
57export function ContextChipPopover(props: {
58 kind: ContextChipKind;
59 icon: ReactNode;
60 label: string;
61 copyMarkdown?: string | undefined;
62 accessibleLabel: string;
63 children: ReactNode;
64}) {
65 return (
66 <Popover>
67 <PopoverTrigger
68 render={
69 <ContextChip
70 kind={props.kind}
71 render={<button type="button" />}
72 aria-label={`${props.accessibleLabel}. Show details`}
73 data-markdown-copy={props.copyMarkdown}
74 />
75 }
76 >
77 {props.icon}
78 <ContextChipLabel>{props.label}</ContextChipLabel>
79 </PopoverTrigger>
80 <PopoverPopup side="top" width="lg" padding="compact">
81 <PopoverTitle className="sr-only">{props.accessibleLabel}</PopoverTitle>
82 {props.children}
83 </PopoverPopup>
84 </Popover>
85 );
86}
87
88export function PullRequestChip(props: {
89 metadata: PullRequestContextMetadata;
90 environmentId: EnvironmentId | null;
91 label: string;
92 kindLabel: string;
93 kind: ContextChipKind;
94 copyMarkdown?: string;
95 onOpen: (event: MouseEvent<HTMLElement>, url: string) => void;
96}) {
97 const previewTarget = usePullRequestPreviewTarget(props.environmentId, props.metadata.url);
98 const displayState =
99 props.metadata.state === "open" && props.metadata.isDraft ? "draft" : props.metadata.state;
100 const StateIcon = PULL_REQUEST_STATE_PRESENTATION[displayState].Icon;
101 const button = (
102 <ContextChip
103 kind={props.kind}
104 render={<button type="button" />}
105 aria-label={`Open ${props.kindLabel} ${props.label}: ${props.metadata.title}`}
106 data-markdown-copy={props.copyMarkdown}
107 onClick={(event) => props.onOpen(event, props.metadata.url)}
108 onAuxClick={(event) => props.onOpen(event, props.metadata.url)}
109 >
110 <StateIcon />
111 <ContextChipLabel>{props.label}</ContextChipLabel>
112 </ContextChip>
113 );
114 if (previewTarget !== null) {
115 return (
116 <PullRequestLinkPreview
117 link={button}
118 originalUrl={props.metadata.url}
119 target={previewTarget}
120 confirmBeforeOpen={false}
121 fallback={<PullRequestContextDetails metadata={props.metadata} />}
122 />
123 );
124 }
125 return (
126 <Tooltip>
127 <TooltipTrigger render={button} />
128 <TooltipPopup side="top">
129 <PullRequestContextDetails metadata={props.metadata} />
130 </TooltipPopup>
131 </Tooltip>
132 );
133}
134
135export function ImageChipButton({
136 name,
137 previewUrl,
138 size,
139 suffix,
140 disabled,
141 onPreviewError,
142 ...props
143}: Omit<ComponentProps<typeof ContextChip>, "kind" | "render"> & {
144 name: string;
145 previewUrl: string | undefined;
146 size: string;
147 suffix?: string | null | undefined;
148 disabled?: boolean | undefined;
149 onPreviewError?: (() => void) | undefined;
150}) {
151 return (
152 <ContextChip
153 kind="image"
154 render={<button type="button" disabled={disabled} />}
155 aria-label={`Image attachment, ${name}, ${size}`}
156 {...props}
157 >
158 {previewUrl ? (
159 <img
160 key={previewUrl}
161 src={previewUrl}
162 alt=""
163 className="size-[1em] shrink-0 self-center rounded-sm object-cover"
164 onError={onPreviewError}
165 />
166 ) : (
167 <ImageIcon />
168 )}
169 <ContextChipLabel className="flex items-baseline gap-[0.33em]">
170 <span className="min-w-0 max-w-72 overflow-x-clip text-ellipsis whitespace-nowrap">
171 {middleTruncateAttachmentName(name)}
172 </span>
173 <span className="shrink-0 text-3xs text-current">{size}</span>
174 {suffix ? <span className="text-3xs text-current">{suffix}</span> : null}
175 </ContextChipLabel>
176 </ContextChip>
177 );
178}
179
180export function FileChip(props: {
181 name: string;
182 size: string;
183 isVideo: boolean;
184 theme: "light" | "dark";
185 accessibleLabel: string;
186 tooltip: string;
187 suffix?: string | null;
188 copyMarkdown?: string;
189 disabled?: boolean;
190 error?: boolean;
191 unresolved?: boolean;
192 onOpen?: (() => void) | undefined;
193 svgPreview?:
194 | {
195 file?: Blob | null | undefined;
196 environmentId?: EnvironmentId | undefined;
197 attachmentId?: string | undefined;
198 }
199 | undefined;
200}) {
201 if (props.svgPreview && !props.unresolved) return <SvgFileChip {...props} />;
202 const content = <FileChipContent {...props} />;
203 const state = props.unresolved
204 ? ("unresolved" as const)
205 : props.error
206 ? ("invalid" as const)
207 : null;
208 const attributes = {
209 kind: props.isVideo ? "video" : (documentChipKindForFileName(props.name) ?? "file"),
210 ...(state ? { state } : {}),
211 "aria-label": [props.accessibleLabel, props.suffix].filter(Boolean).join(", "),
212 "data-markdown-copy": props.copyMarkdown,
213 "data-context-unresolved": props.unresolved ? "true" : undefined,
214 } as const;
215 return (
216 <Tooltip>
217 <TooltipTrigger
218 render={
219 props.onOpen ? (
220 <ContextChip
221 render={<button type="button" disabled={props.disabled} />}
222 onClick={props.onOpen}
223 {...attributes}
224 >
225 {content}
226 </ContextChip>
227 ) : (
228 <ContextChip tabIndex={0} {...attributes}>
229 {content}
230 </ContextChip>
231 )
232 }
233 />
234 <TooltipPopup side="top" className="whitespace-pre-wrap">
235 {props.tooltip}
236 </TooltipPopup>
237 </Tooltip>
238 );
239}
240
241function SvgFileChip(props: ComponentProps<typeof FileChip>) {
242 const preview = props.svgPreview;
243 const [local, setLocal] = useState<{ file: Blob; url: string }>();
244 const [failedPreviewUrl, setFailedPreviewUrl] = useState<string>();
245 const asset = useAssetUrlState(
246 preview?.file ? null : (preview?.environmentId ?? null),
247 preview?.attachmentId
248 ? {
249 _tag: "attachment",
250 attachmentId: preview.attachmentId,
251 fileName: props.name,
252 mimeType: "image/svg+xml",
253 disposition: "inline",
254 }
255 : null,
256 );
257 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- creates an object URL for the local SVG file and revokes it on cleanup
258 useEffect(() => {
259 const file = preview?.file;
260 if (!file) return;
261 const url = URL.createObjectURL(file);
262 // oxlint-disable-next-line react/set-state-in-effect -- Publish a Blob URL with its cleanup registered.
263 setLocal({ file, url });
264 return () => URL.revokeObjectURL(url);
265 }, [preview?.file]);
266 const url = preview?.file
267 ? local?.file === preview.file
268 ? local.url
269 : undefined
270 : asset._tag === "Success"
271 ? asset.url
272 : undefined;
273 if (url !== undefined && failedPreviewUrl === url) {
274 return <FileChip {...props} svgPreview={undefined} />;
275 }
276 return (
277 <Tooltip>
278 <TooltipTrigger
279 render={
280 <ImageChipButton
281 name={props.name}
282 size={props.size}
283 previewUrl={url}
284 suffix={props.suffix}
285 disabled={props.disabled}
286 onClick={props.onOpen}
287 onPreviewError={() => setFailedPreviewUrl(url)}
288 data-markdown-copy={props.copyMarkdown}
289 aria-label={props.accessibleLabel}
290 {...(props.error ? { state: "invalid" as const } : {})}
291 />
292 }
293 />
294 <TooltipPopup side="top" className="whitespace-pre-wrap">
295 {props.tooltip}
296 </TooltipPopup>
297 </Tooltip>
298 );
299}
300
301function FileChipContent(props: {
302 name: string;
303 size: string;
304 isVideo: boolean;
305 theme: "light" | "dark";
306 suffix?: string | null;
307 unresolved?: boolean;
308}) {
309 return (
310 <>
311 {props.unresolved ? (
312 <CircleDashedIcon />
313 ) : props.isVideo ? (
314 <FilmIcon />
315 ) : (
316 <PierreEntryIcon pathValue={props.name} kind="file" theme={props.theme} />
317 )}
318 <ContextChipLabel className="flex items-baseline gap-[0.33em]">
319 <span className="min-w-0 max-w-72 overflow-x-clip text-ellipsis whitespace-nowrap">
320 {middleTruncateAttachmentName(props.name)}
321 </span>
322 <span className="shrink-0 text-3xs text-current">{props.size}</span>
323 {props.suffix ? <span className="text-3xs text-current">{props.suffix}</span> : null}
324 </ContextChipLabel>
325 </>
326 );
327}
328
329export function UnresolvedChip(props: { label: string; tooltip: string; copyMarkdown?: string }) {
330 return (
331 <ContextChipShell
332 icon={<CircleDashedIcon />}
333 label={props.label}
334 state="unresolved"
335 aria-label={`Unavailable context, ${props.label}`}
336 data-markdown-copy={props.copyMarkdown}
337 tooltip={props.tooltip}
338 />
339 );
340}