Skip to content

πŸ™Œ OpenHands: AI-Driven Development

refactor(frontend): migration of conversation-slice.ts to zustand (#11032)

Hiep Le5457392eaed5
diff --git a/frontend/src/components/features/chat/chat-interface.tsx b/frontend/src/components/features/chat/chat-interface.tsx
index 41dce4a463..c6a025a0c7 100644
--- a/frontend/src/components/features/chat/chat-interface.tsx
+++ b/frontend/src/components/features/chat/chat-interface.tsx
@@ -1,4 +1,4 @@
-import { useSelector, useDispatch } from "react-redux";
+import { useSelector } from "react-redux";
import React from "react";
import posthog from "posthog-js";
import { useParams } from "react-router";
@@ -33,7 +33,7 @@ import {
import { useUploadFiles } from "#/hooks/mutation/use-upload-files";
import { useConfig } from "#/hooks/query/use-config";
import { validateFiles } from "#/utils/file-validation";
-import { setMessageToSend } from "#/state/conversation-slice";
+import { useConversationStore } from "#/state/conversation-store";
import ConfirmationModeEnabled from "./confirmation-mode-enabled";
function getEntryPoint(
@@ -46,7 +46,7 @@ function getEntryPoint(
}
export function ChatInterface() {
- const dispatch = useDispatch();
+ const { setMessageToSend } = useConversationStore();
const { getErrorMessage } = useWSErrorMessage();
const { send, isLoadingMessages, parsedEvents } = useWsClient();
const { setOptimisticUserMessage, getOptimisticUserMessage } =
@@ -141,7 +141,7 @@ export function ChatInterface() {
send(createChatMessage(prompt, imageUrls, uploadedFiles, timestamp));
setOptimisticUserMessage(content);
- dispatch(setMessageToSend(null));
+ setMessageToSend("");
};
const handleStop = () => {
@@ -180,9 +180,7 @@ export function ChatInterface() {
!optimisticUserMessage &&
!userEventsExist && (
<ChatSuggestions
- onSuggestionsClick={(message) =>
- dispatch(setMessageToSend(message))
- }
+ onSuggestionsClick={(message) => setMessageToSend(message)}
/>
)}
{/* Note: We only hide chat suggestions when there's a user message */}
diff --git a/frontend/src/components/features/chat/chat-suggestions.tsx b/frontend/src/components/features/chat/chat-suggestions.tsx
index d0ff2f7c0f..8abc5aa41c 100644
--- a/frontend/src/components/features/chat/chat-suggestions.tsx
+++ b/frontend/src/components/features/chat/chat-suggestions.tsx
@@ -1,11 +1,10 @@
import { useTranslation } from "react-i18next";
-import { useSelector } from "react-redux";
import { motion, AnimatePresence } from "framer-motion";
import { Suggestions } from "#/components/features/suggestions/suggestions";
import { I18nKey } from "#/i18n/declaration";
import BuildIt from "#/icons/build-it.svg?react";
import { SUGGESTIONS } from "#/utils/suggestions";
-import { RootState } from "#/store";
+import { useConversationStore } from "#/state/conversation-store";
interface ChatSuggestionsProps {
onSuggestionsClick: (value: string) => void;
@@ -13,9 +12,7 @@ interface ChatSuggestionsProps {
export function ChatSuggestions({ onSuggestionsClick }: ChatSuggestionsProps) {
const { t } = useTranslation();
- const shouldHideSuggestions = useSelector(
- (state: RootState) => state.conversation.shouldHideSuggestions,
- );
+ const { shouldHideSuggestions } = useConversationStore();
return (
<AnimatePresence>
diff --git a/frontend/src/components/features/chat/custom-chat-input.tsx b/frontend/src/components/features/chat/custom-chat-input.tsx
index 33f09bd857..c48b1e2314 100644
--- a/frontend/src/components/features/chat/custom-chat-input.tsx
+++ b/frontend/src/components/features/chat/custom-chat-input.tsx
@@ -1,12 +1,5 @@
import React, { useEffect } from "react";
-import { useDispatch, useSelector } from "react-redux";
import { ConversationStatus } from "#/types/conversation-status";
-import {
- clearAllFiles,
- setShouldHideSuggestions,
- setSubmittedMessage,
-} from "#/state/conversation-slice";
-import { RootState } from "#/store";
import { useChatInputLogic } from "#/hooks/chat/use-chat-input-logic";
import { useFileHandling } from "#/hooks/chat/use-file-handling";
import { useGripResize } from "#/hooks/chat/use-grip-resize";
@@ -15,6 +8,7 @@ import { useChatSubmission } from "#/hooks/chat/use-chat-submission";
import { ChatInputGrip } from "./components/chat-input-grip";
import { ChatInputContainer } from "./components/chat-input-container";
import { HiddenFileInput } from "./components/hidden-file-input";
+import { useConversationStore } from "#/state/conversation-store";
export interface CustomChatInputProps {
disabled?: boolean;
@@ -41,10 +35,12 @@ export function CustomChatInput({
className = "",
buttonClassName = "",
}: CustomChatInputProps) {
- const { submittedMessage } = useSelector(
- (state: RootState) => state.conversation,
- );
- const dispatch = useDispatch();
+ const {
+ submittedMessage,
+ clearAllFiles,
+ setShouldHideSuggestions,
+ setSubmittedMessage,
+ } = useConversationStore();
// Disable input when conversation is stopped
const isConversationStopped = conversationStatus === "STOPPED";
@@ -56,8 +52,8 @@ export function CustomChatInput({
return;
}
onSubmit(submittedMessage);
- dispatch(setSubmittedMessage(null));
- }, [submittedMessage, disabled, onSubmit, dispatch]);
+ setSubmittedMessage(null);
+ }, [submittedMessage, disabled, onSubmit, setSubmittedMessage]);
// Custom hooks
const {
@@ -112,10 +108,10 @@ export function CustomChatInput({
// Cleanup: reset suggestions visibility when component unmounts
useEffect(
() => () => {
- dispatch(setShouldHideSuggestions(false));
- dispatch(clearAllFiles());
+ setShouldHideSuggestions(false);
+ clearAllFiles();
},
- [dispatch],
+ [setShouldHideSuggestions, clearAllFiles],
);
return (
diff --git a/frontend/src/components/features/chat/interactive-chat-box.tsx b/frontend/src/components/features/chat/interactive-chat-box.tsx
index ef69e4074f..3f33e3c930 100644
--- a/frontend/src/components/features/chat/interactive-chat-box.tsx
+++ b/frontend/src/components/features/chat/interactive-chat-box.tsx
@@ -1,22 +1,14 @@
-import { useSelector, useDispatch } from "react-redux";
+import { useSelector } from "react-redux";
import { isFileImage } from "#/utils/is-file-image";
import { displayErrorToast } from "#/utils/custom-toast-handlers";
import { validateFiles } from "#/utils/file-validation";
import { CustomChatInput } from "./custom-chat-input";
-import { RootState } from "#/store";
import { AgentState } from "#/types/agent-state";
import { useActiveConversation } from "#/hooks/query/use-active-conversation";
import { GitControlBar } from "./git-control-bar";
-import {
- addImages,
- addFiles,
- clearAllFiles,
- addFileLoading,
- removeFileLoading,
- addImageLoading,
- removeImageLoading,
-} from "#/state/conversation-slice";
+import { useConversationStore } from "#/state/conversation-store";
import { processFiles, processImages } from "#/utils/file-processing";
+import { RootState } from "#/store";
interface InteractiveChatBoxProps {
onSubmit: (message: string, images: File[], files: File[]) => void;
@@ -33,12 +25,20 @@ export function InteractiveChatBox({
hasSubstantiveAgentActions,
optimisticUserMessage,
}: InteractiveChatBoxProps) {
- const dispatch = useDispatch();
+ const {
+ images,
+ files,
+ addImages,
+ addFiles,
+ clearAllFiles,
+ addFileLoading,
+ removeFileLoading,
+ addImageLoading,
+ removeImageLoading,
+ } = useConversationStore();
const curAgentState = useSelector(
(state: RootState) => state.agent.curAgentState,
);
- const images = useSelector((state: RootState) => state.conversation.images);
- const files = useSelector((state: RootState) => state.conversation.files);
const { data: conversation } = useActiveConversation();
// Helper function to validate and filter files
@@ -58,26 +58,24 @@ export function InteractiveChatBox({
// Helper function to show loading indicators for files
const showLoadingIndicators = (validFiles: File[], validImages: File[]) => {
- validFiles.forEach((file) => dispatch(addFileLoading(file.name)));
- validImages.forEach((image) => dispatch(addImageLoading(image.name)));
+ validFiles.forEach((file) => addFileLoading(file.name));
+ validImages.forEach((image) => addImageLoading(image.name));
};
// Helper function to handle successful file processing results
const handleSuccessfulFiles = (fileResults: { successful: File[] }) => {
if (fileResults.successful.length > 0) {
- dispatch(addFiles(fileResults.successful));
- fileResults.successful.forEach((file) =>
- dispatch(removeFileLoading(file.name)),
- );
+ addFiles(fileResults.successful);
+ fileResults.successful.forEach((file) => removeFileLoading(file.name));
}
};
// Helper function to handle successful image processing results
const handleSuccessfulImages = (imageResults: { successful: File[] }) => {
if (imageResults.successful.length > 0) {
- dispatch(addImages(imageResults.successful));
+ addImages(imageResults.successful);
imageResults.successful.forEach((image) =>
- dispatch(removeImageLoading(image.name)),
+ removeImageLoading(image.name),
);
}
};
@@ -88,14 +86,14 @@ export function InteractiveChatBox({
imageResults: { failed: { file: File; error: Error }[] },
) => {
fileResults.failed.forEach(({ file, error }) => {
- dispatch(removeFileLoading(file.name));
+ removeFileLoading(file.name);
displayErrorToast(
`Failed to process file ${file.name}: ${error.message}`,
);
});
imageResults.failed.forEach(({ file, error }) => {
- dispatch(removeImageLoading(file.name));
+ removeImageLoading(file.name);
displayErrorToast(
`Failed to process image ${file.name}: ${error.message}`,
);
@@ -104,8 +102,8 @@ export function InteractiveChatBox({
// Helper function to clear loading states on error
const clearLoadingStates = (validFiles: File[], validImages: File[]) => {
- validFiles.forEach((file) => dispatch(removeFileLoading(file.name)));
- validImages.forEach((image) => dispatch(removeImageLoading(image.name)));
+ validFiles.forEach((file) => removeFileLoading(file.name));
+ validImages.forEach((image) => removeImageLoading(image.name));
};
const handleUpload = async (selectedFiles: File[]) => {
@@ -140,7 +138,7 @@ export function InteractiveChatBox({
const handleSubmit = (message: string) => {
onSubmit(message, images, files);
- dispatch(clearAllFiles());
+ clearAllFiles();
};
const handleSuggestionsClick = (suggestion: string) => {
diff --git a/frontend/src/components/features/chat/uploaded-files.tsx b/frontend/src/components/features/chat/uploaded-files.tsx
index d725afeacf..78b0c3dc49 100644
--- a/frontend/src/components/features/chat/uploaded-files.tsx
+++ b/frontend/src/components/features/chat/uploaded-files.tsx
@@ -1,26 +1,23 @@
-import { useSelector, useDispatch } from "react-redux";
-import { RootState } from "#/store";
import { UploadedFile } from "./uploaded-file";
import { UploadedImage } from "./uploaded-image";
-import { removeFile, removeImage } from "#/state/conversation-slice";
+import { useConversationStore } from "#/state/conversation-store";
export function UploadedFiles() {
- const dispatch = useDispatch();
- const images = useSelector((state: RootState) => state.conversation.images);
- const files = useSelector((state: RootState) => state.conversation.files);
- const loadingFiles = useSelector(
- (state: RootState) => state.conversation.loadingFiles,
- );
- const loadingImages = useSelector(
- (state: RootState) => state.conversation.loadingImages,
- );
+ const {
+ images,
+ files,
+ loadingFiles,
+ loadingImages,
+ removeFile,
+ removeImage,
+ } = useConversationStore();
const handleRemoveFile = (index: number) => {
- dispatch(removeFile(index));
+ removeFile(index);
};
const handleRemoveImage = (index: number) => {
- dispatch(removeImage(index));
+ removeImage(index);
};
// Don't render anything if there are no files, images, or loading items
diff --git a/frontend/src/components/features/controls/agent-status.tsx b/frontend/src/components/features/controls/agent-status.tsx
index 6870bc924e..bb09cce983 100644
--- a/frontend/src/components/features/controls/agent-status.tsx
+++ b/frontend/src/components/features/controls/agent-status.tsx
@@ -1,5 +1,5 @@
import { useTranslation } from "react-i18next";
-import { useSelector, useDispatch } from "react-redux";
+import { useSelector } from "react-redux";
import { useEffect } from "react";
import { RootState } from "#/store";
import { useStatusStore } from "#/state/status-store";
@@ -12,7 +12,7 @@ import ClockIcon from "#/icons/u-clock-three.svg?react";
import { ChatResumeAgentButton } from "../chat/chat-play-button";
import { cn } from "#/utils/utils";
import { AgentLoading } from "./agent-loading";
-import { setShouldShownAgentLoading } from "#/state/conversation-slice";
+import { useConversationStore } from "#/state/conversation-store";
import CircleErrorIcon from "#/icons/circle-error.svg?react";
export interface AgentStatusProps {
@@ -29,7 +29,7 @@ export function AgentStatus({
disabled = false,
}: AgentStatusProps) {
const { t } = useTranslation();
- const dispatch = useDispatch();
+ const { setShouldShownAgentLoading } = useConversationStore();
const { curAgentState } = useSelector((state: RootState) => state.agent);
const { curStatusMessage } = useStatusStore();
const { webSocketStatus } = useWsClient();
@@ -58,8 +58,8 @@ export function AgentStatus({
// Update global state when agent loading condition changes
useEffect(() => {
- dispatch(setShouldShownAgentLoading(shouldShownAgentLoading));
- }, [shouldShownAgentLoading, dispatch]);
+ setShouldShownAgentLoading(shouldShownAgentLoading);
+ }, [shouldShownAgentLoading, setShouldShownAgentLoading]);
return (
<div className={`flex items-center gap-1 ${className}`}>
diff --git a/frontend/src/components/features/controls/git-tools-submenu.tsx b/frontend/src/components/features/controls/git-tools-submenu.tsx
index b291f1de82..e55dd93952 100644
--- a/frontend/src/components/features/controls/git-tools-submenu.tsx
+++ b/frontend/src/components/features/controls/git-tools-submenu.tsx
@@ -1,5 +1,4 @@
import { useTranslation } from "react-i18next";
-import { useDispatch } from "react-redux";
import { ContextMenu } from "#/ui/context-menu";
import { ContextMenuListItem } from "../context-menu/context-menu-list-item";
import { ToolsContextMenuIconText } from "./tools-context-menu-icon-text";
@@ -11,7 +10,7 @@ import {
getCreatePRPrompt,
getCreateNewBranchPrompt,
} from "#/utils/utils";
-import { setMessageToSend } from "#/state/conversation-slice";
+import { useConversationStore } from "#/state/conversation-store";
import ArrowUpIcon from "#/icons/u-arrow-up.svg?react";
import ArrowDownIcon from "#/icons/u-arrow-down.svg?react";
@@ -28,28 +27,28 @@ interface GitToolsSubmenuProps {
export function GitToolsSubmenu({ onClose }: GitToolsSubmenuProps) {
const { t } = useTranslation();
- const dispatch = useDispatch();
+ const { setMessageToSend } = useConversationStore();
const { data: conversation } = useActiveConversation();
const currentGitProvider = conversation?.git_provider as Provider;
const onGitPull = () => {
- dispatch(setMessageToSend(getGitPullPrompt()));
+ setMessageToSend(getGitPullPrompt());
onClose();
};
const onGitPush = () => {
- dispatch(setMessageToSend(getGitPushPrompt(currentGitProvider)));
+ setMessageToSend(getGitPushPrompt(currentGitProvider));
onClose();
};
const onCreatePR = () => {
- dispatch(setMessageToSend(getCreatePRPrompt(currentGitProvider)));
+ setMessageToSend(getCreatePRPrompt(currentGitProvider));
onClose();
};
const onCreateNewBranch = () => {
- dispatch(setMessageToSend(getCreateNewBranchPrompt()));
+ setMessageToSend(getCreateNewBranchPrompt());
onClose();
};
diff --git a/frontend/src/components/features/controls/macros-submenu.tsx b/frontend/src/components/features/controls/macros-submenu.tsx
index 20fff53503..8705e11959 100644
--- a/frontend/src/components/features/controls/macros-submenu.tsx
+++ b/frontend/src/components/features/controls/macros-submenu.tsx
@@ -1,5 +1,4 @@
import { useTranslation } from "react-i18next";
-import { useDispatch } from "react-redux";
import { ContextMenu } from "#/ui/context-menu";
import { ContextMenuListItem } from "../context-menu/context-menu-list-item";
import { ToolsContextMenuIconText } from "./tools-context-menu-icon-text";
@@ -9,7 +8,7 @@ import PrStatusIcon from "#/icons/pr-status.svg?react";
import DocumentIcon from "#/icons/document.svg?react";
import WaterIcon from "#/icons/u-water.svg?react";
import { I18nKey } from "#/i18n/declaration";
-import { setMessageToSend } from "#/state/conversation-slice";
+import { useConversationStore } from "#/state/conversation-store";
import { REPO_SUGGESTIONS } from "#/utils/suggestions/repo-suggestions";
import { CONTEXT_MENU_ICON_TEXT_CLASSNAME } from "#/utils/constants";
@@ -22,22 +21,22 @@ interface MacrosSubmenuProps {
export function MacrosSubmenu({ onClose }: MacrosSubmenuProps) {
const { t } = useTranslation();
- const dispatch = useDispatch();
+ const { setMessageToSend } = useConversationStore();
const onIncreaseTestCoverage = () => {
- dispatch(setMessageToSend(REPO_SUGGESTIONS.INCREASE_TEST_COVERAGE));
+ setMessageToSend(REPO_SUGGESTIONS.INCREASE_TEST_COVERAGE);
onClose();
};
const onFixReadme = () => {
- dispatch(setMessageToSend(REPO_SUGGESTIONS.FIX_README));
+ setMessageToSend(REPO_SUGGESTIONS.FIX_README);
onClose();
};
const onAutoMergePRs = () => {
- dispatch(setMessageToSend(REPO_SUGGESTIONS.AUTO_MERGE_PRS));
+ setMessageToSend(REPO_SUGGESTIONS.AUTO_MERGE_PRS);
onClose();
};
const onCleanDependencies = () => {
- dispatch(setMessageToSend(REPO_SUGGESTIONS.CLEAN_DEPENDENCIES));
+ setMessageToSend(REPO_SUGGESTIONS.CLEAN_DEPENDENCIES);
onClose();
};
diff --git a/frontend/src/components/features/conversation/conversation-main/conversation-main.tsx b/frontend/src/components/features/conversation/conversation-main/conversation-main.tsx
index b570f3c679..d5c7ad3a0e 100644
--- a/frontend/src/components/features/conversation/conversation-main/conversation-main.tsx
+++ b/frontend/src/components/features/conversation/conversation-main/conversation-main.tsx
@@ -1,14 +1,11 @@
-import { useSelector } from "react-redux";
import { useWindowSize } from "@uidotdev/usehooks";
-import { RootState } from "#/store";
import { MobileLayout } from "./mobile-layout";
import { DesktopLayout } from "./desktop-layout";
+import { useConversationStore } from "#/state/conversation-store";
export function ConversationMain() {
const { width } = useWindowSize();
- const isRightPanelShown = useSelector(
- (state: RootState) => state.conversation.isRightPanelShown,
- );
+ const { isRightPanelShown } = useConversationStore();
if (width && width <= 1024) {
return <MobileLayout isRightPanelShown={isRightPanelShown} />;
diff --git a/frontend/src/components/features/conversation/conversation-tabs/conversation-tab-content/conversation-tab-content.tsx b/frontend/src/components/features/conversation/conversation-tabs/conversation-tab-content/conversation-tab-content.tsx
index 6fad4ea286..271e7a750a 100644
--- a/frontend/src/components/features/conversation/conversation-tabs/conversation-tab-content/conversation-tab-content.tsx
+++ b/frontend/src/components/features/conversation/conversation-tabs/conversation-tab-content/conversation-tab-content.tsx
@@ -1,7 +1,5 @@
import { lazy, useMemo } from "react";
import { useTranslation } from "react-i18next";
-import { useSelector } from "react-redux";
-import { RootState } from "#/store";
import { ConversationLoading } from "../../conversation-loading";
import { I18nKey } from "#/i18n/declaration";
import { TabWrapper } from "./tab-wrapper";
@@ -9,6 +7,7 @@ import { TabContainer } from "./tab-container";
import { TabContentArea } from "./tab-content-area";
import { ConversationTabTitle } from "../conversation-tab-title";
import Terminal from "#/components/features/terminal/terminal";
+import { useConversationStore } from "#/state/conversation-store";
// Lazy load all tab components
const EditorTab = lazy(() => import("#/routes/changes-tab"));
@@ -18,12 +17,7 @@ const ServedTab = lazy(() => import("#/routes/served-tab"));
const VSCodeTab = lazy(() => import("#/routes/vscode-tab"));
export function ConversationTabContent() {
- const selectedTab = useSelector(
- (state: RootState) => state.conversation.selectedTab,
- );
- const { shouldShownAgentLoading } = useSelector(
- (state: RootState) => state.conversation,
- );
+ const { selectedTab, shouldShownAgentLoading } = useConversationStore();
const { t } = useTranslation();
diff --git a/frontend/src/components/features/conversation/conversation-tabs/conversation-tabs.tsx b/frontend/src/components/features/conversation/conversation-tabs/conversation-tabs.tsx
index a329a4aaa0..c4240d0121 100644
--- a/frontend/src/components/features/conversation/conversation-tabs/conversation-tabs.tsx
+++ b/frontend/src/components/features/conversation/conversation-tabs/conversation-tabs.tsx
@@ -1,5 +1,4 @@
import { useEffect } from "react";
-import { useDispatch, useSelector } from "react-redux";
import { useTranslation } from "react-i18next";
import { useLocalStorage } from "@uidotdev/usehooks";
import JupyterIcon from "#/icons/jupyter.svg?react";
@@ -14,21 +13,18 @@ import { ChatActionTooltip } from "../../chat/chat-action-tooltip";
import { I18nKey } from "#/i18n/declaration";
import { VSCodeTooltipContent } from "./vscode-tooltip-content";
import {
- setHasRightPanelToggled,
- setSelectedTab,
- setIsRightPanelShown,
+ useConversationStore,
type ConversationTab,
-} from "#/state/conversation-slice";
-import { RootState } from "#/store";
+} from "#/state/conversation-store";
export function ConversationTabs() {
- const dispatch = useDispatch();
- const selectedTab = useSelector(
- (state: RootState) => state.conversation.selectedTab,
- );
- const { isRightPanelShown } = useSelector(
- (state: RootState) => state.conversation,
- );
+ const {
+ selectedTab,
+ isRightPanelShown,
+ setHasRightPanelToggled,
+ setSelectedTab,
+ setIsRightPanelShown,
+ } = useConversationStore();
// Persist selectedTab and isRightPanelShown in localStorage
const [persistedSelectedTab, setPersistedSelectedTab] =
@@ -41,18 +37,24 @@ export function ConversationTabs() {
useLocalStorage<boolean>("conversation-right-panel-shown", true);
const onTabChange = (value: ConversationTab | null) => {
- dispatch(setSelectedTab(value));
+ setSelectedTab(value);
// Persist the selected tab to localStorage
setPersistedSelectedTab(value);
};
- // Initialize Redux state from localStorage on component mount
+ // Initialize Zustand state from localStorage on component mount
useEffect(() => {
// Initialize selectedTab from localStorage if available
- dispatch(setSelectedTab(persistedSelectedTab));
- dispatch(setIsRightPanelShown(persistedIsRightPanelShown));
- dispatch(setHasRightPanelToggled(persistedIsRightPanelShown));
- }, []);
+ setSelectedTab(persistedSelectedTab);
+ setIsRightPanelShown(persistedIsRightPanelShown);
+ setHasRightPanelToggled(persistedIsRightPanelShown);
+ }, [
+ setSelectedTab,
+ setIsRightPanelShown,
+ setHasRightPanelToggled,
+ persistedSelectedTab,
+ persistedIsRightPanelShown,
+ ]);
useEffect(() => {
const handlePanelVisibilityChange = () => {
@@ -72,13 +74,13 @@ export function ConversationTabs() {
const onTabSelected = (tab: ConversationTab) => {
if (selectedTab === tab && isRightPanelShown) {
// If clicking the same active tab, close the drawer
- dispatch(setHasRightPanelToggled(false));
+ setHasRightPanelToggled(false);
setPersistedIsRightPanelShown(false);
} else {
// If clicking a different tab or drawer is closed, open drawer and select tab
onTabChange(tab);
if (!isRightPanelShown) {
- dispatch(setHasRightPanelToggled(true));
+ setHasRightPanelToggled(true);
setPersistedIsRightPanelShown(true);
}
}
diff --git a/frontend/src/hooks/chat/use-chat-input-logic.ts b/frontend/src/hooks/chat/use-chat-input-logic.ts
index e539cd4fc1..d908882a40 100644
--- a/frontend/src/hooks/chat/use-chat-input-logic.ts
+++ b/frontend/src/hooks/chat/use-chat-input-logic.ts
@@ -1,15 +1,10 @@
import { useRef, useCallback, useEffect } from "react";
-import { useDispatch, useSelector } from "react-redux";
-import {
- setMessageToSend,
- setIsRightPanelShown,
-} from "#/state/conversation-slice";
-import { RootState } from "#/store";
import {
isContentEmpty,
clearEmptyContent,
getTextContent,
} from "#/components/features/chat/utils/chat-input.utils";
+import { useConversationStore } from "#/state/conversation-store";
/**
* Hook for managing chat input content logic
@@ -17,20 +12,21 @@ import {
export const useChatInputLogic = () => {
const chatInputRef = useRef<HTMLDivElement | null>(null);
- const { messageToSend, hasRightPanelToggled } = useSelector(
- (state: RootState) => state.conversation,
- );
-
- const dispatch = useDispatch();
+ const {
+ messageToSend,
+ hasRightPanelToggled,
+ setMessageToSend,
+ setIsRightPanelShown,
+ } = useConversationStore();
// Save current input value when drawer state changes
useEffect(() => {
if (chatInputRef.current) {
const currentText = getTextContent(chatInputRef.current);
- dispatch(setMessageToSend(currentText));
- dispatch(setIsRightPanelShown(hasRightPanelToggled));
+ setMessageToSend(currentText);
+ setIsRightPanelShown(hasRightPanelToggled);
}
- }, [hasRightPanelToggled, dispatch]);
+ }, [hasRightPanelToggled, setMessageToSend, setIsRightPanelShown]);
// Helper function to check if contentEditable is truly empty
const checkIsContentEmpty = useCallback(
diff --git a/frontend/src/hooks/chat/use-grip-resize.ts b/frontend/src/hooks/chat/use-grip-resize.ts
index 976663f99e..d57097fcdb 100644
--- a/frontend/src/hooks/chat/use-grip-resize.ts
+++ b/frontend/src/hooks/chat/use-grip-resize.ts
@@ -1,11 +1,10 @@
import { useRef, useState, useCallback } from "react";
-import { useDispatch } from "react-redux";
import { useAutoResize } from "#/hooks/use-auto-resize";
+import { CHAT_INPUT } from "#/utils/constants";
import {
IMessageToSend,
- setShouldHideSuggestions,
-} from "#/state/conversation-slice";
-import { CHAT_INPUT } from "#/utils/constants";
+ useConversationStore,
+} from "#/state/conversation-store";
/**
* Hook for managing grip resize functionality
@@ -14,11 +13,11 @@ export const useGripResize = (
chatInputRef: React.RefObject<HTMLDivElement | null>,
messageToSend: IMessageToSend | null,
) => {
- const gripRef = useRef<HTMLDivElement | null>(null);
-
const [isGripVisible, setIsGripVisible] = useState(false);
- const dispatch = useDispatch();
+ const { setShouldHideSuggestions } = useConversationStore();
+
+ const gripRef = useRef<HTMLDivElement | null>(null);
// Drag state management callbacks
const handleDragStart = useCallback(() => {
@@ -48,9 +47,9 @@ export const useGripResize = (
(height: number) => {
// Hide suggestions when input height exceeds the threshold
const shouldHideChatSuggestions = height > CHAT_INPUT.HEIGHT_THRESHOLD;
- dispatch(setShouldHideSuggestions(shouldHideChatSuggestions));
+ setShouldHideSuggestions(shouldHideChatSuggestions);
},
- [dispatch],
+ [setShouldHideSuggestions],
);
// Use the auto-resize hook with height change callback
diff --git a/frontend/src/hooks/use-auto-resize.ts b/frontend/src/hooks/use-auto-resize.ts
index 5edc5ad44d..d92b111e0e 100644
--- a/frontend/src/hooks/use-auto-resize.ts
+++ b/frontend/src/hooks/use-auto-resize.ts
@@ -1,5 +1,5 @@
import { useCallback, useEffect, RefObject } from "react";
-import { IMessageToSend } from "#/state/conversation-slice";
+import { IMessageToSend } from "#/state/conversation-store";
import { useDragResize } from "./use-drag-resize";
// Constants
diff --git a/frontend/src/routes/conversation.tsx b/frontend/src/routes/conversation.tsx
index 7467b66fdd..e8493594af 100644
--- a/frontend/src/routes/conversation.tsx
+++ b/frontend/src/routes/conversation.tsx
@@ -7,7 +7,7 @@ import { useConversationId } from "#/hooks/use-conversation-id";
import { useCommandStore } from "#/state/command-store";
import { useEffectOnce } from "#/hooks/use-effect-once";
import { clearJupyter } from "#/state/jupyter-slice";
-import { resetConversationState } from "#/state/conversation-slice";
+import { useConversationStore } from "#/state/conversation-store";
import { setCurrentAgentState } from "#/state/agent-slice";
import { AgentState } from "#/types/agent-state";
@@ -38,6 +38,7 @@ function AppContent() {
const { mutate: startConversation } = useStartConversation();
const { data: isAuthed } = useIsAuthed();
const { providers } = useUserProviders();
+ const { resetConversationState } = useConversationStore();
const dispatch = useDispatch();
const navigate = useNavigate();
const clearTerminal = useCommandStore((state) => state.clearTerminal);
@@ -86,14 +87,14 @@ function AppContent() {
React.useEffect(() => {
clearTerminal();
dispatch(clearJupyter());
- dispatch(resetConversationState());
+ resetConversationState();
dispatch(setCurrentAgentState(AgentState.LOADING));
- }, [conversationId, clearTerminal]);
+ }, [conversationId, clearTerminal, resetConversationState]);
useEffectOnce(() => {
clearTerminal();
dispatch(clearJupyter());
- dispatch(resetConversationState());
+ resetConversationState();
dispatch(setCurrentAgentState(AgentState.LOADING));
});
diff --git a/frontend/src/state/conversation-slice.tsx b/frontend/src/state/conversation-slice.tsx
deleted file mode 100644
index e25d59cdc9..0000000000
--- a/frontend/src/state/conversation-slice.tsx
+++ /dev/null
@@ -1,151 +0,0 @@
-import { createSlice } from "@reduxjs/toolkit";
-
-export type ConversationTab =
- | "editor"
- | "browser"
- | "jupyter"
- | "served"
- | "vscode"
- | "terminal";
-
-export interface IMessageToSend {
- text: string;
- timestamp: number;
-}
-
-interface ConversationState {
- isRightPanelShown: boolean;
- selectedTab: ConversationTab | null;
- images: File[];
- files: File[];
- loadingFiles: string[]; // File names currently being processed
- loadingImages: string[]; // Image names currently being processed
- messageToSend: IMessageToSend | null;
- shouldShownAgentLoading: boolean;
- submittedMessage: string | null;
- shouldHideSuggestions: boolean; // New state to hide suggestions when input expands
- hasRightPanelToggled: boolean;
-}
-
-export const conversationSlice = createSlice({
- name: "conversation",
- initialState: {
- isRightPanelShown: true,
- selectedTab: "editor" as ConversationTab,
- shouldStopConversation: false,
- shouldStartConversation: false,
- images: [],
- files: [],
- loadingFiles: [],
- loadingImages: [],
- messageToSend: null,
- shouldShownAgentLoading: false,
- submittedMessage: null,
- shouldHideSuggestions: false, // Initialize to false
- hasRightPanelToggled: true,
- } as ConversationState,
- reducers: {
- setIsRightPanelShown: (state, action) => {
- state.isRightPanelShown = action.payload;
- },
- setSelectedTab: (state, action) => {
- state.selectedTab = action.payload;
- },
- setShouldShownAgentLoading: (state, action) => {
- state.shouldShownAgentLoading = action.payload;
- },
- setShouldHideSuggestions: (state, action) => {
- state.shouldHideSuggestions = action.payload;
- },
- addImages: (state, action) => {
- state.images = [...state.images, ...action.payload];
- },
- addFiles: (state, action) => {
- state.files = [...state.files, ...action.payload];
- },
- removeImage: (state, action) => {
- state.images.splice(action.payload, 1);
- },
- removeFile: (state, action) => {
- state.files.splice(action.payload, 1);
- },
- clearImages: (state) => {
- state.images = [];
- },
- clearFiles: (state) => {
- state.files = [];
- },
- clearAllFiles: (state) => {
- state.images = [];
- state.files = [];
- state.loadingFiles = [];
- state.loadingImages = [];
- },
- // Loading state management
- addFileLoading: (state, action) => {
- if (!state.loadingFiles.includes(action.payload)) {
- state.loadingFiles.push(action.payload);
- }
- },
- removeFileLoading: (state, action) => {
- state.loadingFiles = state.loadingFiles.filter(
- (name) => name !== action.payload,
- );
- },
- addImageLoading: (state, action) => {
- if (!state.loadingImages.includes(action.payload)) {
- state.loadingImages.push(action.payload);
- }
- },
- removeImageLoading: (state, action) => {
- state.loadingImages = state.loadingImages.filter(
- (name) => name !== action.payload,
- );
- },
- clearAllLoading: (state) => {
- state.loadingFiles = [];
- state.loadingImages = [];
- },
- setMessageToSend: (state, action) => {
- state.messageToSend = {
- text: action.payload,
- timestamp: Date.now(),
- };
- },
- setSubmittedMessage: (state, action) => {
- state.submittedMessage = action.payload;
- },
- // Reset conversation state (useful for cleanup)
- resetConversationState: (state) => {
- state.shouldHideSuggestions = false;
- },
- setHasRightPanelToggled: (state, action) => {
- state.hasRightPanelToggled = action.payload;
- },
- },
-});
-
-export const {
- setIsRightPanelShown,
- setSelectedTab,
- setShouldShownAgentLoading,
- setShouldHideSuggestions,
- addImages,
- addFiles,
- removeImage,
- removeFile,
- clearImages,
- clearFiles,
- clearAllFiles,
- addFileLoading,
- removeFileLoading,
- addImageLoading,
- removeImageLoading,
- clearAllLoading,
- setMessageToSend,
- setSubmittedMessage,
- resetConversationState,
- setHasRightPanelToggled,
-} = conversationSlice.actions;
-
-export default conversationSlice.reducer;
diff --git a/frontend/src/state/conversation-store.ts b/frontend/src/state/conversation-store.ts
new file mode 100644
index 0000000000..934d87ec06
--- /dev/null
+++ b/frontend/src/state/conversation-store.ts
@@ -0,0 +1,211 @@
+import { create } from "zustand";
+import { devtools } from "zustand/middleware";
+
+export type ConversationTab =
+ | "editor"
+ | "browser"
+ | "jupyter"
+ | "served"
+ | "vscode"
+ | "terminal";
+
+export interface IMessageToSend {
+ text: string;
+ timestamp: number;
+}
+
+interface ConversationState {
+ isRightPanelShown: boolean;
+ selectedTab: ConversationTab | null;
+ images: File[];
+ files: File[];
+ loadingFiles: string[]; // File names currently being processed
+ loadingImages: string[]; // Image names currently being processed
+ messageToSend: IMessageToSend | null;
+ shouldShownAgentLoading: boolean;
+ submittedMessage: string | null;
+ shouldHideSuggestions: boolean; // New state to hide suggestions when input expands
+ hasRightPanelToggled: boolean;
+}
+
+interface ConversationActions {
+ setIsRightPanelShown: (isRightPanelShown: boolean) => void;
+ setSelectedTab: (selectedTab: ConversationTab | null) => void;
+ setShouldShownAgentLoading: (shouldShownAgentLoading: boolean) => void;
+ setShouldHideSuggestions: (shouldHideSuggestions: boolean) => void;
+ addImages: (images: File[]) => void;
+ addFiles: (files: File[]) => void;
+ removeImage: (index: number) => void;
+ removeFile: (index: number) => void;
+ clearImages: () => void;
+ clearFiles: () => void;
+ clearAllFiles: () => void;
+ addFileLoading: (fileName: string) => void;
+ removeFileLoading: (fileName: string) => void;
+ addImageLoading: (imageName: string) => void;
+ removeImageLoading: (imageName: string) => void;
+ clearAllLoading: () => void;
+ setMessageToSend: (text: string) => void;
+ setSubmittedMessage: (message: string | null) => void;
+ resetConversationState: () => void;
+ setHasRightPanelToggled: (hasRightPanelToggled: boolean) => void;
+}
+
+type ConversationStore = ConversationState & ConversationActions;
+
+export const useConversationStore = create<ConversationStore>()(
+ devtools(
+ (set) => ({
+ // Initial state
+ isRightPanelShown: true,
+ selectedTab: "editor" as ConversationTab,
+ images: [],
+ files: [],
+ loadingFiles: [],
+ loadingImages: [],
+ messageToSend: null,
+ shouldShownAgentLoading: false,
+ submittedMessage: null,
+ shouldHideSuggestions: false,
+ hasRightPanelToggled: true,
+
+ // Actions
+ setIsRightPanelShown: (isRightPanelShown) =>
+ set({ isRightPanelShown }, false, "setIsRightPanelShown"),
+
+ setSelectedTab: (selectedTab) =>
+ set({ selectedTab }, false, "setSelectedTab"),
+
+ setShouldShownAgentLoading: (shouldShownAgentLoading) =>
+ set({ shouldShownAgentLoading }, false, "setShouldShownAgentLoading"),
+
+ setShouldHideSuggestions: (shouldHideSuggestions) =>
+ set({ shouldHideSuggestions }, false, "setShouldHideSuggestions"),
+
+ addImages: (images) =>
+ set(
+ (state) => ({ images: [...state.images, ...images] }),
+ false,
+ "addImages",
+ ),
+
+ addFiles: (files) =>
+ set(
+ (state) => ({ files: [...state.files, ...files] }),
+ false,
+ "addFiles",
+ ),
+
+ removeImage: (index) =>
+ set(
+ (state) => {
+ const newImages = [...state.images];
+ newImages.splice(index, 1);
+ return { images: newImages };
+ },
+ false,
+ "removeImage",
+ ),
+
+ removeFile: (index) =>
+ set(
+ (state) => {
+ const newFiles = [...state.files];
+ newFiles.splice(index, 1);
+ return { files: newFiles };
+ },
+ false,
+ "removeFile",
+ ),
+
+ clearImages: () => set({ images: [] }, false, "clearImages"),
+
+ clearFiles: () => set({ files: [] }, false, "clearFiles"),
+
+ clearAllFiles: () =>
+ set(
+ {
+ images: [],
+ files: [],
+ loadingFiles: [],
+ loadingImages: [],
+ },
+ false,
+ "clearAllFiles",
+ ),
+
+ addFileLoading: (fileName) =>
+ set(
+ (state) => {
+ if (!state.loadingFiles.includes(fileName)) {
+ return { loadingFiles: [...state.loadingFiles, fileName] };
+ }
+ return state;
+ },
+ false,
+ "addFileLoading",
+ ),
+
+ removeFileLoading: (fileName) =>
+ set(
+ (state) => ({
+ loadingFiles: state.loadingFiles.filter(
+ (name) => name !== fileName,
+ ),
+ }),
+ false,
+ "removeFileLoading",
+ ),
+
+ addImageLoading: (imageName) =>
+ set(
+ (state) => {
+ if (!state.loadingImages.includes(imageName)) {
+ return { loadingImages: [...state.loadingImages, imageName] };
+ }
+ return state;
+ },
+ false,
+ "addImageLoading",
+ ),
+
+ removeImageLoading: (imageName) =>
+ set(
+ (state) => ({
+ loadingImages: state.loadingImages.filter(
+ (name) => name !== imageName,
+ ),
+ }),
+ false,
+ "removeImageLoading",
+ ),
+
+ clearAllLoading: () =>
+ set({ loadingFiles: [], loadingImages: [] }, false, "clearAllLoading"),
+
+ setMessageToSend: (text) =>
+ set(
+ {
+ messageToSend: {
+ text,
+ timestamp: Date.now(),
+ },
+ },
+ false,
+ "setMessageToSend",
+ ),
+
+ setSubmittedMessage: (submittedMessage) =>
+ set({ submittedMessage }, false, "setSubmittedMessage"),
+
+ resetConversationState: () =>
+ set({ shouldHideSuggestions: false }, false, "resetConversationState"),
+
+ setHasRightPanelToggled: (hasRightPanelToggled) =>
+ set({ hasRightPanelToggled }, false, "setHasRightPanelToggled"),
+ }),
+ {
+ name: "conversation-store",
+ },
+ ),
+);
diff --git a/frontend/src/store.ts b/frontend/src/store.ts
index 8da8d3734d..4890710d43 100644
--- a/frontend/src/store.ts
+++ b/frontend/src/store.ts
@@ -5,7 +5,6 @@ import fileStateReducer from "./state/file-state-slice";
import { jupyterReducer } from "./state/jupyter-slice";
import securityAnalyzerReducer from "./state/security-analyzer-slice";
import microagentManagementReducer from "./state/microagent-management-slice";
-import conversationReducer from "./state/conversation-slice";
import eventMessageReducer from "./state/event-message-slice";
export const rootReducer = combineReducers({
@@ -15,7 +14,6 @@ export const rootReducer = combineReducers({
jupyter: jupyterReducer,
securityAnalyzer: securityAnalyzerReducer,
microagentManagement: microagentManagementReducer,
- conversation: conversationReducer,
eventMessage: eventMessageReducer,
});