π OpenHands: AI-Driven Development
fix(frontend): Prevent editor from changing width unpredictably (#4659)
sp.wack73c7375b9291
diff --git a/frontend/__tests__/components/file-explorer/FileExplorer.test.tsx b/frontend/__tests__/components/file-explorer/FileExplorer.test.tsxindex b1faa3c18b..a1c0717783 100644--- a/frontend/__tests__/components/file-explorer/FileExplorer.test.tsx+++ b/frontend/__tests__/components/file-explorer/FileExplorer.test.tsx@@ -16,13 +16,16 @@ vi.mock("../../services/fileService", async () => ({}));const renderFileExplorerWithRunningAgentState = () =>- renderWithProviders(<FileExplorer error={null} />, {- preloadedState: {- agent: {- curAgentState: AgentState.RUNNING,+ renderWithProviders(+ <FileExplorer error={null} isOpen onToggle={() => {}} />,+ {+ preloadedState: {+ agent: {+ curAgentState: AgentState.RUNNING,+ },},},- });+ );describe.skip("FileExplorer", () => {afterEach(() => {diff --git a/frontend/src/components/file-explorer/FileExplorer.tsx b/frontend/src/components/file-explorer/FileExplorer.tsxindex c6e2c249fe..c61990b4d6 100644--- a/frontend/src/components/file-explorer/FileExplorer.tsx+++ b/frontend/src/components/file-explorer/FileExplorer.tsx@@ -91,14 +91,15 @@ function ExplorerActions({}interface FileExplorerProps {+ isOpen: boolean;+ onToggle: () => void;error: string | null;}-function FileExplorer({ error }: FileExplorerProps) {+function FileExplorer({ error, isOpen, onToggle }: FileExplorerProps) {const { revalidate } = useRevalidator();const { paths, setPaths } = useFiles();- const [isHidden, setIsHidden] = React.useState(false);const [isDragging, setIsDragging] = React.useState(false);const { curAgentState } = useSelector((state: RootState) => state.agent);@@ -211,7 +212,7 @@ function FileExplorer({ error }: FileExplorerProps) {<divclassName={twMerge("bg-neutral-800 h-full border-r-1 border-r-neutral-600 flex flex-col",- isHidden ? "w-12" : "w-60",+ !isOpen ? "w-12" : "w-60",)}><div className="flex flex-col relative h-full px-3 py-2">@@ -219,17 +220,17 @@ function FileExplorer({ error }: FileExplorerProps) {<divclassName={twMerge("flex items-center",- isHidden ? "justify-center" : "justify-between",+ !isOpen ? "justify-center" : "justify-between",)}>- {!isHidden && (+ {isOpen && (<div className="text-neutral-300 font-bold text-sm">{t(I18nKey.EXPLORER$LABEL_WORKSPACE)}</div>)}<ExplorerActions- isHidden={isHidden}- toggleHidden={() => setIsHidden((prev) => !prev)}+ isHidden={!isOpen}+ toggleHidden={onToggle}onRefresh={refreshWorkspace}onUpload={selectFileInput}/>@@ -237,7 +238,7 @@ function FileExplorer({ error }: FileExplorerProps) {</div>{!error && (<div className="overflow-auto flex-grow">- <div style={{ display: isHidden ? "none" : "block" }}>+ <div style={{ display: !isOpen ? "none" : "block" }}><ExplorerTree files={paths} /></div></div>diff --git a/frontend/src/routes/_oh.app._index/code-editor-component.tsx b/frontend/src/routes/_oh.app._index/code-editor-component.tsxindex cf94ed863a..9288fc225e 100644--- a/frontend/src/routes/_oh.app._index/code-editor-component.tsx+++ b/frontend/src/routes/_oh.app._index/code-editor-component.tsx@@ -1,18 +1,21 @@-import { Editor, Monaco } from "@monaco-editor/react";+import { Editor, EditorProps } from "@monaco-editor/react";import React from "react";import { useTranslation } from "react-i18next";import { VscCode } from "react-icons/vsc";-import { type editor } from "monaco-editor";import toast from "react-hot-toast";import { I18nKey } from "#/i18n/declaration";import { useFiles } from "#/context/files";import OpenHands from "#/api/open-hands";interface CodeEditorCompoonentProps {+ onMount: EditorProps["onMount"];isReadOnly: boolean;}-function CodeEditorCompoonent({ isReadOnly }: CodeEditorCompoonentProps) {+function CodeEditorCompoonent({+ onMount,+ isReadOnly,+}: CodeEditorCompoonentProps) {const { t } = useTranslation();const {files,@@ -22,22 +25,6 @@ function CodeEditorCompoonent({ isReadOnly }: CodeEditorCompoonentProps) {saveFileContent: saveNewFileContent,} = useFiles();- const handleEditorDidMount = React.useCallback(- (editor: editor.IStandaloneCodeEditor, monaco: Monaco): void => {- monaco.editor.defineTheme("my-theme", {- base: "vs-dark",- inherit: true,- rules: [],- colors: {- "editor.background": "#171717",- },- });-- monaco.editor.setTheme("my-theme");- },- [],- );-const handleEditorChange = (value: string | undefined) => {if (selectedPath && value) modifyFileContent(selectedPath, value);};@@ -68,7 +55,7 @@ function CodeEditorCompoonent({ isReadOnly }: CodeEditorCompoonentProps) {return (<divdata-testid="code-editor-empty-message"- className="flex flex-col items-center text-neutral-400"+ className="flex flex-col h-full items-center justify-center text-neutral-400"><VscCode size={100} />{t(I18nKey.CODE_EDITOR$EMPTY_MESSAGE)}@@ -79,7 +66,6 @@ function CodeEditorCompoonent({ isReadOnly }: CodeEditorCompoonentProps) {return (<Editordata-testid="code-editor"- height="100%"path={selectedPath ?? undefined}defaultValue=""value={@@ -87,7 +73,7 @@ function CodeEditorCompoonent({ isReadOnly }: CodeEditorCompoonentProps) {? modifiedFiles[selectedPath] || files[selectedPath]: undefined}- onMount={handleEditorDidMount}+ onMount={onMount}onChange={handleEditorChange}options={{ readOnly: isReadOnly }}/>diff --git a/frontend/src/routes/_oh.app._index/route.tsx b/frontend/src/routes/_oh.app._index/route.tsxindex ba20e003f7..7f63350f12 100644--- a/frontend/src/routes/_oh.app._index/route.tsx+++ b/frontend/src/routes/_oh.app._index/route.tsx@@ -2,6 +2,8 @@ import React from "react";import { useSelector } from "react-redux";import { json, useLoaderData, useRouteError } from "@remix-run/react";import toast from "react-hot-toast";+import { editor } from "monaco-editor";+import { EditorProps } from "@monaco-editor/react";import { RootState } from "#/store";import AgentState from "#/types/AgentState";import FileExplorer from "#/components/file-explorer/FileExplorer";@@ -37,6 +39,27 @@ function CodeEditor() {saveFileContent: saveNewFileContent,discardChanges,} = useFiles();+ const [fileExplorerIsOpen, setFileExplorerIsOpen] = React.useState(true);+ const editorRef = React.useRef<editor.IStandaloneCodeEditor | null>(null);++ const toggleFileExplorer = () => {+ setFileExplorerIsOpen((prev) => !prev);+ editorRef.current?.layout({ width: 0, height: 0 });+ };++ const handleEditorDidMount: EditorProps["onMount"] = (e, monaco) => {+ editorRef.current = e;++ monaco.editor.defineTheme("oh-dark", {+ base: "vs-dark",+ inherit: true,+ rules: [],+ colors: {+ "editor.background": "#171717",+ },+ });+ monaco.editor.setTheme("oh-dark");+ };const [errors, setErrors] = React.useState<{ getFiles: string | null }>({getFiles: null,@@ -85,9 +108,13 @@ function CodeEditor() {};return (- <div className="flex h-full w-full bg-neutral-900 relative">- <FileExplorer error={errors.getFiles} />- <div className="flex flex-col min-h-0 w-full">+ <div className="flex h-full bg-neutral-900 relative">+ <FileExplorer+ isOpen={fileExplorerIsOpen}+ onToggle={toggleFileExplorer}+ error={errors.getFiles}+ />+ <div className="w-full">{selectedPath && (<div className="flex w-full items-center justify-between self-end p-2"><span className="text-sm text-neutral-500">{selectedPath}</span>@@ -98,9 +125,10 @@ function CodeEditor() {/></div>)}- <div className="flex grow items-center justify-center">- <CodeEditorCompoonent isReadOnly={!isEditingAllowed} />- </div>+ <CodeEditorCompoonent+ onMount={handleEditorDidMount}+ isReadOnly={!isEditingAllowed}+ /></div></div>);diff --git a/frontend/src/routes/_oh.app.tsx b/frontend/src/routes/_oh.app.tsxindex 9af5ad278b..43659724d5 100644--- a/frontend/src/routes/_oh.app.tsx+++ b/frontend/src/routes/_oh.app.tsx@@ -315,7 +315,7 @@ function App() {return (<div className="flex flex-col h-full gap-3"><div className="flex h-full overflow-auto gap-3">- <Container className="w-[375px] max-h-full">+ <Container className="w-[390px] max-h-full"><ChatInterface /></Container>