| 1 | 'use client' |
| 2 | |
| 3 | import { pdf } from '@react-pdf/renderer' |
| 4 | import { Download, FileText |
| 5 | import { useEffect, useRef |
| 6 | import { Document, Page |
| 7 | import 'react-pdf/dist/Page/AnnotationLayer.css' |
| 8 | import 'react-pdf/dist/Page/TextLayer.css' |
| 9 | |
| 10 | import { Button } |
| 11 | import type { InvoicePDFData |
| 12 | import InvoicePDF from './invoice-pdf |
| 13 | |
| 14 | pdfjs.GlobalWorkerOptions.workerSrc |
| 15 | |
| 16 | interface InvoicePreviewProps { |
| 17 | data: InvoicePDFData |
| 18 | } |
| 19 | |
| 20 | export default function InvoicePreview |
| 21 | const [blobUrl, |
| 22 | const [loading, |
| 23 | const [error, |
| 24 | const timerRef = |
| 25 | const dataRef = |
| 26 | dataRef.current = |
| 27 | |
| 28 | const generate = |
| 29 | setLoading(true) |
| 30 | setError(null) |
| 31 | try { |
| 32 | const blob = |
| 33 | setBlobUrl((prev) |
| 34 | if (prev) { |
| 35 | URL.revokeObjectURL(prev |
| 36 | } |
| 37 | return URL.createObjectURL |
| 38 | }) |
| 39 | } catch (err) |
| 40 | console.error('PDF generation failed: |
| 41 | setError(err instanceof |
| 42 | } finally { |
| 43 | setLoading(false) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // biome-ignore lint/correctness/useExhaustiveDependencies: generate not needed here |
| 48 | useEffect(() => |
| 49 | if (timerRef.current |
| 50 | clearTimeout(timerRef.current |
| 51 | } |
| 52 | timerRef.current = |
| 53 | generate() |
| 54 | }, 1500) |
| 55 | |
| 56 | return () => |
| 57 | if (timerRef.current |
| 58 | clearTimeout(timerRef.current |
| 59 | } |
| 60 | } |
| 61 | }, [JSON.stringify |
| 62 | |
| 63 | // Cleanup blob URL on unmount |
| 64 | useEffect( |
| 65 | () => () => |
| 66 | setBlobUrl((prev) |
| 67 | if (prev) { |
| 68 | URL.revokeObjectURL(prev |
| 69 | } |
| 70 | return null |
| 71 | }) |
| 72 | }, |
| 73 | [] |
| 74 | ) |
| 75 | |
| 76 | const handleDownload = |
| 77 | if (!blobUrl) |
| 78 | return |
| 79 | } |
| 80 | const a = |
| 81 | a.href = |
| 82 | a.download = |
| 83 | a.click() |
| 84 | } |
| 85 | |
| 86 | return ( |
| 87 | <div className='rounded-lg border bg-muted/30 |
| 88 | <div className='flex items-center justify-between border-b px-4 py-3 |
| 89 | <h3 className='flex items-center gap-2 font-medium text-sm |
| 90 | <FileText className=' |
| 91 | PDF Preview |
| 92 | </h3> |
| 93 | <div className='flex items-center gap-2 |
| 94 | <Button disabled={ |
| 95 | <RefreshCw |
| 96 | className={`size-3.5 ${loading |
| 97 | /> |
| 98 | </Button> |
| 99 | <Button |
| 100 | disabled={!blobUrl |
| 101 | onClick={handleDownload} |
| 102 | variant='outline' |
| 103 | > |
| 104 | <Download className=' |
| 105 | Download |
| 106 | </Button> |
| 107 | </div> |
| 108 | </div> |
| 109 | <div className='flex items-center justify-center overflow-auto p-4 |
| 110 | {loading ? ( |
| 111 | <div className='flex h-[600px] items-center justify-center |
| 112 | <Loader2 className=' |
| 113 | </div> |
| 114 | ) : error ? |
| 115 | <div className='flex h-[400px] flex-col items-center justify-center gap-3 |
| 116 | <p className='text-muted-foreground text-sm |
| 117 | <Button onClick={ |
| 118 | <RefreshCw className=' |
| 119 | Retry |
| 120 | </Button> |
| 121 | </div> |
| 122 | ) : blobUrl ? |
| 123 | <Document error={ |
| 124 | <Page |
| 125 | pageNumber={1} |
| 126 | renderAnnotationLayer={false} |
| 127 | renderTextLayer={false} |
| 128 | width={520} |
| 129 | /> |
| 130 | </Document> |
| 131 | ) : ( |
| 132 | <div className='flex h-[400px] items-center justify-center |
| 133 | <p className='text-muted-foreground text-sm |
| 134 | Preview will appear here |
| 135 | </p> |
| 136 | </div> |
| 137 | )} |
| 138 | </div> |
| 139 | </div> |
| 140 | ) |
| 141 | } |