"use client"; import { use, useEffect, useRef, useState } from "react"; import Link from "next/link"; import { getMail, downloadMailAttachment, downloadMailRaw, exportMailPDF, getLabels, getMailLabelIds, type MailDetail, type MailAttachment, type MailLabel, } from "@/lib/api"; import { useAuth } from "@/hooks/useAuth"; import { Navbar } from "@/components/navbar"; import { LabelPicker } from "@/components/LabelPicker"; import { Button } from "@/components/ui/button"; import { Badge } from "@/components/ui/badge"; import { Card, CardContent, CardHeader } from "@/components/ui/card"; import { Separator } from "@/components/ui/separator"; import { Skeleton } from "@/components/ui/skeleton"; import { Alert, AlertDescription } from "@/components/ui/alert"; // ── Helpers ──────────────────────────────────────────────────────────────── function formatBytes(bytes: number): string { if (bytes < 1024) return `${bytes} B`; if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`; return `${(bytes / (1024 * 1024)).toFixed(1)} MB`; } function formatDate(iso: string): string { try { return new Date(iso).toLocaleString("de-DE", { day: "2-digit", month: "2-digit", year: "numeric", hour: "2-digit", minute: "2-digit", }); } catch { return iso; } } function triggerDownload(blob: Blob, filename: string) { const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = filename; a.click(); URL.revokeObjectURL(url); } function blockExternalSrcs(html: string): string { // Replace src= in img/video/audio tags with data-src= to block loading return html .replace(/<(img|video|audio|source)(\s[^>]*?\s)src(\s*=\s*["']https?:)/gi, "<$1$2data-src$3") .replace(/<(img|video|audio|source)(\s)src(\s*=\s*["']https?:)/gi, "<$1$2data-src$3"); } // ── Sub-components ───────────────────────────────────────────────────────── function MailHeaderGrid({ mail }: { mail: MailDetail }) { const [showRaw, setShowRaw] = useState(false); return (
Von: {mail.from || "–"} An: {mail.to || "–"} {mail.cc && ( <> CC: {mail.cc} )} Datum: {formatDate(mail.date)} Betreff: {mail.subject || "(kein Betreff)"} Größe: {formatBytes(mail.size)} {/* Verification status */} Integrität: {mail.verify_ok === true ? ( Verifiziert ) : mail.verify_ok === false ? ( Manipuliert! ) : ( Noch nicht geprüft )}
{showRaw && (
          {mail.raw_headers}
        
)}
); } function MailBodyView({ mail }: { mail: MailDetail }) { const iframeRef = useRef(null); const [showExternal, setShowExternal] = useState(false); const html = mail.body_html ?? null; const plain = mail.body_plain ?? null; // Adjust iframe height to content function handleIframeLoad() { const iframe = iframeRef.current; if (!iframe) return; try { const body = iframe.contentDocument?.body; if (body) { iframe.style.height = `${body.scrollHeight + 32}px`; } } catch { iframe.style.height = "600px"; } } if (!html && !plain) { return (
Kein Inhalt vorhanden.
); } if (html) { const srcdoc = showExternal ? html : blockExternalSrcs(html); return (
{!showExternal && ( Externe Inhalte (Bilder, Tracker) sind blockiert. )}