|
| 1 | +import { File } from "@phosphor-icons/react"; |
| 2 | +import { |
| 3 | + SESSION_SERVICE, |
| 4 | + type SessionService, |
| 5 | +} from "@posthog/core/sessions/sessionService"; |
| 6 | +import { useService } from "@posthog/di/react"; |
| 7 | +import { isRasterImageFile, parseImageDataUrl } from "@posthog/shared"; |
| 8 | +import { |
| 9 | + getAuthIdentity, |
| 10 | + useAuthStateValue, |
| 11 | +} from "@posthog/ui/features/auth/store"; |
| 12 | +import { readFileAsDataUrl } from "@posthog/ui/features/message-editor/hostApi"; |
| 13 | +import { MentionChip } from "@posthog/ui/features/sessions/components/session-update/parseFileMentions"; |
| 14 | +import type { UserMessageAttachment } from "@posthog/ui/features/sessions/userMessageTypes"; |
| 15 | +import { useSessionTaskId } from "@posthog/ui/features/sessions/useSessionTaskId"; |
| 16 | +import { SafeImagePreview } from "@posthog/ui/primitives/SafeImagePreview"; |
| 17 | +import { Dialog, Text } from "@radix-ui/themes"; |
| 18 | +import { useQuery } from "@tanstack/react-query"; |
| 19 | + |
| 20 | +function attachmentFilePath(id: string): string | null { |
| 21 | + if (id.startsWith("/") || /^[A-Za-z]:[\\/]/.test(id)) return id; |
| 22 | + if (!id.startsWith("file://")) return null; |
| 23 | + |
| 24 | + try { |
| 25 | + return decodeURIComponent(new URL(id).pathname); |
| 26 | + } catch { |
| 27 | + return null; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +function ImageAttachment({ |
| 32 | + attachment, |
| 33 | +}: { |
| 34 | + attachment: UserMessageAttachment; |
| 35 | +}) { |
| 36 | + const filePath = attachmentFilePath(attachment.id); |
| 37 | + const taskId = useSessionTaskId(); |
| 38 | + const authIdentity = useAuthStateValue(getAuthIdentity); |
| 39 | + const sessionService = useService<SessionService>(SESSION_SERVICE); |
| 40 | + const cloudArtifact = attachment.cloudArtifact; |
| 41 | + const { data: previewUrl } = useQuery({ |
| 42 | + queryKey: cloudArtifact |
| 43 | + ? [ |
| 44 | + "cloudArtifactPreview", |
| 45 | + authIdentity, |
| 46 | + taskId, |
| 47 | + cloudArtifact.runId, |
| 48 | + cloudArtifact.artifactId, |
| 49 | + ] |
| 50 | + : ["os", "readFileAsDataUrl", filePath], |
| 51 | + queryFn: () => { |
| 52 | + if (cloudArtifact && taskId) { |
| 53 | + return sessionService.getCloudAttachmentPreviewUrl( |
| 54 | + taskId, |
| 55 | + cloudArtifact.runId, |
| 56 | + cloudArtifact.artifactId, |
| 57 | + ); |
| 58 | + } |
| 59 | + return readFileAsDataUrl({ filePath: filePath ?? "" }); |
| 60 | + }, |
| 61 | + enabled: cloudArtifact |
| 62 | + ? taskId !== null && authIdentity !== null |
| 63 | + : filePath !== null, |
| 64 | + retry: false, |
| 65 | + staleTime: cloudArtifact ? 50 * 60 * 1000 : Infinity, |
| 66 | + }); |
| 67 | + const parsedImage = previewUrl?.startsWith("data:") |
| 68 | + ? parseImageDataUrl(previewUrl) |
| 69 | + : null; |
| 70 | + |
| 71 | + if (!previewUrl) { |
| 72 | + return <MentionChip icon={<File size={12} />} label={attachment.label} />; |
| 73 | + } |
| 74 | + |
| 75 | + return ( |
| 76 | + <Dialog.Root> |
| 77 | + <Dialog.Trigger> |
| 78 | + <button |
| 79 | + type="button" |
| 80 | + className="group relative h-16 w-20 overflow-hidden rounded-md border border-gray-6 bg-gray-3" |
| 81 | + aria-label={`Preview ${attachment.label}`} |
| 82 | + > |
| 83 | + <img |
| 84 | + src={previewUrl} |
| 85 | + alt={attachment.label} |
| 86 | + className="size-full object-cover transition-transform group-hover:scale-105" |
| 87 | + /> |
| 88 | + <span className="absolute inset-x-0 bottom-0 truncate bg-black/60 px-1.5 py-0.5 text-left text-[10px] text-white"> |
| 89 | + {attachment.label} |
| 90 | + </span> |
| 91 | + </button> |
| 92 | + </Dialog.Trigger> |
| 93 | + <Dialog.Content maxWidth="85vw" className="w-fit p-[16px]"> |
| 94 | + <Dialog.Title mb="2" className="text-sm"> |
| 95 | + {attachment.label} |
| 96 | + </Dialog.Title> |
| 97 | + {parsedImage ? ( |
| 98 | + <SafeImagePreview |
| 99 | + base64={parsedImage.base64} |
| 100 | + mimeType={parsedImage.mimeType} |
| 101 | + alt={attachment.label} |
| 102 | + className="max-h-[75vh] max-w-[80vw]" |
| 103 | + /> |
| 104 | + ) : previewUrl.startsWith("data:") ? ( |
| 105 | + <Text color="gray" className="text-sm"> |
| 106 | + Unable to load image preview |
| 107 | + </Text> |
| 108 | + ) : ( |
| 109 | + <img |
| 110 | + src={previewUrl} |
| 111 | + alt={attachment.label} |
| 112 | + className="max-h-[75vh] max-w-[80vw] object-contain" |
| 113 | + /> |
| 114 | + )} |
| 115 | + </Dialog.Content> |
| 116 | + </Dialog.Root> |
| 117 | + ); |
| 118 | +} |
| 119 | + |
| 120 | +export function UserMessageAttachments({ |
| 121 | + attachments, |
| 122 | +}: { |
| 123 | + attachments: UserMessageAttachment[]; |
| 124 | +}) { |
| 125 | + return ( |
| 126 | + <div className="flex flex-wrap items-center gap-1.5"> |
| 127 | + {attachments.map((attachment) => |
| 128 | + isRasterImageFile(attachment.label) ? ( |
| 129 | + <ImageAttachment key={attachment.id} attachment={attachment} /> |
| 130 | + ) : ( |
| 131 | + <MentionChip |
| 132 | + key={attachment.id} |
| 133 | + icon={<File size={12} />} |
| 134 | + label={attachment.label} |
| 135 | + /> |
| 136 | + ), |
| 137 | + )} |
| 138 | + </div> |
| 139 | + ); |
| 140 | +} |
0 commit comments