From 43ba306b343d24ee4323eb954904d8e95d193f2d Mon Sep 17 00:00:00 2001 From: cristhianzl Date: Wed, 29 May 2024 16:01:28 -0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20(chatInput):=20add=20useHandleFileC?= =?UTF-8?q?hange=20hook=20to=20handle=20file=20input=20changes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hooks/use-handle-file-change.tsx | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/frontend/src/modals/IOModal/components/chatView/chatInput/hooks/use-handle-file-change.tsx diff --git a/src/frontend/src/modals/IOModal/components/chatView/chatInput/hooks/use-handle-file-change.tsx b/src/frontend/src/modals/IOModal/components/chatView/chatInput/hooks/use-handle-file-change.tsx new file mode 100644 index 000000000..2de36397c --- /dev/null +++ b/src/frontend/src/modals/IOModal/components/chatView/chatInput/hooks/use-handle-file-change.tsx @@ -0,0 +1,29 @@ +import ShortUniqueId from "short-unique-id"; +import useFileUpload from "./use-file-upload"; + +export const useHandleFileChange = (setFiles, currentFlowId) => { + const handleFileChange = async ( + event: React.ChangeEvent, + ) => { + const file = event.target.files?.[0]; + if (file) { + const uid = new ShortUniqueId({ length: 3 }); + const id = uid(); + const type = file.type.split("/")[0]; + const blob = file; + + setFiles((prevFiles) => [ + ...prevFiles, + { file: blob, loading: true, error: false, id, type }, + ]); + + useFileUpload(blob, currentFlowId, setFiles, id); + } + }; + + return { + handleFileChange, + }; +}; + +export default useHandleFileChange;