Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve file uploader size validation - custom hook #3952

Merged
merged 4 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/frontend/src/components/inputFileComponent/index.tsx
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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 !== "") {
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -22,7 +21,7 @@ export default function IOFileInput({ field, updateValue }: IOFileInputProps) {
const [filePath, setFilePath] = useState("");
const [image, setImage] = useState<string | null>(null);
const setErrorData = useAlertStore((state) => state.setErrorData);
const maxFileSizeUpload = useUtilityStore((state) => state.maxFileSizeUpload);
const { validateFileSize } = useFileSizeValidator(setErrorData);

useEffect(() => {
if (filePath) {
Expand Down Expand Up @@ -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) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -38,12 +37,12 @@ export default function ChatInput({
const [inputFocus, setInputFocus] = useState<boolean>(false);
const fileInputRef = useRef<HTMLInputElement>(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<HTMLInputElement> | ClipboardEvent,
Expand All @@ -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;
}

Expand Down
22 changes: 22 additions & 0 deletions src/frontend/src/shared/hooks/use-file-size-validator.tsx
Original file line number Diff line number Diff line change
@@ -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<string> }) => 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;
Loading