Open History to see the last commit
1import type {
2 ChatAttachment,
3 ChatFileAttachment,
4 EnvironmentId,
5 ScopedThreadRef,
6 SnapShotSource,
7} from "@t3tools/contracts";
8import { formatAttachmentSize } from "@t3tools/client-runtime/state/attachments";
9import { CircleAlertIcon, PaperclipIcon, PlayIcon, XIcon } from "lucide-react";
10import { type ComponentProps, useLayoutEffect, useRef } from "react";
11
12import {
13 type ComposerFileAttachment,
14 type ComposerImageAttachment,
15 type DraftId,
16 composerFileNeedsReattach,
17} from "../../composerDraftStore";
18import { retryAttachmentUpload, useAttachmentUploadStore } from "../../lib/attachmentUploadQueue";
19import { formatAttachmentUploadProgress } from "../../lib/attachmentUploadState";
20import { isHeicImageFile } from "../../lib/imageCompression";
21import {
22 type PendingSnapShotAnimation,
23 scheduleSnapShotAnimationDestination,
24 setSnapShotAnimationDestination,
25 shouldAnimateSnapShotArrival,
26} from "../../lib/snapShotAnimation";
27import { cn } from "~/lib/utils";
28import { getRestingComposerImagePreviewCounts } from "../composerFooterLayout";
29import { RefreshIcon } from "~/components/ui/refresh-icon";
30import { AttachmentFilePreview } from "../files/AttachmentFilePreview";
31import { AttachmentImage } from "../media/AttachmentImage";
32import { Button } from "../ui/button";
33import { Dialog, DialogPopup, DialogTitle } from "../ui/dialog";
34import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
35import { ComposerImageThumbnail } from "./ComposerImageThumbnail";
36import { composerFloatingLayerProps } from "./composerEventScope";
37import type { PreparingComposerImage } from "./preparingComposerImages";
38import {
39 attachVideoThumbnail,
40 buildExpandedImagePreview,
41 type ExpandedImagePreview,
42} from "./ExpandedImagePreview";
43import { PierreEntryIcon } from "./PierreEntryIcon";
44import {
45 SNAP_SHOT_ATTACHMENT_FRAME_CLASS,
46 SnapShotAttachmentDetails,
47} from "./SnapShotAttachmentDetails";
48import { prepareVideoFirstFrame } from "../../lib/videoFirstFrame";
49
50function ComposerVideoThumbnail({ file }: { file: File }) {
51 const setVideo = (video: HTMLVideoElement | null) => {
52 if (!video) return;
53 return attachVideoThumbnail(video, file);
54 };
55
56 return (
57 <video
58 ref={setVideo}
59 muted
60 playsInline
61 preload="metadata"
62 aria-hidden="true"
63 onLoadedMetadata={(event) => prepareVideoFirstFrame(event.currentTarget)}
64 className="pointer-events-none absolute inset-0 size-full object-cover"
65 />
66 );
67}
68
69function SnapShotAttachmentFrame({
70 animationId,
71 animationSource,
72 arrival,
73 animateArrival,
74 className,
75 ...props
76}: ComponentProps<"div"> & {
77 readonly animationId?: string | undefined;
78 readonly animationSource?: SnapShotSource | undefined;
79 readonly arrival?: boolean | undefined;
80 readonly animateArrival?: boolean | undefined;
81}) {
82 const frameRef = useRef<HTMLDivElement>(null);
83
84 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- scrolls the mounted tile into view and registers it as the SnapShot animation destination
85 useLayoutEffect(() => {
86 const frame = frameRef.current;
87 if (!frame || (!animationId && !arrival)) return;
88 frame.scrollIntoView({ block: "nearest", inline: "nearest" });
89 if (!animationId) return;
90
91 return scheduleSnapShotAnimationDestination(animationId, () =>
92 setSnapShotAnimationDestination(animationId, frame, animationSource),
93 );
94 }, [animationId, animationSource, arrival]);
95
96 return (
97 <div
98 ref={frameRef}
99 className={cn(
100 animateArrival &&
101 !animationId &&
102 "origin-center transition-[opacity,scale] duration-300 ease-drawer starting:scale-95 starting:opacity-0 motion-reduce:transition-none motion-reduce:starting:scale-100 motion-reduce:starting:opacity-100",
103 className,
104 )}
105 {...props}
106 />
107 );
108}
109
110/** Image, SnapShot and video tiles above the composer editor. */
111export function ComposerAttachmentMediaRow({
112 images,
113 expandedImages,
114 preparingImages,
115 videos,
116 pendingSnapShotAnimations,
117 pendingSnapShotIds,
118 uncommittedSnapShotIds,
119 nonPersistedImageIds,
120 snapShotAnimations,
121 supportsAttachmentUploads,
122 maxFileAttachmentBytes,
123 environmentId,
124 attachmentDraftTarget,
125 canOperateThread,
126 onExpandImage,
127 onFileOpen,
128 onRemoveImage,
129 onRemoveFile,
130}: {
131 readonly images: ReadonlyArray<ComposerImageAttachment>;
132 readonly expandedImages: ReadonlyArray<ComposerImageAttachment>;
133 readonly preparingImages: ReadonlyArray<PreparingComposerImage>;
134 readonly videos: ReadonlyArray<ComposerFileAttachment>;
135 readonly pendingSnapShotAnimations: ReadonlyArray<PendingSnapShotAnimation>;
136 readonly pendingSnapShotIds: ReadonlyArray<string>;
137 readonly uncommittedSnapShotIds: ReadonlyArray<string>;
138 readonly nonPersistedImageIds: ReadonlyArray<string>;
139 readonly snapShotAnimations: boolean;
140 readonly supportsAttachmentUploads: boolean;
141 readonly maxFileAttachmentBytes: number | null;
142 readonly environmentId: EnvironmentId;
143 readonly attachmentDraftTarget: ScopedThreadRef | DraftId;
144 readonly canOperateThread: boolean;
145 readonly onExpandImage: (preview: ExpandedImagePreview) => void;
146 readonly onFileOpen: (attachment: ChatFileAttachment) => void;
147 readonly onRemoveImage: (imageId: string) => void;
148 readonly onRemoveFile: (fileId: string) => void;
149}) {
150 const uploadsByImageId = useAttachmentUploadStore((state) => state.uploadsByImageId);
151 const pendingSnapShotIdSet = new Set(pendingSnapShotIds);
152 const nonPersistedImageIdSet = new Set(nonPersistedImageIds);
153 return (
154 <div
155 className={cn(
156 "mb-3 flex max-w-full gap-2",
157 pendingSnapShotIds.length > 0 ||
158 expandedImages.some((image) => image.source?.kind === "snap-shot")
159 ? "snap-x snap-proximity overflow-x-auto overscroll-x-contain pb-1 scrollbar-thumb-foreground/18 scrollbar-track-transparent [scrollbar-width:thin] [&::-webkit-scrollbar]:h-2.5 [&::-webkit-scrollbar-thumb]:rounded-full [&::-webkit-scrollbar-thumb]:border-3 [&::-webkit-scrollbar-thumb]:border-transparent [&::-webkit-scrollbar-thumb]:bg-foreground/18 [&::-webkit-scrollbar-thumb]:bg-clip-content [&::-webkit-scrollbar-thumb:hover]:bg-foreground/28 [&::-webkit-scrollbar-track]:mx-1 [&::-webkit-scrollbar-track]:bg-transparent"
160 : "flex-wrap",
161 )}
162 >
163 {/* A preparing image renders through the same tile, keyed by the id its compressed image
164 takes, so the thumbnail element survives the swap. */}
165 {[
166 ...expandedImages.map((image) => ({ image, ready: image })),
167 ...preparingImages.map((image) => ({ image, ready: null })),
168 ]
169 .map(({ image, ready }) => {
170 const upload =
171 ready && supportsAttachmentUploads ? uploadsByImageId[image.id] : undefined;
172 const source = ready?.source;
173 const snapShotAnimationPending =
174 source?.kind === "snap-shot" && pendingSnapShotIdSet.has(image.id);
175 const snapShotArrival =
176 source?.kind === "snap-shot" && shouldAnimateSnapShotArrival(source.capturedAt);
177 return (
178 <SnapShotAttachmentFrame
179 key={image.id}
180 data-chat-composer-expanded-image="true"
181 aria-hidden={snapShotAnimationPending || undefined}
182 inert={snapShotAnimationPending || undefined}
183 arrival={snapShotArrival}
184 animateArrival={snapShotAnimations && !snapShotAnimationPending && snapShotArrival}
185 animationId={snapShotAnimationPending ? image.id : undefined}
186 animationSource={snapShotAnimationPending ? source : undefined}
187 className={cn(
188 "group/attachment shrink-0 snap-start bg-background",
189 source?.kind === "snap-shot"
190 ? SNAP_SHOT_ATTACHMENT_FRAME_CLASS
191 : "relative h-16 w-16 overflow-hidden rounded-lg border border-border/80",
192 snapShotAnimationPending && "invisible",
193 )}
194 >
195 {!ready || ready.previewUrl ? (
196 <button
197 type="button"
198 className={cn("h-full w-full", ready && "cursor-zoom-in")}
199 aria-label={`Preview ${image.name}`}
200 disabled={!ready}
201 onClick={() => {
202 const preview = buildExpandedImagePreview(images, image.id);
203 if (!preview) return;
204 onExpandImage(preview);
205 }}
206 >
207 <ComposerImageThumbnail
208 file={image.file}
209 alt={image.name}
210 className="h-full w-full object-cover"
211 fallback={
212 ready && isHeicImageFile({ name: ready.name, type: ready.mimeType }) ? (
213 <AttachmentImage
214 name={ready.name}
215 mimeType={ready.mimeType}
216 src={ready.previewUrl}
217 alt={image.name}
218 className="h-full w-full object-cover"
219 />
220 ) : (
221 <span className="flex h-full items-center justify-center px-1 text-3xs text-secondary-label">
222 {image.name}
223 </span>
224 )
225 }
226 />
227 </button>
228 ) : (
229 <div className="flex h-full w-full items-center justify-center px-1 text-center text-3xs text-secondary-label">
230 {image.name}
231 </div>
232 )}
233 {source?.kind === "snap-shot" ? (
234 <SnapShotAttachmentDetails
235 source={source}
236 className={cn(
237 upload?.status === "uploading" && "bottom-4",
238 upload?.status === "failed" && "bottom-8",
239 )}
240 />
241 ) : null}
242 {nonPersistedImageIdSet.has(image.id) && (
243 <Tooltip>
244 <TooltipTrigger
245 render={
246 <span
247 role="img"
248 aria-label="Draft attachment may not persist"
249 className="absolute left-1 top-1 inline-flex items-center justify-center rounded bg-background/85 p-0.5 text-warning-foreground"
250 >
251 <CircleAlertIcon className="size-3" />
252 </span>
253 }
254 />
255 <TooltipPopup side="top">
256 Draft attachment could not be saved locally and may be lost on navigation.
257 </TooltipPopup>
258 </Tooltip>
259 )}
260 {(ready ? upload?.status === "uploading" : supportsAttachmentUploads) && (
261 <span className="pointer-events-none absolute inset-x-0 bottom-0 bg-background/85 px-1 text-center text-3xs text-foreground">
262 {formatAttachmentUploadProgress(
263 upload?.status === "uploading" ? upload.progress : 0,
264 )}
265 </span>
266 )}
267 {ready && upload?.status === "failed" && (
268 <Tooltip>
269 <TooltipTrigger
270 render={
271 <Button
272 variant="overlay"
273 size="icon-xs"
274 className="absolute bottom-1 left-1"
275 onClick={() =>
276 retryAttachmentUpload({
277 environmentId,
278 image: ready,
279 draftTarget: attachmentDraftTarget,
280 })
281 }
282 disabled={!canOperateThread}
283 aria-label={`Retry upload for ${image.name}`}
284 />
285 }
286 >
287 <RefreshIcon />
288 </TooltipTrigger>
289 <TooltipPopup side="top">{upload.reason}</TooltipPopup>
290 </Tooltip>
291 )}
292 <span
293 className={cn(
294 "absolute right-1 top-1 flex",
295 source?.kind === "snap-shot" &&
296 "opacity-0 transition-opacity pointer-coarse:opacity-100 group-hover/attachment:opacity-100 group-focus-within/attachment:opacity-100",
297 )}
298 >
299 <Button
300 variant="media-close"
301 size="icon-xs"
302 onClick={() => onRemoveImage(image.id)}
303 aria-label={`Remove ${image.name}`}
304 >
305 <XIcon />
306 </Button>
307 </span>
308 </SnapShotAttachmentFrame>
309 );
310 })
311 .concat(
312 uncommittedSnapShotIds.map((captureId) => (
313 <SnapShotAttachmentFrame
314 key={captureId}
315 aria-hidden="true"
316 animationId={captureId}
317 animationSource={
318 pendingSnapShotAnimations.find((capture) => capture.id === captureId)?.source
319 }
320 className={cn(
321 SNAP_SHOT_ATTACHMENT_FRAME_CLASS,
322 "invisible shrink-0 snap-start bg-background",
323 )}
324 />
325 )),
326 )}
327 {videos.map((file) => {
328 const fileCanUpload =
329 supportsAttachmentUploads &&
330 maxFileAttachmentBytes !== null &&
331 file.sizeBytes <= maxFileAttachmentBytes;
332 const upload = fileCanUpload ? uploadsByImageId[file.id] : undefined;
333 return (
334 <div
335 key={file.id}
336 className="relative h-16 w-16 shrink-0 snap-start overflow-hidden rounded-lg border border-border/80 bg-black"
337 >
338 <button
339 type="button"
340 className="flex h-full w-full cursor-zoom-in flex-col items-center justify-center gap-1 px-1 text-white"
341 aria-label={`Play ${file.name}`}
342 onClick={() => {
343 if (file.file !== null) {
344 const preview = buildExpandedImagePreview([file], file.id);
345 if (preview) onExpandImage(preview);
346 return;
347 }
348 if (!file.uploadedAttachmentId) return;
349 onFileOpen({ ...file, id: file.uploadedAttachmentId });
350 }}
351 >
352 {file.file && (
353 <>
354 <ComposerVideoThumbnail file={file.file} />
355 <span className="pointer-events-none absolute inset-0 bg-gradient-to-t from-black/70 via-black/10 to-black/10" />
356 </>
357 )}
358 <PlayIcon className="relative z-10 size-4 fill-current drop-shadow-md" />
359 </button>
360 {upload?.status === "uploading" && (
361 <span className="pointer-events-none absolute inset-x-0 bottom-0 bg-background/85 px-1 text-center text-3xs text-foreground">
362 {formatAttachmentUploadProgress(upload.progress)}
363 </span>
364 )}
365 {upload?.status === "failed" && (
366 <Tooltip>
367 <TooltipTrigger
368 render={
369 <Button
370 variant="overlay"
371 size="icon-xs"
372 className="absolute bottom-1 left-1"
373 onClick={() =>
374 retryAttachmentUpload({
375 environmentId,
376 image: file,
377 draftTarget: attachmentDraftTarget,
378 })
379 }
380 disabled={!canOperateThread}
381 aria-label={`Retry upload for ${file.name}`}
382 />
383 }
384 >
385 <RefreshIcon />
386 </TooltipTrigger>
387 <TooltipPopup side="top">{upload.reason}</TooltipPopup>
388 </Tooltip>
389 )}
390 <Button
391 variant="media-close"
392 size="icon-xs"
393 className="absolute right-1 top-1"
394 onClick={() => onRemoveFile(file.id)}
395 aria-label={`Remove ${file.name}`}
396 >
397 <XIcon />
398 </Button>
399 </div>
400 );
401 })}
402 </div>
403 );
404}
405
406/** Non-media file attachments, listed under the media row. */
407export function ComposerAttachmentFileList({
408 files,
409 resolvedTheme,
410 supportsAttachmentUploads,
411 maxFileAttachmentBytes,
412 fileStagingLimit,
413 environmentId,
414 attachmentDraftTarget,
415 canOperateThread,
416 onPreviewFile,
417 onRemoveFile,
418}: {
419 readonly files: ReadonlyArray<ComposerFileAttachment>;
420 readonly resolvedTheme: "light" | "dark";
421 readonly supportsAttachmentUploads: boolean;
422 readonly maxFileAttachmentBytes: number | null;
423 readonly fileStagingLimit: number | null;
424 readonly environmentId: EnvironmentId;
425 readonly attachmentDraftTarget: ScopedThreadRef | DraftId;
426 readonly canOperateThread: boolean;
427 readonly onPreviewFile: (fileId: string) => void;
428 readonly onRemoveFile: (fileId: string) => void;
429}) {
430 const uploadsByImageId = useAttachmentUploadStore((state) => state.uploadsByImageId);
431 return (
432 <div className="mb-3 flex flex-col gap-1">
433 {files.map((file) => {
434 const fileCanUpload =
435 supportsAttachmentUploads &&
436 maxFileAttachmentBytes !== null &&
437 file.sizeBytes <= maxFileAttachmentBytes;
438 const upload = fileCanUpload ? uploadsByImageId[file.id] : undefined;
439 const needsReattach = composerFileNeedsReattach(file);
440 const canReattachFile = fileStagingLimit !== null && file.sizeBytes <= fileStagingLimit;
441 return (
442 <div
443 key={file.id}
444 className="flex min-w-0 items-center gap-2 py-1 text-sm text-foreground"
445 >
446 <PierreEntryIcon pathValue={file.name} kind="file" theme={resolvedTheme} />
447 <button
448 type="button"
449 disabled={needsReattach}
450 className="min-w-0 flex-1 truncate text-left hover:underline focus-visible:outline-2"
451 onClick={() => onPreviewFile(file.id)}
452 >
453 {file.name}
454 </button>
455 <span className="shrink-0 text-xs text-secondary-label">
456 {needsReattach
457 ? canReattachFile
458 ? "Attach again"
459 : "Remove to send"
460 : upload?.status === "uploading"
461 ? formatAttachmentUploadProgress(upload.progress)
462 : formatAttachmentSize(file.sizeBytes)}
463 </span>
464 {!needsReattach && upload?.status === "failed" ? (
465 <Tooltip>
466 <TooltipTrigger
467 render={
468 <Button
469 variant="ghost"
470 size="icon-xs"
471 onClick={() =>
472 retryAttachmentUpload({
473 environmentId,
474 image: file,
475 draftTarget: attachmentDraftTarget,
476 })
477 }
478 disabled={!canOperateThread}
479 aria-label={`Retry upload for ${file.name}`}
480 />
481 }
482 >
483 <RefreshIcon />
484 </TooltipTrigger>
485 <TooltipPopup side="top">{upload.reason}</TooltipPopup>
486 </Tooltip>
487 ) : null}
488 <Button
489 variant="ghost"
490 size="icon-xs"
491 onClick={() => onRemoveFile(file.id)}
492 aria-label={`Remove ${file.name}`}
493 >
494 <XIcon />
495 </Button>
496 </div>
497 );
498 })}
499 </div>
500 );
501}
502
503export function ComposerFilePreviewDialog({
504 file,
505 onRemove,
506 onPreviewFileIdChange,
507}: {
508 readonly file: ComposerFileAttachment;
509 readonly onRemove: (fileId: string) => void;
510 readonly onPreviewFileIdChange: (fileId: string | null) => void;
511}) {
512 const onClose = () => onPreviewFileIdChange(null);
513 return (
514 <Dialog
515 open
516 onOpenChange={(open) => {
517 if (!open) onClose();
518 }}
519 >
520 <DialogPopup
521 {...composerFloatingLayerProps}
522 className="h-[min(85vh,52rem)] max-w-4xl overflow-hidden"
523 showCloseButton={false}
524 >
525 <DialogTitle className="sr-only">{file.name}</DialogTitle>
526 <AttachmentFilePreview
527 key={file.id}
528 name={file.name}
529 mimeType={file.mimeType}
530 sizeBytes={file.sizeBytes}
531 file={file.file}
532 origin="Draft"
533 {...(file.uploadedAttachmentId && file.uploadEnvironmentId
534 ? {
535 asset: {
536 environmentId: file.uploadEnvironmentId,
537 attachmentId: file.uploadedAttachmentId,
538 },
539 }
540 : {})}
541 onRemove={() => {
542 onRemove(file.id);
543 onClose();
544 }}
545 onClose={onClose}
546 />
547 </DialogPopup>
548 </Dialog>
549 );
550}
551
552/** Thumbnails beside the single-line prompt while the composer rests. */
553export function ComposerRestingImagePreviews({
554 images,
555 allImages,
556 resolvedTheme,
557 onExpandImage,
558 onShowMore,
559}: {
560 readonly images: ReadonlyArray<ComposerImageAttachment>;
561 readonly allImages: ReadonlyArray<ComposerImageAttachment>;
562 readonly resolvedTheme: "light" | "dark";
563 readonly onExpandImage: (preview: ExpandedImagePreview) => void;
564 readonly onShowMore: () => void;
565}) {
566 const counts = getRestingComposerImagePreviewCounts(images.length);
567 const visibleImages = images.slice(0, counts.visibleCount);
568 return (
569 <div data-chat-composer-resting-images="true" className="flex shrink-0 items-center gap-1 ps-1">
570 {visibleImages.map((image) => (
571 <button
572 key={image.id}
573 type="button"
574 className="relative size-7 shrink-0 cursor-zoom-in overflow-hidden rounded-md border border-border/70 bg-muted/60"
575 aria-label={`Preview ${image.name}`}
576 onPointerDown={(event) => event.preventDefault()}
577 onClick={() => {
578 const preview = buildExpandedImagePreview(allImages, image.id);
579 if (preview) onExpandImage(preview);
580 }}
581 >
582 {image.previewUrl ? (
583 <ComposerImageThumbnail
584 file={image.file}
585 alt=""
586 className="size-full object-cover"
587 fallback={
588 isHeicImageFile({ name: image.name, type: image.mimeType }) ? (
589 <AttachmentImage
590 name={image.name}
591 mimeType={image.mimeType}
592 src={image.previewUrl}
593 alt=""
594 className="size-full object-cover"
595 />
596 ) : (
597 <PierreEntryIcon
598 pathValue={image.name}
599 kind="file"
600 theme={resolvedTheme}
601 className="m-auto size-3.5"
602 />
603 )
604 }
605 />
606 ) : (
607 <PierreEntryIcon
608 pathValue={image.name}
609 kind="file"
610 theme={resolvedTheme}
611 className="m-auto size-3.5"
612 />
613 )}
614 </button>
615 ))}
616 {counts.overflowCount > 0 ? (
617 <button
618 type="button"
619 className="flex size-7 shrink-0 cursor-pointer items-center justify-center rounded-md border border-border/70 bg-muted/60 font-medium text-secondary-label text-xs tabular-nums outline-none hover:bg-muted focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring focus-visible:ring-offset-1 focus-visible:ring-offset-background"
620 aria-label={`Show ${String(counts.overflowCount)} more image attachments`}
621 onPointerDown={(event) => event.preventDefault()}
622 onClick={onShowMore}
623 >
624 +{counts.overflowCount}
625 </button>
626 ) : null}
627 </div>
628 );
629}
630
631/** Attachments of the queued message being edited; they are already uploaded. */
632export function ComposerQueuedAttachmentRow({
633 attachments,
634 onRemove,
635}: {
636 readonly attachments: ReadonlyArray<{
637 readonly attachment: ChatAttachment;
638 readonly url: string | null;
639 }>;
640 readonly onRemove: (attachmentId: string) => void;
641}) {
642 return (
643 <div className="mb-3 flex flex-wrap gap-2">
644 {attachments.map(({ attachment, url }) => (
645 <div
646 key={attachment.id}
647 className="relative h-16 w-16 overflow-hidden rounded-lg border border-border/80 bg-background"
648 >
649 {attachment.type === "image" && url ? (
650 <img src={url} alt={attachment.name} className="h-full w-full object-cover" />
651 ) : (
652 <div className="flex h-full w-full items-center justify-center px-1 text-center text-3xs text-secondary-label">
653 {attachment.name}
654 </div>
655 )}
656 <Button
657 variant="media-close"
658 size="icon-xs"
659 className="absolute right-1 top-1"
660 onClick={() => onRemove(attachment.id)}
661 aria-label={`Remove ${attachment.name}`}
662 >
663 <XIcon />
664 </Button>
665 </div>
666 ))}
667 </div>
668 );
669}
670
671export function ComposerAttachButton({
672 addComposerAttachments,
673 focusComposer,
674}: {
675 readonly addComposerAttachments: (files: File[]) => Promise<boolean>;
676 readonly focusComposer: () => void;
677}) {
678 const inputRef = useRef<HTMLInputElement>(null);
679 return (
680 <>
681 <input
682 ref={inputRef}
683 type="file"
684 multiple
685 className="hidden"
686 onChange={(event) => {
687 const files = Array.from(event.currentTarget.files ?? []);
688 event.currentTarget.value = "";
689 // Inserting a chip refocuses the editor after the draft renders;
690 // focusing synchronously here would report the editor's stale text
691 // over the prompt that was just written.
692 void addComposerAttachments(files).then((inserted) => {
693 if (!inserted) focusComposer();
694 });
695 }}
696 />
697 <Tooltip>
698 <TooltipTrigger
699 render={
700 <Button
701 type="button"
702 variant="ghost"
703 size="icon-sm"
704 onPointerDown={(event) => event.preventDefault()}
705 onClick={() => inputRef.current?.click()}
706 aria-label="Attach files"
707 />
708 }
709 >
710 {/* The clip's weight sits in its upper loop; 1px down reads as centered on the row. */}
711 <PaperclipIcon className="translate-y-px" />
712 </TooltipTrigger>
713 <TooltipPopup>Attach files</TooltipPopup>
714 </Tooltip>
715 </>
716 );
717}