From 9b83f44611a1df4e1fc1226460f2ef1ceaf27efa Mon Sep 17 00:00:00 2001 From: Cristhian Zanforlin Lousa Date: Fri, 6 Sep 2024 17:19:00 -0300 Subject: [PATCH] feat: add stop button on Playground Chat (#3704) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 📝 (buttonSendWrapper/index.tsx): refactor button styling logic to use constants for better readability and maintainability 🐛 (buttonSendWrapper/index.tsx): fix logic to disable button when chat is locked and not building 🐛 (buttonSendWrapper/index.tsx): fix conditional rendering of icons based on button state * ✨ (stop-button-playground.spec.ts): Add end-to-end test for stopping building from inside Playground. * 🔧 (buttonSendWrapper/index.tsx): refactor conditional logic to correctly prioritize showStopButton condition and improve code readability * ✅ (stop-button-playground.spec.ts): add assertion to check if "build stopped" text is visible after clicking on the stop button --- .../components/buttonSendWrapper/index.tsx | 59 +++++-- .../stop-button-playground.spec.ts | 159 ++++++++++++++++++ 2 files changed, 200 insertions(+), 18 deletions(-) create mode 100644 src/frontend/tests/scheduled-end-to-end/stop-button-playground.spec.ts diff --git a/src/frontend/src/modals/IOModal/components/chatView/chatInput/components/buttonSendWrapper/index.tsx b/src/frontend/src/modals/IOModal/components/chatView/chatInput/components/buttonSendWrapper/index.tsx index 4c148575c..bfc7b65c9 100644 --- a/src/frontend/src/modals/IOModal/components/chatView/chatInput/components/buttonSendWrapper/index.tsx +++ b/src/frontend/src/modals/IOModal/components/chatView/chatInput/components/buttonSendWrapper/index.tsx @@ -1,9 +1,17 @@ +import useFlowStore from "@/stores/flowStore"; import IconComponent from "../../../../../../../components/genericIconComponent"; import { Button } from "../../../../../../../components/ui/button"; import { Case } from "../../../../../../../shared/components/caseComponent"; import { FilePreviewType } from "../../../../../../../types/components"; import { classNames } from "../../../../../../../utils/utils"; +const BUTTON_STATES = { + NO_INPUT: "bg-high-indigo text-background", + HAS_CHAT_VALUE: "text-primary", + SHOW_STOP: "bg-error text-background cursor-pointer", + DEFAULT: "bg-chat-send text-background", +}; + type ButtonSendWrapperProps = { send: () => void; lockChat: boolean; @@ -19,29 +27,48 @@ const ButtonSendWrapper = ({ chatValue, files, }: ButtonSendWrapperProps) => { + const stopBuilding = useFlowStore((state) => state.stopBuilding); + + const isBuilding = useFlowStore((state) => state.isBuilding); + const showStopButton = lockChat || files.some((file) => file.loading); + const showPlayButton = !lockChat && noInput; + const showSendButton = + !(lockChat || files.some((file) => file.loading)) && !noInput; + + const getButtonState = () => { + if (showStopButton) return BUTTON_STATES.SHOW_STOP; + if (noInput) return BUTTON_STATES.NO_INPUT; + if (chatValue) return BUTTON_STATES.HAS_CHAT_VALUE; + + return BUTTON_STATES.DEFAULT; + }; + + const buttonClasses = classNames("form-modal-send-button", getButtonState()); + + const handleClick = () => { + if (showStopButton && isBuilding) { + stopBuilding(); + } else if (!showStopButton) { + send(); + } + }; + return (