11"use client" ;
22
33import { X , ExternalLink , Download } from "lucide-react" ;
4- import PDFViewer from "./PDFViewer" ;
54
65interface FilePreviewModalProps {
76 isOpen : boolean ;
@@ -14,26 +13,25 @@ interface FilePreviewModalProps {
1413export default function FilePreviewModal ( { isOpen, onClose, fileUrl, fileName, fileType } : FilePreviewModalProps ) {
1514 if ( ! isOpen ) return null ;
1615
17- const safeFileType = fileType || "" ;
18- const safeFileName = fileName || "" ;
16+ // 1. Robust File Type Detection
17+ // Prefer extension from URL as it's the most reliable source of truth
18+ const urlExtension = fileUrl . split ( '.' ) . pop ( ) ?. toLowerCase ( ) || "" ;
19+ const titleExtension = fileName . split ( '.' ) . pop ( ) ?. toLowerCase ( ) || "" ;
20+ const effectiveExtension = urlExtension . length > 1 && urlExtension . length < 5 ? urlExtension : titleExtension ;
1921
20- const isImage = safeFileType . includes ( "image" ) || safeFileName . match ( / \. ( j p g | j p e g | p n g | g i f | w e b p | s v g | b m p ) $ / i) ;
21- const isVideo = safeFileType . includes ( "video" ) || safeFileName . match ( / \. ( m p 4 | w e b m | o g g | m o v ) $ / i) ;
22- const isPDF = safeFileType . includes ( "pdf" ) || safeFileName . match ( / \. p d f $ / i) ;
22+ // MIME type check as backup
23+ const mimeType = fileType || "" ;
2324
24- // Generic check for any document that isn't media or PDF
25- // Google Viewer supports many formats including .ai, .psd, .dxf, .svg, .eps, .ps, .ttf, .xps, .zip, .rar
26- const isGoogleDocSupported =
27- ! isImage &&
28- ! isVideo &&
29- ! isPDF &&
30- (
31- safeFileType . includes ( "application/" ) ||
32- safeFileType . includes ( "text/" ) ||
33- safeFileName . match ( / \. ( d o c | d o c x | x l s | x l s x | p p t | p p t x | t x t | r t f | c s v | o d t | o d s | o d p | a i | p s d | d x f | e p s | p s | x p s | t t f | p a g e s | n u m b e r s | k e y | j s o n | x m l | c | c p p | h | j a v a | p y | j s | t s | h t m l | c s s | p h p | r b | g o | r s | s w i f t | k t ) $ / i)
34- ) ;
25+ // 2. Strict Category Checks
26+ const isImage = mimeType . includes ( "image" ) || [ "jpg" , "jpeg" , "png" , "gif" , "webp" , "svg" , "bmp" , "ico" , "tiff" ] . includes ( effectiveExtension ) ;
27+ const isVideo = mimeType . includes ( "video" ) || [ "mp4" , "webm" , "ogg" , "mov" , "avi" , "mkv" , "wmv" ] . includes ( effectiveExtension ) ;
28+ const isPDF = mimeType . includes ( "pdf" ) || effectiveExtension === "pdf" ;
3529
36- const showGoogleViewer = isGoogleDocSupported ;
30+ // 3. Universal Catch-All Strategy
31+ // If it's not media (Image/Video) and not PDF (Custom Viewer),
32+ // we send IT ALL to Google Docs Viewer.
33+ // Google Viewer handles Office, Text, Code, Adobe, CAD, and gives a decent fallback for others.
34+ const showGoogleViewer = ! isImage && ! isVideo && ! isPDF ;
3735
3836 return (
3937 < div style = { {
@@ -75,7 +73,11 @@ export default function FilePreviewModal({ isOpen, onClose, fileUrl, fileName, f
7573 ) }
7674
7775 { isPDF && (
78- < PDFViewer fileUrl = { fileUrl } fileName = { fileName } />
76+ < iframe
77+ src = { fileUrl }
78+ style = { { width : "100%" , height : "100%" , border : "none" , borderRadius : "8px" } }
79+ title = { fileName }
80+ />
7981 ) }
8082
8183 { showGoogleViewer && (
@@ -87,10 +89,16 @@ export default function FilePreviewModal({ isOpen, onClose, fileUrl, fileName, f
8789 ) }
8890
8991 { ! isImage && ! isVideo && ! isPDF && ! showGoogleViewer && (
90- < div style = { { textAlign : "center" , color : "#64748b" } } >
91- < p style = { { marginBottom : "1rem" } } > Preview not available for this file type.</ p >
92- < a href = { fileUrl } download className = "btn btn-primary" style = { { display : "inline-flex" , alignItems : "center" , gap : "0.5rem" , padding : "0.5rem 1rem" , background : "#2563eb" , color : "white" , borderRadius : "6px" , textDecoration : "none" } } >
93- < Download size = { 18 } /> Download to View
92+ < div style = { { textAlign : "center" , color : "#64748b" , padding : "2rem" } } >
93+ < p style = { { marginBottom : "1rem" , fontSize : "1.1rem" } } > Preview unavailable for this file.</ p >
94+ < a
95+ href = { fileUrl }
96+ target = "_blank"
97+ rel = "noreferrer"
98+ className = "btn btn-primary"
99+ style = { { display : "inline-flex" , alignItems : "center" , gap : "0.5rem" , padding : "0.5rem 1rem" , background : "#2563eb" , color : "white" , borderRadius : "6px" , textDecoration : "none" } }
100+ >
101+ < Download size = { 18 } /> Open in New Tab
94102 </ a >
95103 </ div >
96104 ) }
0 commit comments