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

84 lines
2.2 KiB
TypeScript

import { PDFViewer } from "@/components";
import { cn } from "@/lib/utils";
import { Card, CardContent } from "@/ui";
import { IListQuotes_Response_DTO } from "@shared/contexts";
import { t } from "i18next";
import { useCallback, useEffect, useState } from "react";
import { useQuotes } from "../hooks";
import { PDFGenerationLoading } from "./PDFGenerationLoading";
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, ...otherReportProps } = 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);
console.log(blobURL);
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'>{t("quotes.list.resume.select_quote")}</p>
</CardContent>
</Card>
);
}
if (isInProgress) {
return <PDFGenerationLoading isInProgress={isInProgress} {...otherReportProps} />;
}
return (
<PDFViewer file={URLReport} className='object-contain' onThumbnailClick={handleDownload} />
);
};
QuotePDFPreview.whyDidYouRender = true;
export { QuotePDFPreview };