import React, { useState } from "react"; import { FileText, FileSpreadsheet, Presentation, Download, Sparkles, Check, AlertCircle, Lock } from "lucide-react"; import type { DocType } from "../utils/documentGenerator"; interface InteractiveDocumentCardProps { docData: { type: DocType; title: string; subtitle?: string; headers?: string[]; rows?: Array>; sections?: Array<{ heading?: string; body: string | string[] }>; slides?: Array<{ title: string; bullets?: string[]; content?: string; keyTakeaway?: string }>; }; currentUserTier?: "guest" | "free" | "pro" | "tiniagents"; onUpgradeClick?: () => void; } export const InteractiveDocumentCard: React.FC = ({ docData, currentUserTier = "free", onUpgradeClick }) => { const [isGenerating, setIsGenerating] = useState(false); const [downloadSuccess, setDownloadSuccess] = useState(false); const [errorMsg, setErrorMsg] = useState(null); const docType = docData.type || "docx"; const title = docData.title || "Generated Document"; const slidesCount = docData.slides?.length || 1; // Document creation is currently available to every tier. const perm: { allowed: boolean; maxSlidesAllowed: number; reason?: string } = { allowed: true, maxSlidesAllowed: 20 }; const getDocIcon = () => { switch (docType) { case "xlsx": return ; case "pdf": return ; case "pptx": return ; case "docx": default: return ; } }; const getDocBadge = () => { switch (docType) { case "xlsx": return { label: "Excel Spreadsheet (.xlsx)", color: "bg-emerald-500/10 text-emerald-600 dark:text-emerald-400 border-emerald-500/30" }; case "pdf": return { label: "PDF Document (.pdf)", color: "bg-rose-500/10 text-rose-600 dark:text-rose-400 border-rose-500/30" }; case "pptx": return { label: `PowerPoint (${slidesCount} Slides)`, color: "bg-amber-500/10 text-amber-600 dark:text-amber-400 border-amber-500/30" }; case "docx": default: return { label: "Word Document (.docx)", color: "bg-indigo-500/10 text-indigo-600 dark:text-indigo-400 border-indigo-500/30" }; } }; const badge = getDocBadge(); const handleDownload = async () => { setErrorMsg(null); if (!perm.allowed) { setErrorMsg(perm.reason || "This document format is restricted for your active subscription tier."); return; } try { setIsGenerating(true); const { createWordDocument, createExcelSpreadsheet, createPdfDocument, createPptxPresentation, triggerFileDownload } = await import("../utils/documentGenerator"); let blob: Blob; let defaultFileName = `${title.toLowerCase().replace(/[^a-z0-9]/g, "_")}.${docType}`; if (docType === "docx") { blob = await createWordDocument({ title, subtitle: docData.subtitle, sections: docData.sections || [{ heading: "Summary", body: title }] }); } else if (docType === "xlsx") { blob = createExcelSpreadsheet({ fileName: title, headers: docData.headers || ["Item", "Description", "Value"], rows: docData.rows || [] }); } else if (docType === "pdf") { blob = createPdfDocument({ title, subtitle: docData.subtitle, content: (docData.sections || []).map((s) => ({ heading: s.heading, text: Array.isArray(s.body) ? s.body.join("\n\n") : s.body })) }); } else if (docType === "pptx") { const maxSlides = perm.maxSlidesAllowed || 10; blob = await createPptxPresentation( { title, subtitle: docData.subtitle, slides: docData.slides || [] }, maxSlides ); } else { throw new Error("Unsupported document type"); } triggerFileDownload(blob, defaultFileName); setDownloadSuccess(true); setTimeout(() => setDownloadSuccess(false), 4000); // Track document generation usage in background try { const currentUsername = (window as any)?.__AURA_CURRENT_USER__?.username || localStorage.getItem("aura_user_name") || ""; fetch("/api/subscriptions/track-usage", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ username: currentUsername, type: "documents", count: 1 }) }).catch(() => {}); } catch (e) {} } catch (err: any) { console.error("Failed to generate document:", err); setErrorMsg(err.message || "An error occurred while generating the document."); } finally { setIsGenerating(false); } }; return (
{getDocIcon()}
{badge.label} 100% FREE

{title}

{docData.subtitle && (

{docData.subtitle}

)}
{/* Action Button */}
{/* Error / Restriction Warning */} {errorMsg && (
{errorMsg}
)}
); };