'use client'; import { createContext, useContext, useRef, ReactNode } from 'react'; interface PdfContextType { downloadPdf: () => Promise; setPdfData: (data: ArrayBuffer | null, fileName: string) => void; } const PdfContext = createContext(null); export function PdfProvider({ children }: { children: ReactNode }) { const pdfDataRef = useRef(null); const fileNameRef = useRef(''); const setPdfData = (data: ArrayBuffer | null, fileName: string) => { pdfDataRef.current = data; fileNameRef.current = fileName; }; const downloadPdf = async () => { if (!pdfDataRef.current) { console.log('No PDF data available'); return; } try { const blob = new Blob([pdfDataRef.current], { type: 'application/pdf' }); const url = URL.createObjectURL(blob); const link = document.createElement('a'); link.href = url; link.download = fileNameRef.current || 'document.pdf'; document.body.appendChild(link); link.click(); document.body.removeChild(link); URL.revokeObjectURL(url); } catch (err) { console.log('Error downloading PDF:', err); } }; return ( {children} ); } export function usePdf() { const context = useContext(PdfContext); if (!context) { throw new Error('usePdf must be used within PdfProvider'); } return context; }