import { useEffect, useMemo, useState, type ReactNode } from "react"; import { Archive, LockKeyhole, Paperclip } from "lucide-react"; import { i18nMessage, usePlatformLanguage } from "../i18n/LanguageContext"; import SegmentedControl from "./SegmentedControl"; export type MessageDisplayField = { label: string; value?: ReactNode | null; }; export type MessageDisplayAttachment = { id?: string | null; filename?: string | null; contentType?: string | null; sizeBytes?: number | null; detail?: string | null; archiveGroup?: string | null; archiveLabel?: string | null; protected?: boolean | null; protectionNote?: string | null; }; type MessageDisplayBodyMode = "text" | "html"; type MessageDisplayPanelProps = { title?: string | null; fields?: MessageDisplayField[]; bodyText?: string | null; bodyHtml?: string | null; bodyPreview?: string | null; preferredBodyMode?: MessageDisplayBodyMode; deriveTextFromHtml?: boolean; headers?: Record; attachments?: MessageDisplayAttachment[]; emptyText?: string; }; export default function MessageDisplayPanel({ title, fields = [], bodyText, bodyHtml, bodyPreview, preferredBodyMode, deriveTextFromHtml = true, headers = {}, attachments = [], emptyText = "i18n:govoplan-core.select_an_item_to_inspect_its_content.1f67f131" }: MessageDisplayPanelProps) { const { translateText } = usePlatformLanguage(); const visibleFields = fields.filter((field) => hasRenderableValue(field.value)); const headerEntries = Object.entries(headers); const hasHtml = Boolean(bodyHtml?.trim()); const explicitTextBody = bodyText?.trim() || bodyPreview?.trim() || ""; const textBody = explicitTextBody || (deriveTextFromHtml && hasHtml ? stripHtml(bodyHtml || "") : ""); const hasText = Boolean(textBody.trim()); const defaultBodyMode = preferredBodyMode === "html" && hasHtml ? "html" : preferredBodyMode === "text" && hasText ? "text" : hasHtml ? "html" : "text"; const [bodyMode, setBodyMode] = useState(defaultBodyMode); const groupedAttachments = useMemo(() => groupAttachments(attachments), [attachments]); useEffect(() => { setBodyMode(defaultBodyMode); }, [defaultBodyMode, title, bodyText, bodyHtml, bodyPreview]); if (!title && visibleFields.length === 0 && !hasText && !hasHtml && attachments.length === 0 && headerEntries.length === 0) { return

{translateText(emptyText)}

; } const activeBodyMode = bodyMode === "html" && hasHtml ? "html" : "text"; const showBodySwitch = hasHtml && hasText; const htmlBodyFallback = translateText("i18n:govoplan-core.p_no_html_body_content_p.c305bfc6"); const textBodyFallback = translateText("i18n:govoplan-core.no_readable_body_content.37643a01"); return (

{title || "i18n:govoplan-core.no_subject.49b20da0"}

{visibleFields.length > 0 &&
{visibleFields.map((field) =>
{field.label}
{field.value}
)}
}

i18n:govoplan-core.message.68f4145f

{showBodySwitch && }
{activeBodyMode === "html" ?