File size: 2,170 Bytes
6a66d01 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | import { useState } from 'react';
import { Button } from '@/components/ui/button';
import { Download, Loader2 } from 'lucide-react';
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu';
import { downloadImage, downloadImageAsPDF } from '@/lib/downloadUtils';
import { toast } from 'sonner';
interface DownloadButtonProps {
imageUrl: string;
fileName: string;
title: string;
variant?: 'default' | 'outline' | 'ghost';
size?: 'default' | 'sm' | 'lg';
}
export function DownloadButton({
imageUrl,
fileName,
title,
variant = 'outline',
size = 'default',
}: DownloadButtonProps) {
const [isLoading, setIsLoading] = useState(false);
const handleDownloadImage = async () => {
setIsLoading(true);
try {
await downloadImage(imageUrl, fileName, 'high');
toast.success(`Downloaded ${fileName} as PNG`);
} catch (error) {
toast.error('Failed to download image');
} finally {
setIsLoading(false);
}
};
const handleDownloadPDF = async () => {
setIsLoading(true);
try {
await downloadImageAsPDF(imageUrl, fileName, title);
toast.success(`Downloaded ${fileName} as PDF`);
} catch (error) {
toast.error('Failed to download PDF');
} finally {
setIsLoading(false);
}
};
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
variant={variant}
size={size}
disabled={isLoading}
className="gap-2"
>
{isLoading ? (
<Loader2 className="w-4 h-4 animate-spin" />
) : (
<Download className="w-4 h-4" />
)}
Download
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuItem onClick={handleDownloadImage} disabled={isLoading}>
<span>Download as PNG (High-Res)</span>
</DropdownMenuItem>
<DropdownMenuItem onClick={handleDownloadPDF} disabled={isLoading}>
<span>Download as PDF</span>
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
);
}
|