import { createFileRoute, useParams } from '@tanstack/react-router' import { FileText, Download, RefreshCcw, ExternalLink, CheckCircle2, Clock, FileJson, } from 'lucide-react' import { useCallback, useEffect, useRef, useState } from 'react' import { Button } from '@/components/ui/button' import { Card, CardContent } from '@/components/ui/card' import { usePlan } from '@/data' import { fetchPlanPdf } from '@/data/api/document.api' export const Route = createFileRoute('/planes/$planId/_detalle/documento')({ component: RouteComponent, }) function RouteComponent() { const { planId } = useParams({ from: '/planes/$planId/_detalle/documento' }) const { data: plan } = usePlan(planId) const [pdfUrl, setPdfUrl] = useState(null) const pdfUrlRef = useRef(null) const isMountedRef = useRef(false) const [isLoading, setIsLoading] = useState(true) const planFileBaseName = sanitizeFileBaseName(plan?.nombre ?? 'plan_estudios') const loadPdfPreview = useCallback(async () => { try { if (isMountedRef.current) setIsLoading(true) const pdfBlob = await fetchPlanPdf({ plan_estudio_id: planId, convertTo: 'pdf', }) if (!isMountedRef.current) return const url = window.URL.createObjectURL(pdfBlob) if (pdfUrlRef.current) window.URL.revokeObjectURL(pdfUrlRef.current) pdfUrlRef.current = url setPdfUrl(url) } catch (error) { console.error('Error cargando preview:', error) } finally { if (isMountedRef.current) setIsLoading(false) } }, [planId]) useEffect(() => { isMountedRef.current = true loadPdfPreview() return () => { isMountedRef.current = false if (pdfUrlRef.current) window.URL.revokeObjectURL(pdfUrlRef.current) } }, [loadPdfPreview]) const handleDownloadPdf = async () => { try { const pdfBlob = await fetchPlanPdf({ plan_estudio_id: planId, convertTo: 'pdf', }) const url = window.URL.createObjectURL(pdfBlob) const link = document.createElement('a') link.href = url link.download = `${planFileBaseName}.pdf` document.body.appendChild(link) link.click() link.remove() window.URL.revokeObjectURL(url) } catch (error) { console.error(error) alert('No se pudo generar el PDF') } } const handleDownloadWord = async () => { try { const docBlob = await fetchPlanPdf({ plan_estudio_id: planId, }) const url = window.URL.createObjectURL(docBlob) const link = document.createElement('a') link.href = url link.download = `${planFileBaseName}.docx` document.body.appendChild(link) link.click() link.remove() setTimeout(() => window.URL.revokeObjectURL(url), 1000) } catch (error) { console.error(error) alert('No se pudo generar el Word') } } return (
{/* HEADER DE ACCIONES */}

Documento del Plan

Vista previa y descarga del documento oficial

{/* TARJETAS DE ESTADO */}
} label="Estado" value="Generado" /> } label="Última generación" value="28 Ene 2024, 11:30" /> } label="Versión" value="v1.2" />
{/* CONTENEDOR DEL DOCUMENTO (Visor) */} {/* CONTENEDOR DEL VISOR REAL */}
Preview_Documento.pdf
{pdfUrl && ( )}
{isLoading ? (

Generando vista previa del PDF...

) : pdfUrl ? ( /* 3. VISOR DE PDF REAL */