"use client" /** * EmailAttachmentPreviewDialog — look at an attachment before sending. * * Uploaded images and PDFs show as they are. A template file with a hosted `url` shows that * copy. Anything else shows a one-page placeholder naming the file, so the sender can still * see what recipients will get. */ import * as React from "react" import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from "../ui/dialog" import { formatFileSize, type EmailAttachmentFile } from "./email-composer-types" const IMAGE = /\.(png|jpe?g|gif|webp|svg)$/i const PDF = /\.pdf$/i export interface EmailAttachmentPreviewDialogProps { file: EmailAttachmentFile | null open: boolean onOpenChange: (open: boolean) => void /** Sender line for the placeholder, e.g. the brand name. */ from?: string /** Layer above a rail (`z-[80]`). */ className?: string overlayClassName?: string } export function EmailAttachmentPreviewDialog({ file, open, onOpenChange, from = "Sender", className, overlayClassName, }: EmailAttachmentPreviewDialogProps) { const isImage = Boolean(file) && (file?.file?.type.startsWith("image/") || IMAGE.test(file?.name ?? "")) // Object URLs are made in an effect, not during render, and released when the file changes. const [source, setSource] = React.useState<{ url: string } | null>(null) React.useEffect(() => { const objectUrl = file?.file ? URL.createObjectURL(file.file) : null const url = objectUrl ?? file?.url ?? null setSource(url ? { url } : null) return () => { if (objectUrl) URL.revokeObjectURL(objectUrl) } }, [file]) const asImage = isImage && source const asPdf = source && !isImage const asPlaceholder = Boolean(file) && !source return ( {file?.name ?? "Attachment"} {file ? `Preview of this attachment${file.size > 0 ? ` · ${formatFileSize(file.size)}` : ""}.` : "Preview"} {asImage ? ( {file?.name ) : null} {asPdf ? (

This browser can’t show the PDF here.{" "} Open it in a new tab .

) : null} {asPlaceholder && file ? (
{from}

{file.name.replace(/\.[^.]+$/, "") || "Attachment"}

File attached to this email.

File name
{file.name}
Size
{file.size > 0 ? formatFileSize(file.size) : "Not available"}
From
{from}

Placeholder preview. Recipients receive the original file.

) : null}
) }