From 30aca5e7db56092a1ed181e5d2aa07eeddd658b1 Mon Sep 17 00:00:00 2001 From: anovazzi1 Date: Fri, 12 Apr 2024 15:55:59 -0300 Subject: [PATCH] Update chatInput component to include file preview functionality --- .../components/chatView/chatInput/index.tsx | 34 ++++++++++++------- .../chatView/filePreviewChat/index.tsx | 13 +++++++ src/frontend/src/types/components/index.ts | 2 ++ 3 files changed, 37 insertions(+), 12 deletions(-) create mode 100644 src/frontend/src/modals/IOModal/components/chatView/filePreviewChat/index.tsx 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 a92ede935..d5133f868 100644 --- a/src/frontend/src/modals/IOModal/components/chatView/chatInput/index.tsx +++ b/src/frontend/src/modals/IOModal/components/chatView/chatInput/index.tsx @@ -6,10 +6,11 @@ import { CHAT_INPUT_PLACEHOLDER_SEND, } from "../../../../../constants/constants"; import useFlowsManagerStore from "../../../../../stores/flowsManagerStore"; -import { chatInputType } from "../../../../../types/components"; +import { FilePreviewType, chatInputType } from "../../../../../types/components"; import { classNames } from "../../../../../utils/utils"; import { uploadFile } from "../../../../../controllers/API"; - +import ShortUniqueId from "short-unique-id"; +import FilePreview from "../filePreviewChat"; export default function ChatInput({ lockChat, chatValue, @@ -21,6 +22,9 @@ export default function ChatInput({ const [repeat, setRepeat] = useState(1); const saveLoading = useFlowsManagerStore((state) => state.saveLoading); const currentFlowId = useFlowsManagerStore((state) => state.currentFlowId); + const uid = new ShortUniqueId({ length: 3 }); + const [files, setFiles] = useState([]); + useEffect(() => { if (!lockChat && inputRef.current) { inputRef.current.focus(); @@ -43,16 +47,15 @@ export default function ChatInput({ if (items[i].type.indexOf("image") !== -1) { const blob = items[i].getAsFile(); if (blob) { - // const reader = new FileReader(); - // reader.onload = (e) => { - // const data = e.target?.result; - // if (typeof data === "string") { - // // sendMessage(data); - // } - // }; - // reader.readAsDataURL(blob); + const id = uid() + setFiles([...files, { file: blob, loading: true, error: false,id}]); uploadFile(blob, currentFlowId).then((res) => { - console.log(res); + setFiles(prev=>{ + const newFiles = [...prev]; + const updatedIndex = newFiles.findIndex((file)=>file.id===id); + newFiles[updatedIndex].loading = false; + return newFiles; + }) }); } } @@ -66,7 +69,7 @@ export default function ChatInput({ }, []); return ( -
+