Files
sign/packages/ui/components/document/document-download-button.tsx

55 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-08-17 19:56:18 +10:00
'use client';
import type { HTMLAttributes } from 'react';
import { useState } from 'react';
2023-08-17 19:56:18 +10:00
import { Download } from 'lucide-react';
import { downloadFile } from '@documenso/lib/client-only/download-pdf';
import type { DocumentData } from '@documenso/prisma/client';
import { Button } from '../../primitives/button';
2023-08-17 19:56:18 +10:00
export type DownloadButtonProps = HTMLAttributes<HTMLButtonElement> & {
disabled?: boolean;
fileName?: string;
documentData?: DocumentData;
2023-08-17 19:56:18 +10:00
};
2023-09-20 13:48:30 +10:00
export const DocumentDownloadButton = ({
2023-08-17 19:56:18 +10:00
className,
fileName,
documentData,
2023-08-17 19:56:18 +10:00
disabled,
...props
}: DownloadButtonProps) => {
const [isLoading, setIsLoading] = useState(false);
2023-08-17 19:56:18 +10:00
const onDownloadClick = async () => {
setIsLoading(true);
2023-08-17 19:56:18 +10:00
if (!documentData) {
return;
}
2023-09-14 13:21:03 +10:00
await downloadFile({ documentData, fileName }).then(() => {
setIsLoading(false);
});
2023-08-17 19:56:18 +10:00
};
return (
<Button
type="button"
variant="outline"
className={className}
disabled={disabled || !documentData}
2023-08-17 19:56:18 +10:00
onClick={onDownloadClick}
loading={isLoading}
2023-08-17 19:56:18 +10:00
{...props}
>
<Download className="mr-2 h-5 w-5" />
Download
</Button>
);
};