Open History to see the last commit
1import type { ChatAttachment } from "@t3tools/contracts";
2import { formatAttachmentSize } from "@t3tools/client-runtime/state/attachments";
3
4import { useTheme } from "~/hooks/useTheme";
5import { cn } from "~/lib/utils";
6import { type ChatFileAttachment, isFileAttachment, isImageAttachment } from "../../types";
7import { AttachmentImage } from "../media/AttachmentImage";
8import { PierreEntryIcon } from "./PierreEntryIcon";
9
10const IMAGE_FRAME =
11 "aspect-[4/3] overflow-hidden rounded-lg border border-border/80 bg-background/70";
12const FILE_ROW = "flex min-w-0 items-center gap-2 py-1 text-sm";
13
14/**
15 * A guest message's files, drawn like a sent message's: image thumbnails, then file rows. The
16 * owner opens them in the app; the guest loads them from the guest route.
17 */
18export function GuestMessageAttachments(props: {
19 readonly attachments: ReadonlyArray<ChatAttachment>;
20 /** Where an image loads from; undefined while its URL is not ready. */
21 readonly imageUrl: (attachment: ChatAttachment) => string | undefined;
22 readonly onOpenImage: (attachment: ChatAttachment) => void;
23 /** A file row as a link, or a button that calls `onOpenFile`. */
24 readonly fileHref?: (attachment: ChatFileAttachment) => string;
25 readonly onOpenFile?: (attachment: ChatFileAttachment) => void;
26}) {
27 const { resolvedTheme } = useTheme();
28 const images = props.attachments.filter(isImageAttachment);
29 const files = props.attachments.filter(isFileAttachment);
30 if (images.length === 0 && files.length === 0) return null;
31 return (
32 <div className="flex flex-col items-end gap-2">
33 {images.length > 0 ? (
34 <div className="grid w-[210px] grid-cols-2 gap-2">
35 {images.map((image) => {
36 const src = props.imageUrl(image);
37 return (
38 <div key={image.id} className={IMAGE_FRAME}>
39 {src ? (
40 <button
41 type="button"
42 className="block size-full cursor-zoom-in"
43 aria-label={`Preview ${image.name}`}
44 onClick={() => props.onOpenImage(image)}
45 >
46 <AttachmentImage
47 name={image.name}
48 mimeType={image.mimeType}
49 src={src}
50 alt={image.name}
51 decoding="async"
52 className="block size-full object-cover"
53 />
54 </button>
55 ) : (
56 <div className="flex size-full items-center justify-center px-2 text-center text-2xs text-muted-foreground/70">
57 {image.name}
58 </div>
59 )}
60 </div>
61 );
62 })}
63 </div>
64 ) : null}
65 {files.map((file) => {
66 const identity = (
67 <>
68 <PierreEntryIcon pathValue={file.name} kind="file" theme={resolvedTheme} />
69 <span className="min-w-0 truncate">{file.name}</span>
70 <span className="shrink-0 text-xs text-muted-foreground">
71 {formatAttachmentSize(file.sizeBytes)}
72 </span>
73 </>
74 );
75 return props.fileHref ? (
76 <a
77 key={file.id}
78 href={props.fileHref(file)}
79 download={file.name}
80 className={cn(FILE_ROW, "hover:underline")}
81 >
82 {identity}
83 </a>
84 ) : props.onOpenFile ? (
85 <button
86 key={file.id}
87 type="button"
88 aria-label={`Preview ${file.name}`}
89 onClick={() => props.onOpenFile?.(file)}
90 className={cn(FILE_ROW, "cursor-pointer hover:underline")}
91 >
92 {identity}
93 </button>
94 ) : (
95 <div key={file.id} className={FILE_ROW}>
96 {identity}
97 </div>
98 );
99 })}
100 </div>
101 );
102}