"use client"; import React, { useEffect, useState } from "react"; import { Dialog, DialogContent, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Button } from "@/components/ui/button"; import { Badge } from "@/components/ui/badge"; import { X, Paperclip, Reply, Forward, Loader2, AlertCircle, } from "lucide-react"; import { MailDetail, getMailDetail, markMailAsRead } from "@/lib/api/mail"; import DOMPurify from "isomorphic-dompurify"; interface MailDetailModalProps { isOpen: boolean; onClose: () => void; accountId: string; mailId: string; // "accountId-seqno" 형식 onMailRead?: () => void; // 읽음 처리 후 목록 갱신용 } export default function MailDetailModal({ isOpen, onClose, accountId, mailId, onMailRead, }: MailDetailModalProps) { const [mail, setMail] = useState(null); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [showHtml, setShowHtml] = useState(true); // HTML/텍스트 토글 useEffect(() => { if (isOpen && mailId) { loadMailDetail(); } }, [isOpen, mailId]); const loadMailDetail = async () => { setLoading(true); setError(null); try { // mailId에서 seqno 추출 (예: "account123-45" -> 45) const seqno = parseInt(mailId.split("-").pop() || "0", 10); if (isNaN(seqno)) { throw new Error("유효하지 않은 메일 ID입니다."); } // 메일 상세 조회 const mailDetail = await getMailDetail(accountId, seqno); setMail(mailDetail); // 읽음 처리 if (!mailDetail.isRead) { await markMailAsRead(accountId, seqno); onMailRead?.(); // 목록 갱신 } } catch (err) { console.error("메일 상세 조회 실패:", err); setError( err instanceof Error ? err.message : "메일을 불러오는데 실패했습니다." ); } finally { setLoading(false); } }; const formatDate = (dateString: string) => { const date = new Date(dateString); return date.toLocaleString("ko-KR", { year: "numeric", month: "long", day: "numeric", hour: "2-digit", minute: "2-digit", }); }; const formatFileSize = (bytes: number) => { if (bytes < 1024) return `${bytes} B`; if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`; return `${(bytes / (1024 * 1024)).toFixed(1)} MB`; }; const sanitizeHtml = (html: string) => { return DOMPurify.sanitize(html, { ALLOWED_TAGS: [ "p", "br", "strong", "em", "u", "a", "ul", "ol", "li", "h1", "h2", "h3", "h4", "h5", "h6", "img", "div", "span", "table", "tr", "td", "th", "thead", "tbody", ], ALLOWED_ATTR: ["href", "src", "alt", "title", "style", "class"], }); }; const handleDownloadAttachment = async (index: number, filename: string) => { try { const seqno = parseInt(mailId.split("-").pop() || "0", 10); // 다운로드 URL const downloadUrl = `http://localhost:8080/api/mail/receive/${accountId}/${seqno}/attachment/${index}`; // 다운로드 트리거 const link = document.createElement('a'); link.href = downloadUrl; link.download = filename; document.body.appendChild(link); link.click(); document.body.removeChild(link); } catch (err) { console.error('첨부파일 다운로드 실패:', err); alert('첨부파일 다운로드에 실패했습니다.'); } }; return ( 메일 상세 {loading ? (
메일을 불러오는 중...
) : error ? (

{error}

) : mail ? (
{/* 메일 헤더 */}

{mail.subject}

보낸사람:{" "} {mail.from}
받는사람:{" "} {mail.to}
{mail.cc && (
참조:{" "} {mail.cc}
)}
날짜:{" "} {formatDate(mail.date)}
{/* 첨부파일 */} {mail.attachments && mail.attachments.length > 0 && (
첨부파일 ({mail.attachments.length})
{mail.attachments.map((attachment, index) => (
{attachment.filename} {formatFileSize(attachment.size)}
))}
)} {/* HTML/텍스트 토글 */} {mail.htmlBody && mail.textBody && (
)} {/* 메일 본문 */}
{showHtml && mail.htmlBody ? (
) : (
                  {mail.textBody || "본문 내용이 없습니다."}
                
)}
) : null}
); }