'use client' import { useState, useEffect } from 'react' import { X, Download, ExternalLink, AlertCircle, ShoppingCart } from 'lucide-react' import { Button } from '@/components/ui/button' import { Dialog, DialogContent, DialogHeader, DialogDescription, DialogTitle, DialogClose, } from '@/components/ui/dialog' import { Sheet, SheetContent, SheetHeader, SheetTitle, } from '@/components/ui/sheet' import { PartsPanel } from '@/components/parts-panel' import { useIsMobile } from '@/hooks/use-mobile' interface ManualViewerProps { manualUrl: string filename: string isOpen: boolean onClose: () => void } export function ManualViewer({ manualUrl, filename, isOpen, onClose }: ManualViewerProps) { const [pdfError, setPdfError] = useState(false) const [isLoading, setIsLoading] = useState(true) const [showPartsPanel, setShowPartsPanel] = useState(true) const isMobile = useIsMobile() // Reset error state when manual URL changes useEffect(() => { if (isOpen) { setPdfError(false) setIsLoading(true) } }, [manualUrl, isOpen]) // The URL is already properly encoded in getManualUrl function // Just use it as-is for the iframe src const encodedUrl = manualUrl const handleIframeLoad = () => { setIsLoading(false) } const handleIframeError = () => { setIsLoading(false) setPdfError(true) } // Mobile layout - use Sheet if (isMobile) { return ( {filename.replace(/\.pdf$/i, '')} { const link = document.createElement('a') link.href = encodedUrl link.download = filename link.click() }} className="h-8 px-2" > {isLoading && !pdfError && ( Loading PDF... )} {pdfError ? ( Unable to Load PDF The PDF file could not be loaded. This may be due to file corruption or an encoding issue. window.open(encodedUrl, '_blank')} className="w-full" > Try Opening in New Tab { const link = document.createElement('a') link.href = encodedUrl link.download = filename link.click() }} className="w-full" > Download Instead ) : ( )} ) } // Desktop layout - use Dialog return ( PDF viewer for {filename.replace(/\.pdf$/i, '')}. Use the actions to open the manual in a new tab, download it, or browse available parts. {filename.replace(/\.pdf$/i, '')} setShowPartsPanel(!showPartsPanel)} > {showPartsPanel ? 'Hide' : 'Show'} Parts window.open(encodedUrl, '_blank')} > Open in New Tab { const link = document.createElement('a') link.href = encodedUrl link.download = filename link.click() }} > Download {/* PDF Viewer - responsive width based on parts panel */} {isLoading && !pdfError && ( Loading PDF... )} {pdfError ? ( Unable to Load PDF The PDF file could not be loaded. This may be due to file corruption or an encoding issue. window.open(encodedUrl, '_blank')} > Try Opening in New Tab { const link = document.createElement('a') link.href = encodedUrl link.download = filename link.click() }} > Download Instead ) : ( )} {/* Parts Panel - right side, responsive width */} {showPartsPanel && ( )} ) }
Loading PDF...
The PDF file could not be loaded. This may be due to file corruption or an encoding issue.