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