import clsx from "clsx"; import { ReactNode, useEffect, useState } from "react"; import { FileBase64 } from "~/application/dtos/shared/FileBase64"; import { useTranslation } from "react-i18next"; interface Props { name?: string; title?: string; accept?: string; multiple?: boolean; description?: string; icon?: ReactNode; disabled?: boolean; className?: string; uploadText?: string; autoFocus?: boolean; onDropped?: (base64: string, file: File) => void; onDroppedFiles?: (fileBase64: FileBase64[], files: any[]) => void; } export default function UploadDocuments({ name = "uploadmyfile", className, title = "", accept, multiple, description, icon = "", disabled, onDropped, onDroppedFiles, uploadText, autoFocus, }: Props) { const { t } = useTranslation(); const [isDragging, setIsDragging] = useState(false); const [loading] = useState(false); const [customClasses, setCustomClasses] = useState(""); function dragOver(e: any) { e.preventDefault(); if (!loading) { setIsDragging(true); } } function dragLeave() { setIsDragging(false); } // async function compressFile(imageFile: File): Promise { // const options = { // maxSizeMB: 0.5, // maxWidthOrHeight: 1920 / 2, // useWebWorker: true, // }; // try { // return await imageCompression(imageFile, options); // } catch (error) { // return await Promise.reject(error); // } // } // async function compressFileNotImage(imageFile: File): Promise { // return Promise.resolve(imageFile); // } async function drop(e: any) { try { e.preventDefault(); } catch { // ignore } const files: File[] = await Promise.all( [...e.dataTransfer.files].map(async (element: File) => { return element; // if (element.type.includes("image")) { // return await compressFile(element); // } else { // return await compressFileNotImage(element); // } }) ); const filesArray: FileBase64[] = []; await Promise.all( files.map(async (file) => { const base64 = await getBase64(file); filesArray.push({ base64, file, }); if (onDropped) { onDropped(base64, file); } }) ); if (onDroppedFiles) { onDroppedFiles(filesArray, files); } setIsDragging(false); } function requestUploadFile() { const src = document.querySelector("#" + name); drop({ dataTransfer: src }); } function getBase64(file: File): Promise { const reader = new FileReader(); return new Promise((resolve) => { reader.onload = (ev) => { resolve(ev?.target?.result?.toString() ?? ""); }; reader.readAsDataURL(file); }); } useEffect(() => { // Subframe FileUpload "hover"/dragging state: 2px dashed information border + subtle information fill. setCustomClasses( isDragging && !loading && !disabled ? "border-2 border-dashed border-border-information-solid bg-surface-feedback-subtle-information" : "" ); }, [isDragging, loading, disabled]); return (
{(() => { if (loading) { return
{t("shared.loading")}...
; } else { return ( // Subframe FileUpload content: vertically centered column with gap-5 between // icon/label cluster and the description hint.
{title}
{/* Subframe upload icon color token */} {icon}
{/*

{t("shared.or")} {t("shared.dragAndDrop")}

*/}

{description ?? ( {t("shared.or")} {t("shared.dragAndDrop")} )}

); } })()}
); }