From e1e9d7baef3c5350a6474212d9f2fc8616d056a2 Mon Sep 17 00:00:00 2001 From: Cristhian Zanforlin Lousa Date: Tue, 11 Feb 2025 13:39:56 -0300 Subject: [PATCH] refactor: migrate chat lock state to useFlowStore (#6166) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🔧 (frontend): remove unused lockChat and setLockChat props from ChatViewWrapper and ChatView components ♻️ (frontend): refactor ChatMessage component to use useFlowStore for lockChat state management instead of passing it as a prop * 🔧 (chat-message.tsx): Remove unused variables setLockChat and lockChat ♻️ (chat-message.tsx): Refactor code to use isBuilding state instead of setLockChat and lockChat variables ♻️ (new-modal.tsx): Refactor code to remove setLockChat function and references ♻️ (flowStore.ts): Refactor code to remove setLockChat function and lockChat variable ♻️ (components/index.ts): Refactor code to remove setLockChat function and lockChat variable ♻️ (flow/index.ts): Refactor code to remove setLockChat function and lockChat variable ♻️ (buildUtils.ts): Refactor code to remove setLockChat function and references * ♻️ (chat-view.tsx): refactor variable name 'lockChat' to 'isBuilding' for better clarity and semantics in the code. * 🔧 (chat-view.tsx, chat-input.tsx, button-send-wrapper.tsx, text-area-wrapper.tsx, upload-file-button.tsx, use-focus-unlock.ts, use-upload.ts, chat-message.tsx, index.ts): Remove lockChat variable and replace it with isBuilding variable to improve code readability and consistency. * ♻️ (button-send-wrapper.tsx): remove unnecessary disabled prop from Button component to improve code readability and maintainability --- .../IOModal/components/chat-view-wrapper.tsx | 4 ---- .../IOModal/components/chatView/chat-view.tsx | 11 ++++------ .../chatView/chatInput/chat-input.tsx | 18 +++++++--------- .../components/button-send-wrapper.tsx | 8 ++----- .../components/text-area-wrapper.tsx | 8 +++---- .../components/upload-file-button.tsx | 8 +++---- .../chatInput/hooks/use-focus-unlock.ts | 6 +++--- .../chatView/chatInput/hooks/use-upload.ts | 7 ++++--- .../chatView/chatMessage/chat-message.tsx | 19 ++++++----------- src/frontend/src/modals/IOModal/new-modal.tsx | 21 ++----------------- .../modals/IOModal/types/chat-view-wrapper.ts | 2 -- src/frontend/src/stores/flowStore.ts | 15 ------------- src/frontend/src/types/components/index.ts | 9 -------- src/frontend/src/types/zustand/flow/index.ts | 4 ---- src/frontend/src/utils/buildUtils.ts | 8 ------- 15 files changed, 37 insertions(+), 111 deletions(-) diff --git a/src/frontend/src/modals/IOModal/components/chat-view-wrapper.tsx b/src/frontend/src/modals/IOModal/components/chat-view-wrapper.tsx index 32f01029c..6bcf576fe 100644 --- a/src/frontend/src/modals/IOModal/components/chat-view-wrapper.tsx +++ b/src/frontend/src/modals/IOModal/components/chat-view-wrapper.tsx @@ -19,8 +19,6 @@ export const ChatViewWrapper = ({ messagesFetched, sessionId, sendMessage, - lockChat, - setLockChat, canvasOpen, setOpen, }: ChatViewWrapperProps) => { @@ -93,8 +91,6 @@ export const ChatViewWrapper = ({ { export default function ChatView({ sendMessage, - lockChat, - setLockChat, visibleSession, focusChat, closeChat, @@ -51,6 +49,8 @@ export default function ChatView({ (state) => state.displayLoadingMessage, ); + const isBuilding = useFlowStore((state) => state.isBuilding); + const inputTypes = inputs.map((obj) => obj.type); const updateFlowPool = useFlowStore((state) => state.updateFlowPool); const setChatValueStore = useUtilityStore((state) => state.setChatValueStore); @@ -99,7 +99,7 @@ export default function ChatView({ return new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime(); }); - if (messages.length === 0 && !lockChat && chatInputNode && isTabHidden) { + if (messages.length === 0 && !isBuilding && chatInputNode && isTabHidden) { setChatValueStore( chatInputNode.data.node.template["input_value"].value ?? "", ); @@ -164,12 +164,10 @@ export default function ChatView({ >
{chatHistory && - (lockChat || chatHistory?.length > 0 ? ( + (isBuilding || chatHistory?.length > 0 ? ( <> {chatHistory?.map((chat, index) => ( { sendMessage({ repeat, files }); track("Playground Message Sent"); diff --git a/src/frontend/src/modals/IOModal/components/chatView/chatInput/chat-input.tsx b/src/frontend/src/modals/IOModal/components/chatView/chatInput/chat-input.tsx index fbb32f10e..50a04151b 100644 --- a/src/frontend/src/modals/IOModal/components/chatView/chatInput/chat-input.tsx +++ b/src/frontend/src/modals/IOModal/components/chatView/chatInput/chat-input.tsx @@ -26,7 +26,6 @@ import UploadFileButton from "./components/upload-file-button"; import useAutoResizeTextArea from "./hooks/use-auto-resize-text-area"; import useFocusOnUnlock from "./hooks/use-focus-unlock"; export default function ChatInput({ - lockChat, sendMessage, inputRef, noInput, @@ -39,10 +38,10 @@ export default function ChatInput({ const setErrorData = useAlertStore((state) => state.setErrorData); const { validateFileSize } = useFileSizeValidator(setErrorData); const stopBuilding = useFlowStore((state) => state.stopBuilding); - + const isBuilding = useFlowStore((state) => state.isBuilding); const chatValue = useUtilityStore((state) => state.chatValueStore); - useFocusOnUnlock(lockChat, inputRef); + useFocusOnUnlock(isBuilding, inputRef); useAutoResizeTextArea(chatValue, inputRef); const { mutate } = usePostUploadFile(); @@ -134,7 +133,7 @@ export default function ChatInput({ return () => { document.removeEventListener("paste", handleFileChange); }; - }, [handleFileChange, currentFlowId, lockChat]); + }, [handleFileChange, currentFlowId, isBuilding]); const send = () => { sendMessage({ @@ -147,7 +146,7 @@ export default function ChatInput({ const checkSendingOk = (event: React.KeyboardEvent) => { return ( event.key === "Enter" && - !lockChat && + !isBuilding && !event.shiftKey && !event.nativeEvent.isComposing ); @@ -168,7 +167,7 @@ export default function ChatInput({ return (
- {!lockChat ? ( + {!isBuilding ? (