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

93 lines
2.5 KiB
TypeScript
Raw Normal View History

2024-08-19 16:13:07 +00:00
import { PDFViewer } from "@/components";
import { cn } from "@/lib/utils";
2024-08-29 14:29:10 +00:00
import { Card, CardContent, CardHeader, Skeleton } from "@/ui";
2024-08-21 11:16:56 +00:00
import { useToast } from "@/ui/use-toast";
2024-08-19 16:13:07 +00:00
import { IListQuotes_Response_DTO } from "@shared/contexts";
import { t } from "i18next";
2024-08-22 17:23:12 +00:00
import { useCallback, useEffect, useState } from "react";
2024-08-19 16:13:07 +00:00
import { useNavigate } from "react-router-dom";
import { useQuotes } from "../hooks";
2024-08-22 17:23:12 +00:00
const QuotePDFPreview = ({
2024-08-19 16:13:07 +00:00
quote,
className,
}: {
quote?: IListQuotes_Response_DTO;
2024-08-29 14:29:10 +00:00
className?: string;
2024-08-19 16:13:07 +00:00
}) => {
const navigate = useNavigate();
2024-08-21 11:16:56 +00:00
const { toast } = useToast();
const { useReport, getQuotePDFFilename, useDownloader } = useQuotes();
2024-08-22 17:34:51 +00:00
const { download, ...downloadProps } = useDownloader();
const { report, preview, isInProgress } = useReport();
2024-08-22 17:23:12 +00:00
const [URLReport, setURLReport] = useState<string | undefined>(undefined);
2024-08-19 16:13:07 +00:00
2024-08-21 11:16:56 +00:00
const handleFinishDownload = useCallback(() => {
toast({
description: t("quotes.downloading_dialog.toast_success"),
});
}, [toast]);
2024-08-22 17:34:51 +00:00
const handleDownload = useCallback(() => {
if (quote) download(quote.id, getQuotePDFFilename(quote));
}, [quote]);
2024-08-22 17:23:12 +00:00
useEffect(() => {
const timer = setTimeout(() => {
2024-08-22 17:34:51 +00:00
if (quote && quote.id) {
preview(quote.id);
2024-08-22 17:23:12 +00:00
}
}, 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]);
2024-08-19 16:13:07 +00:00
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>
);
}
2024-08-22 17:23:12 +00:00
if (isInProgress) {
2024-08-19 16:13:07 +00:00
return (
2024-08-19 18:55:36 +00:00
<Card className={cn("overflow-hidden flex flex-col", className)}>
<CardHeader>
2024-08-20 11:44:04 +00:00
<Skeleton className='w-full h-8' />
<Skeleton className='w-full h-8' />
2024-08-19 16:13:07 +00:00
</CardHeader>
2024-08-19 18:55:36 +00:00
<CardContent className='py-4'>
<Skeleton className='w-full aspect-[3/4] relative bg-white shadow flex-1' />
2024-08-19 16:13:07 +00:00
</CardContent>
</Card>
);
}
return (
2024-08-29 14:29:10 +00:00
<PDFViewer file={URLReport} className='object-contain' onThumbnailClick={handleDownload} />
2024-08-19 16:13:07 +00:00
);
};
2024-08-22 17:23:12 +00:00
QuotePDFPreview.whyDidYouRender = true;
export { QuotePDFPreview };