import React, { useState, useRef } from "react"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogClose } from "@/components/ui/dialog"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@/components/ui/dropdown-menu"; import { Button } from "@/components/ui/button"; import { ScrollArea } from "@/components/ui/scroll-area"; import { Share, Download, X, Maximize2, MoreHorizontal, FileText, FileType, File, CloudUpload, Check, Copy } from "lucide-react"; import { jsPDF } from "jspdf"; import { Document, Packer, Paragraph, TextRun, HeadingLevel } from "docx"; import { saveAs } from "file-saver"; interface ResultViewerProps { isOpen: boolean; onClose: () => void; title: string; recipient?: string; sender?: string; date?: string; content: string; } function parseMarkdownContent(content: string) { const lines = content.split('\n'); const sections: { type: 'heading' | 'paragraph' | 'list'; level?: number; text: string }[] = []; lines.forEach(line => { const headingMatch = line.match(/^(#{1,6})\s+(.+)$/); if (headingMatch) { sections.push({ type: 'heading', level: headingMatch[1].length, text: headingMatch[2] }); } else if (line.startsWith('- ') || line.startsWith('* ')) { sections.push({ type: 'list', text: line.substring(2) }); } else if (line.trim()) { sections.push({ type: 'paragraph', text: line }); } }); return sections; } function RenderContent({ content }: { content: string }) { const sections = parseMarkdownContent(content); const renderHeading = (level: number, text: string, key: number) => { const baseClass = "text-[#1f334d]"; switch (level) { case 1: return

{text}

; case 2: return

{text}

; case 3: return

{text}

; case 4: return

{text}

; default: return
{text}
; } }; const elements: React.ReactNode[] = []; let currentListItems: { text: string; idx: number }[] = []; const flushList = () => { if (currentListItems.length > 0) { elements.push( ); currentListItems = []; } }; sections.forEach((section, idx) => { if (section.type === 'list') { currentListItems.push({ text: section.text, idx }); } else { flushList(); if (section.type === 'heading') { elements.push(renderHeading(section.level || 1, section.text, idx)); } else { elements.push(

{section.text}

); } } }); flushList(); return (
{elements}
); } export function ResultViewer({ isOpen, onClose, title, recipient, sender, date, content }: ResultViewerProps) { const [copied, setCopied] = useState(false); const contentRef = useRef(null); const currentDate = date || new Date().toLocaleDateString('pt-BR', { day: 'numeric', month: 'long', year: 'numeric' }); const exportToMarkdown = () => { let md = `# ${title}\n\n`; if (recipient) md += `**Para:** ${recipient}\n\n`; if (sender) md += `**De:** ${sender}\n\n`; md += `**Data:** ${currentDate}\n\n---\n\n`; md += content; const blob = new Blob([md], { type: "text/markdown;charset=utf-8" }); saveAs(blob, `${title.replace(/\s+/g, '-').toLowerCase()}.md`); }; const exportToPDF = () => { const doc = new jsPDF(); const pageWidth = doc.internal.pageSize.getWidth(); const margin = 20; const maxWidth = pageWidth - margin * 2; let currentY = 20; doc.setFontSize(18); doc.setTextColor(31, 51, 77); const titleLines = doc.splitTextToSize(title, maxWidth); doc.text(titleLines, margin, currentY); currentY += titleLines.length * 8 + 10; doc.setFontSize(10); doc.setTextColor(90, 108, 125); if (recipient) { doc.text(`Para: ${recipient}`, margin, currentY); currentY += 6; } if (sender) { doc.text(`De: ${sender}`, margin, currentY); currentY += 6; } doc.text(`Data: ${currentDate}`, margin, currentY); currentY += 10; doc.setDrawColor(200, 200, 200); doc.line(margin, currentY, pageWidth - margin, currentY); currentY += 10; doc.setFontSize(11); doc.setTextColor(31, 51, 77); const cleanContent = content.replace(/^#{1,6}\s+/gm, ''); const contentLines = doc.splitTextToSize(cleanContent, maxWidth); contentLines.forEach((line: string) => { if (currentY > doc.internal.pageSize.getHeight() - margin) { doc.addPage(); currentY = margin; } doc.text(line, margin, currentY); currentY += 6; }); doc.save(`${title.replace(/\s+/g, '-').toLowerCase()}.pdf`); }; const exportToDocx = async () => { const paragraphs = []; paragraphs.push( new Paragraph({ children: [new TextRun({ text: title, bold: true, size: 36 })], heading: HeadingLevel.HEADING_1, }) ); paragraphs.push(new Paragraph({ children: [] })); if (recipient) { paragraphs.push( new Paragraph({ children: [ new TextRun({ text: "Para: ", bold: true }), new TextRun({ text: recipient }) ] }) ); } if (sender) { paragraphs.push( new Paragraph({ children: [ new TextRun({ text: "De: ", bold: true }), new TextRun({ text: sender }) ] }) ); } paragraphs.push( new Paragraph({ children: [ new TextRun({ text: "Data: ", bold: true }), new TextRun({ text: currentDate }) ] }) ); paragraphs.push(new Paragraph({ children: [] })); const sections = parseMarkdownContent(content); sections.forEach(section => { if (section.type === 'heading') { const level = section.level || 3; const getHeadingLevel = (l: number) => { if (l === 1) return HeadingLevel.HEADING_1; if (l === 2) return HeadingLevel.HEADING_2; return HeadingLevel.HEADING_3; }; paragraphs.push( new Paragraph({ children: [new TextRun({ text: section.text, bold: true })], heading: getHeadingLevel(level), }) ); } else { paragraphs.push( new Paragraph({ children: [new TextRun({ text: section.text })] }) ); } }); const doc = new Document({ sections: [{ properties: {}, children: paragraphs }], }); const blob = await Packer.toBlob(doc); saveAs(blob, `${title.replace(/\s+/g, '-').toLowerCase()}.docx`); }; const copyToClipboard = async () => { await navigator.clipboard.writeText(content); setCopied(true); setTimeout(() => setCopied(false), 2000); }; return (
{title}

Última modificação: Há pouco

Markdown PDF Docx Salvar no Google Drive Salvar no OneDrive (pessoal) Salvar no OneDrive (trabalho/escola)
{(recipient || sender) && (
{recipient &&

Para: {recipient}

} {sender &&

De: {sender}

}

Data: {currentDate}

)}
); } export default ResultViewer;