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

contextChipParts.tsx

335 lines10.1 KB

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 );
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 );
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 );
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="max-w-72 truncate">{middleTruncateAttachmentName(name)}</span>
171 <span className="shrink-0 text-3xs text-current">{size}</span>
172 {suffix ? <span className="text-3xs text-current">{suffix}</span> : null}
173 </ContextChipLabel>
174 </ContextChip>
175 );
177​
178export function FileChip(props: {
179 name: string;
180 size: string;
181 isVideo: boolean;
182 theme: "light" | "dark";
183 accessibleLabel: string;
184 tooltip: string;
185 suffix?: string | null;
186 copyMarkdown?: string;
187 disabled?: boolean;
188 error?: boolean;
189 unresolved?: boolean;
190 onOpen?: (() => void) | undefined;
191 svgPreview?:
192 | {
193 file?: Blob | null | undefined;
194 environmentId?: EnvironmentId | undefined;
195 attachmentId?: string | undefined;
196 }
197 | undefined;
198}) {
199 if (props.svgPreview && !props.unresolved) return <SvgFileChip {...props} />;
200 const content = <FileChipContent {...props} />;
201 const state = props.unresolved
202 ? ("unresolved" as const)
203 : props.error
204 ? ("invalid" as const)
205 : null;
206 const attributes = {
207 kind: props.isVideo ? "video" : (documentChipKindForFileName(props.name) ?? "file"),
208 ...(state ? { state } : {}),
209 "aria-label": [props.accessibleLabel, props.suffix].filter(Boolean).join(", "),
210 "data-markdown-copy": props.copyMarkdown,
211 "data-context-unresolved": props.unresolved ? "true" : undefined,
212 } as const;
213 return (
214 <Tooltip>
215 <TooltipTrigger
216 render={
217 props.onOpen ? (
218 <ContextChip
219 render={<button type="button" disabled={props.disabled} />}
220 onClick={props.onOpen}
221 {...attributes}
222 >
223 {content}
224 </ContextChip>
225 ) : (
226 <ContextChip tabIndex={0} {...attributes}>
227 {content}
228 </ContextChip>
229 )
230 }
231 />
232 <TooltipPopup side="top" className="whitespace-pre-wrap">
233 {props.tooltip}
234 </TooltipPopup>
235 </Tooltip>
236 );
238​
239function SvgFileChip(props: ComponentProps<typeof FileChip>) {
240 const preview = props.svgPreview;
241 const [local, setLocal] = useState<{ file: Blob; url: string }>();
242 const [failedPreviewUrl, setFailedPreviewUrl] = useState<string>();
243 const asset = useAssetUrlState(
244 preview?.file ? null : (preview?.environmentId ?? null),
245 preview?.attachmentId
246 ? {
247 _tag: "attachment",
248 attachmentId: preview.attachmentId,
249 fileName: props.name,
250 mimeType: "image/svg+xml",
251 disposition: "inline",
252 }
253 : null,
254 );
255 useEffect(() => {
256 const file = preview?.file;
257 if (!file) return;
258 const url = URL.createObjectURL(file);
259 // oxlint-disable-next-line react/set-state-in-effect -- Publish a Blob URL with its cleanup registered.
260 setLocal({ file, url });
261 return () => URL.revokeObjectURL(url);
262 }, [preview?.file]);
263 const url = preview?.file
264 ? local?.file === preview.file
265 ? local.url
266 : undefined
267 : asset._tag === "Success"
268 ? asset.url
269 : undefined;
270 if (url !== undefined && failedPreviewUrl === url) {
271 return <FileChip {...props} svgPreview={undefined} />;
272 }
273 return (
274 <Tooltip>
275 <TooltipTrigger
276 render={
277 <ImageChipButton
278 name={props.name}
279 size={props.size}
280 previewUrl={url}
281 suffix={props.suffix}
282 disabled={props.disabled}
283 onClick={props.onOpen}
284 onPreviewError={() => setFailedPreviewUrl(url)}
285 data-markdown-copy={props.copyMarkdown}
286 aria-label={props.accessibleLabel}
287 {...(props.error ? { state: "invalid" as const } : {})}
288 />
289 }
290 />
291 <TooltipPopup side="top" className="whitespace-pre-wrap">
292 {props.tooltip}
293 </TooltipPopup>
294 </Tooltip>
295 );
297​
298function FileChipContent(props: {
299 name: string;
300 size: string;
301 isVideo: boolean;
302 theme: "light" | "dark";
303 suffix?: string | null;
304 unresolved?: boolean;
305}) {
306 return (
307 <>
308 {props.unresolved ? (
309 <CircleDashedIcon />
310 ) : props.isVideo ? (
311 <FilmIcon />
312 ) : (
313 <PierreEntryIcon pathValue={props.name} kind="file" theme={props.theme} />
314 )}
315 <ContextChipLabel className="flex items-baseline gap-[0.33em]">
316 <span className="max-w-72 truncate">{middleTruncateAttachmentName(props.name)}</span>
317 <span className="shrink-0 text-3xs text-current">{props.size}</span>
318 {props.suffix ? <span className="text-3xs text-current">{props.suffix}</span> : null}
319 </ContextChipLabel>
320 </>
321 );
323​
324export function UnresolvedChip(props: { label: string; tooltip: string; copyMarkdown?: string }) {
325 return (
326 <ContextChipShell
327 icon={<CircleDashedIcon />}
328 label={props.label}
329 state="unresolved"
330 aria-label={`Unavailable context, ${props.label}`}
331 data-markdown-copy={props.copyMarkdown}
332 tooltip={props.tooltip}
333 />
334 );