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