diff --git a/src/frontend/src/components/inputFileComponent/index.tsx b/src/frontend/src/components/inputFileComponent/index.tsx index 55e14c5d1..c93378df8 100644 --- a/src/frontend/src/components/inputFileComponent/index.tsx +++ b/src/frontend/src/components/inputFileComponent/index.tsx @@ -1,5 +1,6 @@ import { usePostUploadFile } from "@/controllers/API/queries/files/use-post-upload-file"; import { createFileUpload } from "@/helpers/create-file-upload"; +import useFileSizeValidator from "@/shared/hooks/use-file-size-validator"; import { useUtilityStore } from "@/stores/utilityStore"; import { useEffect } from "react"; import { @@ -23,7 +24,8 @@ export default function InputFileComponent({ }: FileComponentType): JSX.Element { const currentFlowId = useFlowsManagerStore((state) => state.currentFlowId); const setErrorData = useAlertStore((state) => state.setErrorData); - const maxFileSizeUpload = useUtilityStore((state) => state.maxFileSizeUpload); + const { validateFileSize } = useFileSizeValidator(setErrorData); + // Clear component state useEffect(() => { if (disabled && value !== "") { @@ -47,13 +49,11 @@ export default function InputFileComponent({ createFileUpload({ multiple: false, accept: fileTypes?.join(",") }).then( (files) => { const file = files[0]; - if (file.size > maxFileSizeUpload) { - setErrorData({ - title: INVALID_FILE_SIZE_ALERT(maxFileSizeUpload / 1024 / 1024), - }); - return; - } if (file) { + if (!validateFileSize(file)) { + return; + } + if (checkFileType(file.name)) { // Upload the file mutate( diff --git a/src/frontend/src/modals/IOModal/components/IOFieldView/components/FileInput/index.tsx b/src/frontend/src/modals/IOModal/components/IOFieldView/components/FileInput/index.tsx index f076a2e8f..67526aac0 100644 --- a/src/frontend/src/modals/IOModal/components/IOFieldView/components/FileInput/index.tsx +++ b/src/frontend/src/modals/IOModal/components/IOFieldView/components/FileInput/index.tsx @@ -1,10 +1,9 @@ import { Button } from "../../../../../../components/ui/button"; -import { INVALID_FILE_SIZE_ALERT } from "@/constants/alerts_constants"; import { usePostUploadFile } from "@/controllers/API/queries/files/use-post-upload-file"; import { createFileUpload } from "@/helpers/create-file-upload"; +import useFileSizeValidator from "@/shared/hooks/use-file-size-validator"; import useAlertStore from "@/stores/alertStore"; -import { useUtilityStore } from "@/stores/utilityStore"; import { useEffect, useState } from "react"; import IconComponent from "../../../../../../components/genericIconComponent"; import { @@ -22,7 +21,7 @@ export default function IOFileInput({ field, updateValue }: IOFileInputProps) { const [filePath, setFilePath] = useState(""); const [image, setImage] = useState(null); const setErrorData = useAlertStore((state) => state.setErrorData); - const maxFileSizeUpload = useUtilityStore((state) => state.maxFileSizeUpload); + const { validateFileSize } = useFileSizeValidator(setErrorData); useEffect(() => { if (filePath) { @@ -79,13 +78,9 @@ export default function IOFileInput({ field, updateValue }: IOFileInputProps) { const upload = async (file) => { if (file) { - if (file.size > maxFileSizeUpload) { - setErrorData({ - title: INVALID_FILE_SIZE_ALERT(maxFileSizeUpload / 1024 / 1024), - }); + if (!validateFileSize(file)) { return; } - // Check if a file was selected const fileReader = new FileReader(); fileReader.onload = (event) => { diff --git a/src/frontend/src/modals/IOModal/components/chatView/chatInput/index.tsx b/src/frontend/src/modals/IOModal/components/chatView/chatInput/index.tsx index e66d80205..fe575a1f2 100644 --- a/src/frontend/src/modals/IOModal/components/chatView/chatInput/index.tsx +++ b/src/frontend/src/modals/IOModal/components/chatView/chatInput/index.tsx @@ -1,7 +1,6 @@ -import { INVALID_FILE_SIZE_ALERT } from "@/constants/alerts_constants"; import { usePostUploadFile } from "@/controllers/API/queries/files/use-post-upload-file"; +import useFileSizeValidator from "@/shared/hooks/use-file-size-validator"; import useAlertStore from "@/stores/alertStore"; -import { useUtilityStore } from "@/stores/utilityStore"; import { useEffect, useRef, useState } from "react"; import ShortUniqueId from "short-unique-id"; import { @@ -38,12 +37,12 @@ export default function ChatInput({ const [inputFocus, setInputFocus] = useState(false); const fileInputRef = useRef(null); const setErrorData = useAlertStore((state) => state.setErrorData); - const maxFileSizeUpload = useUtilityStore((state) => state.maxFileSizeUpload); + const { validateFileSize } = useFileSizeValidator(setErrorData); useFocusOnUnlock(lockChat, inputRef); useAutoResizeTextArea(chatValue, inputRef); - const { mutate, isPending } = usePostUploadFile(); + const { mutate } = usePostUploadFile(); const handleFileChange = async ( event: React.ChangeEvent | ClipboardEvent, @@ -68,10 +67,7 @@ export default function ChatInput({ if (file) { const fileExtension = file.name.split(".").pop()?.toLowerCase(); - if (file.size > maxFileSizeUpload) { - setErrorData({ - title: INVALID_FILE_SIZE_ALERT(maxFileSizeUpload / 1024 / 1024), - }); + if (!validateFileSize(file)) { return; } diff --git a/src/frontend/src/shared/hooks/use-file-size-validator.tsx b/src/frontend/src/shared/hooks/use-file-size-validator.tsx new file mode 100644 index 000000000..502ac5602 --- /dev/null +++ b/src/frontend/src/shared/hooks/use-file-size-validator.tsx @@ -0,0 +1,22 @@ +import { INVALID_FILE_SIZE_ALERT } from "@/constants/alerts_constants"; +import { useUtilityStore } from "@/stores/utilityStore"; + +const useFileSizeValidator = ( + setErrorData: (newState: { title: string; list?: Array }) => void, +) => { + const maxFileSizeUpload = useUtilityStore((state) => state.maxFileSizeUpload); + + const validateFileSize = (file) => { + if (file.size > maxFileSizeUpload) { + setErrorData({ + title: INVALID_FILE_SIZE_ALERT(maxFileSizeUpload / 1024 / 1024), + }); + return false; + } + return true; + }; + + return { validateFileSize }; +}; + +export default useFileSizeValidator;