import { useEffect, type ReactNode } from "react"; import { ArrowBigLeft, ArrowBigLeftDash, ArrowBigRight, ArrowBigRightDash } from "lucide-react"; import { Button, MessageDisplayPanel, type MessageDisplayAttachment } from "@govoplan/core-webui"; // Campaign review/template/mock previews need recipient navigation, review notes, // raw MIME inspection and attachment grouping. Generic mailbox reading uses // core MessageDisplayPanel instead. export type CampaignMessagePreviewAttachment = MessageDisplayAttachment & { label?: string | null; }; export type CampaignMessagePreviewMetaItem = { label: string; value: ReactNode; }; export type CampaignMessagePreviewNavigation = { index: number; total: number; onFirst: () => void; onPrevious: () => void; onNext: () => void; onLast: () => void; }; export type CampaignMessagePreviewOverlayProps = { title?: string; subject?: string | null; bodyMode?: "text" | "html"; text?: string | null; html?: string | null; recipientLabel?: ReactNode; recipientNote?: ReactNode; metaItems?: CampaignMessagePreviewMetaItem[]; attachments?: CampaignMessagePreviewAttachment[]; raw?: string | null; rawLabel?: string; navigation?: CampaignMessagePreviewNavigation; closeLabel?: string; onClose: () => void; }; export default function CampaignMessagePreviewOverlay({ title = "i18n:govoplan-campaign.message_preview.58de1450", subject, bodyMode = "text", text, html, recipientLabel, recipientNote, metaItems = [], attachments = [], raw, rawLabel = "i18n:govoplan-campaign.raw_mime.82c612d4", navigation, closeLabel = "i18n:govoplan-campaign.close.bbfa773e", onClose }: CampaignMessagePreviewOverlayProps) { const shownSubject = subject?.trim() || "i18n:govoplan-campaign.no_subject.7b4e8035"; const fields = metaItems.map((item) => ({ label: item.label, value: item.value })); useEffect(() => { const handleKeyDown = (event: KeyboardEvent) => { if (isEditableTarget(event.target)) return; if (event.key === "Escape") { event.preventDefault(); onClose(); return; } if (!navigation) return; if (event.key === "ArrowLeft") { event.preventDefault(); if (navigation.index > 0) navigation.onPrevious(); } else if (event.key === "ArrowRight") { event.preventDefault(); if (navigation.index < navigation.total - 1) navigation.onNext(); } else if (event.key === "Home") { event.preventDefault(); if (navigation.index > 0) navigation.onFirst(); } else if (event.key === "End") { event.preventDefault(); if (navigation.index < navigation.total - 1) navigation.onLast(); } }; window.addEventListener("keydown", handleKeyDown); return () => window.removeEventListener("keydown", handleKeyDown); }, [navigation, onClose]); return (

{title}

{(recipientLabel || recipientNote || navigation) &&
{recipientLabel && {recipientLabel}} {recipientNote &&

{recipientNote}

}
{navigation &&
{navigation.index + 1} / {navigation.total}
}
} {raw &&
{rawLabel}
{raw}
}
); } function isEditableTarget(target: EventTarget | null): boolean { if (!(target instanceof HTMLElement)) return false; return target.isContentEditable || ["INPUT", "SELECT", "TEXTAREA"].includes(target.tagName); } export type MessagePreviewAttachment = CampaignMessagePreviewAttachment; export type MessagePreviewMetaItem = CampaignMessagePreviewMetaItem; export type MessagePreviewNavigation = CampaignMessagePreviewNavigation; export type MessagePreviewOverlayProps = CampaignMessagePreviewOverlayProps;