Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix(frontend): rebuild the file explorer; goodbye lag (#1221)

sp.wacke82543747cce
diff --git a/frontend/src/components/CodeEditor.tsx b/frontend/src/components/CodeEditor.tsx
index a1b735f884..d19fd87c2e 100644
--- a/frontend/src/components/CodeEditor.tsx
+++ b/frontend/src/components/CodeEditor.tsx
@@ -2,13 +2,16 @@ import Editor, { Monaco } from "@monaco-editor/react";
import { Tab, Tabs } from "@nextui-org/react";
import type { editor } from "monaco-editor";
import React, { useState } from "react";
-import { useSelector } from "react-redux";
+import { useDispatch, useSelector } from "react-redux";
import { RootState } from "../store";
-import Files from "./Files";
+import { selectFile } from "../services/fileService";
+import { setCode } from "../state/codeSlice";
+import FileExplorer from "./file-explorer/FileExplorer";
function CodeEditor(): JSX.Element {
const [selectedFileName, setSelectedFileName] = useState("welcome");
- const [explorerOpen, setExplorerOpen] = useState(true);
+
+ const dispatch = useDispatch();
const code = useSelector((state: RootState) => state.code.code);
const handleEditorDidMount = (
@@ -29,13 +32,20 @@ function CodeEditor(): JSX.Element {
monaco.editor.setTheme("my-theme");
};
+ const onSelectFile = async (absolutePath: string) => {
+ const paths = absolutePath.split("/");
+ const fileName = paths[paths.length - 1];
+ const rootlessPath = paths.slice(1).join("/");
+
+ setSelectedFileName(fileName);
+
+ const newCode = await selectFile(rootlessPath);
+ dispatch(setCode(newCode));
+ };
+
return (
<div className="flex h-full w-full bg-neutral-900 transition-all duration-500 ease-in-out">
- <Files
- setSelectedFileName={setSelectedFileName}
- setExplorerOpen={setExplorerOpen}
- explorerOpen={explorerOpen}
- />
+ <FileExplorer onFileClick={onSelectFile} />
<div className="flex flex-col min-h-0 w-full">
<Tabs
disableCursorAnimation
diff --git a/frontend/src/components/Files.tsx b/frontend/src/components/Files.tsx
deleted file mode 100644
index be3d91554f..0000000000
--- a/frontend/src/components/Files.tsx
+++ /dev/null
@@ -1,202 +0,0 @@
-import { Accordion, AccordionItem } from "@nextui-org/react";
-import React, { useEffect } from "react";
-import TreeView, {
- ITreeViewOnNodeSelectProps,
- flattenTree,
-} from "react-accessible-treeview";
-import { AiOutlineFolder } from "react-icons/ai";
-
-import {
- IoIosArrowBack,
- IoIosArrowDown,
- IoIosArrowForward,
- IoIosRefresh,
-} from "react-icons/io";
-
-import { useDispatch, useSelector } from "react-redux";
-import { getWorkspace, selectFile } from "../services/fileService";
-import { setCode, updateWorkspace } from "../state/codeSlice";
-import { RootState } from "../store";
-import FileIcon from "./FileIcons";
-import FolderIcon from "./FolderIcon";
-import IconButton, { IconButtonProps } from "./IconButton";
-
-interface FilesProps {
- setSelectedFileName: React.Dispatch<React.SetStateAction<string>>;
- setExplorerOpen: React.Dispatch<React.SetStateAction<boolean>>;
- explorerOpen: boolean;
-}
-
-function RefreshButton({
- onClick,
- ariaLabel,
-}: Omit<IconButtonProps, "icon">): React.ReactElement {
- return (
- <IconButton
- icon={
- <IoIosRefresh
- size={20}
- className="text-neutral-400 hover:text-neutral-100 transition"
- />
- }
- onClick={onClick}
- ariaLabel={ariaLabel}
- />
- );
-}
-
-function CloseButton({
- onClick,
- ariaLabel,
-}: Omit<IconButtonProps, "icon">): React.ReactElement {
- return (
- <IconButton
- icon={
- <IoIosArrowBack
- size={20}
- className="text-neutral-400 hover:text-neutral-100 transition"
- />
- }
- onClick={onClick}
- ariaLabel={ariaLabel}
- />
- );
-}
-
-function Files({
- setSelectedFileName,
- setExplorerOpen,
- explorerOpen,
-}: FilesProps): JSX.Element {
- const dispatch = useDispatch();
- const workspaceFolder = useSelector(
- (state: RootState) => state.code.workspaceFolder,
- );
-
- const selectedIds = useSelector((state: RootState) => state.code.selectedIds);
- const workspaceTree = flattenTree(workspaceFolder);
-
- useEffect(() => {
- getWorkspace().then((file) => dispatch(updateWorkspace(file)));
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, []);
-
- if (workspaceTree.length <= 1) {
- <div className="h-full bg-neutral-700 border-neutral-600 items-center border-r-1 flex flex-col">
- <div>No workspace found</div>
- </div>;
- }
-
- if (!explorerOpen) {
- return (
- <div className="h-full min-w-[48px] bg-neutral-800 border-neutral-600 items-center border-r-1 flex flex-col transition-all ease-soft-spring">
- <div className="flex mt-2 p-2 justify-end">
- <IoIosArrowForward
- size={20}
- className="cursor-pointer text-neutral-600 hover:text-neutral-100 transition"
- onClick={() => setExplorerOpen(true)}
- />
- </div>
- </div>
- );
- }
-
- const handleNodeSelect = (node: ITreeViewOnNodeSelectProps) => {
- if (!node.isBranch) {
- let fullPath = node.element.name;
- setSelectedFileName(fullPath);
- let currentNode = workspaceTree.find(
- (file) => file.id === node.element.id,
- );
- while (currentNode !== undefined && currentNode.parent) {
- currentNode = workspaceTree.find(
- (file) => file.id === node.element.parent,
- );
- fullPath = `${currentNode?.name}/${fullPath}`;
- }
- selectFile(fullPath).then((code) => {
- dispatch(setCode(code));
- });
- }
- };
-
- return (
- <div className="bg-neutral-800 min-w-[228px] h-full border-r-1 border-r-neutral-600 flex flex-col transition-all ease-soft-spring">
- <div className="flex p-2 items-center justify-between relative">
- <Accordion className="px-0" defaultExpandedKeys={["1"]} isCompact>
- <AccordionItem
- classNames={{
- title: "editor-accordion-title",
- content: "editor-accordion-content",
- }}
- hideIndicator
- key="1"
- aria-label={workspaceFolder.name}
- title={
- <div className="group flex items-center justify-between">
- <span className="text-neutral-400 text-sm">
- {workspaceFolder.name}
- </span>
- </div>
- }
- className="editor-accordion"
- startContent={
- <div className="flex items-center gap-1">
- <IoIosArrowDown className="text-neutral-400" />
- <AiOutlineFolder className="text-neutral-400" />
- </div>
- }
- >
- <div className="w-full overflow-x-auto h-full pt-[4px]">
- <TreeView
- className="text-sm text-neutral-400"
- data={workspaceTree}
- selectedIds={selectedIds}
- expandedIds={workspaceTree.map((node) => node.id)}
- onNodeSelect={handleNodeSelect}
- // eslint-disable-next-line react/no-unstable-nested-components
- nodeRenderer={({
- element,
- isBranch,
- isExpanded,
- getNodeProps,
- level,
- }) => (
- <div
- // eslint-disable-next-line react/jsx-props-no-spreading
- {...getNodeProps()}
- style={{ paddingLeft: 20 * (level - 1) }}
- className="cursor-pointer rounded-[5px] p-1 nowrap flex items-center gap-2 aria-selected:bg-neutral-600 aria-selected:text-white hover:text-white"
- >
- <div className="shrink-0 pl-[48px]">
- {isBranch ? (
- <FolderIcon isOpen={isExpanded} />
- ) : (
- <FileIcon filename={element.name} />
- )}
- </div>
- {element.name}
- </div>
- )}
- />
- </div>
- </AccordionItem>
- </Accordion>
- <div className="transform flex h-[24px] items-center gap-1 absolute top-2 right-2">
- <RefreshButton
- onClick={() =>
- getWorkspace().then((file) => dispatch(updateWorkspace(file)))
- }
- ariaLabel="Refresh"
- />
- <CloseButton
- onClick={() => setExplorerOpen(false)}
- ariaLabel="Close Explorer"
- />
- </div>
- </div>
- </div>
- );
-}
-
-export default Files;
diff --git a/frontend/src/components/IconButton.tsx b/frontend/src/components/IconButton.tsx
index d7e9390269..30f69cdb48 100644
--- a/frontend/src/components/IconButton.tsx
+++ b/frontend/src/components/IconButton.tsx
@@ -5,12 +5,14 @@ export interface IconButtonProps {
icon: ReactElement;
onClick: MouseEventHandler<HTMLButtonElement>;
ariaLabel: string;
+ testId?: string;
}
function IconButton({
icon,
onClick,
ariaLabel,
+ testId,
}: IconButtonProps): React.ReactElement {
return (
<Button
@@ -19,10 +21,15 @@ function IconButton({
onClick={onClick}
className="cursor-pointer text-[12px] bg-transparent aspect-square px-0 min-w-[20px] h-[20px]"
aria-label={ariaLabel}
+ data-testid={testId}
>
{icon}
</Button>
);
}
+IconButton.defaultProps = {
+ testId: "",
+};
+
export default IconButton;
diff --git a/frontend/src/components/file-explorer/ExplorerTree.test.tsx b/frontend/src/components/file-explorer/ExplorerTree.test.tsx
new file mode 100644
index 0000000000..5969a7d13a
--- /dev/null
+++ b/frontend/src/components/file-explorer/ExplorerTree.test.tsx
@@ -0,0 +1,48 @@
+import React from "react";
+import { render } from "@testing-library/react";
+import ExplorerTree from "./ExplorerTree";
+import { WorkspaceFile } from "../../services/fileService";
+
+const NODE: WorkspaceFile = {
+ name: "root-folder-1",
+ children: [
+ { name: "file-1-1.ts" },
+ { name: "folder-1-2", children: [{ name: "file-1-2.ts" }] },
+ ],
+};
+
+const onFileClick = vi.fn();
+
+describe("ExplorerTree", () => {
+ afterEach(() => {
+ vi.resetAllMocks();
+ });
+
+ it("should render the explorer", () => {
+ const { getByText, queryByText } = render(
+ <ExplorerTree root={NODE} onFileClick={onFileClick} defaultOpen />,
+ );
+
+ expect(getByText("root-folder-1")).toBeInTheDocument();
+ expect(getByText("file-1-1.ts")).toBeInTheDocument();
+ expect(getByText("folder-1-2")).toBeInTheDocument();
+ expect(queryByText("file-1-2.ts")).not.toBeInTheDocument();
+ });
+
+ it("should render the explorer given the defaultExpanded prop", () => {
+ const { getByText, queryByText } = render(
+ <ExplorerTree root={NODE} onFileClick={onFileClick} />,
+ );
+
+ expect(getByText("root-folder-1")).toBeInTheDocument();
+ expect(queryByText("file-1-1.ts")).not.toBeInTheDocument();
+ expect(queryByText("folder-1-2")).not.toBeInTheDocument();
+ expect(queryByText("file-1-2.ts")).not.toBeInTheDocument();
+ });
+
+ it.todo("should render all children as collapsed when defaultOpen is false");
+
+ it.todo(
+ "should maintain the expanded state of child folders when closing and opening a parent folder",
+ );
+});
diff --git a/frontend/src/components/file-explorer/ExplorerTree.tsx b/frontend/src/components/file-explorer/ExplorerTree.tsx
new file mode 100644
index 0000000000..5b776c7a8f
--- /dev/null
+++ b/frontend/src/components/file-explorer/ExplorerTree.tsx
@@ -0,0 +1,32 @@
+import React from "react";
+import TreeNode from "./TreeNode";
+import { WorkspaceFile } from "../../services/fileService";
+
+interface ExplorerTreeProps {
+ root: WorkspaceFile;
+ onFileClick: (path: string) => void;
+ defaultOpen?: boolean;
+}
+
+function ExplorerTree({
+ root,
+ onFileClick,
+ defaultOpen = false,
+}: ExplorerTreeProps) {
+ return (
+ <div className="w-full overflow-x-auto h-full pt-[4px]">
+ <TreeNode
+ node={root}
+ path={root.name}
+ onFileClick={onFileClick}
+ defaultOpen={defaultOpen}
+ />
+ </div>
+ );
+}
+
+ExplorerTree.defaultProps = {
+ defaultOpen: false,
+};
+
+export default ExplorerTree;
diff --git a/frontend/src/components/file-explorer/FileExplorer.test.tsx b/frontend/src/components/file-explorer/FileExplorer.test.tsx
new file mode 100644
index 0000000000..b41134703d
--- /dev/null
+++ b/frontend/src/components/file-explorer/FileExplorer.test.tsx
@@ -0,0 +1,85 @@
+import React from "react";
+import { render, waitFor } from "@testing-library/react";
+import userEvent from "@testing-library/user-event";
+import { act } from "react-dom/test-utils";
+import FileExplorer from "./FileExplorer";
+import { getWorkspace } from "../../services/fileService";
+
+vi.mock("../../services/fileService", async () => ({
+ getWorkspace: vi.fn(async () => ({
+ name: "root",
+ children: [
+ { name: "file1.ts" },
+ { name: "folder1", children: [{ name: "file2.ts" }] },
+ ],
+ })),
+
+ selectFile: vi.fn(async (file: string) => file),
+}));
+
+describe("FileExplorer", () => {
+ afterEach(() => {
+ vi.clearAllMocks();
+ });
+
+ it("should get the workspace directory", async () => {
+ const { getByText } = render(<FileExplorer onFileClick={vi.fn} />);
+
+ expect(getWorkspace).toHaveBeenCalledTimes(1);
+ await waitFor(() => {
+ expect(getByText("root")).toBeInTheDocument();
+ });
+ });
+
+ it.todo("should render an empty workspace");
+
+ it("calls the onFileClick function when a file is clicked", async () => {
+ const onFileClickMock = vi.fn();
+ const { getByText } = render(
+ <FileExplorer onFileClick={onFileClickMock} />,
+ );
+
+ await waitFor(() => {
+ expect(getByText("folder1")).toBeInTheDocument();
+ });
+
+ act(() => {
+ userEvent.click(getByText("folder1"));
+ });
+
+ act(() => {
+ userEvent.click(getByText("file2.ts"));
+ });
+
+ const absPath = "root/folder1/file2.ts";
+ expect(onFileClickMock).toHaveBeenCalledWith(absPath);
+ });
+
+ it("should refetch the workspace when clicking the refresh button", () => {
+ const { getByTestId } = render(<FileExplorer onFileClick={vi.fn} />);
+
+ act(() => {
+ userEvent.click(getByTestId("refresh"));
+ });
+
+ expect(getWorkspace).toHaveBeenCalledTimes(2); // 1 from initial render, 1 from refresh button
+ });
+
+ it("should toggle the explorer visibility when clicking the close button", async () => {
+ const { getByTestId, getByText, queryByText } = render(
+ <FileExplorer onFileClick={vi.fn} />,
+ );
+
+ await waitFor(() => {
+ expect(getByText("root")).toBeInTheDocument();
+ });
+
+ act(() => {
+ userEvent.click(getByTestId("close"));
+ });
+
+ // it should be hidden rather than removed from the DOM
+ expect(queryByText("root")).toBeInTheDocument();
+ expect(queryByText("root")).not.toBeVisible();
+ });
+});
diff --git a/frontend/src/components/file-explorer/FileExplorer.tsx b/frontend/src/components/file-explorer/FileExplorer.tsx
new file mode 100644
index 0000000000..24b1244f2f
--- /dev/null
+++ b/frontend/src/components/file-explorer/FileExplorer.tsx
@@ -0,0 +1,114 @@
+import React from "react";
+import {
+ IoIosArrowBack,
+ IoIosArrowForward,
+ IoIosRefresh,
+} from "react-icons/io";
+import { twMerge } from "tailwind-merge";
+import { WorkspaceFile, getWorkspace } from "../../services/fileService";
+import ExplorerTree from "./ExplorerTree";
+import { removeEmptyNodes } from "./utils";
+import IconButton from "../IconButton";
+
+interface ExplorerActionsProps {
+ onRefresh: () => void;
+ toggleHidden: () => void;
+ isHidden: boolean;
+}
+
+function ExplorerActions({
+ toggleHidden,
+ onRefresh,
+ isHidden,
+}: ExplorerActionsProps) {
+ return (
+ <div
+ className={twMerge(
+ "transform flex h-[24px] items-center gap-1 absolute top-4 right-2",
+ isHidden ? "right-3" : "right-2",
+ )}
+ >
+ {!isHidden && (
+ <IconButton
+ icon={
+ <IoIosRefresh
+ size={20}
+ className="text-neutral-400 hover:text-neutral-100 transition"
+ />
+ }
+ testId="refresh"
+ ariaLabel="Refresh workspace"
+ onClick={onRefresh}
+ />
+ )}
+
+ <IconButton
+ icon={
+ isHidden ? (
+ <IoIosArrowForward
+ size={20}
+ className="text-neutral-400 hover:text-neutral-100 transition"
+ />
+ ) : (
+ <IoIosArrowBack
+ size={20}
+ className="text-neutral-400 hover:text-neutral-100 transition"
+ />
+ )
+ }
+ testId="close"
+ ariaLabel="Close workspace"
+ onClick={toggleHidden}
+ />
+ </div>
+ );
+}
+
+interface FileExplorerProps {
+ onFileClick: (path: string) => void;
+}
+
+function FileExplorer({ onFileClick }: FileExplorerProps) {
+ const [workspace, setWorkspace] = React.useState<WorkspaceFile>();
+ const [isHidden, setIsHidden] = React.useState(false);
+
+ const getWorkspaceData = async () => {
+ const wsFile = await getWorkspace();
+ setWorkspace(removeEmptyNodes(wsFile));
+ };
+
+ React.useEffect(() => {
+ (async () => {
+ await getWorkspaceData();
+ })();
+ }, []);
+
+ return (
+ <div
+ className={twMerge(
+ "bg-neutral-800 h-full border-r-1 border-r-neutral-600 flex flex-col transition-all ease-soft-spring overflow-auto",
+ isHidden ? "min-w-[48px]" : "min-w-[228px]",
+ )}
+ >
+ <div className="flex p-2 items-center justify-between relative">
+ <div style={{ display: isHidden ? "none" : "block" }}>
+ {workspace && (
+ <ExplorerTree
+ root={workspace}
+ onFileClick={onFileClick}
+ defaultOpen
+ />
+ )}
+ </div>
+
+ <ExplorerActions
+ isHidden={isHidden}
+ toggleHidden={() => setIsHidden((prev) => !prev)}
+ onRefresh={getWorkspaceData}
+ />
+ </div>
+ </div>
+ );
+}
+
+export default FileExplorer;
diff --git a/frontend/src/components/file-explorer/TreeNode.test.tsx b/frontend/src/components/file-explorer/TreeNode.test.tsx
new file mode 100644
index 0000000000..b42c138ba7
--- /dev/null
+++ b/frontend/src/components/file-explorer/TreeNode.test.tsx
@@ -0,0 +1,148 @@
+import React from "react";
+import { act, render } from "@testing-library/react";
+import userEvent from "@testing-library/user-event";
+import TreeNode from "./TreeNode";
+import { WorkspaceFile } from "../../services/fileService";
+
+const onFileClick = vi.fn();
+
+const NODE: WorkspaceFile = {
+ name: "folder",
+ children: [
+ { name: "file.ts" },
+ { name: "folder2", children: [{ name: "file2.ts" }] },
+ ],
+};
+
+describe("TreeNode", () => {
+ afterEach(() => {
+ vi.resetAllMocks();
+ });
+
+ it("should render a file if property has no children", () => {
+ const { getByText } = render(
+ <TreeNode
+ node={NODE}
+ path={NODE.name}
+ onFileClick={onFileClick}
+ defaultOpen
+ />,
+ );
+
+ expect(getByText("file.ts")).toBeInTheDocument();
+ });
+
+ it("should render a folder if property has children", () => {
+ const { getByText } = render(
+ <TreeNode
+ node={NODE}
+ path={NODE.name}
+ onFileClick={onFileClick}
+ defaultOpen
+ />,
+ );
+
+ expect(getByText("folder")).toBeInTheDocument();
+ expect(getByText("file.ts")).toBeInTheDocument();
+ });
+
+ it("should close a folder when clicking on it", () => {
+ const { getByText, queryByText } = render(
+ <TreeNode
+ node={NODE}
+ path={NODE.name}
+ onFileClick={onFileClick}
+ defaultOpen
+ />,
+ );
+
+ act(() => {
+ userEvent.click(getByText("folder"));
+ });
+
+ expect(queryByText("folder2")).not.toBeInTheDocument();
+ expect(queryByText("file2.ts")).not.toBeInTheDocument();
+ expect(queryByText("file.ts")).not.toBeInTheDocument();
+ });
+
+ it("should open a folder when clicking on it", () => {
+ const { getByText } = render(
+ <TreeNode node={NODE} path={NODE.name} onFileClick={onFileClick} />,
+ );
+
+ act(() => {
+ userEvent.click(getByText("folder"));
+ });
+
+ expect(getByText("folder2")).toBeInTheDocument();
+ expect(getByText("file.ts")).toBeInTheDocument();
+ });
+
+ it("should call a fn and return the full path of a file when clicking on it", () => {
+ const { getByText } = render(
+ <TreeNode
+ node={NODE}
+ path={NODE.name}
+ onFileClick={onFileClick}
+ defaultOpen
+ />,
+ );
+
+ act(() => {
+ userEvent.click(getByText("file.ts"));
+ });
+
+ expect(onFileClick).toHaveBeenCalledWith("folder/file.ts");
+
+ act(() => {
+ userEvent.click(getByText("folder2"));
+ });
+
+ act(() => {
+ userEvent.click(getByText("file2.ts"));
+ });
+
+ expect(onFileClick).toHaveBeenCalledWith("folder/folder2/file2.ts");
+ });
+
+ it("should render the explorer given the defaultExpanded prop", () => {
+ const { getByText, queryByText } = render(
+ <TreeNode node={NODE} path={NODE.name} onFileClick={onFileClick} />,
+ );
+
+ expect(getByText("folder")).toBeInTheDocument();
+ expect(queryByText("folder2")).not.toBeInTheDocument();
+ expect(queryByText("file2.ts")).not.toBeInTheDocument();
+ expect(queryByText("file.ts")).not.toBeInTheDocument();
+
+ act(() => {
+ userEvent.click(getByText("folder"));
+ });
+
+ expect(getByText("folder2")).toBeInTheDocument();
+ expect(getByText("file.ts")).toBeInTheDocument();
+ });
+
+ it("should render all children as collapsed when defaultOpen is false", () => {
+ const { getByText, queryByText } = render(
+ <TreeNode node={NODE} path={NODE.name} onFileClick={onFileClick} />,
+ );
+
+ expect(getByText("folder")).toBeInTheDocument();
+ expect(queryByText("folder2")).not.toBeInTheDocument();
+ expect(queryByText("file2.ts")).not.toBeInTheDocument();
+ expect(queryByText("file.ts")).not.toBeInTheDocument();
+
+ act(() => {
+ userEvent.click(getByText("folder"));
+ });
+
+ expect(getByText("folder2")).toBeInTheDocument();
+ expect(getByText("file.ts")).toBeInTheDocument();
+ expect(queryByText("file2.ts")).not.toBeInTheDocument();
+ });
+
+ it.todo(
+ "should maintain the expanded state of child folders when closing and opening a parent folder",
+ );
+});
diff --git a/frontend/src/components/file-explorer/TreeNode.tsx b/frontend/src/components/file-explorer/TreeNode.tsx
new file mode 100644
index 0000000000..7917b011a9
--- /dev/null
+++ b/frontend/src/components/file-explorer/TreeNode.tsx
@@ -0,0 +1,78 @@
+import React from "react";
+import FolderIcon from "../FolderIcon";
+import FileIcon from "../FileIcons";
+import { WorkspaceFile } from "../../services/fileService";
+
+interface TitleProps {
+ name: string;
+ type: "folder" | "file";
+ isOpen: boolean;
+ onClick: () => void;
+}
+
+function Title({ name, type, isOpen, onClick }: TitleProps) {
+ return (
+ <div
+ onClick={onClick}
+ className="cursor-pointer rounded-[5px] p-1 nowrap flex items-center gap-2 aria-selected:bg-neutral-600 aria-selected:text-white hover:text-white"
+ >
+ {type === "folder" && <FolderIcon isOpen={isOpen} />}
+ {type === "file" && <FileIcon filename={name} />}
+ {name}
+ </div>
+ );
+}
+
+interface TreeNodeProps {
+ node: WorkspaceFile;
+ path: string;
+ onFileClick: (path: string) => void;
+ defaultOpen?: boolean;
+}
+
+function TreeNode({
+ node,
+ path,
+ onFileClick,
+ defaultOpen = false,
+}: TreeNodeProps) {
+ const [isOpen, setIsOpen] = React.useState(defaultOpen);
+
+ const handleClick = React.useCallback(() => {
+ if (node.children) {
+ setIsOpen((prev) => !prev);
+ } else {
+ onFileClick(path);
+ }
+ }, [node, path, onFileClick]);
+
+ return (
+ <div className="text-sm text-neutral-400">
+ <Title
+ name={node.name}
+ type={node.children ? "folder" : "file"}
+ isOpen={isOpen}
+ onClick={handleClick}
+ />
+
+ {isOpen && node.children && (
+ <div className="ml-5">
+ {node.children.map((child, index) => (
+ <TreeNode
+ key={index}
+ node={child}
+ path={`${path}/${child.name}`}
+ onFileClick={onFileClick}
+ />
+ ))}
+ </div>
+ )}
+ </div>
+ );
+}
+
+TreeNode.defaultProps = {
+ defaultOpen: false,
+};
+
+export default React.memo(TreeNode);
diff --git a/frontend/src/components/file-explorer/utils.test.ts b/frontend/src/components/file-explorer/utils.test.ts
new file mode 100644
index 0000000000..fd51731dad
--- /dev/null
+++ b/frontend/src/components/file-explorer/utils.test.ts
@@ -0,0 +1,39 @@
+import { removeEmptyNodes } from "./utils";
+
+test("removeEmptyNodes removes empty arrays", () => {
+ const root = {
+ name: "a",
+ children: [
+ {
+ name: "b",
+ children: [],
+ },
+ {
+ name: "c",
+ children: [
+ {
+ name: "d",
+ children: [],
+ },
+ ],
+ },
+ ],
+ };
+
+ expect(removeEmptyNodes(root)).toEqual({
+ name: "a",
+ children: [
+ {
+ name: "b",
+ },
+ {
+ name: "c",
+ children: [
+ {
+ name: "d",
+ },
+ ],
+ },
+ ],
+ });
+});
diff --git a/frontend/src/components/file-explorer/utils.ts b/frontend/src/components/file-explorer/utils.ts
new file mode 100644
index 0000000000..d8d4fc336e
--- /dev/null
+++ b/frontend/src/components/file-explorer/utils.ts
@@ -0,0 +1,14 @@
+import { WorkspaceFile } from "../../services/fileService";
+
+export const removeEmptyNodes = (root: WorkspaceFile): WorkspaceFile => {
+ if (root.children) {
+ const children = root.children
+ .map(removeEmptyNodes)
+ .filter((node) => node !== undefined);
+ return {
+ ...root,
+ children: children.length ? children : undefined,
+ };
+ }
+ return root;
+};