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: props.accessibleLabel }}. Show details`}
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: props.kindLabel }} ${{ label: props.label }}: ${{ title: props.metadata.title }}`}
116 data-markdown-copy={props.copyMarkdown}
117 onClick={(event) => props.onOpen(event, props.metadata.url)}
118 onAuxClick={(event) => props.onOpen(event, props.metadata.url)}
119 >
120 <StateIcon />
121 <ContextChipLabel>{props.label}</ContextChipLabel>
122 </ContextChip>
123 );
124 if (previewTarget !== null) {
125 return (
126 <PullRequestLinkPreview
127 link={button}
128 originalUrl={props.metadata.url}
129 target={previewTarget}
130 confirmBeforeOpen={false}
131 fallback={<PullRequestContextDetails metadata={props.metadata} />}
132 />
133 );
134 }
135 return (
136 <SharedTooltipTrigger
137 render={button}
138 label={
139 <>
140 <PullRequestContextDetails metadata={props.metadata} />
141 </>
142 }
143 />
144 );
145}
146
147export function ImageChipButton({
148 name,
149 previewUrl,
150 size,
151 suffix,
152 disabled,
153 onPreviewError,
154 ...props
155}: Omit<ComponentProps<typeof ContextChip>, "kind" | "render"> & {
156 name: string;
157 previewUrl: string | undefined;
158 size: string;
159 suffix?: string | null | undefined;
160 disabled?: boolean | undefined;
161 onPreviewError?: (() => void) | undefined;
162}) {
163 const gt = useGT();
164 return (
165 <ContextChip
166 kind="image"
167 render={<button type="button" disabled={disabled} />}
168 aria-label={gt`Image attachment, ${{ name }}, ${{ size }}`}
169 {...props}
170 >
171 {previewUrl ? (
172 <img
173 key={previewUrl}
174 src={previewUrl}
175 alt=""
176 className="size-[1em] shrink-0 self-center rounded-sm object-cover"
177 onError={onPreviewError}
178 />
179 ) : (
180 <ImageIcon />
181 )}
182 <ContextChipLabel className="flex items-baseline gap-[0.33em]">
183 <span className="min-w-0 max-w-72 overflow-x-clip text-ellipsis whitespace-nowrap">
184 {middleTruncateAttachmentName(name)}
185 </span>
186 <span className="shrink-0 text-3xs text-current">{size}</span>
187 {suffix ? <span className="text-3xs text-current">{suffix}</span> : null}
188 </ContextChipLabel>
189 </ContextChip>
190 );
191}
192
193export function FileChip(props: {
194 name: string;
195 size: string;
196 isVideo: boolean;
197 theme: "light" | "dark";
198 accessibleLabel: string;
199 tooltip: string;
200 suffix?: string | null;
201 copyMarkdown?: string;
202 disabled?: boolean;
203 error?: boolean;
204 unresolved?: boolean;
205 onOpen?: (() => void) | undefined;
206 svgPreview?:
207 | {
208 file?: Blob | null | undefined;
209 environmentId?: EnvironmentId | undefined;
210 attachmentId?: string | undefined;
211 }
212 | undefined;
213}) {
214 if (props.svgPreview && !props.unresolved) return <SvgFileChip {...props} />;
215 const content = <FileChipContent {...props} />;
216 const state = props.unresolved
217 ? ("unresolved" as const)
218 : props.error
219 ? ("invalid" as const)
220 : null;
221 const attributes = {
222 kind: props.isVideo ? "video" : (documentChipKindForFileName(props.name) ?? "file"),
223 ...(state ? { state } : {}),
224 "aria-label": [props.accessibleLabel, props.suffix].filter(Boolean).join(", "),
225 "data-markdown-copy": props.copyMarkdown,
226 "data-context-unresolved": props.unresolved ? "true" : undefined,
227 } as const;
228 return (
229 <SharedTooltipTrigger
230 render={
231 props.onOpen ? (
232 <ContextChip
233 render={<button type="button" disabled={props.disabled} />}
234 onClick={props.onOpen}
235 {...attributes}
236 >
237 {content}
238 </ContextChip>
239 ) : (
240 <ContextChip tabIndex={0} {...attributes}>
241 {content}
242 </ContextChip>
243 )
244 }
245 label={props.tooltip}
246 className="whitespace-pre-wrap"
247 />
248 );
249}
250
251function SvgFileChip(props: ComponentProps<typeof FileChip>) {
252 const preview = props.svgPreview;
253 const [local, setLocal] = useState<{ file: Blob; url: string }>();
254 const [failedPreviewUrl, setFailedPreviewUrl] = useState<string>();
255 const asset = useAssetUrlState(
256 preview?.file ? null : (preview?.environmentId ?? null),
257 preview?.attachmentId
258 ? {
259 _tag: "attachment",
260 attachmentId: preview.attachmentId,
261 fileName: props.name,
262 mimeType: "image/svg+xml",
263 disposition: "inline",
264 }
265 : null,
266 );
267 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- creates an object URL for the local SVG file and revokes it on cleanup
268 useEffect(() => {
269 const file = preview?.file;
270 if (!file) return;
271 const url = URL.createObjectURL(file);
272 // oxlint-disable-next-line react/set-state-in-effect -- Publish a Blob URL with its cleanup registered.
273 setLocal({ file, url });
274 return () => URL.revokeObjectURL(url);
275 }, [preview?.file]);
276 const url = preview?.file
277 ? local?.file === preview.file
278 ? local.url
279 : undefined
280 : asset._tag === "Success"
281 ? asset.url
282 : undefined;
283 if (url !== undefined && failedPreviewUrl === url) {
284 return <FileChip {...props} svgPreview={undefined} />;
285 }
286 return (
287 <SharedTooltipTrigger
288 render={
289 <ImageChipButton
290 name={props.name}
291 size={props.size}
292 previewUrl={url}
293 suffix={props.suffix}
294 disabled={props.disabled}
295 onClick={props.onOpen}
296 onPreviewError={() => setFailedPreviewUrl(url)}
297 data-markdown-copy={props.copyMarkdown}
298 aria-label={props.accessibleLabel}
299 {...(props.error ? { state: "invalid" as const } : {})}
300 />
301 }
302 label={props.tooltip}
303 className="whitespace-pre-wrap"
304 />
305 );
306}
307
308function FileChipContent(props: {
309 name: string;
310 size: string;
311 isVideo: boolean;
312 theme: "light" | "dark";
313 suffix?: string | null;
314 unresolved?: boolean;
315}) {
316 return (
317 <>
318 {props.unresolved ? (
319 <CircleDashedIcon />
320 ) : props.isVideo ? (
321 <FilmIcon />
322 ) : (
323 <PierreEntryIcon pathValue={props.name} kind="file" theme={props.theme} />
324 )}
325 <ContextChipLabel className="flex items-baseline gap-[0.33em]">
326 <span className="min-w-0 max-w-72 overflow-x-clip text-ellipsis whitespace-nowrap">
327 {middleTruncateAttachmentName(props.name)}
328 </span>
329 <span className="shrink-0 text-3xs text-current">{props.size}</span>
330 {props.suffix ? <span className="text-3xs text-current">{props.suffix}</span> : null}
331 </ContextChipLabel>
332 </>
333 );
334}
335
336export function UnresolvedChip(props: { label: string; tooltip: string; copyMarkdown?: string }) {
337 const gt = useGT();
338 return (
339 <ContextChipShell
340 icon={<CircleDashedIcon />}
341 label={props.label}
342 state="unresolved"
343 aria-label={gt`Unavailable context, ${{ label: props.label }}`}
344 data-markdown-copy={props.copyMarkdown}
345 tooltip={props.tooltip}
346 />
347 );
348}