Presupuestador_web/client/src/app/quotes/components/QuotePDFPreview.tsx

90 lines
2.4 KiB
TypeScript

import { PDFViewer } from "@/components";
import { cn } from "@/lib/utils";
import { Card, CardContent, CardHeader, Skeleton } from "@/ui";
import { IListQuotes_Response_DTO } from "@shared/contexts";
import { useCallback, useEffect, useState } from "react";
import { useQuotes } from "../hooks";
const QuotePDFPreview = ({
quote,
className,
}: {
quote?: IListQuotes_Response_DTO;
className?: string;
}) => {
//const navigate = useNavigate();
//const { toast } = useToast();
const { useReport, getQuotePDFFilename, useDownloader } = useQuotes();
const { download } = useDownloader();
const { report, preview, isInProgress } = useReport();
const [URLReport, setURLReport] = useState<string | undefined>(undefined);
/*const handleFinishDownload = useCallback(() => {
toast({
description: t("quotes.downloading_dialog.toast_success"),
});
}, [toast]);*/
const handleDownload = useCallback(() => {
if (quote) download(quote.id, getQuotePDFFilename(quote));
}, [quote]);
useEffect(() => {
const timer = setTimeout(() => {
if (quote && quote.id) {
preview(quote.id);
}
}, 200);
return () => clearTimeout(timer);
}, [quote]);
useEffect(() => {
if (!isInProgress && report) {
const blobURL =
window.URL && window.URL.createObjectURL
? window.URL.createObjectURL(report)
: window.webkitURL.createObjectURL(report);
setURLReport(blobURL);
return () => {
setURLReport(undefined);
window.URL.revokeObjectURL(blobURL);
};
}
}, [report, isInProgress]);
if (!quote) {
return (
<Card className={cn("overflow-hidden", className)}>
<CardContent className='px-4 py-6 text-center'>
<p className='mx-auto'>Select a quote</p>
</CardContent>
</Card>
);
}
if (isInProgress) {
return (
<Card className={cn("overflow-hidden flex flex-col", className)}>
<CardHeader>
<Skeleton className='w-full h-8' />
<Skeleton className='w-full h-8' />
</CardHeader>
<CardContent className='py-4'>
<Skeleton className='w-full aspect-[3/4] relative bg-white shadow flex-1' />
</CardContent>
</Card>
);
}
return (
<PDFViewer file={URLReport} className='object-contain' onThumbnailClick={handleDownload} />
);
};
QuotePDFPreview.whyDidYouRender = true;
export { QuotePDFPreview };