import { type ReactNode, useState } from 'react'; import { msg } from '@lingui/core/macro'; import { useLingui } from '@lingui/react'; import { Trans } from '@lingui/react/macro'; import { Loader } from 'lucide-react'; import { ErrorCode, type FileRejection, useDropzone } from 'react-dropzone'; import { useNavigate, useParams } from 'react-router'; import { match } from 'ts-pattern'; import { APP_DOCUMENT_UPLOAD_SIZE_LIMIT } from '@documenso/lib/constants/app'; import { megabytesToBytes } from '@documenso/lib/universal/unit-convertions'; import { putPdfFile } from '@documenso/lib/universal/upload/put-file'; import { formatTemplatesPath } from '@documenso/lib/utils/teams'; import { trpc } from '@documenso/trpc/react'; import { cn } from '@documenso/ui/lib/utils'; import { useToast } from '@documenso/ui/primitives/use-toast'; import { useCurrentTeam } from '~/providers/team'; export interface TemplateDropZoneWrapperProps { children: ReactNode; className?: string; } export const TemplateDropZoneWrapper = ({ children, className }: TemplateDropZoneWrapperProps) => { const { _ } = useLingui(); const { toast } = useToast(); const { folderId } = useParams(); const team = useCurrentTeam(); const navigate = useNavigate(); const [isLoading, setIsLoading] = useState(false); const { mutateAsync: createTemplate } = trpc.template.createTemplate.useMutation(); const onFileDrop = async (file: File) => { try { setIsLoading(true); const documentData = await putPdfFile(file); const { legacyTemplateId: id } = await createTemplate({ title: file.name, templateDocumentDataId: documentData.id, folderId: folderId ?? undefined, }); toast({ title: _(msg`Template uploaded`), description: _( msg`Your template has been uploaded successfully. You will be redirected to the template page.`, ), duration: 5000, }); await navigate(`${formatTemplatesPath(team.url)}/${id}/edit`); } catch { toast({ title: _(msg`Something went wrong`), description: _(msg`Please try again later.`), variant: 'destructive', }); } finally { setIsLoading(false); } }; const onFileDropRejected = (fileRejections: FileRejection[]) => { if (!fileRejections.length) { return; } // Since users can only upload only one file (no multi-upload), we only handle the first file rejection const { file, errors } = fileRejections[0]; if (!errors.length) { return; } const errorNodes = errors.map((error, index) => ( {match(error.code) .with(ErrorCode.FileTooLarge, () => ( File is larger than {APP_DOCUMENT_UPLOAD_SIZE_LIMIT}MB )) .with(ErrorCode.FileInvalidType, () => Only PDF files are allowed) .with(ErrorCode.FileTooSmall, () => File is too small) .with(ErrorCode.TooManyFiles, () => ( Only one file can be uploaded at a time )) .otherwise(() => ( Unknown error ))} )); const description = ( <> {file.name} couldn't be uploaded: {errorNodes} ); toast({ title: _(msg`Upload failed`), description, duration: 5000, variant: 'destructive', }); }; const { getRootProps, getInputProps, isDragActive } = useDropzone({ accept: { 'application/pdf': ['.pdf'], }, //disabled: isUploadDisabled, multiple: false, maxSize: megabytesToBytes(APP_DOCUMENT_UPLOAD_SIZE_LIMIT), onDrop: ([acceptedFile]) => { if (acceptedFile) { void onFileDrop(acceptedFile); } }, onDropRejected: (fileRejections) => { onFileDropRejected(fileRejections); }, noClick: true, noDragEventsBubbling: true, }); return (
{children} {isDragActive && (

Upload Template

Drag and drop your PDF file here

)} {isLoading && (

Uploading template...

)}
); };